<p>I have a big yaml file that I want to store as a secret in my kubernetes cluster. The following command succeeds:</p>
<pre><code>k create secret generic values --from-file=my-values.yaml
</code></pre>
<p>But in my code, I want to use the k8s python client.
So I want to do something like this:</p>
<pre><code>def make_k8s_client(kubeconig):
....

def create_secret(name, data, client_api):
    secret = client.V1Secret(
        api_version=&quot;v1&quot;,
        kind=&quot;Secret&quot;,
        metadata=client.V1ObjectMeta(name=name),
        data=data,
    )

    client_api.create_namespaced_secret(namespace=&quot;default&quot;,
                                              body=secret)

k8s_api = make_k8s_client(&quot;path-to-kubeconfig&quot;)

with open(&quot;path-to/my-values.yaml&quot;) as f:
    values = yaml.load(f)
</code></pre>
<p>If I pass the yaml like this:</p>
<pre><code>create_secret(&quot;mysecret&quot;, values, k8s_api)
</code></pre>
<p>I get this error:</p>
<pre><code>HTTP response body: {&quot;kind&quot;:&quot;Status&quot;,&quot;apiVersion&quot;:&quot;v1&quot;,&quot;metadata&quot;:{},&quot;status&quot;:&quot;Failure&quot;,&quot;message&quot;:&quot;Secret in version \&quot;v1\&quot; cannot be handled as a Secret: v1.Secret.Data: decode base64: illegal base64 data at input byte 0, error found in #10 byte of ...|pe\&quot;: \&quot;abc\&quot;, \&quot;def|..., bigger context ...|{\&quot;apiVersion\&quot;: \&quot;v1\&quot;, \&quot;data\&quot;: {\&quot;k8sType\&quot;: \&quot;abc\&quot;, \&quot;secret\&quot;: \&quot;mysecret\&quot;, \&quot;type\&quot;: \&quot;mytype&quot;,&quot;reason&quot;:&quot;BadRequest&quot;,&quot;code&quot;:400}
</code></pre>
<p>If I pass the secret like this:</p>
<pre><code>create_secret(&quot;mysecret&quot;, base64.urlsafe_b64encode(json.dumps(values).encode()).decode(), k8s_api)
</code></pre>
<p>I get this error:</p>
<pre><code>HTTP response body: {&quot;kind&quot;:&quot;Status&quot;,&quot;apiVersion&quot;:&quot;v1&quot;,&quot;metadata&quot;:{},&quot;status&quot;:&quot;Failure&quot;,&quot;message&quot;:&quot;Secret in version \&quot;v1\&quot; cannot be handled as a Secret: v1.Secret.Data: ReadMapCB: expect { or n, but found \&quot;, error found in #10 byte of ...| \&quot;data\&quot;: \&quot;eyJrOHNUeX|..., bigger context ...|{\&quot;apiVersion\&quot;: \&quot;v1\&quot;, \&quot;data\&quot;: \&quot;eyJrOHNUeXBlIjogImF3cyIsICJnYXJkZW5lclNlY3JldCI6IC|...&quot;,&quot;reason&quot;:&quot;BadRequest&quot;,&quot;code&quot;:400}
</code></pre>
<p>How do I have to encode the json file in order to be able to pass it to the python k8s client?</p>

harrisonfooord
<p>Data contains the secret data. Each key must consist of alphanumeric characters, '-', '_' or '.'. The serialized form of the secret data is a base64 encoded string, representing the arbitrary (possibly non-string) data value here (<a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Secret.md" rel="nofollow noreferrer">1</a>).</p>
<pre><code>V1Secret(
    api_version=&quot;v1&quot;,
    kind=&quot;Secret&quot;,
    metadata=client.V1ObjectMeta(name=name),
    data={
    'my-values.yaml': base64.b64encode(json.dumps(values).encode()).decode(&quot;utf-8&quot;)
     },
</code></pre>
<p><a href="https://github.com/kubernetes-client/python/blob/v24.2.0/examples/notebooks/create_secret.ipynb" rel="nofollow noreferrer">How to create and use a Secret</a></p>
