Slava
Slava

Reputation: 435

How to find what is the name of assigned to V1ObjectMeta in kubernetes?

I have the following code using the k8s python client:

config.load_incluster_config()
v1 = client.CoreV1Api()
object_meta = k8s.V1ObjectMeta(generate_name='myprefix',
                               namespace='my_name_space')
body = k8s.V1Secret(string_data=data, kind='Secret', type='my_type', metadata=object_meta)
api_response = v1.create_namespaced_secret(namespace='my_name_space', body=body)

This creates a secret in my K8s namespace. Since I'm using generate_name it assigned to it some random value with the prefix I gave so the name can be myprefix-fbdsu3 or anything like it.

My question is how do I get the name assigned to that secret after it was created?

Upvotes: 2

Views: 329

Answers (1)

Harsh Manvar
Harsh Manvar

Reputation: 30208

You can pass the secret name into this code and get the example of the secret

from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
secret = v1.read_namespaced_secret("Secret-name", "Namespace-Name")
print(secret)

decode the secret and get the detail s

from kubernetes import client, config
import base64
import sys    
config.load_kube_config()
v1 = client.CoreV1Api()
sec = str(v1.read_namespaced_secret("secret-name", "namespace-name").data)
pas = base64.b64decode(sec.strip().split()[1].translate(None, '}\''))
print(pas)

Read more at : https://www.programcreek.com/python/?CodeExample=create+secret

Upvotes: 2

Related Questions