create_namespaced_secret error while creating with multiple secret data - kubenetes python client

Viewed 174

Following this example Using create_namespaced_secret API in Kubernetes Python client, I am trying to create multiple secret data inside same secret and namespace. However after I create the first secret, I am not able to create the second one with different secret data (different key and value pairs). Say for example I write a create_secret() which will create secret with same secret name and namespace with different secret data - key/value pair.

HTTP response body: {
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Failure",
  "message": "secrets \"mysecret\" already exists",
  "reason": "AlreadyExists",
  "details": {
    "name": "mysecret",
    "kind": "secrets"
  },
  "code": 409
}
def create_secret(secret_data, secret_name, namespace):
    """Create a K8S Secret.

    args:
        secret_data (dict): Data to store as key/value hash.
        secret_name (str) : Name of the Secret.
        namespace   (str) : Name of namespace.

    """
    config.load_incluster_config()
    secret = client.V1Secret()

    core_api_instance = client.CoreV1Api()

    pretty = 'true'

    secret.api_version = 'v1'
    secret.data = secret_data
    secret.kind = 'Secret'
    secret.metadata = {'name': secret_name}
    secret.type = 'Opaque'
    
    try:
        api_response = core_api_instance.create_namespaced_secret(namespace, secret, pretty=pretty)
        
        pprint(api_response)    
    except ApiException as e:
        traceback.print_exc()
        raise

    logging.info("Created Secret " + secret_name + " in namespace " + namespace)

I am calling this function with same secret name (mysecret) and namespace (testnamespace) with different secret data. So my intention is to create secret like this-

namespace: testnamespace
  secretname: mysecret
    #multiple secret data - key/value
    username: "base64 encoded username"
    passwd: "base64 encoded passwd"
    someotherkey: "base64 encoded some other key value"

After the secret is created with the very first username/value pair, next time it gives error as secret already exists and I am not able to create/add the second passwd/value pair into the same secret and namespace.

Please let me know how this can be achieved through python k8s client api. Also in future I want to update the value for the keys username, passwd whenever it changes. Whether k8s python client api provides such facility for this kind of requirement.

PS: I am using Client v1.18.0

Thanks a lot for your valuable answers.

1 Answers

You didn't supply your stacktrace but I'd guess it shows a 409 Conflict error. If you look further you will likely see that there's a rest client POST request - Kubernetes semantics are a bit odd to be honest but basically you aren't allowed to POST two things with the same name (so shouldn't it be a PUT?)

Anyway, if you look in the docs for kubernetes.client.api.core_v1_api.CoreV1Api you will find the following methods patch_namespaced_secret - try that instead of create.

Related