<p>I have to create a secret like this, but with Python:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl create secret generic mysecret -n mynamespace \
    --from-literal=etcdpasswd=$(echo -n &quot;PASSWORD&quot; | base64)
</code></pre>
<p>How do I do it  using the <code>create_namespaced_secret</code> API of the <code>kubernetes</code> Python client library?</p>

<p>Try this:</p>

<pre><code>from kubernetes import client, config
from kubernetes.client.rest import ApiException
import base64
import traceback
from pprint import pprint

secret_name = 'mysecret'
namespace = 'mynamespace'
data = {'etcdpasswd': base64.b64encode('&lt;PASSWORD&gt;')}

config.load_kube_config()
core_api_instance = client.CoreV1Api()
dry_run = None
pretty = 'true'
body = client.V1Secret()
body.api_version = 'v1'
body.data = data
body.kind = 'Secret'
body.metadata = {'name': secret_name}
body.type = 'Opaque'
try:
    if dry_run != None:
        api_response = core_api_instance.create_namespaced_secret(namespace, body, pretty=pretty, dry_run=dry_run)
    else:
        api_response = core_api_instance.create_namespaced_secret(namespace, body, pretty=pretty)
    pprint(api_response)
except ApiException as e:
    print("%s" % (str(e)))
    traceback.print_exc()
    raise
</code></pre>
