Sunday, February 10, 2019

Error: Couldn't find key username in Secret default/test-stg-secret

Symptom:

  We create test-stg-secret via yaml file below
apiVersion: v1
kind: Secret
metadata:
  name: 
 test-stg-secret
type: Opaque
stringData:
  config.yaml: |-
    username: test
    password:  test

 However we got "Error: Couldn't find key username in Secret default/test-stg-secret" when we create statefulset. Part of secret usages are:
env:
            - name: TEST_USER
              valueFrom:
                  secretKeyRef:
                     name: test-stg-secret
                     key: username
            - name: TEST_PASSWORD
              valueFrom:
                  secretKeyRef:
                     name: test-stg-secret
                     key: password

Solution:

The reason is due to when we create secret it regards the whole text as one single string as config.yaml
It does not take username or password as key
To workaround it, we need to get decoded string via base64
echo -n 'test' | base64  --->  dGVzdA==

-n will take new line at the end

Then correct yaml file is :
apiVersion: v1kind: Secretmetadata:  name:  test-stg-secret
type: Opaque
data:
    username: dGVzdA==
    password:  dGVzdA==

No comments: