Skip to content

Secret mount path must be unique for secrets on containers #4075

Closed

Description

@alexellis

In what area(s)?

/area API

/kind spec

What version of Knative?

0.5.x

Expected Behavior

Ability to mount two secrets to the same mountPath as per a Kubernetes deployment object / spec.

Actual Behavior

When creating a Serving definition with two secrets mounted to the same directory I saw this:

spec.template.spec.containers[0].volumeMounts[1].mountPath: Invalid value: "/var/openfaas/secrets/": must be unique

Steps to Reproduce the Problem


# kubectl create secret generic matias -n openfaas-fn --from-literal keyboard=mechanical
# kubectl create secret generic alex -n openfaas-fn --from-literal name=alex

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: secret-print
  namespace: openfaas-fn
spec:
  runLatest:
    configuration:
      revisionTemplate:
        spec:
          container:
            image: functions/alpine:latest
            env:
            - name: fprocess
              value: cat /var/openfaas/secrets/name
            volumeMounts:
            - name: alex
              mountPath: /var/openfaas/secrets/
              readOnly: true
            - name: matias
              mountPath: /var/openfaas/secrets/
              readOnly: true
          volumes:
          - name: alex
            secret:
              secretName: alex
          - name: matias
            secret:
              secretName: matias

Notes from @mattmoor

You can do it with `subPath`, but we should probably fix it

can you open an issue?
The `subPath` change is only at HEAD
not in 0.5

Activity

  1. added
    kind/bug
    on May 11, 2019
  2. added
    area/API
    kind/spec
    on May 11, 2019
  3. added this to the Serving 0.7 milestone on May 11, 2019
  4. mattmoor commented on May 11, 2019

    @mattmoor
    Contributor

    This isn't our validation, this is the K8s Deployment resource rejecting the Deployment the Revision is passing it. It sounds like we should be rejecting things with this message, as K8s Deployment does.

    $ kubectl apply -f deployment.yaml 
    The Deployment "secret-print" is invalid: spec.template.spec.containers[0].volumeMounts[1].mountPath: Invalid value: "/var/openfaas/secrets/": must be unique
    
    $ cat deployment.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: secret-print
    spec:
      selector:
        matchLabels:
          app: secrets
      template:
        metadata:
          labels:
            app: secrets
        spec:
          containers:
          - name: "foo"
            image: ubuntu
            command: ["/bin/bash"]
            args: ["-c", "sleep 3600"]
            volumeMounts:
            - name: alex
              mountPath: /var/openfaas/secrets/
              readOnly: true
            - name: matias
              mountPath: /var/openfaas/secrets/
              readOnly: true
          volumes:
          - name: alex
            secret:
              secretName: alex
          - name: matias
            secret:
              secretName: matias
    
  5. alexellis commented on May 11, 2019

    @alexellis
    Author

    Yes you're right. Did you see my note about the projected volumes?

  6. mattmoor commented on May 11, 2019

    @mattmoor
    Contributor

    This works:

    $ cat deployment.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: secret-print
    spec:
      selector:
        matchLabels:
          app: secrets
      template:
        metadata:
          labels:
            app: secrets
        spec:
          containers:
          - name: "foo"
            image: ubuntu
            command: ["/bin/bash"]
            args: ["-c", "sleep 3600"]
            volumeMounts:
            - name: alex
              mountPath: /var/openfaas/secrets/name
              subPath: name
              readOnly: true
            - name: matias
              mountPath: /var/openfaas/secrets/keyboard
              subPath: keyboard
              readOnly: true
          volumes:
          - name: alex
            secret:
              secretName: alex
          - name: matias
            secret:
              secretName: matias
    

    We don't allow subPath through in 0.5, but we will in 0.6 (Tuesday). I believe it would be relatively simple to add support for projected volumes; this came up when we were first letting volumes through and they seemed complex enough that we wanted to wait for a clear use case.

    Here's the issue tracking that. Let's use this to track improving our validation to match K8s Deployment, so we don't let through the bad volume mount config.

  7. alexellis commented on May 11, 2019

    @alexellis
    Author

    Thanks that sounds promising. This is how we have implemented it for our CRD at present - https://github.com/openfaas-incubator/openfaas-operator/blob/master/pkg/controller/secrets.go#L19

    The complexity is somewhat hidden from the user in our stack.yml / CRD entry - it's a case of declaring an array of secret names to be bound and then they are all mounted in /var/openfaas/secrets/ as a convention.

  8. mattmoor commented on May 11, 2019

    @mattmoor
    Contributor

    Yeah this works without knowing the list of keys in the secret:

    $ cat deployment.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: secret-print
    spec:
      selector:
        matchLabels:
          app: secrets
      template:
        metadata:
          labels:
            app: secrets
        spec:
          containers:
          - name: "foo"
            image: ubuntu
            command: ["/bin/bash"]
            args: ["-c", "sleep 3600"]
            volumeMounts:
            - name: all-in-one
              mountPath: /var/openfaas/secrets/
              readOnly: true
          volumes:
          - name: all-in-one
            projected:
              sources:
              - secret:
                  name: alex
              - secret:
                  name: matias
    
  9. mattmoor commented on May 11, 2019

    @mattmoor
    Contributor

    I wonder how it deals with collisions across secrets. Time to try :)

  10. mattmoor commented on May 11, 2019

    @mattmoor
    Contributor

    Looks like last one wins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/APIkind/bugkind/spec

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions