gcp python function - delete kubernetes namespaces, and get .kube/config file

2/20/2020

I want to create a python script in google functions - that deletes given namespace, But there is 2 problems with that - the function can't access to the global kubeconfig file - I got errors that there is no configuration file so: I have created config file, uploaded it to bucket and trying to use it from the bucket in KubeConfig() this way:

storage_client = storage.Client()
bucket = storage_client.get_bucket("kyc-tfstate-staging")
blob=bucket.get_blob("storage/config2")
conf = KubeConfig(blob)
print(conf)

I got the next output:

kubeconfig.kubeconfig.KubeConfig object at 0x7feee1868a58

after that i try to print all the namespaces that i have:

v1 = kubernetes.client.CoreV1Api()
response = v1.list_namespace()
print(response)

and I got the next error:

urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: /api/v1/namespaces (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fe49e098a90>: Failed to establish a new connection: [Errno 111] Connection refused',))
-- KrisDEV
bucket
google-cloud-platform
google-kubernetes-engine
kubernetes
python

1 Answer

2/21/2020

It seems like this is a known issue but as it can see here, in the version 3.0 must be fixed. In addition, as a workaround, you use the following:

    os.environ['KUBERNETES_SERVICE_HOST'] = 'kubernetes'
    config.load_incluster_config()
-- Samuel Romero
Source: StackOverflow