import urllib3
from pprint import pprint
from kubernetes import client
from os import path
import yaml
class K8sApi(object):
def __init__(self):
# self.config = config.kube_config.load_kube_config()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
self.configuration = client.Configuration()
self.configuration.host = "https://192.168.100.111:6443"
self.configuration.api_key[
'authorization'] = 'Bearer token'
self.configuration.verify_ssl = False
self.k8s_apps_v1 = client.AppsV1Api(client.ApiClient(self.configuration))
self.Api_Instance = client.CoreV1Api(client.ApiClient(self.configuration))
self.Api_Instance_Extensions = client.ExtensionsV1beta1Api(client.ApiClient(self.configuration))
####################################################################################################################
def list_deployment(self, namespace="default"):
api_response = self.k8s_apps_v1.list_namespaced_deployment(namespace)
return api_response
def read_deployment(self, name="nginx-deployment", namespace="default"):
api_response = self.k8s_apps_v1.read_namespaced_deployment(name, namespace)
return api_response
def create_deployment(self, file="deploy-nginx.yaml"):
with open(path.join(path.dirname(__file__), file)) as f:
dep = yaml.safe_load(f)
resp = self.k8s_apps_v1.create_namespaced_deployment(
body=dep, namespace="default")
return resp
def replace_deployment(self, file="deploy-nginx.yaml", name="nginx-deployment", namespace="default"):
with open(path.join(path.dirname(__file__), file)) as f:
dep = yaml.safe_load(f)
re