<p>I want to read the DAYA label value from the kubectl get secret -o wide command shown below.</p>
<p><a href="https://i.stack.imgur.com/ciknD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ciknD.png" alt="enter image description here" /></a></p>
<p>I using this code:</p>
<pre><code>from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
secret = v1.read_namespaced_secret(&quot;mysql-pass&quot;, &quot;default&quot;)
print(secret)
</code></pre>
<p>However, the value of the data label was not found...</p>
<pre><code>{'api_version': None,
 'data': {'ca.crt': 'LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM1ekNDQWMrZ0F3SUJBZ0lCQVRBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwdGFXNXAKYTNWaVpVTkJNQjRYRFRJd01EZ3hOekV4TkRjMU5Gb1hEVE13TURneE5qRXhORGMxTkZvd0ZURVRNQkVHQTFVRQpBeE1LYldsdWFXdDFZbVZEUVRDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTVBiClBMQ2t3cTZUQzV2RWVtSEswbGx6T3k0OFZsWjNtUXYzS1NnOHI5TW9ualhheDhZaXhHMGVkaVMvL1NBMTJUUWUKVWFWZEJIWE5Lc2wxNXhPcnRnTWkxbmdpcWdoazZ6eFNXUVZpa0dxWFFJSzlqeTJwamo5WGZFQ0I1Sk5QcDFaUQowS1ZSUWtZV2ZwMTV3dEsyVnVleFhNZDdTT2plb1R3OGhmOHdKeXJUaVliSC8vZVZrNzExbmJHRVBSckh6SlVQClBMckRkN0RNaVArRWtmR2syVlBFN3I3N0pmUGJNRU5za0dEMlZRelQrbjFpZkd3NlJDS3BZSFVYVUZoekVHek4KcDZ5YkEwUGJUNVRCZks0ZEVUZWlYOFQrVnBrNlpNbzJBYUs1TkNsN2NIZEJSVjZwWWcvNElOaFdCZzV1WjFHKwpZMTZ4RW5JMXBwVDFLVGRhRnJNQ0F3RUFBYU5DT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lc0dGVuWnNPR3NpTENKcmRXSmxjbTVsZEdWekxtbHZMM05sY25acFkyVmhZMk52ZFc1MEwzTmxjblpwWTJVdFlXTmpiM1Z1ZEM1dVlXMWxJam9pWkdWbVlYVnNkQ0lzSW10MVltVnlibVYwWlhNdWFXOHZjMlZ5ZG1salpXRmpZMjkxYm5RdmMyVnlkbWxqWlMxaFkyTnZkVzUwTG5WcFpDSTZJbUptWkdVek5tSmlMV1UyTjJZdE5ESXdaQzFoWlRZekxXTXdaVGRtWkRobE56VXdaQ0lzSW5OMVlpSTZJbk41YzNSbGJUcHpaWEoyYVdObFlXTmpiM1Z1ZERwa1pXWmhkV3gwT21SbFptRjFiSFFpZlEuU2taV05IcE1zTHk5enBpOEdpT0FvN29rSXE4U05oME41Yy15QkNKU0VESWs5M2lhNFZnNjRyZGNSTVdGTTFiWTBNZk91WnVvNnV2bEgxeXhRRmlOaDJXaEp6OEZfcXRCUDRmQnRMUE4xX3hBRzNGWllYLXA3eWJ2M19RQnU2R01aS3dMSi1vNEpqLVlveXdlQVBIRnRUVGpiYk9fNGpidmROTEhyUml0Q3RyOGJRMGFQWmxiU3JGSW1XNUxtbmVjLUFUYXluVm83NjdSRmdueWNQd2Z5RmxELTJlVzBmcndac0tCSXhyUFpiSUFfaEZXRXFFdGllaHhUUm12Q3dGSjByUGlDbG5iUkloekpZV29IWVg4NnVuR09CMGVPdmNMT29DSmM1VnVMX1pFeFpmQUN6OE5OVW9iaEVwUFZrOURPNmlINXc0cDNrS28xZHdwYkZSZDN3'},
 'kind': None,
 'metadata': {'annotations': {'kubernetes.io/service-account.name': 'default',
                              'kubernetes.io/service-account.uid': 'bfde36bb-e67f-420d-ae63-c0e7fd8e750d'},
              'cluster_name': None,
              'creation_timestamp': datetime.datetime(2020, 8, 18, 11, 48, 19, tzinfo=tzutc()),
              'deletion_grace_period_seconds': None,
              'deletion_timestamp': None,
              'finalizers': None,
              'generate_name': None,
              'generation': None,
              'initializers': None,
              'labels': None,
              'managed_fields': [{'api_version': 'v1',
                                  'fields': None,
                                  'manager': 'kube-controller-manager',
                                  'operation': 'Update',
                                  'time': datetime.datetime(2020, 8, 18, 11, 48, 19, tzinfo=tzutc())}],
              'name': 'default-token-zvl8k',
              'namespace': 'default',
              'owner_references': None,
              'resource_version': '319',
              'self_link': '/api/v1/namespaces/default/secrets/default-token-zvl8k',
              'uid': 'd11e5725-7ce5-4a9d-b6fb-4bb76c8fb9eb'},
 'string_data': None,
 'type': 'kubernetes.io/service-account-token'}

</code></pre>

유동근
<p>As pointed by user Dandy, there are 2 possible interpretations to this question:</p>
<ul>
<li>Getting the <code>data</code> field from the <code>secret</code> - comment (I thought this first)</li>
<li>Getting the <code>DATA</code> (counter) from the <code>kubectl get secret -o yaml</code> - answer</li>
</ul>
<p>Not really sure why did you post a code sample referencing the secret <code>mysql-pass</code> and included the output of the <code>default-token-zvl8k</code></p>
<hr />
<p>I've created an example to show you both options:</p>
<p><code>secret.yaml</code></p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Secret
metadata:
  name: example-secret
type: Opaque
data:
  username: a3J1awo= # kruk 
  password: c3VwZXJoYXJkcGFzc3dvcmQ= # superhardpassword
</code></pre>
<p><code>Python3</code> code:</p>
<pre class="lang-py prettyprint-override"><code>from kubernetes import client, config
import base64

config.load_kube_config()
v1 = client.CoreV1Api()
secret = v1.read_namespaced_secret(&quot;example-secret&quot;, &quot;default&quot;) # get the secret 

data = secret.data # extract .data from the secret 
name = secret.metadata.name
password = secret.data['password'] # extract .data.password from the secret

decoded = base64.b64decode(password) # decode (base64) value from pasw

print (&quot;Secret: &quot; + name)
print(&quot;-------------&quot;)
print(&quot;Here you have the data: &quot;)
print(data)
print(&quot;COUNTED DATA: &quot; + str(len(data))) # &lt;- ANSWER FOR THE DATA COUNTER
print(&quot;-------------&quot;)
print(&quot;Here you have the decoded password: &quot;)
print(decoded)
</code></pre>
<p>With above code you can:</p>
<ul>
<li>extract needed information from secret named <code>example-secret</code>.</li>
<li>count the slices from the data field (as in <code>$ kubectl get secret -o yaml</code>) - pointed by user Dandy</li>
</ul>
<p>The output of above <code>Python3</code> script:</p>
<pre class="lang-sh prettyprint-override"><code>Secret: example-secret
-------------
Here you have the data: 
{'password': 'c3VwZXJoYXJkcGFzc3dvcmQ=', 'username': 'a3J1awo='}
COUNTED DATA: 2
-------------
Here you have the decoded password: 
b'superhardpassword'
</code></pre>
<p>As for <code>b''</code> you can look here:</p>
<ul>
<li><em><a href="https://stackoverflow.com/questions/41918836/how-do-i-get-rid-of-the-b-prefix-in-a-string-in-python">Stackoverflow.com: Questions: How do I get rid of the b prefix in a string in python</a></em></li>
</ul>
