def create_secret_in_namespace_if_not_exist(self, payload, namespace): if self.in_cluster: config.load_incluster_config() else: config.load_kube_config(config_file="~/.kube/config") try: api_instance = client.CoreV1Api() api_instance.read_namespaced_secret(payload['metadata']['name'], namespace) except ApiException as e: if e.status == 404: try: api_instance = client.CoreV1Api() meta_data = client.V1ObjectMeta() meta_data.name = payload['metadata']['name'] body = client.V1Secret(metadata=meta_data, data=payload['data']) api_instance.create_namespaced_secret(namespace, body) except ApiException as create_e: logger.error( "Exception when calling CoreV1Api->create_namespaced_secret: %s\n" % create_e) sys.exit(1) else: logger.error( "Exception when calling CoreV1Api->read_namespaced_secret: %s\n" % e) sys.exit(1)
def replace_secret_in_namespace(self, payload, namespace): if self.in_cluster: config.load_incluster_config() else: config.load_kube_config(config_file="~/.kube/config") try: api_instance = client.CoreV1Api() meta_data = client.V1ObjectMeta() meta_data.name = payload['metadata']['name'] body = client.V1Secret(metadata=meta_data, data=payload['data']) # don't use patch, which can't handle empty string: https://github.com/kubernetes/kubernetes/issues/37216 api_instance.replace_namespaced_secret(payload['metadata']['name'], namespace, body) except ApiException as e: logger.error( "Exception when calling CoreV1Api->patch_namespaced_secret: %s\n" % e) sys.exit(1)
def create_group_if_not_exist(self, name): if self.in_cluster: config.load_incluster_config() else: config.load_kube_config(config_file="~/.kube/config") try: api_instance = client.CoreV1Api() api_instance.read_namespace(name) except ApiException as e: if e.status == 404: api_instance = client.CoreV1Api() meta_data = client.V1ObjectMeta() meta_data.name = name body = client.V1Namespace(metadata=meta_data) api_instance.create_namespace(body) return True logger.error("Failed to create namespace [{0}]".format(name)) sys.exit(1) return False