kubernetes-client / python Goto Github PK
View Code? Open in Web Editor NEWOfficial Python client library for kubernetes
Home Page: http://kubernetes.io/
License: Apache License 2.0
Official Python client library for kubernetes
Home Page: http://kubernetes.io/
License: Apache License 2.0
I am not sure how the account setup is going to be handled.
I have my own account in pypi where I could upload this, but there may be an official kubernetes/google way of making this happen.
Kubernetes has a number of hack/verify-*.sh scripts that some of them may be useful here. For example, to prevent #15 we could adopt kubernetes's hack/verify-boilerplate.sh. This is the issue to track efforts on finding relevant verify scripts and add them to client-python.
The client appears to be unable to parse authentication information from GKE clusters.
The exception is being raised here: https://github.com/kubernetes-incubator/client-python/blob/master/kubernetes/config/kube_config.py#L185, when _load_cluster_info
expects there to be a client-certificate
key in the user dictionary.
With the GKE
provider, my config looks like this:
{'auth-provider': {'name': 'gcp', 'config': {'expiry': datetime.datetime(2016, 12, 27, 22, 18, 24, 911838), 'access-token': '<redacted>'}}}
I also had to fix this in pykube, as follows: https://github.com/kelproject/pykube/pull/67/files. Would you accept a fix along those lines here too?
Here is the full stack trace:
Traceback (most recent call last):
File "dns_controller.py", line 17, in <module>
main()
File "dns_controller.py", line 6, in main
config.load_kube_config()
File "/Users/paul/.virtualenvs/dns-controller/lib/python3.5/site-packages/kubernetes/config/kube_config.py", line 297, in load_kube_config
client_configuration=client_configuration).load_and_set()
File "/Users/paul/.virtualenvs/dns-controller/lib/python3.5/site-packages/kubernetes/config/kube_config.py", line 203, in load_and_set
self._load_cluster_info()
File "/Users/paul/.virtualenvs/dns-controller/lib/python3.5/site-packages/kubernetes/config/kube_config.py", line 187, in _load_cluster_info
file_base_path=self._config_base_path).as_file()
File "/Users/paul/.virtualenvs/dns-controller/lib/python3.5/site-packages/kubernetes/config/kube_config.py", line 82, in as_file
if not os.path.isfile(self._file):
File "/Users/paul/.virtualenvs/dns-controller/bin/../lib/python3.5/genericpath.py", line 30, in isfile
st = os.stat(path)
TypeError: stat: can't specify None for path argument
Problem: We have two identical package for alpha2 in pypi with different names.
diagnosis: That is because we build and release the same thing on different environments (linux vs osx, different version of python).
Solution: we need to have a script to make sure the environment we are making release on, is consistent. Probably running release inside a docker container is the best solution.
Whenever I try to open an exec call I get:
{
"kind":"Status",
"apiVersion":"v1",
"metadata":{},
"status":"Failure",
"message":"Upgrade request required",
"reason":"BadRequest","code":400
}
Which is related with the fact that requests
is not SPDY compatible. Are you going to support this kind of requests in the near future?
Hello,
I'm creating deployments with a NFS volume like this:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
...
labels:
...
spec:
strategy:
type: Recreate
template:
metadata:
labels:
...
spec:
containers:
- image: ...
name: ...
imagePullPolicy: Always
env:
- name: foo
value: "bar"
ports:
- containerPort: 80
name: foobar
volumeMounts:
- mountPath: /var/www/foo
name: foo-mount
resources:
requests:
memory: "128Mi"
limits:
memory: "512Mi"
volumes:
- name: foo-mount
nfs:
path: /opt/nfs/foo
server: 10.1.0.123
Is this possible the python kubernetes client? The V1Volume don't specify the needed parameters, like the "nfs" option.
The k8s API definition for V1Volume
mention several possible options, including NFS: http://kubernetes.io/docs/api-reference/extensions/v1beta1/definitions/#_v1_volume
There is also the v1.NFSVolumeSource
: http://kubernetes.io/docs/api-reference/extensions/v1beta1/definitions/#_v1_nfsvolumesource
If this is not implemented yet, can I help somehow?
Now that we have a top level package "Kubernetes", we can separate util package into meaningful packages such as config or watch.
Please also release an sdist tarball on pypi. This is useful for distributions which want to build from source.
It does make it to the kubernetes.client.models
via this line, but then it gets lost in kubernetes/client/__init__.py
.
I would submit a PR, but it looks like the code is auto-generated, so it should be fixed somewhere else.
Either way, awesome work @mbohlool, many thanks for that!
In addition to fully wrapping the k8s client and replacing kubectl, I've found it very useful to be able to generate raw JSON/YAML for objects created using the swagger generated schema models.
This makes a few other deployment methods easier, such as Spread or Helm.
Thoughts on whether it's worth adding to this project, and where the cleanest place would be? Seems like it should likely be some utility functions in the model layer, rather than patching the client (unless there's a built-in way of doing this with the swagger-codegen client).
support kubernetes custom format "int-or-string".
enable CLA bot, Mungegithub bot, and Travis (if applicable) for this repo.
Check if our client can be run in appengine.
Some of the calls like logs does support json and text response but there is no way to get the text response as the generated client automatically select json as the accepted response. One example of this is kubernetes/kubernetes#37881. The workaround in that issue is working (and shows the value of _preload_content we added to generated code) but we can do better by having a solution like Watch class to get response as text and even stream the result if the call supports it. In this case read_namespaced_pod_log supports streaming but the parameter is named follow not watch.
Possible Solution:
v1 = client.CoreV1Api()
# either:
raw = kubernetes.raw_response.RawResponse(v1.read_namespaced_pod_log,
namespace="...", name="...")
raw.read()
# or
raw = kubernetes.raw_response.RawResponse(v1.read_namespaced_pod_log,
namespace="...", name="...", follow=True)
for line in raw.stream():
print(line)
We should follow up on these items after releasing the client:
I'm trying to give environment variables to a V1Container constructor, and particularly I want those environment variables to come from a config map. In yaml it would look like:
env:
- name: MY_ENV_VAR
valueFrom:
configMapKeyRef:
name: myconfigmap
key: foo
However, the V1ConfigMapKeySelector that is expected in the constructor of V1EnvVarSource only takes a 'key' argument, not the 'name' argument. Is this a change in the API or am I using the wrong types? My apologies if I've misunderstood! The same issue seems to apply to V1SecretKeySelector too.
Should follow up with swagger-codegen developers on the reasoning around this. We should fix the generator to have one configuration class per client.
Hi,
I have he following code snippet (I want all config maps in a specific namespace):
api_instance = client.CoreV1Api()
api_response = None
try:
api_response = api_instance.list_namespaced_config_map(namespace, pretty=True)
except ApiException as e:
print("Exception when calling CoreV1Api->list_namespaced_config_map: %s\n" % e)
but when I execute it I get the following Error (if no config map exists in the namespace) :
python3 quobyte-k8s-deployer.py
Traceback (most recent call last):
File "quobyte-k8s-deployer.py", line 126, in <module>
main()
File "quobyte-k8s-deployer.py", line 112, in main
create_configmap(quobyte_config['namespace'], quobyte_config['kubernetes_files']['path'])
File "quobyte-k8s-deployer.py", line 45, in create_configmap
api_response = api_instance.list_namespaced_config_map(namespace, pretty=True)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/apis/core_v1_api.py", line 12359, in list_namespaced_config_map
(data) = self.list_namespaced_config_map_with_http_info(namespace, **kwargs)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/apis/core_v1_api.py", line 12463, in list_namespaced_config_map_with_http_info
collection_formats=collection_formats)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api_client.py", line 328, in call_api
_return_http_data_only, collection_formats, _preload_content, _request_timeout)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api_client.py", line 160, in __call_api
return_data = self.deserialize(response_data, response_type)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api_client.py", line 236, in deserialize
return self.__deserialize(data, response_type)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api_client.py", line 281, in __deserialize
return self.__deserialize_model(data, klass)
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api_client.py", line 629, in __deserialize_model
setattr(instance, attr, self.__deserialize(value, attr_type))
File "/usr/local/lib/python3.6/site-packages/kubernetes/client/models/v1_config_map_list.py", line 108, in items
raise ValueError("Invalid value for `items`, must not be `None`")
ValueError: Invalid value for `items`, must not be `None`
So I added the following part as workaround:
except ValueError:
# no config maps found
pass
I wanted to ask if this behaviour is wanted, actually I thought the items list would be empty like this if there are no existing config maps:
{"api_version": "v1",
"items": null,
"kind": "ConfigMapList",
"metadata": {"resource_version": "3049",
"self_link": "/api/v1/namespaces/quobyte/configmaps"}}
How to reproduce:
execute api_instance.list_namespaced_config_map(namespace, pretty=True)
on an namespace without config maps.
Thanks!
When I use kubectl logs ms-999-near-090248-357595620-lze7r
, I can get container log messages. But the following code get response with None message:
import os
from kubernetes import client, config
# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config(os.environ["HOME"] + '/.kube/config')
def callback_func(response):
print(response)
api = client.CoreV1Api()
api.read_namespaced_pod_log("ms-999-near-090248-357595620-lze7r", "default", callback=callback_func)
The exec result is:
{'metadata': None, 'spec': None, 'status': None}
Is this way to call read_namespaced_pod_log
not appropriate?
I'm currently using this client for my Kubernetes Operational View dashboard, but I will probably switch to pykube as it looks much cleaner (e.g. config loading does not modify a global object), directly uses requests
(which I'm using too) and supports insecure-skip-tls-verify
(see #99).
Did you consider merging this client with pykube
or what are compelling arguments to use client-python
instead of pykube
?
The client is very low level, it would be great to add a few convenience functions ala kubectl
like:
kubectl run
kubectl expose
Right now starting a simple Pod is a bit "complex" and requires digging through the docs quite a bit to find the correct classes to instantiate. For example:
pod=client.V1Pod()
pod.metadata=client.V1ObjectMeta(name="busy box")
spec=client.V1PodSpec()
container=client.V1Container()
container.image="busybox"
container.args=["sleep", "3600"]
container.name="busybox"
spec.containers = [container]
pod.spec = spec
v1.create_namespaced_pod(namespace="default",body=pod)
It would be nice to have something like:
def run_pod(namespace, image):
....
return v1.create_namespaced_pod(namespace=namespace, body)
KubeConfigLoader does not honor the insecure-skip-tls-verify
cluster property mentioned on https://kubernetes.io/docs/user-guide/kubeconfig-file/
Obviously nobody should disable TLS verification, but it's sometimes useful for development etc 😏
is there recommended slack channel API-machinery or should we ask to create a dedicated one ?
The release notes mention that we need to uninstall the client before installing the new version.
Why not pip install --upgrade kubernetes
?
If we create a 3rd party resource, it creates a new API group.
I am not sure how kubectl implements it, but we need to have an example on how to do this with this client.
For example, say I create a 3rd party resource called snake. In kubectl I can do:
kubectl get snakes
It dynamically learns of new resources in new API groups.
There is a template project that is good to follow to include a few basic markdown files:
I tried to create a few things, here is an example to create a namespace:
import k8sclient
from k8sclient.rest import ApiException
import time
from pprint import pprint
k8sclient.configuration.host="http://127.0.0.1:8001"
api_instance=k8sclient.CoreV1Api()
body = k8sclient.V1Namespace() # V1Namespace |
body.metadata = k8sclient.V1ObjectMeta()
body.metadata.name = "foobar"
body.spec = k8sclient.V1NamespaceSpec()
body.status = k8sclient.V1NamespaceStatus()
print body
try:
api_response = api_instance.create_core_v1_namespace(body)
pprint(api_response)
except ApiException as e:
print("Exception when calling CoreV1Api->create_core_v1_namespace: %s\n" % e)
I just cannot get passed the content type error, even though reading the API docs I have all the right type and struct in place:
$ ./podcreate.py
{'metadata': {'annotations': None,
'cluster_name': None,
'creation_timestamp': None,
'deletion_grace_period_seconds': None,
'deletion_timestamp': None,
'finalizers': None,
'generate_name': None,
'generation': None,
'labels': None,
'name': 'foobar',
'namespace': None,
'owner_references': None,
'resource_version': None,
'self_link': None,
'uid': None},
'spec': {'finalizers': None},
'status': {'phase': None}}
Exception when calling CoreV1Api->create_core_v1_namespace: (0)
Reason: Cannot prepare a request message for provided arguments.
Please check that your arguments match declared content type.
Same type of error when trying to create a pod. I get this on a v1.4.0 endpoint
any suggestions ?
Right now the load kube config function loads the default context. We should support loading other contexts and [maybe?] listing them.
I saw a commit flying by that said this was fixed but I get:
>>> import os
>>> config.load_kube_config(os.environ["HOME"] + '/.kube/config')
>>> v1=client.CoreV1Api()
>>> ret = v1.list_pod_for_all_namespaces(watch=False)
2016-11-23 10:26:29,195 ERROR Certificate did not match expected hostname: 192.168.99.100. Certificate: {'subjectAltName': (('DNS', 'kubernetes.default.svc.cluster.local'), ('DNS', 'kubernetes.default.svc'), ('DNS', 'kubernetes.default'), ('DNS', 'kubernetes'), ('IP Address', '192.168.99.100'), ('IP Address', '10.0.0.1')), 'notBefore': u'Nov 16 09:38:49 2016 GMT', 'serialNumber': u'02', 'notAfter': 'Nov 16 09:38:49 2017 GMT', 'version': 3L, 'subject': ((('commonName', u'minikube'),),), 'issuer': ((('commonName', u'minikubeCA'),),)}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "kubernetes/client/apis/core_v1_api.py", line 14377, in list_pod_for_all_namespaces
(data) = self.list_pod_for_all_namespaces_with_http_info(**kwargs)
File "kubernetes/client/apis/core_v1_api.py", line 14475, in list_pod_for_all_namespaces_with_http_info
collection_formats=collection_formats)
File "kubernetes/client/api_client.py", line 326, in call_api
_return_http_data_only, collection_formats, _preload_content, _request_timeout)
File "kubernetes/client/api_client.py", line 150, in __call_api
_request_timeout=_request_timeout)
File "kubernetes/client/api_client.py", line 349, in request
headers=headers)
File "kubernetes/client/rest.py", line 222, in GET
query_params=query_params)
File "kubernetes/client/rest.py", line 199, in request
raise ApiException(status=0, reason=msg)
kubernetes.client.rest.ApiException: (0)
Reason: SSLError
hostname '192.168.99.100' doesn't match either of 'kubernetes.default.svc.cluster.local', 'kubernetes.default.svc', 'kubernetes.default', 'kubernetes', '192.168.99.100', '10.0.0.1'
Adapt client-go's InClusterConfig to enable client-python to be used inside a pod.
Things like #63 is possible with current client but not clear how to. A set of how-to articles would be really useful for these kind of stuff.
I'm wondering if asyncio was considered. If I wanted to write a Kubernetes controller/operator in Python using ThirdPartyResource, then an asynchronous programming model like aiohttp offers would definitely be a matter match than the blocking one from urllib.
I understand people have uses cases for a blocking client too, but that would be relatively easy to implement on top of an asynchronous one, while the other way around is harder.
When using relative paths in ~/.kube/config
like this:
- cluster:
certificate-authority: certs/cfm.ca.crt
The following error message is thrown:
kubernetes.client.rest.ApiException: (0)
Reason: SSLError
[Errno 2] No such file or directory
Additionally it would be nice when in case of a No such file or directory
error, the tried path would be shown. 😃
There is no top description for this project in the Github page
Current scripts/genrate.sh script assumes kubernetes is on $KUBE_ROOT and gets the swagger.spec file from there. It should actually download an specific version of spec from github instead. This version should be in passed to preprocessing script to be set inside the spec (until kubernetes set the right version).
since the nose tests are running in travis, we should add a travis badge.
I want to update the image for one of my containers in a deployment and it seems I can't provide an UnversionedPatch. Using a valid JSON patch string, or the strategic-merge string I use in kubectl ends up with variations of
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"json: cannot unmarshal string into Go value of type jsonpatch.Patch","code":500}
Am I doing something wrong or is this not implemented yet? The link to UnversionedPatch returns a 404 https://github.com/kubernetes-incubator/client-python/blob/master/kubernetes/docs/UnversionedPatch.md If it's not implemented I'd be happy to work on it.
I am going to do another release soon now that kubernetes 1.5 is out. We don't expect to change the interface at all at this point and there could be some bug fixes before 1.0.0 release.
Let me know if you have any suggestions/concerns.
@mml suggested that a multi-watcher would be a nice addition to the Watch Class. I could be a simple multi-threading class that uses existing Watch operations or we can think of something more elegant.
I would suggest moving the generated README to something like /docs
And make the README.prefix the main README
Otherwise it is a very long file.
It should also containe instructions on how to build the new client
Hi, I had made a PR to your original repo before it moved.
I guess you want me to move the PR here ?
the versions released look weird to me. See https://www.python.org/dev/peps/pep-0440/
how are you getting the latest installed?
sudo pip3 install -v -v --upgrade --pre kubernetes
Getting page https://pypi.python.org/simple/kubernetes/
URLs to search for versions for kubernetes in /usr/local/lib/python3.4/dist-packages:
* https://pypi.python.org/simple/kubernetes/
Analyzing links from page https://pypi.python.org/simple/kubernetes/
Found link https://pypi.python.org/packages/07/09/afd16baa9623eb1fd5436957b25b7438b4ea00dff91af8a1c9007bc0a910/kubernetes-1.0.0a3-py2.py3-none-any.whl#md5=1149a59d7d087d5858c1e04a1a1ab5b1 (from https://pypi.python.org/simple/kubernetes/), version: 1.0.0a3
Found link https://pypi.python.org/packages/2a/a2/9ae68644fcb0d93dc2275222c21fd8c2e49a61353328dfa90161bdec2e4e/kubernetes-1.0.0a5-py2.py3-none-any.whl#md5=8266196f912c98232aba589c0feee48b (from https://pypi.python.org/simple/kubernetes/), version: 1.0.0a5
Found link https://pypi.python.org/packages/61/c1/68e7ca2b11545cc62d89a104a1a546ddc6eb7020010192f20122f517a335/kubernetes-1.0.0a4-py2.py3-none-any.whl#md5=ddb84f6e4eb276fdf4b25d5139c43f57 (from https://pypi.python.org/simple/kubernetes/), version: 1.0.0a4
Found link https://pypi.python.org/packages/98/60/4a434a088fb6d7fc3daefbebf89e79bcd14bb49a617b62fe9c667a81582e/kubernetes-1.0.0a2.tar.gz#md5=5f4bb4b47e0051b84b0c9815c8742768 (from https://pypi.python.org/simple/kubernetes/), version: 1.0.0a2
Found link https://pypi.python.org/packages/a2/bb/dd0fc10b51e251c88da54852eafeb673630313b1e5156d1633db476444f9/kubernetes-1.0.0a5.tar.gz#md5=bef4a6332c10c2a85d89da4ec36f6989 (from https://pypi.python.org/simple/kubernetes/), version: 1.0.0a5
Found link https://pypi.python.org/packages/ab/b3/d051e868c1aeb221b343aaca13fc0979ee9f86aeeb21aedd723f99e9aff3/kubernetes-1.0.0a4.tar.gz#md5=ac798a2e262e1a3268d49e588d61e3e2 (from https://pypi.python.org/simple/kubernetes/), version: 1.0.0a4
Found link https://pypi.python.org/packages/ed/cd/b867b4836f318550539b252bde8078fd515b407a058ed47b81834b6b176f/kubernetes-1.0.0-alpha.2.tar.gz#md5=df45611842dbb0f1ee4ce92cf657853c (from https://pypi.python.org/simple/kubernetes/), version: 1.0.0-alpha.2
Installed version (1.0.0-alpha.2) is most up-to-date (past versions: 1.0.0-alpha.2, 1.0.0a5, 1.0.0a5, 1.0.0a4, 1.0.0a4, 1.0.0a3, 1.0.0a2)
For now I requested the exact version:
pip3 install --upgrade 'kubernetes==1.0.0a5'
Maybe call the next release 1.0.1 to ensure it is considered newer.
watch_* operations are deprecated in kubernetes in favor of "watch" query flag. Consider removing them from the swagger.json in the preprocessing step.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.