4

I have been trying to play around with creating secrets for Kubernetes cluster using the python client. I keep getting an error that says

Traceback (most recent call last):
File "create_secrets.py", line 19, in <module>
api_response = v1.create_namespaced_secret(namespace, body)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/apis/core_v1_api.py", line 7271, in create_namespaced_secret
(data) = self.create_namespaced_secret_with_http_info(namespace, body, **kwargs)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/apis/core_v1_api.py", line 7361, in create_namespaced_secret_with_http_info
collection_formats=collection_formats)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api_client.py", line 335, in call_api
_preload_content, _request_timeout)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api_client.py", line 148, in __call_api
_request_timeout=_request_timeout)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api_client.py", line 393, in request
body=body)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/rest.py", line 287, in POST
body=body)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/rest.py", line 240, in request
raise ApiException(http_resp=r)
kubernetes.client.rest.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json', 'Date': 'Mon, 16 Oct 2017 04:17:35 GMT', 'Content-Length': '234'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"none in version \"v1\" cannot be handled as a Secret: no kind \"none\" is registered for version \"v1\"","reason":"BadRequest","code":400}

This is my code that I am trying to execute to create a secret.

from __future__ import print_function
import time
import kubernetes.client
from pprint import pprint
from kubernetes import client, config

config.load_kube_config()
v1 = client.CoreV1Api()
namespace = 'kube-system'
metadata = {'name': 'pk-test-tls', 'namespace': 'kube-system'}
data=  {'tls.crt': '###BASE64 encoded crt###', 'tls.key': '###BASE64 encoded Key###'}
api_version = 'v1'
kind = 'none'
body = kubernetes.client.V1Secret(api_version, data , kind, metadata, 
type='kubernetes.io/tls')

api_response = v1.create_namespaced_secret(namespace, body)
pprint(api_response)

What am I missing here?

blo0old
  • 115
  • 1
  • 2
  • 7

1 Answers1

4

Almost everything that you have written is alright but pay attention to the message received from kube-apiserver:

HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"none in version "v1" cannot be handled as a Secret: no kind "none" is registered for version "v1"","reason":"BadRequest","code":400}

Especially no kind "none". Is it just typo or do you have something on your mind here?

You have list of kinds here https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md#types-kinds

If you change kind to "Secret" then everything will be working fine.

bosari
  • 1,922
  • 1
  • 19
  • 38
3h4x
  • 936
  • 8
  • 14
  • I took your advice and changed the kind = Secret and got the following error. HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Secret in version \"none\" cannot be handled as a Secret: no kind \"Secret\" is registered for version \"none\"","reason":"BadRequest","code":400} – blo0old Oct 16 '17 at 16:05
  • I then changed the api_version to v1 and that fixed it. – blo0old Oct 16 '17 at 16:06
  • You have the `api_version` = "v1" in original post. – 3h4x Oct 16 '17 at 16:38
  • @3h4x why is doesn't accept string_data. I gettin following error. `{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Secret in version \"v1\" cannot be handled as a Secret: v1.Secret.ObjectMeta: v1.ObjectMeta.TypeMeta: Kind: Data: decode base64: illegal base64 data at input byte 12` I passed string_data as argument, but it processing it as `Data` type not `stringData` type – Arsen May 15 '20 at 16:30