Skip to content

Challenges with setting namespace when using generators #2466

@howardjohn

Description

@howardjohn

Describe your problem

I am trying to write a generator function. Basically it inserts some data into a configmap, nothing too fancy.

My Kptfile looks like:

pipeline:
  mutators:
    - image: my-generator
      configMap:
        data: "..."
   - image: gcr.io/kpt-fn/set-namespace:v0.1
     configMap:
       namespace: grafana

My function generates a new config, and inserts it into the list of items with merging:

	rl.Items = append(rl.Items, yaml.NewRNode(&node))
	rl.Items, err = filters.MergeFilter{}.Filter(rl.Items)

The problem is that the generator doesn't add a namespace, but then it later is added. This means that the second run of the pipeline ends up appending the config again - so we end up with 2x copies.

Possible solutions:

  • Generator functions should not include namespace on merge key. This seems prone to failure in cases of multiple namespaces handled by one Kptfile
  • Generator functions should always explicit set the namespace. This requires all generators to take a namespace param, and all usages to set it as well - a lot of churn and duplication.
  • When configMap is used, set the namespace equal to that of the Kptfile namespace. Bonus points if set-namespace also uses this. Then, the whole Kptfile could look like:
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
  name: grafana
  namespace: grafana
pipeline:
  mutators:
    - image: my-generator
      configMap:
        data: "..."
   - image: gcr.io/kpt-fn/set-namespace:v0.1
     configMap: {}

To get the namespace that should be used, functions can use rl.FunctionConfig.GetNamespace()

  • if set-namespaces generates a duplicate resource, it could filter one of them out. This feels like the wrong solution - especially if it picks the wrong copy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions