Coder Social home page Coder Social logo

kubernetes-python-client's Introduction

Kubernetes Python Client

Kubernetes python(based on Python 2.7.12) client supports by Alpine in a docker container.

    

  

References

How to use this image

Start an instance

To start a container, use the following:

$ docker run -it --name test-kubernetes-python-client \
           maiwj/kubernetes-python-client

Watching resources from Kubernetes Pod

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys
import os
from kubernetes import client
from kubernetes.client.rest import ApiException

def main():
    SERVICE_TOKEN_FILENAME = "/var/run/secrets/kubernetes.io/serviceaccount/token"
    SERVICE_CERT_FILENAME = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
    KUBERNETES_HOST = "https://%s:%s" % (os.getenv("KUBERNETES_SERVICE_HOST"), os.getenv("KUBERNETES_SERVICE_PORT"))

    ## configure 
    configuration = client.Configuration()
    configuration.host = KUBERNETES_HOST
    if not os.path.isfile(SERVICE_TOKEN_FILENAME):
        raise ApiException("Service token file does not exists.")
    with open(SERVICE_TOKEN_FILENAME) as f:
        token = f.read()
        if not token:
            raise ApiException("Token file exists but empty.")
        configuration.api_key['authorization'] = "bearer " + token.strip('\n')
    if not os.path.isfile(SERVICE_CERT_FILENAME):
        raise ApiException("Service certification file does not exists.")
    with open(SERVICE_CERT_FILENAME) as f:
        if not f.read():
            raise ApiException("Cert file exists but empty.")
        configuration.ssl_ca_cert = SERVICE_CERT_FILENAME
    client.Configuration.set_default(configuration)

    try:
        ret = client.CoreV1Api().list_namespaced_config_map(namespace=os.getenv("CHART_NAMESPACE"), field_selector=("metadata.name=%s" % os.getenv("CHART_FULLNAME")), watch=False)
        print ret
    except ApiException as e:
        print("Exception when calling CoreV1Api->list_namespaced_config_map: %s\n" % e)

if __name__ == '__main__':
    main()

License

kubernetes-python-client's People

Contributors

thxcode avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

dumberdore

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.