Search code examples
kuberneteskubernetes-helmkubectlkubernetes-pod

using generateName for yaml file to be installed by helm


I have upload.yaml file which is uploads a script to mongo, I package with helm.

apiVersion: batch/v1
kind: Job
metadata:
  generateName: upload-strategy-to-mongo-v2
spec:
  parallelism: 1
  completions: 1
  template:
    metadata:
      name: upload-strategy-to-mongo
    spec:
      volumes:
      - name: upload-strategy-to-mongo-scripts-volume
        configMap:
          name: upload-strategy-to-mongo-scripts-v3
      containers:
      - name: upload-strategy-to-mongo
        image: mongo
        env:
        - name: MONGODB_URI
          value: @@@@
        - name: MONGODB_USERNAME
          valueFrom:
            secretKeyRef:
              name: mongodb-user
              key: @@@@
        - name: MONGODB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mongodb-user
              key: @@@@@
        volumeMounts:
          - mountPath: /scripts
            name: upload-strategy-to-mongo-scripts-volume
        command: ["mongo"]
        args:
          - $(MONGODB_URI)/ravnml
          - --username
          - $(MONGODB_USERNAME)
          - --password
          - $(MONGODB_PASSWORD)
          - --authenticationDatabase
          - admin
          - /scripts/upload.js
      restartPolicy: Never
---
apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: null
  name:  upload-strategy-to-mongo-scripts-v3
data:
  upload.js: |
    // Read the object from file and parse it
    var data = cat('/scripts/strategy.json');
    var obj = JSON.parse(data);

    // Upsert strategy
    print(db.strategy.find());
    db.strategy.replaceOne(
        { name : obj.name },
          obj,
        { upsert: true }
    )
    print(db.strategy.find());
  strategy.json: {{ .Files.Get "strategy.json" | quote }}

now I am using generateName to generate a custom name every time I install it. I require to have multiple packages been installed and I require the name to be dynamic.

Error When I install this script with helm install <name> <tar.gz file> -n <namespace> I get the following error

Error: rendered manifests contain a resource that already exists. Unable to continue with install: could not get information about the resource: resource name may not be empty

but I am able to install if I don't use generateName. Any ideas?

I looked at various resources but they don't seem to answer how to install via helm. references looked: Add random string on Kubernetes pod deployment name https://github.com/kubernetes/kubernetes/issues/44501 ; https://zknill.io/posts/kubernetes-generated-names/


Solution

  • This seems to be a known issue. Helm doesn't work with generateName. For unique names, you can use the Helm's build in properties like Revision or Name. See the following link for reference: