kubernetes-client | Java client for Kubernetes & OpenShift | REST library

 by   fabric8io Java Version: 6.3.1 License: Apache-2.0

kandi X-RAY | kubernetes-client Summary

kubernetes-client is a Java library typically used in Web Services, REST applications. kubernetes-client has no bugs, it has build file available, it has a Permissive License and it has medium support. However kubernetes-client has 1 vulnerabilities. You can download it from GitHub, Maven.
This client provides access to the full Kubernetes & OpenShift REST APIs via a fluent DSL.
    Support
      Quality
        Security
          License
            Reuse
            Support
              Quality
                Security
                  License
                    Reuse

                      kandi-support Support

                        summary
                        kubernetes-client has a medium active ecosystem.
                        summary
                        It has 2903 star(s) with 1337 fork(s). There are 128 watchers for this library.
                        summary
                        There were 4 major release(s) in the last 6 months.
                        summary
                        There are 96 open issues and 2032 have been closed. On average issues are closed in 111 days. There are 32 open pull requests and 0 closed requests.
                        summary
                        It has a neutral sentiment in the developer community.
                        summary
                        The latest version of kubernetes-client is 6.3.1
                        kubernetes-client Support
                          Best in #REST
                            Average in #REST
                            kubernetes-client Support
                              Best in #REST
                                Average in #REST

                                  kandi-Quality Quality

                                    summary
                                    kubernetes-client has 0 bugs and 0 code smells.
                                    kubernetes-client Quality
                                      Best in #REST
                                        Average in #REST
                                        kubernetes-client Quality
                                          Best in #REST
                                            Average in #REST

                                              kandi-Security Security

                                                summary
                                                kubernetes-client has 1 vulnerability issues reported (0 critical, 1 high, 0 medium, 0 low).
                                                summary
                                                kubernetes-client code analysis shows 0 unresolved vulnerabilities.
                                                summary
                                                There are 0 security hotspots that need review.
                                                kubernetes-client Security
                                                  Best in #REST
                                                    Average in #REST
                                                    kubernetes-client Security
                                                      Best in #REST
                                                        Average in #REST

                                                          kandi-License License

                                                            summary
                                                            kubernetes-client is licensed under the Apache-2.0 License. This license is Permissive.
                                                            summary
                                                            Permissive licenses have the least restrictions, and you can use them in most projects.
                                                            kubernetes-client License
                                                              Best in #REST
                                                                Average in #REST
                                                                kubernetes-client License
                                                                  Best in #REST
                                                                    Average in #REST

                                                                      kandi-Reuse Reuse

                                                                        summary
                                                                        kubernetes-client releases are available to install and integrate.
                                                                        summary
                                                                        Deployable package is available in Maven.
                                                                        summary
                                                                        Build file is available. You can build the component from source.
                                                                        summary
                                                                        Installation instructions are not available. Examples and code snippets are available.
                                                                        summary
                                                                        It has 340258 lines of code, 33353 functions and 3672 files.
                                                                        summary
                                                                        It has medium code complexity. Code complexity directly impacts maintainability of the code.
                                                                        kubernetes-client Reuse
                                                                          Best in #REST
                                                                            Average in #REST
                                                                            kubernetes-client Reuse
                                                                              Best in #REST
                                                                                Average in #REST
                                                                                  Top functions reviewed by kandi - BETA
                                                                                  kandi has reviewed kubernetes-client and discovered the below as its top functions. This is intended to give you an instant insight into kubernetes-client implemented functionality, and help decide if they suit your requirements.
                                                                                  • Build http client for mock http .
                                                                                    • Forward to the given port .
                                                                                      • Creates a cluster scoped resource .
                                                                                        • Set the Kubernetes configuration from the system properties
                                                                                          • Normalize a file name .
                                                                                            • Activate the Kubernetes configuration .
                                                                                              • Get the quantity in bytes .
                                                                                                • Performs a roll update .
                                                                                                  • Initialize the role bindings .
                                                                                                    • Handle a patch request .
                                                                                                      Get all kandi verified functions for this library.
                                                                                                      Get all kandi verified functions for this library.

                                                                                                      kubernetes-client Key Features

                                                                                                      Java client for Kubernetes & OpenShift

                                                                                                      kubernetes-client Examples and Code Snippets

                                                                                                      No Code Snippets are available at this moment for kubernetes-client.
                                                                                                      Community Discussions

                                                                                                      Trending Discussions on kubernetes-client

                                                                                                      How to spawn a docker container in a remote machine
                                                                                                      chevron right
                                                                                                      Unable to exec command into kubernetes pod
                                                                                                      chevron right
                                                                                                      Create a gke pod from a google cloud function
                                                                                                      chevron right
                                                                                                      How to read a Kubernetes Deployment with python kubernetes client
                                                                                                      chevron right
                                                                                                      How to use python kubernetes-client to get given resources' corresponding YAML file
                                                                                                      chevron right
                                                                                                      Is "current-context" a mandatory key in a kubeconfig file?
                                                                                                      chevron right
                                                                                                      Accessing a service in Kubernetes via the Kubernetes Python client
                                                                                                      chevron right
                                                                                                      Call Pod by IP from another Pod
                                                                                                      chevron right
                                                                                                      Micronaut app fails to access values from Kubernetes config map
                                                                                                      chevron right
                                                                                                      Kubernetes Operators: Informers vs. reconcile loop
                                                                                                      chevron right

                                                                                                      QUESTION

                                                                                                      How to spawn a docker container in a remote machine
                                                                                                      Asked 2022-Mar-28 at 05:16

                                                                                                      Is it possible, using the docker SDK for Python, to launch a container in a remote machine?

                                                                                                      import docker
                                                                                                      client = docker.from_env()
                                                                                                      
                                                                                                      client.containers.run("bfirsh/reticulate-splines", detach=True)
                                                                                                      # I'd like to run this container ^^^ in a machine that I have ssh access to.
                                                                                                      

                                                                                                      Going through the documentation it seems like this type of management is out of scope for said SDK, so searching online I got hints that the kubernetes client for Python could be of help, but don't know where to begin.

                                                                                                      ANSWER

                                                                                                      Answered 2022-Mar-21 at 11:49

                                                                                                      It's possible, simply do this:

                                                                                                      client = docker.DockerClient(base_url=your_remote_docker_url)
                                                                                                      

                                                                                                      Here's the document I found related to this:

                                                                                                      https://docker-py.readthedocs.io/en/stable/client.html#client-reference

                                                                                                      If you only has SSH access to it, there is an use_ssh_client option

                                                                                                      Source https://stackoverflow.com/questions/71531344

                                                                                                      QUESTION

                                                                                                      Unable to exec command into kubernetes pod
                                                                                                      Asked 2022-Mar-21 at 21:45

                                                                                                      Python version 3.8.10 Kubernetes version 23.3.0

                                                                                                      I'm trying to run a command into a specific pod in kubernetes using python. I've tried to reduce the code as much as I could, so I'm running this.

                                                                                                      from kubernetes import client, config
                                                                                                      
                                                                                                      config.load_kube_config()
                                                                                                      
                                                                                                      v1 = client.CoreV1Api()
                                                                                                      response = v1.connect_get_namespaced_pod_exec(pod_name , namespace, command="df -h", stderr=True, stdin=True, stdout=True, tty=True)
                                                                                                      print(response)
                                                                                                      
                                                                                                      

                                                                                                      But it's not working. I'm getting this response.

                                                                                                      kubernetes.client.exceptions.ApiException: (400)
                                                                                                      Reason: Bad Request
                                                                                                      HTTP response headers: HTTPHeaderDict({'Audit-Id': '511c23ce-03bb-4b52-a559-3f354fc80235', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'Date': 'Fri, 18 Mar 2022 18:06:11 GMT', 'Content-Length': '139'})
                                                                                                      HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Upgrade request required","reason":"BadRequest","code":400}
                                                                                                      

                                                                                                      If I run typical example of list all pods . It's working fine. So, it should not be a configuration issue. I've read about this problem in the past here and here. But I assume it cannot be that, due to they are closed issues.

                                                                                                      If I run k9s shell request, I can connect with pod with no problem. This is what I see in ps a when I'm doing this /usr/bin/kubectl --context gke_cloudpak_europe-west2-xxxxx exec -it -n namespace_name pod_name -c rt -- sh -c command -v bash >/dev/null && exec bash || exec sh

                                                                                                      Another update, I've found this info. At last of page there is a paragraph with says.

                                                                                                      Why Exec/Attach calls doesn’t work
                                                                                                      Starting from 4.0 release, we do not support directly calling exec or attach calls. you should use stream module to call them. so instead of resp = api.connect_get_namespaced_pod_exec(name, ... you should call resp = stream(api.connect_get_namespaced_pod_exec, name, ....
                                                                                                      
                                                                                                      Using Stream will overwrite the requests protocol in core_v1_api.CoreV1Api() This will cause a failure in non-exec/attach calls. If you reuse your api client object, you will need to recreate it between api calls that use stream and other api calls.
                                                                                                      

                                                                                                      I've tried to do it in this way, but same result :(

                                                                                                      Any idea about what I'm doing wrong?

                                                                                                      Thanks a lot for your help.

                                                                                                      Regards

                                                                                                      ANSWER

                                                                                                      Answered 2022-Mar-21 at 21:45

                                                                                                      Yes, this official guide says that you should use resp = **stream**(api.connect_get_namespaced_pod_exec(name, ... instead.

                                                                                                      So you have to edit your code like this:

                                                                                                      ...
                                                                                                      from kubernetes.stream import stream
                                                                                                      ...
                                                                                                      v1 = client.CoreV1Api()
                                                                                                      response = stream(v1.connect_get_namespaced_pod_exec, pod_name , namespace, command="df -h", stderr=True, stdin=True, stdout=True, tty=True)
                                                                                                      print(response)
                                                                                                      

                                                                                                      Source https://stackoverflow.com/questions/71531543

                                                                                                      QUESTION

                                                                                                      Create a gke pod from a google cloud function
                                                                                                      Asked 2022-Feb-17 at 10:21

                                                                                                      I want to create a google cloud function to create pods on my gke cluster. I use the python kubernetes client to create them (I don't know if there is a better way to achive this).

                                                                                                      Normally I would use the command: gcloud container clusters get-credentials cluster_name --region=cluster_region but cloud sdk is not installed in the cloud function environment.

                                                                                                      I've read the python api documentation and I found that it is possible to pass the path to the kubeconfig file, but I didn't found how to create that file

                                                                                                      ANSWER

                                                                                                      Answered 2022-Feb-09 at 16:11

                                                                                                      The get credential does nothing special:

                                                                                                      • Check if the cluster exists
                                                                                                      • Check if you have the permission on the cluster
                                                                                                      • Create the Kube config file with your access token.

                                                                                                      That's all.

                                                                                                      Now, when you use the kubectl command, the access token is used and put in the Authorization: Bearer header and perform an API call to Kubernetes control plane.

                                                                                                      Therefore, if you want to reach directly the control plane from your Cloud Functions with an API call, simply the Cloud Functions access token in the security header and that's all!

                                                                                                      Source https://stackoverflow.com/questions/71051154

                                                                                                      QUESTION

                                                                                                      How to read a Kubernetes Deployment with python kubernetes client
                                                                                                      Asked 2022-Feb-02 at 08:21

                                                                                                      what is python kubernetes client equivalent for

                                                                                                      kubectl get deploy -o yaml 
                                                                                                      

                                                                                                      CRUD python Client example

                                                                                                      i referred this example for getting python deployment but there is no read deployment option

                                                                                                      ANSWER

                                                                                                      Answered 2022-Feb-02 at 08:21

                                                                                                      read_namespaced_deployment() does the thing:

                                                                                                      from kubernetes import client, config
                                                                                                      config.load_kube_config()
                                                                                                      api = client.AppsV1Api()
                                                                                                      
                                                                                                      deployment = api.read_namespaced_deployment(name='foo', namespace='bar')
                                                                                                      

                                                                                                      Source https://stackoverflow.com/questions/70952155

                                                                                                      QUESTION

                                                                                                      How to use python kubernetes-client to get given resources' corresponding YAML file
                                                                                                      Asked 2022-Feb-01 at 21:38

                                                                                                      With kubectl, I know i can run below command if I want to see specific resources YAML file

                                                                                                      kubectl -n  get   -o yaml
                                                                                                      

                                                                                                      How would I get this same data using python's kubernetes-client ? Everything I've found so far only talks about creating a resource from a given yaml file.

                                                                                                      In looking at docs, I noticed that each resource type generally has a get_api_resources() function which returns a V1ApiResourceList, where each item is a V1ApiResource. I was hoping there would be a way to get the resource's yaml-output by using a V1ApiResource object, but doesnt appear like that's the way to go about it.

                                                                                                      Do you all have any suggestions ? Is this possible with kubernetes-client API ?

                                                                                                      ANSWER

                                                                                                      Answered 2022-Feb-01 at 21:38

                                                                                                      If you take a look at the methods available on an object, e.g.:

                                                                                                      >>> import kubernetes.config
                                                                                                      >>> client = kubernetes.config.new_client_from_config()
                                                                                                      >>> core = kubernetes.client.CoreV1Api(client)
                                                                                                      >>> res = core.read_namespace('kube-system')
                                                                                                      >>> dir(res)
                                                                                                      ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_api_version', '_kind', '_metadata', '_spec', '_status', 'api_version', 'attribute_map', 'discriminator', 'kind', 'local_vars_configuration', 'metadata', 'openapi_types', 'spec', 'status', 'to_dict', 'to_str']
                                                                                                      

                                                                                                      ...you'll see there is a to_dict method. That returns the object as a dictionary, which you can then serialize to YAML or JSON or whatever:

                                                                                                      >>> import yaml
                                                                                                      >>> print(yaml.safe_dump(res.to_dict()))
                                                                                                      api_version: v1
                                                                                                      kind: Namespace
                                                                                                      metadata:
                                                                                                      [...]
                                                                                                      

                                                                                                      Source https://stackoverflow.com/questions/70946978

                                                                                                      QUESTION

                                                                                                      Is "current-context" a mandatory key in a kubeconfig file?
                                                                                                      Asked 2022-Jan-31 at 17:24

                                                                                                      THE PLOT:

                                                                                                      I am working on a kubernetes environment where we have PROD and ITG setup. The ITG setup has multi-cluster environment whereas PROD setup is a single-cluster environment. I am trying to automate some process using Python where I have to deal with kubeconfig file and I am using the kubernetes library for it.

                                                                                                      THE PROBLEM:

                                                                                                      The kubeconfig file for PROD has "current-context" key available but the same is missing from the kubeconfig file for ITG.

                                                                                                      prdconfig:

                                                                                                      apiVersion: v1
                                                                                                      clusters:
                                                                                                      - cluster:
                                                                                                          insecure-skip-tls-verify: true
                                                                                                          server: https://cluster3.url.com:3600
                                                                                                        name: cluster-ABC
                                                                                                      contexts:
                                                                                                      - context:
                                                                                                          cluster: cluster-LMN
                                                                                                          user: cluster-user
                                                                                                        name: cluster-LMN-context
                                                                                                      current-context: cluster-LMN-context
                                                                                                      kind: Config
                                                                                                      preferences: {}
                                                                                                      users:
                                                                                                      - name: cluster-user
                                                                                                        user:
                                                                                                          exec:
                                                                                                            command: kubectl
                                                                                                            apiVersion: 
                                                                                                            args:
                                                                                                            - kubectl-custom-plugin
                                                                                                            - authenticate
                                                                                                            - https://cluster.url.com:8080
                                                                                                            - --user=user
                                                                                                            - --token=/api/v2/session/xxxx
                                                                                                            - --token-expiry=1000000000
                                                                                                            - --force-reauth=false
                                                                                                            - --insecure-skip-tls-verify=true
                                                                                                      

                                                                                                      itgconfig:

                                                                                                      apiVersion: v1
                                                                                                      clusters:
                                                                                                      - cluster:
                                                                                                          insecure-skip-tls-verify: true
                                                                                                          server: https://cluster1.url.com:3600
                                                                                                        name: cluster-ABC
                                                                                                      - cluster:
                                                                                                          insecure-skip-tls-verify: true
                                                                                                          server: https://cluster2.url.com:3601
                                                                                                        name: cluster-XYZ
                                                                                                      contexts:
                                                                                                      - context:
                                                                                                          cluster: cluster-ABC
                                                                                                          user: cluster-user
                                                                                                        name: cluster-ABC-context
                                                                                                      - context:
                                                                                                          cluster: cluster-XYZ
                                                                                                          user: cluster-user
                                                                                                        name: cluster-XYZ-context
                                                                                                      kind: Config
                                                                                                      preferences: {}
                                                                                                      users:
                                                                                                      - name: cluster-user
                                                                                                        user:
                                                                                                          exec:
                                                                                                            command: kubectl
                                                                                                            apiVersion: 
                                                                                                            args:
                                                                                                            - kubectl-custom-plugin
                                                                                                            - authenticate
                                                                                                            - https://cluster.url.com:8080
                                                                                                            - --user=user
                                                                                                            - --token=/api/v2/session/xxxx
                                                                                                            - --token-expiry=1000000000
                                                                                                            - --force-reauth=false
                                                                                                            - --insecure-skip-tls-verify=true
                                                                                                      

                                                                                                      When I try loading the kubeconfig file for PROD using config.load_kube_config(os.path.expanduser('~/.kube/prdconfig')) it works.

                                                                                                      And when I try loading the kubeconfig file for ITG using config.load_kube_config(os.path.expanduser('~/.kube/itgconfig')), I get the following error:

                                                                                                      ConfigException: Invalid kube-config file. Expected key current-context in C:\Users/.kube/itgconfig

                                                                                                      Although it is very clear from the error message that it is considering the kubeconfig file as invalid, as it does not have "current-context" key in it.

                                                                                                      THE SUB-PLOT:

                                                                                                      When working with kubectl, the missing "current-context" does not make any difference as we can always specify context along with the command. But the 'load_kube_config()' function makes it mandatory to have "current-context" available.

                                                                                                      THE QUESTION:

                                                                                                      So, is "current-context" a mandatory key in kubeconfig file?

                                                                                                      THE DISCLAIMER:

                                                                                                      I am very new to kubernetes and have very little experience working with it.

                                                                                                      ANSWER

                                                                                                      Answered 2021-Aug-25 at 09:01

                                                                                                      As described in the comments: If we want to use kubeconfig file to work out of the box by default, with specific cluster using kubectl or python script we can mark one of the contexts in our kubeconfig file as the default by specifying current-context.

                                                                                                      Note about Context:

                                                                                                      A context element in a kubeconfig file is used to group access parameters under a convenient name. Each context has three parameters: cluster, namespace, and user. By default, the kubectl command-line tool uses parameters from the current context to communicate with the cluster.

                                                                                                      In order to mark one of our contexts (f.e. dev-fronted) in our kubeconfig file as the default one please run:

                                                                                                      kubectl config use-context dev-fronted
                                                                                                      

                                                                                                      Now whenever you run a kubectl command, the action will apply to the cluster, and namespace listed in the dev-frontend context. And the command will use the credentials of the user listed in the dev-frontend context

                                                                                                      Please take a look at:

                                                                                                      - Mering kubeconfig files:

                                                                                                      1. determine the context to use based on the first hit in this chain:

                                                                                                        Use the --context command-line flag if it exists. Use the current-context from the merged kubeconfig files.

                                                                                                      An empty context is allowed at this point.

                                                                                                      1. determine the cluster and user. At this point, there might or might not be a context. Determine the cluster and user based on the first hit in this chain, which is run twice: once for user and once for cluster:

                                                                                                        Use a command-line flag if it exists: --user or --cluster. If the context is non-empty, take the user or cluster from the context.

                                                                                                      The user and cluster can be empty at this point.

                                                                                                      Whenever we run kubectl commands without specified current-context we should provide additional configuration parameters to tell kubectl which configuration to use, in your example it could be f.e.:

                                                                                                      kubectl --kubeconfig=/your_directory/itgconfig get pods --context cluster-ABC-context
                                                                                                      

                                                                                                      As described earlier - to simplify this task we can use configure current-context in kubeconfig file configuration:

                                                                                                      kubectl config --kubeconfig=c/your_directory/itgconfig use-context cluster-ABC-context
                                                                                                      

                                                                                                      Going further into errors generated by your script we should notice errors from config/kube_config.py:

                                                                                                      config/kube_config.py", line 257, in set_active_context context_name = self._config['current-context']
                                                                                                      
                                                                                                      kubernetes.config.config_exception.ConfigException:: Invalid kube-config file. Expected key current-context in ...
                                                                                                      

                                                                                                      Here is an example with additional context="cluster-ABC-context" parameter:

                                                                                                      from kubernetes import client, config
                                                                                                      
                                                                                                      config.load_kube_config(config_file='/example/data/merged/itgconfig', context="cluster-ABC-context")
                                                                                                      
                                                                                                      v1 = client.CoreV1Api()
                                                                                                      print("Listing pods with their IPs:")
                                                                                                      ret = v1.list_pod_for_all_namespaces(watch=False)
                                                                                                      for i in ret.items:
                                                                                                          print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
                                                                                                      
                                                                                                      ...
                                                                                                      Listing pods with their IPs:
                                                                                                      10.200.xxx.xxx  kube-system coredns-558bd4d5db-qpzb8
                                                                                                      192.168.xxx.xxx kube-system etcd-debian-test
                                                                                                      ...
                                                                                                      

                                                                                                      Source https://stackoverflow.com/questions/68888442

                                                                                                      QUESTION

                                                                                                      Accessing a service in Kubernetes via the Kubernetes Python client
                                                                                                      Asked 2022-Jan-28 at 11:21

                                                                                                      I have a service running in Kubernetes and currently, there are two ways of making GET requests to the REST API.

                                                                                                      The first is

                                                                                                      kubectl port-forward --namespace test service/test-svc 9090
                                                                                                      

                                                                                                      and then running

                                                                                                      curl http://localhost:9090/sub/path \
                                                                                                      -d param1=abcd \
                                                                                                      -d param2=efgh \
                                                                                                      -G
                                                                                                      

                                                                                                      For the second one, we do a kubctl proxy

                                                                                                      kubectl proxy --port=8080
                                                                                                      

                                                                                                      followed by

                                                                                                      curl -lk 'http://127.0.0.1:8080/api/v1/namespaces/test/services/test-svc:9090/proxy/sub/path?param1=abcd&param2=efgh'
                                                                                                      

                                                                                                      Both work nicely. However, my question is: How do we repeat one of these with the Python Kubernetes client (https://github.com/kubernetes-client/python)?

                                                                                                      Many thanks for your support in advance!

                                                                                                      Progress

                                                                                                      I found a solution that brings us closer to the desired result:

                                                                                                      from kubernetes import client, config
                                                                                                      
                                                                                                      
                                                                                                      config.load_kube_config("~/.kube/config", context="my-context")
                                                                                                      
                                                                                                      api_instance = client.CoreV1Api()
                                                                                                      
                                                                                                      
                                                                                                      
                                                                                                      name = 'test-svc' # str | name of the ServiceProxyOptions
                                                                                                      namespace = 'test' # str | object name and auth scope, such as for teams and projects
                                                                                                      
                                                                                                      api_response = api_instance.api_client.call_api(
                                                                                                      '/api/v1/namespaces/{namespace}/services/{name}/proxy/ping'.format(namespace=namespace, name=name), 'GET',
                                                                                                          auth_settings = ['BearerToken'], response_type='json', _preload_content=False
                                                                                                      )
                                                                                                      
                                                                                                      print(api_response)
                                                                                                      

                                                                                                      yet the result is

                                                                                                      (, 200, HTTPHeaderDict({'Audit-Id': '1ad9861c-f796-4e87-a16d-8328790c50c3', 'Cache-Control': 'no-cache, private', 'Content-Length': '16', 'Content-Type': 'application/json', 'Date': 'Thu, 27 Jan 2022 15:05:10 GMT', 'Server': 'uvicorn'}))
                                                                                                      

                                                                                                      Whereas the desired output was

                                                                                                      {
                                                                                                        "ping": "pong!"
                                                                                                      }
                                                                                                      

                                                                                                      Do you know how to extract it form here?

                                                                                                      ANSWER

                                                                                                      Answered 2022-Jan-28 at 11:21

                                                                                                      This should be something which uses:

                                                                                                      from kubernetes.stream import portforward
                                                                                                      

                                                                                                      To find which command maps to an API call in Python, you can used

                                                                                                      kubectl -v 10 ...
                                                                                                      

                                                                                                      For example:

                                                                                                      k -v 10 port-forward --namespace znc service/znc 1666
                                                                                                      

                                                                                                      It spits a lot of output, the most important out put is the curl commands:

                                                                                                      POST https://myk8s:16443/api/v1/namespaces/znc/pods/znc-57647bb8d8-dcq6b/portforward 101 Switching Protocols in 123 milliseconds
                                                                                                      

                                                                                                      This allows you to search the code of the python client. For example there is:

                                                                                                      core_v1.connect_get_namespaced_pod_portforward
                                                                                                      

                                                                                                      However, using it is not so straight forward. Luckily, the maintainers include a great example on how to use portforward method:

                                                                                                      # Copyright 2020 The Kubernetes Authors.
                                                                                                      #
                                                                                                      # Licensed under the Apache License, Version 2.0 (the "License");
                                                                                                      # you may not use this file except in compliance with the License.
                                                                                                      # You may obtain a copy of the License at
                                                                                                      #
                                                                                                      #     http://www.apache.org/licenses/LICENSE-2.0
                                                                                                      #
                                                                                                      # Unless required by applicable law or agreed to in writing, software
                                                                                                      # distributed under the License is distributed on an "AS IS" BASIS,
                                                                                                      # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
                                                                                                      # See the License for the specific language governing permissions and
                                                                                                      # limitations under the License.
                                                                                                      
                                                                                                      """
                                                                                                      Shows the functionality of portforward streaming using an nginx container.
                                                                                                      """
                                                                                                      
                                                                                                      import select
                                                                                                      import socket
                                                                                                      import time
                                                                                                      
                                                                                                      import six.moves.urllib.request as urllib_request
                                                                                                      
                                                                                                      from kubernetes import config
                                                                                                      from kubernetes.client import Configuration
                                                                                                      from kubernetes.client.api import core_v1_api
                                                                                                      from kubernetes.client.rest import ApiException
                                                                                                      from kubernetes.stream import portforward
                                                                                                      
                                                                                                      ##############################################################################
                                                                                                      # Kubernetes pod port forwarding works by directly providing a socket which
                                                                                                      # the python application uses to send and receive data on. This is in contrast
                                                                                                      # to the go client, which opens a local port that the go application then has
                                                                                                      # to open to get a socket to transmit data.
                                                                                                      #
                                                                                                      # This simplifies the python application, there is not a local port to worry
                                                                                                      # about if that port number is available. Nor does the python application have
                                                                                                      # to then deal with opening this local port. The socket used to transmit data
                                                                                                      # is immediately provided to the python application.
                                                                                                      #
                                                                                                      # Below also is an example of monkey patching the socket.create_connection
                                                                                                      # function so that DNS names of the following formats will access kubernetes
                                                                                                      # ports:
                                                                                                      #
                                                                                                      #    ..kubernetes
                                                                                                      #    .pod..kubernetes
                                                                                                      #    .svc..kubernetes
                                                                                                      #    .service..kubernetes
                                                                                                      #
                                                                                                      # These DNS name can be used to interact with pod ports using python libraries,
                                                                                                      # such as urllib.request and http.client. For example:
                                                                                                      #
                                                                                                      # response = urllib.request.urlopen(
                                                                                                      #     'https://metrics-server.service.kube-system.kubernetes/'
                                                                                                      # )
                                                                                                      #
                                                                                                      ##############################################################################
                                                                                                      
                                                                                                      
                                                                                                      def portforward_commands(api_instance):
                                                                                                          name = 'portforward-example'
                                                                                                          resp = None
                                                                                                          try:
                                                                                                              resp = api_instance.read_namespaced_pod(name=name,
                                                                                                                                                      namespace='default')
                                                                                                          except ApiException as e:
                                                                                                              if e.status != 404:
                                                                                                                  print("Unknown error: %s" % e)
                                                                                                                  exit(1)
                                                                                                      
                                                                                                          if not resp:
                                                                                                              print("Pod %s does not exist. Creating it..." % name)
                                                                                                              pod_manifest = {
                                                                                                                  'apiVersion': 'v1',
                                                                                                                  'kind': 'Pod',
                                                                                                                  'metadata': {
                                                                                                                      'name': name
                                                                                                                  },
                                                                                                                  'spec': {
                                                                                                                      'containers': [{
                                                                                                                          'image': 'nginx',
                                                                                                                          'name': 'nginx',
                                                                                                                      }]
                                                                                                                  }
                                                                                                              }
                                                                                                              api_instance.create_namespaced_pod(body=pod_manifest,
                                                                                                                                                 namespace='default')
                                                                                                              while True:
                                                                                                                  resp = api_instance.read_namespaced_pod(name=name,
                                                                                                                                                          namespace='default')
                                                                                                                  if resp.status.phase != 'Pending':
                                                                                                                      break
                                                                                                                  time.sleep(1)
                                                                                                              print("Done.")
                                                                                                      
                                                                                                          pf = portforward(
                                                                                                              api_instance.connect_get_namespaced_pod_portforward,
                                                                                                              name, 'default',
                                                                                                              ports='80',
                                                                                                          )
                                                                                                          http = pf.socket(80)
                                                                                                          http.setblocking(True)
                                                                                                          http.sendall(b'GET / HTTP/1.1\r\n')
                                                                                                          http.sendall(b'Host: 127.0.0.1\r\n')
                                                                                                          http.sendall(b'Connection: close\r\n')
                                                                                                          http.sendall(b'Accept: */*\r\n')
                                                                                                          http.sendall(b'\r\n')
                                                                                                          response = b''
                                                                                                          while True:
                                                                                                              select.select([http], [], [])
                                                                                                              data = http.recv(1024)
                                                                                                              if not data:
                                                                                                                  break
                                                                                                              response += data
                                                                                                          http.close()
                                                                                                          print(response.decode('utf-8'))
                                                                                                          error = pf.error(80)
                                                                                                          if error is None:
                                                                                                              print("No port forward errors on port 80.")
                                                                                                          else:
                                                                                                              print("Port 80 has the following error: %s" % error)
                                                                                                      
                                                                                                          # Monkey patch socket.create_connection which is used by http.client and
                                                                                                          # urllib.request. The same can be done with urllib3.util.connection.create_connection
                                                                                                          # if the "requests" package is used.
                                                                                                          socket_create_connection = socket.create_connection
                                                                                                      
                                                                                                          def kubernetes_create_connection(address, *args, **kwargs):
                                                                                                              dns_name = address[0]
                                                                                                              if isinstance(dns_name, bytes):
                                                                                                                  dns_name = dns_name.decode()
                                                                                                              dns_name = dns_name.split(".")
                                                                                                              if dns_name[-1] != 'kubernetes':
                                                                                                                  return socket_create_connection(address, *args, **kwargs)
                                                                                                              if len(dns_name) not in (3, 4):
                                                                                                                  raise RuntimeError("Unexpected kubernetes DNS name.")
                                                                                                              namespace = dns_name[-2]
                                                                                                              name = dns_name[0]
                                                                                                              port = address[1]
                                                                                                              if len(dns_name) == 4:
                                                                                                                  if dns_name[1] in ('svc', 'service'):
                                                                                                                      service = api_instance.read_namespaced_service(name, namespace)
                                                                                                                      for service_port in service.spec.ports:
                                                                                                                          if service_port.port == port:
                                                                                                                              port = service_port.target_port
                                                                                                                              break
                                                                                                                      else:
                                                                                                                          raise RuntimeError(
                                                                                                                              "Unable to find service port: %s" % port)
                                                                                                                      label_selector = []
                                                                                                                      for key, value in service.spec.selector.items():
                                                                                                                          label_selector.append("%s=%s" % (key, value))
                                                                                                                      pods = api_instance.list_namespaced_pod(
                                                                                                                          namespace, label_selector=",".join(label_selector)
                                                                                                                      )
                                                                                                                      if not pods.items:
                                                                                                                          raise RuntimeError("Unable to find service pods.")
                                                                                                                      name = pods.items[0].metadata.name
                                                                                                                      if isinstance(port, str):
                                                                                                                          for container in pods.items[0].spec.containers:
                                                                                                                              for container_port in container.ports:
                                                                                                                                  if container_port.name == port:
                                                                                                                                      port = container_port.container_port
                                                                                                                                      break
                                                                                                                              else:
                                                                                                                                  continue
                                                                                                                              break
                                                                                                                          else:
                                                                                                                              raise RuntimeError(
                                                                                                                                  "Unable to find service port name: %s" % port)
                                                                                                                  elif dns_name[1] != 'pod':
                                                                                                                      raise RuntimeError(
                                                                                                                          "Unsupported resource type: %s" %
                                                                                                                          dns_name[1])
                                                                                                              pf = portforward(api_instance.connect_get_namespaced_pod_portforward,
                                                                                                                               name, namespace, ports=str(port))
                                                                                                              return pf.socket(port)
                                                                                                          socket.create_connection = kubernetes_create_connection
                                                                                                      
                                                                                                          # Access the nginx http server using the
                                                                                                          # ".pod..kubernetes" dns name.
                                                                                                          response = urllib_request.urlopen(
                                                                                                              'http://%s.pod.default.kubernetes' % name)
                                                                                                          html = response.read().decode('utf-8')
                                                                                                          response.close()
                                                                                                          print('Status Code: %s' % response.code)
                                                                                                          print(html)
                                                                                                      
                                                                                                      
                                                                                                      def main():
                                                                                                          config.load_kube_config()
                                                                                                          c = Configuration.get_default_copy()
                                                                                                          c.assert_hostname = False
                                                                                                          Configuration.set_default(c)
                                                                                                          core_v1 = core_v1_api.CoreV1Api()
                                                                                                      
                                                                                                          portforward_commands(core_v1)
                                                                                                      
                                                                                                      
                                                                                                      if __name__ == '__main__':
                                                                                                          main()
                                                                                                      

                                                                                                      Source https://stackoverflow.com/questions/70869655

                                                                                                      QUESTION

                                                                                                      Call Pod by IP from another Pod
                                                                                                      Asked 2022-Jan-26 at 04:54

                                                                                                      I've developed a python script, using python kubernetes-client to harvest Pods' internal IPs.

                                                                                                      But when I try to make an http request to these IPs, from another pod, I get Connection refused error.

                                                                                                      I spin up a temporary curl container:

                                                                                                      kubectl run curl --image=radial/busyboxplus:curl -it --rm
                                                                                                      

                                                                                                      And having the internal IP of one of the pods, I try to make a GET request:

                                                                                                      curl http://10.133.0.2/stats
                                                                                                      

                                                                                                      and the response is:

                                                                                                      curl: (7) Failed to connect to 10.133.0.2 port 80: Connection refused
                                                                                                      

                                                                                                      Both pods are in the same default namespace and use the same default ServiceAccount.

                                                                                                      I know that I can call the Pods thru the ClusterIP service by which they're load-balanced, but this way I will only access a single Pod at random (depending which one the service forwards the call to), when I have multiple replicas of the same Deployment.

                                                                                                      I need to be able to call each Pod of a multi-replica Deployment separately. That's why I'm going for the internal IPs.

                                                                                                      ANSWER

                                                                                                      Answered 2022-Jan-26 at 04:54

                                                                                                      I guess you missed the port number here

                                                                                                      It should be like this

                                                                                                      curl POD_IP:PORT/stats

                                                                                                      Source https://stackoverflow.com/questions/70852058

                                                                                                      QUESTION

                                                                                                      Micronaut app fails to access values from Kubernetes config map
                                                                                                      Asked 2022-Jan-04 at 11:21

                                                                                                      I'm using Micronaut 3.2.3 with the Kubernetes integration to inject configuration values from config maps and secrets.

                                                                                                      Dependencies:

                                                                                                          implementation("io.micronaut.kubernetes:micronaut-kubernetes-client")
                                                                                                          implementation("io.micronaut.kubernetes:micronaut-kubernetes-discovery-client")
                                                                                                      

                                                                                                      bootstrap.yml

                                                                                                      micronaut:
                                                                                                        application:
                                                                                                          name: ingestor
                                                                                                        config-client:
                                                                                                          enabled: true
                                                                                                      
                                                                                                      kubernetes:
                                                                                                        client:
                                                                                                          config-maps:
                                                                                                            enabled: true
                                                                                                            includes:
                                                                                                              - application
                                                                                                              - ingestor
                                                                                                          secrets:
                                                                                                            enabled: true
                                                                                                            includes:
                                                                                                              - application
                                                                                                              - ingestor
                                                                                                      

                                                                                                      As you can see the application config map includes the kafka.brokers value:

                                                                                                      kubectl get configmaps application -o yaml   
                                                                                                        
                                                                                                      apiVersion: v1
                                                                                                      data:
                                                                                                        application.yml: |
                                                                                                          kafka.brokers: xxx
                                                                                                      
                                                                                                      
                                                                                                      kubectl get configmaps ingestor -o yaml   
                                                                                                        
                                                                                                      apiVersion: v1
                                                                                                      data:
                                                                                                        application.yml: |
                                                                                                          prop1: value1
                                                                                                      

                                                                                                      I've added a simple singleton class the check whether the projerty can be injected:

                                                                                                      @Singleton
                                                                                                      class SomeConfiguration(@Value("\${kafka.brokers}") private val kafkaBrokers: String) {
                                                                                                      
                                                                                                          init {
                                                                                                              println(kafkaBrokers)
                                                                                                          }
                                                                                                      
                                                                                                      }
                                                                                                      

                                                                                                      Log traces seem to indicate that those config maps are correctly accessed:

                                                                                                      15:59:24.206 [OkHttp https://10.222.0.1/...] -  -   DEBUG i.m.k.c.KubernetesConfigurationClient - Adding config map with name application
                                                                                                      15:59:24.218 [OkHttp https://10.222.0.1/...] -  -   DEBUG i.m.k.c.KubernetesConfigurationClient - Adding config map with name ingestor
                                                                                                      

                                                                                                      However the application crashes because it couln'd find it:

                                                                                                      fun main(args: Array) {
                                                                                                          build()
                                                                                                              .args(*args)
                                                                                                              .eagerInitSingletons(true)
                                                                                                      
                                                                                                      15:59:25.484 [main] -  -   ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [xxx.SomeConfiguration] could not be loaded: Failed to inject value for parameter [kafkaBrokers] of class: xxx.configuration.SomeConfiguration
                                                                                                      
                                                                                                      Message: Failed to inject value for parameter [kafkaBrokers] of class: xxx.configuration.SomeConfiguration
                                                                                                      
                                                                                                      Message: Error resolving property value [${kafka.brokers}]. Property doesn't exist
                                                                                                      
                                                                                                      

                                                                                                      ANSWER

                                                                                                      Answered 2022-Jan-04 at 11:21

                                                                                                      This problem was kind of tricky. The key was the name of the yml file used in the config maps, both application and ingestor config maps defined it as application.yml:

                                                                                                      apiVersion: v1
                                                                                                      data:
                                                                                                        application.yml: |
                                                                                                      

                                                                                                      They can't overlap, so I changed the ingestor configmap to be:

                                                                                                      kubectl get configmaps ingestor -o yaml   
                                                                                                        
                                                                                                      apiVersion: v1
                                                                                                      data:
                                                                                                        ingestor.yml: |
                                                                                                          prop1: value1
                                                                                                      

                                                                                                      And now the kafka.brokers value is found and injected.

                                                                                                      Source https://stackoverflow.com/questions/70568480

                                                                                                      QUESTION

                                                                                                      Kubernetes Operators: Informers vs. reconcile loop
                                                                                                      Asked 2022-Jan-03 at 15:36

                                                                                                      I recently got started with building a Kubernetes operator. I'm using the Fabric8 Java Kubernetes Client but I think my question is more general and also applies to other programming languages and libraries.

                                                                                                      When reading through blog posts, documentation or textbooks explaining the operator pattern, I found there seem to be two options to design an operator:

                                                                                                      1. Using an infinite reconcile loop, in which all corresponding Kubernetes objects are retrieved from the API and then some action is performed.
                                                                                                      2. Using informers, which are called whenever an observed Kubernetes resource changes.

                                                                                                      However, I don't find any source discussion which option should be used in which case. Are there any best practices?

                                                                                                      ANSWER

                                                                                                      Answered 2022-Jan-03 at 15:36

                                                                                                      You should use both.

                                                                                                      When using informers, it's possible that the handler gets the events out of order or even not at all. The former means the handler needs to define and reconcile state - this approach is referred to as level-based, as opposed to edge-based. The latter means reconciliation needs to be triggered on a regular interval to account for that possibility.

                                                                                                      The way controller-runtime does things, reconciliation is triggered by cluster events (using informers behind the scenes) related to the resources watched by the controller and on a timer. Also, by design, the event is not passed to the reconciler so that it is forced to define and act on a state.

                                                                                                      Source https://stackoverflow.com/questions/70567016

                                                                                                      Community Discussions, Code Snippets contain sources that include Stack Exchange Network

                                                                                                      Vulnerabilities

                                                                                                      No vulnerabilities reported

                                                                                                      Install kubernetes-client

                                                                                                      You can download it from GitHub, Maven.
                                                                                                      You can use kubernetes-client like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the kubernetes-client component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .

                                                                                                      Support

                                                                                                      You can use KubernetesClient mocking mechanism with JUnit5. Since it doesn't support @Rule and @ClassRule there is dedicated annotation @EnableKubernetesMockClient. If you would like to create instance of mocked KubernetesClient for each test (JUnit4 @Rule) you need to declare instance of KubernetesClient as shown below. In case you would like to define static instance of mocked server per all the test (JUnit4 @ClassRule) you need to declare instance of KubernetesClient as shown below. You can also enable crudMode by using annotation field crud.
                                                                                                      Find more information at:
                                                                                                      Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
                                                                                                      Find more libraries
                                                                                                      Explore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits​
                                                                                                      Save this library and start creating your kit
                                                                                                      Install
                                                                                                      Maven
                                                                                                      Gradle
                                                                                                      CLONE
                                                                                                    • HTTPS

                                                                                                      https://github.com/fabric8io/kubernetes-client.git

                                                                                                    • CLI

                                                                                                      gh repo clone fabric8io/kubernetes-client

                                                                                                    • sshUrl

                                                                                                      git@github.com:fabric8io/kubernetes-client.git

                                                                                                    • Share this Page

                                                                                                      share link

                                                                                                      Explore Related Topics

                                                                                                      Reuse Pre-built Kits with kubernetes-client

                                                                                                      Consider Popular REST Libraries

                                                                                                      public-apis

                                                                                                      by public-apis

                                                                                                      json-server

                                                                                                      by typicode

                                                                                                      iptv

                                                                                                      by iptv-org

                                                                                                      fastapi

                                                                                                      by tiangolo

                                                                                                      beego

                                                                                                      by beego

                                                                                                      Try Top Libraries by fabric8io

                                                                                                      docker-maven-plugin

                                                                                                      by fabric8ioJava

                                                                                                      spring-cloud-kubernetes

                                                                                                      by fabric8ioJava

                                                                                                      fabric8-pipeline-library

                                                                                                      by fabric8ioGroovy

                                                                                                      fabric8-maven-plugin

                                                                                                      by fabric8ioJava

                                                                                                      Compare REST Libraries with Highest Support

                                                                                                      fastapi

                                                                                                      by tiangolo

                                                                                                      dropwizard

                                                                                                      by dropwizard

                                                                                                      python

                                                                                                      by kubernetes-client

                                                                                                      Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
                                                                                                      Find more libraries
                                                                                                      Explore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits​
                                                                                                      Save this library and start creating your kit