can i use a configmap created from an init container in the pod

后端 未结 3 1692
半阙折子戏
半阙折子戏 2021-02-14 16:32

I am trying to \"pass\" a value from the init container to a container. Since values in a configmap are shared across the namespace, I figured I can use it for this purpose. Her

3条回答
  •  时光取名叫无心
    2021-02-14 16:56

    If your various reasons, you don't want to use share volume. And you want to create a configmap or a secret, here is a solution.

    First you need to use a docker image which contains kubectl : gcr.io/cloud-builders/kubectl:latest for example. (docker image which contains kubectl manage by Google).

    Then this (init)container needs enough rights to create resource on Kubernetes cluster. Ok by default, kubernetes inject a token of default service account named : "default" in container, but I prefer to make more explicit, then add this line :

    ...
          initContainers:
            - # Already true by default but if use it, prefer to make it explicit
              automountServiceAccountToken: true
              name: artifactory-snapshot
    

    And add "edit" role to "default" service account:

    kubectl create rolebinding default-edit-rb --clusterrole=edit --serviceaccount=default:myapp --namespace=default
    

    Then complete example :

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: installer-test
    spec:
      template:
        spec:
          initContainers:
            - # Already true by default but if use it, prefer to make it explicit.
              automountServiceAccountToken: true
              name: artifactory-snapshot
              # You need to use docker image which contains kubectl
              image: gcr.io/cloud-builders/kubectl:latest
              command:
                - sh
                - -c
                # the "--dry-run -o yaml | kubectl apply -f -" is to make command idempotent
                - kubectl create configmap test-config --from-literal=artifactorySnapshotUrl=http://artifactory.com/some/url --dry-run -o yaml | kubectl apply -f -
          containers:
            - name: installer-test
              image: installer-test:latest
              env:
                - name: clusterId
                  value: "some_cluster_id"
                - name: in_artifactoryUrl
                  valueFrom:
                    configMapKeyRef:
                      name: test-config
                      key: artifactorySnapshotUrl
    
    

提交回复
热议问题