def _create_configmap_object(name, namespace, configmap_data): # Configureate ConfigMap metadata metadata = client.V1ObjectMeta( name=name, namespace=namespace, ) configmap_object = client.V1ConfigMap( api_version="v1", kind="ConfigMap", data=dict(schedules=configmap_data), metadata=metadata, ) return configmap_object
def test_apply_cm_identity_mappings_with_no_mapping(api_client): run_sync(iam_mapping.apply_cm_identity_mappings(CONFIGMAP, [])) expected_cm_data = { "mapRoles": yaml.safe_dump([]), "mapUsers": yaml.safe_dump([]) } expected_configmap = client.V1ConfigMap(api_version="v1", kind="ConfigMap", data=expected_cm_data, metadata={"key": "some_metadata"}) api_client.patch_namespaced_config_map.assert_called_with( "aws-auth", "kube-system", expected_configmap)
def get_obj(self): """ :description: Generate configmap spec. """ return client.V1ConfigMap( metadata=client.V1ObjectMeta(name=self.slug, labels=self.labels, annotations=self.annotations), kind=self.kind, data=self.data if self.data else None, binary_data={str(self.override_name): self.binary} if self.binary else None, )
def create_config_map(name, data, namespace='default', wait=False, **kwargs): # pragma: no cover core_v1_api = get_core_api() metadata = client.V1ObjectMeta(name=name) body = client.V1ConfigMap(data=data, metadata=metadata) try: core_v1_api.create_namespaced_config_map(namespace, body, **kwargs) except ApiException: LOG.exception('Create config map failed') raise
def create_config_map_object(config_map_name, file_paths): # Configure ConfigMap metadata metadata = client.V1ObjectMeta(name=config_map_name, ) config_data = {} for file_path in file_paths: file_name = os.path.basename(file_path) file_content = open(file_path, "r").read() config_data[file_name] = file_content # Instantiate the config_map object config_map = client.V1ConfigMap(api_version="v1", kind="ConfigMap", data=config_data, metadata=metadata) return config_map
def create_configmap_object(ip_list_str, ip_list_hash, sg_update_state): # Configureate ConfigMap metadata metadata = client.V1ObjectMeta( name=configmap_name, namespace=namespace, ) # Instantiate the configmap object configmap = client.V1ConfigMap(api_version="v1", kind="ConfigMap", data=dict(ips=ip_list_str, checksum=ip_list_hash, sg_update_state=sg_update_state), metadata=metadata) return configmap
def create_quick_configmap_definition(cmName, cmNamespace, annotationsDict={}): configmap = client.V1ConfigMap( api_version="v1", kind="ConfigMap", data={ "__init__.py": "", "custompython.py": "import argparse\nimport logging\nimport sys\nimport os\n\nfrom kubernetes import client, config\nfrom kubernetes.client import rest\n\nlogger = logging.getLogger('custom')\nlogging.basicConfig(level=logging.INFO)\n\n\ndef test_custom_code(input=None):\n if input==None:\n logger.info(\"[Message: %s]\" % (\"Custom Code invoked!!!! (v2) \"))\n else:\n logger.info(\"[Message: %s]\" % (\"Custom Input (v2): \" + input))\n\n" }, metadata=client.V1ObjectMeta(name=cmName, namespace=cmNamespace, annotations=annotationsDict)) return configmap
def cm_create(cm_data, name, namespace="default"): """Create a K8S ConfigMap Args: cm_data (dict): Data to store in ConfigMap as key/value hash. name (str): Name of ConfigMap. namespace (str): Name of namespace. """ # TODO: We should check that CM exists before we create it cm = client.V1ConfigMap() cm.metadata = client.V1ObjectMeta(name=name) cm.data = cm_data api.create_namespaced_config_map(namespace=namespace, body=cm) logging.info(f"Created ConfigMap {name} in namespace {namespace}")
def test(): try: V1NamespaceBody = client.V1Namespace() pprint("V1NamespaceBody={}".format(V1NamespaceBody)) V1ObjectReferenceBody = client.V1ObjectReference() pprint("V1ObjectReferenceBody={}".format(V1ObjectReferenceBody)) V1ObjectMetaBody = client.V1ObjectMeta() pprint("V1ObjectMetaBody={}".format(V1ObjectMetaBody)) #V1BindingBody = client.V1Binding() #pprint("V1BindingBody={}".format(V1BindingBody)) V1ConfigMapBody = client.V1ConfigMap() pprint("V1ConfigMapBody={}".format(V1ConfigMapBody)) V1Pod = client.V1Pod() pprint("V1Pod={}".format(V1Pod)) V1PodTemplate = client.V1PodTemplate() pprint("V1PodTemplate={}".format(V1PodTemplate)) V1ReplicationController = client.V1ReplicationController() pprint("V1ReplicationController={}".format(V1ReplicationController)) V1Service = client.V1Service() pprint("V1Service={}".format(V1Service)) V1Node = client.V1Node() pprint("V1Node={}".format(V1Node)) pod = 'nginx-no-split-655b866cfd-54xmg' namespace = 'default' read_pod = apis_api.read_namespaced_pod(name=pod, namespace=namespace) pprint("read_pod={}".format(read_pod)) lifecycle = read_pod.spec.node_selector.lifecycle pprint("lifecycle={}".format(lifecycle)) read_pod.spec.node_selector.lifecycle = 'OnDemand' pprint("read_pod={}".format(read_pod)) #metadata = read_pod.metadata #pprint("metadata={}".format(metadata)) #metadata.cluster_name = 'Ec2SpotEKS4' #pprint("metadata={}".format(metadata)) except Exception as e: print("Exception when calling CoreV1Api->create_namespace: %s\n" % e)
def unlock(self): self.c.metadata.annotations["Owner"] = "" config = build_configmap(self.c_data) configmap = k8sclient.V1ConfigMap() data = {"config": yaml.dump(config)} clusterinit.update_configmap(configmap, self.cm_name, data) try: k8s.patch_config_map(None, self.cm_name, configmap, "default") except K8sApiException as err: logging.error("Error while retreiving configmap {}".format( self.cm_name)) logging.error(err.reason) sys.exit(1) self.c = None self.c_data = None
def create_configmap(self, user: User) -> str: """ To be done after all user generation steps are complete """ cert_o = x509.load_pem_x509_certificate(user.cert, default_backend()) expires = cert_o.not_valid_after if user.admin: cm_data = {user.name: expires.isoformat()} admin_cm_list = self._check_confmap("maintain-kubeusers") if not admin_cm_list: config_map = client.V1ConfigMap( api_version="v1", kind="ConfigMap", metadata=client.V1ObjectMeta(name="maintain-kubeusers"), data=cm_data, ) resp = self.core.create_namespaced_config_map( "maintain-kubeusers", body=config_map) return resp.metadata.name resp = self.core.patch_namespaced_config_map( "maintain-kubeusers", "maintain-kubeusers", {"data": cm_data}) return resp.metadata.name config_map = client.V1ConfigMap( api_version="v1", kind="ConfigMap", metadata=client.V1ObjectMeta(name="maintain-kubeusers"), data={ "status": "user created: {}".format(datetime.utcnow().isoformat()), "expires": expires.isoformat(), }, ) resp = self.core.create_namespaced_config_map("tool-{}".format( user.name), body=config_map) return resp.metadata.name
def create_own_config(k8s_core_api, job_name, pod_name, ip, ssh_port, framework_attempt, task_attempt): config_name = "c-" + pod_name labels = { "run": job_name, "framework_attempt_id": framework_attempt, "task_attempt_id": task_attempt } metadata = k8s_client.V1ObjectMeta( namespace=job_namespace, name=config_name, labels=labels, ) data = json.dumps({"ip": ip, "ssh_port": ssh_port}) body = k8s_client.V1ConfigMap(data={"pod.json": data}, metadata=metadata) for i in range(2): try: resp = k8s_core_api.create_namespaced_config_map( namespace=job_namespace, body=body, ) logger.debug("created configmap %s, resp is %s", config_name, resp) except ApiException as e: if e.status == 409: logger.info( "configmap already exist, maybe from previous retry, delete it, retry %d", i) try: api_response = k8s_core_api.delete_namespaced_config_map( config_name, job_namespace, ) except ApiException as e: logger.warning("delete configmap failed", exc_info=True) continue else: logger.exception("create configmap with data %s failed", data) sys.exit(ERROR_EXIT_CODE["k8s_api"]) except Exception as e: logger.exception("create configmap with data %s failed", data) sys.exit(ERROR_EXIT_CODE["network"]) return config_name sys.exit(ERROR_EXIT_CODE["k8s_api"])
def cm_create(cm_data, name, namespace="default", verbose=False): """Create a K8S ConfigMap Args: cm_data (dict): Data to store in ConfigMap as key/value hash. name (str): Name of ConfigMap. namespace (str): Name of namespace. verbose (bool): Verbosity. False by default. """ # TODO: We should check that CM exists before we create it cm = client.V1ConfigMap() cm.metadata = client.V1ObjectMeta(name=name) cm.data = cm_data api.create_namespaced_config_map(namespace=namespace, body=cm) if verbose: print("Created ConfigMap {} in namespace {}".format(name, namespace))
def create_config_map(self, configmap_name, config_data={}, labels={}): try: if config_data: body = kubernetes_client.V1ConfigMap( data=config_data, metadata=kubernetes_client.V1ObjectMeta( name=configmap_name, namespace=self.namespace, labels=labels)) api_response = self.v1.create_namespaced_config_map( self.namespace, body=body, pretty=True) return api_response except Exception as e: raise OperetoRuntimeError( error="Failed to create config map {}: {}".format( configmap_name, e))
def config_map(input): keyfile = "configjob.yaml" meta = client.V1ObjectMeta( name=input["cm_name"], namespace=input["namespace"], labels={ "task": input["job"] } ) body = client.V1ConfigMap( api_version="v1", kind="ConfigMap", metadata=meta, data={keyfile: yaml.dump(input["configjob"])}, ) return body
def save_schedule_details(schedule_details: dict) -> None: schedule_name = schedule_details["name"] k8s_client = client.CoreV1Api() k8s_client.create_namespaced_config_map( body=client.V1ConfigMap( metadata=client.V1ObjectMeta( name=f"schedule.details-{schedule_name}", namespace="default" ), data={ "encoded_data": json.dumps(schedule_details) } ), namespace="default" )
def create_cm(name: str, namespace: str, data: dict) -> client.V1ConfigMap: """Returns Kubernetes configmap object :param name: configmap name :type name: str :param namespace: Kubernetes namespace :type namespace: str :param data: data in configmap :type data: dict :return: configmap :rtype: client.V1ConfigMap """ cm = client.V1ConfigMap() cm.metadata = client.V1ObjectMeta(namespace=namespace, name=name) cm.data = data return cm
def setUp(self): self.name = 'testname123' self.namespace = 'testnamespace456' svc_list = client.V1ServiceList(items=[]) svc = client.V1Service() svc.metadata = client.V1ObjectMeta(name=self.name, namespace=self.namespace) svc.metadata.labels = { 'operated-by': 'memcached.operator.kubestack.com', 'heritage': 'kubestack.com', 'cluster': self.name } svc_list.items = [svc] self.correct_svc_list = svc_list cm_list = client.V1ConfigMapList(items=[]) cm = client.V1ConfigMap() cm.metadata = client.V1ObjectMeta(name=self.name, namespace=self.namespace) cm.metadata.labels = { 'operated-by': 'memcached.operator.kubestack.com', 'heritage': 'kubestack.com', 'cluster': self.name } cm_list.items = [cm] self.correct_cm_list = cm_list deploy_list = client.AppsV1beta1DeploymentList(items=[]) memcached_deploy = client.AppsV1beta1Deployment() memcached_deploy.metadata = client.V1ObjectMeta( name=self.name, namespace=self.namespace) memcached_deploy.metadata.labels = { 'operated-by': 'memcached.operator.kubestack.com', 'heritage': 'kubestack.com', 'cluster': self.name } mcrouter_deploy = client.AppsV1beta1Deployment() mcrouter_deploy.metadata = client.V1ObjectMeta( name='{}-router'.format(self.name), namespace=self.namespace) mcrouter_deploy.metadata.labels = { 'operated-by': 'memcached.operator.kubestack.com', 'heritage': 'kubestack.com', 'cluster': self.name } deploy_list.items = [memcached_deploy, mcrouter_deploy] self.correct_deploy_list = deploy_list
def create_experiment_env_config_map(v1: client.CoreV1Api(), namespace: str, name: str = "chaostoolkit-env"): """ Create the default configmap to hold experiment environment variables, in case it wasn't already created by the user. If it already exists, we do not return it so that the operator does not take its ownership. """ logger = logging.getLogger('kopf.objects') try: v1.read_namespaced_config_map( namespace=namespace, name="chaostoolkit-env") except ApiException: logger.info("Creating default `chaostoolkit-env` configmap") body = client.V1ConfigMap(metadata=client.V1ObjectMeta(name=name)) return v1.create_namespaced_config_map(namespace, body)
def _register_job_configmap(self, nodes, topology): cm = client.V1ConfigMap() cm.metadata = client.V1ObjectMeta(namespace=NAMESPACE, name=self.resources_identifier) values = self._prepare_data_for_algorithm(nodes, topology) cm.data = dict() cm.data[CONFIG_MAP_KEY_NODES] = json.dumps(values["nodes"]) cm.data[CONFIG_MAP_KEY_TOPOLOGY] = json.dumps(values["topology"]) # cm.data[CONFIG_MAP_KEY_RUS] = str(rus) self.v1_client.create_namespaced_config_map(namespace=NAMESPACE, body=cm) self.rollback_resources.append(ROLLBACK_CONFIG_MAP_KEY)
def create(self, name, namespace, election_record): """ :param electionRecord: Annotation string :param name: Name of the configmap object to be created :param namespace: Namespace in which the configmap object is to be created :return: 'True' if object is created else 'False' if failed """ body = client.V1ConfigMap( metadata={"name": name, "annotations": {self.leader_electionrecord_annotationkey: json.dumps(self.get_lock_dict(election_record))}}) try: api_response = self.api_instance.create_namespaced_config_map(namespace, body, pretty=True) return True except ApiException as e: logging.info("Failed to create lock as {}".format(e)) return False
def some_pipeline(): # create config map object with k6 load test script config_map = k8s.V1ConfigMap( api_version="v1", data={"foo": "bar"}, kind="ConfigMap", metadata=k8s.V1ObjectMeta( name="foo-bar-cm", namespace="default" ) ) # delete the config map in k8s dsl.ResourceOp( name="delete-config-map", action="delete", k8s_resource=config_map )
def write_keys(wallet, name, namespace): api_instance = client.CoreV1Api() sec = client.V1Secret() sec.type = 'Opaque' cmap = client.V1ConfigMap() sec.data = { 'private_key': base64.b64encode(wallet['private_key'].encode()).decode(), 'password': base64.b64encode(wallet['password'].encode()).decode() } cmap.data = {'address': wallet['address'], 'pub_key': wallet['pub_key']} api_instance.patch_namespaced_secret(name, namespace, sec) api_instance.patch_namespaced_config_map(name, namespace, cmap)
def create_configmap_object(source, namespace, configmap_name, dataname): configmap_dict = dict() with open(source) as f: source_content = f.read() configmap_dict[dataname] = source_content # Configurate ConfigMap metadata metadata = client.V1ObjectMeta( deletion_grace_period_seconds=30, name=configmap_name, namespace=namespace, ) # Instantiate the configmap object configmap = client.V1ConfigMap(api_version="v1", kind="ConfigMap", data=configmap_dict, metadata=metadata) return configmap
def create_configmap(file_path, make_cacert_wrong, configmap_name): """ Create configmap with file at file_path if make_cacert_wrong==True then it makes cacert wrong Args: param1: file_path - path of cacert file param2: make_cacert_wrong - for operator testcase, cacert wrong Returns: None Raises: Raises an exception on kubernetes client api failure and asserts """ api_instance = client.CoreV1Api() metadata = client.V1ObjectMeta( name=configmap_name, namespace=namespace_value, ) with open(file_path, 'r') as f: file_content = f.read() if make_cacert_wrong: file_content = file_content[0:50]+file_content[-50:-1] data_dict = {} data_dict[configmap_name] = file_content configmap = client.V1ConfigMap( api_version="v1", kind="ConfigMap", data=data_dict, metadata=metadata ) try: api_response = api_instance.create_namespaced_config_map( namespace=namespace_value, body=configmap, pretty=True, ) LOGGER.debug(str(api_response)) LOGGER.info(f"configmap {configmap_name} has been created") except ApiException as e: LOGGER.error( f"Exception when calling CoreV1Api->create_namespaced_config_map: {e}") assert False
def construct_configmap_object(): cfg_metadata = client.V1ObjectMeta(name="aws-auth", namespace="kube-system") #{ # 'mapRoles': '- rolearn: arn:aws:iam::810309775316:role/Ec2SpotEKS-NodeInstanceRole\n username: system:node:{{EC2PrivateDNSName}}\n groups:\n - system:bootstrappers\n - system:nodes\n\n- rolearn: arn:aws:iam::423718654226:role/TeamRole\n username: TeamRole\n groups:\n - system:masters\n', # 'mapUsers': '- userarn: arn:aws:iam::810309775316:user/sukumar-test\n username: sukumar-test\n groups:\n - system:masters\n' #} #prepare role arn #arn:aws:iam::<account_id>:role/EKS-NodeInstanceRole node_role_arn = 'arn:aws:iam::' + ACCOUNT_ID + ':role/' + CLUSTER_ROLE_NAME map_role_txt = '- rolearn: ' + node_role_arn + '\n username: system:node:{{EC2PrivateDNSName}}\n groups:\n - system:bootstrappers\n - system:nodes\n' if MAP_ROLE_NAME != '': # arn:aws:iam::<account_id>:role/TeamRole iam_role_arn = 'arn:aws:iam::' + ACCOUNT_ID + ':role/' + MAP_ROLE_NAME map_role_txt = map_role_txt + '- rolearn: ' + iam_role_arn + '\n username: '******'\n groups:\n - system:masters\n' #prepare user arn user_arn = '' if MAP_USER_NAME != '': #arn:aws:iam::<account_id>:user/sukumar-test user_arn = 'arn:aws:iam::' + ACCOUNT_ID + ':user/' + MAP_USER_NAME if user_arn != '': map_user_txt = '- userarn: ' + user_arn + '\n username: '******'\n groups:\n - system:masters\n' # cfg_data= { # 'mapRoles': map_role_txt # } cfg_data = {'mapRoles': map_role_txt, 'mapUsers': map_user_txt} print("**Config data:\n\n") print(cfg_data) print("Config data**\n\n") #TODO. add IAM group, once support is added in EKS. refer enhancement request in container roadmap. configmap = client.V1ConfigMap(api_version="v1", kind="ConfigMap", metadata=cfg_metadata, data=cfg_data) return configmap
def get_flink_conf_config_map(job: KubernetesFlinkJob) -> client.V1ConfigMap: flink_conf_map = {} flink_conf_map[ "jobmanager.rpc.address"] = "flink-job-cluster-{}-svc".format(job.uuid) flink_conf_map["taskmanager.numberOfTaskSlots"] = 2 flink_conf_map["blob.server.port"] = 6124 flink_conf_map["jobmanager.rpc.port"] = 6123 flink_conf_map["taskmanager.rpc.port"] = 6122 flink_conf_map["jobmanager.heap.size"] = "1024m" flink_conf_map["taskmanager.memory.process.size"] = "1024m" flink_conf_yaml = '' try: for key, value in job.job_config.flink_conf.items(): flink_conf_map[key] = str(value) except KeyError: pass for key, value in flink_conf_map.items(): flink_conf_yaml += '{}: {}\n'.format(key, value) log4j_properties = "" try: logging_confs = job.job_config.logging_conf for conf_k, conf_v in logging_confs.items(): log4j_properties += "{}={}\n".format(conf_k, conf_v) except KeyError: pass logging.info(flink_conf_yaml) log4j_cli_properties = "" config_map_data = {} config_map_data["flink-conf.yaml"] = flink_conf_yaml config_map_data["log4j.properties"] = log4j_properties config_map_data["log4j-cli.properties"] = log4j_cli_properties config_name = "flink-config-{}".format(job.uuid) labels = {'app': 'flink', 'component': 'config-map-' + str(job.uuid)} config_map = client.V1ConfigMap(api_version="v1", kind="ConfigMap", metadata=client.V1ObjectMeta( name=config_name, labels=labels), data=config_map_data) return config_map
def create_configmap(self, kube_config=None, configmap=None, file=None, namespace="default", configmap_name="Configmap_name"): if configmap is None and file is None: raise Exception( "Please provide either a configmap or a file to create a Kubernetes configmap" ) if kube_config: api_client = ApiClient(kube_config) else: api_client = ApiClient() # Configureate ConfigMap metadata metadata = client.V1ObjectMeta(name=configmap_name, namespace=namespace) v1 = client.CoreV1Api(api_client) if file is not None: file_name = file.split('/')[-1] logger.info('Creating configmap object from file %s' % file_name) # Get File Content with open(file, 'r') as f: file_content = f.read() data = {file_name: file_content} # Instantiate the configmap object configmap = client.V1ConfigMap(api_version="v1", kind="ConfigMap", data=data, metadata=metadata) try: logger.info('Deploying configmap') v1.create_namespaced_config_map( namespace=namespace, body=configmap, ) logger.debug('Deploy configmap successfully') except FailToCreateError as e: for api_exception in e.api_exceptions: body = json.loads(api_exception.body) logger.error('Error: %s, because: %s' % (api_exception.reason, body['message']))
def get_config_map(namespace, project_name, experiment_group_name, experiment_name, project_uuid, experiment_group_uuid, experiment_uuid, original_name, cloning_strategy, cluster_def, persistence_outputs, persistence_data, params, log_level): name = constants.CONFIG_MAP_NAME.format(uuid=experiment_uuid) labels = get_map_labels(project_name, experiment_group_name, experiment_name, project_uuid, experiment_group_uuid, experiment_uuid) metadata = client.V1ObjectMeta(name=name, labels=labels, namespace=namespace) experiment_outputs_path = stores.get_experiment_outputs_path( persistence=persistence_outputs, experiment_name=experiment_name, original_name=original_name, cloning_strategy=cloning_strategy) experiment_logs_path = stores.get_experiment_logs_path( experiment_name=experiment_name, temp=False) data = { constants.CONFIG_MAP_CLUSTER_KEY_NAME: json.dumps(cluster_def), constants.CONFIG_MAP_PARAMS_KEY_NAME: json.dumps(params) or '{}', constants.CONFIG_MAP_EXPERIMENT_INFO_KEY_NAME: json.dumps(labels), constants.CONFIG_MAP_LOG_LEVEL_KEY_NAME: log_level, constants.CONFIG_MAP_RUN_OUTPUTS_PATH_KEY_NAME: experiment_outputs_path, constants.CONFIG_MAP_RUN_LOGS_PATH_KEY_NAME: experiment_logs_path, constants.CONFIG_MAP_RUN_DATA_PATHS_KEY_NAME: persistence_data, API_HTTP_URL: get_settings_http_api_url(), API_WS_HOST: get_settings_ws_api_url(), } return client.V1ConfigMap(api_version=k8s_constants.K8S_API_VERSION_V1, kind=k8s_constants.K8S_CONFIG_MAP_KIND, metadata=metadata, data=data)
def create_config_map_for_outputs(self, pod_name, infrastructure_id, outputs): logger.info("output = {0}".format(str(outputs))) logger.info("output type = {0}".format(str(type(outputs)))) api_response = self.coreV1Api().create_namespaced_config_map( namespace=self.namespace(), body=client.V1ConfigMap( api_version="v1", kind="ConfigMap", metadata=client.V1ObjectMeta( namespace=self.namespace(), name=infrastructure_id, labels={"infrastructure_id": infrastructure_id}), data=outputs)) # TODO handle api_response logger.info("Config Map created. status='%s'" % str(api_response))