Python kubernetes.client.V1ObjectMeta() Examples

The following are 30 code examples of kubernetes.client.V1ObjectMeta(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module kubernetes.client , or try the search function .
Example #1
Source File: conftest.py    From kubetest with GNU General Public License v3.0 8 votes vote down vote up
def simple_persistentvolumeclaim():
    """Return the Kubernetes config matching the simple-persistentvolumeclaim.yaml manifest."""
    return client.V1PersistentVolumeClaim(
        api_version='v1',
        kind='PersistentVolumeClaim',
        metadata=client.V1ObjectMeta(
            name='my-pvc'
        ),
        spec=client.V1PersistentVolumeClaimSpec(
            access_modes=[
                'ReadWriteMany'
            ],
            resources=client.V1ResourceRequirements(
                requests={
                    'storage': '16Mi'
                }
            )
        )
    ) 
Example #2
Source File: config.py    From ray with Apache License 2.0 7 votes vote down vote up
def _configure_namespace(provider_config):
    namespace_field = "namespace"
    if namespace_field not in provider_config:
        raise ValueError("Must specify namespace in Kubernetes config.")

    namespace = provider_config[namespace_field]
    field_selector = "metadata.name={}".format(namespace)
    namespaces = core_api().list_namespace(field_selector=field_selector).items
    if len(namespaces) > 0:
        assert len(namespaces) == 1
        logger.info(log_prefix +
                    using_existing_msg(namespace_field, namespace))
        return namespace

    logger.info(log_prefix + not_found_msg(namespace_field, namespace))
    namespace_config = client.V1Namespace(
        metadata=client.V1ObjectMeta(name=namespace))
    core_api().create_namespace(namespace_config)
    logger.info(log_prefix + created_msg(namespace_field, namespace))
    return namespace 
Example #3
Source File: kubernetes_tools.py    From paasta with Apache License 2.0 7 votes vote down vote up
def get_volume_claim_templates(self) -> Sequence[V1PersistentVolumeClaim]:
        return [
            V1PersistentVolumeClaim(
                metadata=V1ObjectMeta(name=self.get_persistent_volume_name(volume)),
                spec=V1PersistentVolumeClaimSpec(
                    # must be ReadWriteOnce for EBS
                    access_modes=["ReadWriteOnce"],
                    storage_class_name=self.get_storage_class_name(volume),
                    resources=V1ResourceRequirements(
                        requests={"storage": f"{volume['size']}Gi"}
                    ),
                ),
            )
            for volume in self.get_persistent_volumes()
        ] 
Example #4
Source File: copy_dataset.py    From aws-eks-deep-learning-benchmark with Apache License 2.0 7 votes vote down vote up
def create_job_object(runner_image, region, s3_path, pvc_name):
  target_folder = get_target_folder(s3_path)

  # Configureate Pod template container
  container = k8s_client.V1Container(
      name="copy-dataset-worker",
      image=runner_image,
      command=["aws"],
      args=["s3", "sync", s3_path, "/mnt/" + target_folder],
      volume_mounts=[k8s_client.V1VolumeMount(name="data-storage", mount_path='/mnt')],
      env=[k8s_client.V1EnvVar(name="AWS_REGION", value=region),
        k8s_client.V1EnvVar(name="AWS_ACCESS_KEY_ID", value_from=k8s_client.V1EnvVarSource(secret_key_ref=k8s_client.V1SecretKeySelector(key="AWS_ACCESS_KEY_ID", name="aws-secret"))),
        k8s_client.V1EnvVar(name="AWS_SECRET_ACCESS_KEY", value_from=k8s_client.V1EnvVarSource(secret_key_ref=k8s_client.V1SecretKeySelector(key="AWS_SECRET_ACCESS_KEY", name="aws-secret")))
        ],
    )
  volume = k8s_client.V1Volume(
    name='data-storage',
    persistent_volume_claim=k8s_client.V1PersistentVolumeClaimVolumeSource(claim_name=pvc_name)
  )
  # Create and configurate a spec section
  template = k8s_client.V1PodTemplateSpec(
      # metadata=k8s_client.V1ObjectMeta(labels={"app":"copy-dataset-worker"}),
      spec=k8s_client.V1PodSpec(containers=[container], volumes=[volume], restart_policy="OnFailure"))
  # Create the specification of deployment
  spec = k8s_client.V1JobSpec(
      # selector=k8s_client.V1LabelSelector(match_labels={"app":"copy-dataset-worker"}),
      template=template)
  # Instantiate the deployment object
  deployment = k8s_client.V1Job(
      api_version="batch/v1",
      kind="Job",
      metadata=k8s_client.V1ObjectMeta(name=container.name),
      spec=spec)

  return deployment 
Example #5
Source File: test_kubernetes_tools.py    From paasta with Apache License 2.0 6 votes vote down vote up
def test_get_kubernetes_metadata(self):
        with mock.patch(
            "paasta_tools.kubernetes_tools.KubernetesDeploymentConfig.get_service",
            autospec=True,
            return_value="kurupt",
        ) as mock_get_service, mock.patch(
            "paasta_tools.kubernetes_tools.KubernetesDeploymentConfig.get_instance",
            autospec=True,
            return_value="fm",
        ) as mock_get_instance:

            ret = self.deployment.get_kubernetes_metadata("aaa123")
            assert ret == V1ObjectMeta(
                labels={
                    "yelp.com/paasta_git_sha": "aaa123",
                    "yelp.com/paasta_instance": mock_get_instance.return_value,
                    "yelp.com/paasta_service": mock_get_service.return_value,
                    "paasta.yelp.com/git_sha": "aaa123",
                    "paasta.yelp.com/instance": mock_get_instance.return_value,
                    "paasta.yelp.com/service": mock_get_service.return_value,
                },
                name="kurupt-fm",
            ) 
Example #6
Source File: kubernetes_tools.py    From paasta with Apache License 2.0 6 votes vote down vote up
def pod_disruption_budget_for_service_instance(
    service: str, instance: str, max_unavailable: Union[str, int],
) -> V1beta1PodDisruptionBudget:
    return V1beta1PodDisruptionBudget(
        metadata=V1ObjectMeta(
            name=get_kubernetes_app_name(service, instance), namespace="paasta",
        ),
        spec=V1beta1PodDisruptionBudgetSpec(
            max_unavailable=max_unavailable,
            selector=V1LabelSelector(
                match_labels={
                    "paasta.yelp.com/service": service,
                    "paasta.yelp.com/instance": instance,
                }
            ),
        ),
    ) 
Example #7
Source File: kubernetes_tools.py    From paasta with Apache License 2.0 6 votes vote down vote up
def create_secret(
    kube_client: KubeClient,
    secret: str,
    service: str,
    secret_provider: BaseSecretProvider,
) -> None:
    service = sanitise_kubernetes_name(service)
    sanitised_secret = sanitise_kubernetes_name(secret)
    kube_client.core.create_namespaced_secret(
        namespace="paasta",
        body=V1Secret(
            metadata=V1ObjectMeta(
                name=f"paasta-secret-{service}-{sanitised_secret}",
                labels={
                    "yelp.com/paasta_service": service,
                    "paasta.yelp.com/service": service,
                },
            ),
            data={
                secret: base64.b64encode(
                    secret_provider.decrypt_secret_raw(secret)
                ).decode("utf-8")
            },
        ),
    ) 
Example #8
Source File: kubernetes_tools.py    From paasta with Apache License 2.0 6 votes vote down vote up
def update_kubernetes_secret_signature(
    kube_client: KubeClient, secret: str, service: str, secret_signature: str
) -> None:
    service = sanitise_kubernetes_name(service)
    secret = sanitise_kubernetes_name(secret)
    kube_client.core.replace_namespaced_config_map(
        name=f"paasta-secret-{service}-{secret}-signature",
        namespace="paasta",
        body=V1ConfigMap(
            metadata=V1ObjectMeta(
                name=f"paasta-secret-{service}-{secret}-signature",
                labels={
                    "yelp.com/paasta_service": service,
                    "paasta.yelp.com/service": service,
                },
            ),
            data={"signature": secret_signature},
        ),
    ) 
Example #9
Source File: kubernetes_tools.py    From paasta with Apache License 2.0 6 votes vote down vote up
def create_kubernetes_secret_signature(
    kube_client: KubeClient, secret: str, service: str, secret_signature: str
) -> None:
    service = sanitise_kubernetes_name(service)
    secret = sanitise_kubernetes_name(secret)
    kube_client.core.create_namespaced_config_map(
        namespace="paasta",
        body=V1ConfigMap(
            metadata=V1ObjectMeta(
                name=f"paasta-secret-{service}-{secret}-signature",
                labels={
                    "yelp.com/paasta_service": service,
                    "paasta.yelp.com/service": service,
                },
            ),
            data={"signature": secret_signature},
        ),
    ) 
Example #10
Source File: k8s.py    From nephos with Apache License 2.0 6 votes vote down vote up
def secret_create(secret_data, name, namespace="default"):
    """Create a K8S Secret.

    Args:
        secret_data (dict): Data to store in t as key/value hash.
        name (str): Name of the Secret.
        namespace (str): Name of namespace.
    """
    # Encode the data in a copy of the input dictionary
    # TODO: We should check that Secret exists before we create it
    secret_data = secret_data.copy()
    for key, value in secret_data.items():
        if isinstance(value, str):
            value = value.encode("ascii")
        secret_data[key] = base64.b64encode(value).decode("utf-8")
    secret = client.V1Secret()
    secret.metadata = client.V1ObjectMeta(name=name)
    secret.type = "Opaque"
    secret.data = secret_data
    api.create_namespaced_secret(namespace=namespace, body=secret)
    logging.info(f"Created Secret {name} in namespace {namespace}") 
Example #11
Source File: _k8s_job_helper.py    From pipelines with Apache License 2.0 6 votes vote down vote up
def _create_k8s_job(self, yaml_spec):
    """ _create_k8s_job creates a kubernetes job based on the yaml spec """
    pod = k8s_client.V1Pod(metadata=k8s_client.V1ObjectMeta(generate_name=yaml_spec['metadata']['generateName'],
                                                            annotations=yaml_spec['metadata']['annotations']))
    container = k8s_client.V1Container(name = yaml_spec['spec']['containers'][0]['name'],
                                       image = yaml_spec['spec']['containers'][0]['image'],
                                       args = yaml_spec['spec']['containers'][0]['args'])
    pod.spec = k8s_client.V1PodSpec(restart_policy=yaml_spec['spec']['restartPolicy'],
                                    containers = [container],
                                    service_account_name=yaml_spec['spec']['serviceAccountName'])
    try:
      api_response = self._corev1.create_namespaced_pod(yaml_spec['metadata']['namespace'], pod)
      return api_response.metadata.name, True
    except k8s_client.rest.ApiException as e:
      logging.exception("Exception when calling CoreV1Api->create_namespaced_pod: {}\n".format(str(e)))
      return '', False 
Example #12
Source File: kube_configmap.py    From wc-devops-utilities with Apache License 2.0 6 votes vote down vote up
def patchConfigMap(parser_args,CorV1Client):
    print("patch config map")
    data = {}
    if parser_args.rawconfig != None:
        keyvaluePaires = parser_args.rawconfig.split(";")
        for keyvalue in keyvaluePaires:
            print("keyvalue is %s" % keyvalue)
            _keyvalue = keyvalue.split("::")
            print(_keyvalue)
            data.update({_keyvalue[0]: _keyvalue[1]})
    else:
       configMap=readFileForConfigMap(parser_args)
       data.update(configMap)
    objectMeta = client.V1ObjectMeta(name=parser_args.tenant+parser_args.env+parser_args.envtype+"-"+parser_args.name, namespace=parser_args.namespace)
    body = client.V1ConfigMap(kind='ConfigMap', metadata=objectMeta, data=data)
    pretty = 'pretty_example'  # str | If 'true', then the output is pretty printed. (optional)

    try:
        api_response = CorV1Client.patch_namespaced_config_map(parser_args.tenant+parser_args.env+parser_args.envtype+"-"+parser_args.name, parser_args.namespace, body, pretty=pretty)
        print(api_response)
    except ApiException as e:
        print("Exception when calling CoreV1Api->patch_namespaced_config_map: %s\n" % e) 
Example #13
Source File: kube_configmap.py    From wc-devops-utilities with Apache License 2.0 6 votes vote down vote up
def createConfigMap(parser_args,CorV1Client):
    data={}
    if parser_args.rawconfig !=None :
       keyvaluePaires=parser_args.rawconfig.split(";")
       for keyvalue in keyvaluePaires:
           _keyvalue=keyvalue.split("::")
           print(_keyvalue)
           data.update({_keyvalue[0]:_keyvalue[1]})
    else:
       configMap=readFileForConfigMap(parser_args)
       data.update(configMap)

    print("data is %s" % data)
    objectMeta=client.V1ObjectMeta(name=parser_args.tenant+parser_args.env+parser_args.envtype+"-"+parser_args.name,namespace=parser_args.namespace)
    body = client.V1ConfigMap(kind='ConfigMap',metadata=objectMeta,data=data)  # V1ConfigMap
    pretty = 'pretty_example'  # str | If 'true', then the output is pretty printed. (optional)
    try:
        api_response = CorV1Client.create_namespaced_config_map(parser_args.namespace, body, pretty=pretty)
        print(api_response)
    except ApiException as e:
        print("Exception when calling CoreV1Api->create_namespaced_config_map: %s\n" % e) 
Example #14
Source File: test_kubernetes_tools.py    From paasta with Apache License 2.0 6 votes vote down vote up
def test_sanitize_for_config_hash(self):
        with mock.patch(
            "paasta_tools.kubernetes_tools.get_kubernetes_secret_hashes", autospec=True
        ) as mock_get_kubernetes_secret_hashes:
            mock_config = V1Deployment(
                metadata=V1ObjectMeta(name="qwe", labels={"mc": "grindah"}),
                spec=V1DeploymentSpec(
                    replicas=2,
                    selector=V1LabelSelector(match_labels={"freq": "108.9"}),
                    template=V1PodTemplateSpec(),
                ),
            )
            ret = self.deployment.sanitize_for_config_hash(mock_config)
            assert "replicas" not in ret["spec"].keys()
            assert (
                ret["paasta_secrets"] == mock_get_kubernetes_secret_hashes.return_value
            ) 
Example #15
Source File: KubernetesResources.py    From k8s-mongo-operator with GNU Affero General Public License v3.0 6 votes vote down vote up
def createSecret(cls, secret_name: str, namespace: str, secret_data: Dict[str, str],
                     labels: Optional[Dict[str, str]] = None) -> client.V1Secret:
        """
        Creates a secret object.
        :param secret_name: The name of the secret.
        :param namespace: The name space for the secret.
        :param secret_data: The secret data.
        :param labels: Optional labels for this secret, defaults to the default labels (see `cls.createDefaultLabels`).
        :return: The secret model object.
        """
        return client.V1Secret(
            metadata=client.V1ObjectMeta(
                name=secret_name,
                namespace=namespace,
                labels=cls.createDefaultLabels(secret_name) if labels is None else labels
            ),
            string_data=secret_data,
        ) 
Example #16
Source File: tenants_utils.py    From inference-model-manager with Apache License 2.0 6 votes vote down vote up
def create_rolebinding(name, scope_name, id_token):
    api_version = 'rbac.authorization.k8s.io'
    scope = scope_name
    subject = k8s_client.V1Subject(kind='Group', name=scope, namespace=name)
    role_ref = k8s_client.V1RoleRef(api_group=api_version, kind='Role', name=name)
    meta = k8s_client.V1ObjectMeta(name=name, namespace=name)
    rolebinding = k8s_client.V1RoleBinding(metadata=meta, role_ref=role_ref, subjects=[subject])
    rbac_api_instance = get_k8s_rbac_api_client(id_token)

    try:
        response = rbac_api_instance.create_namespaced_role_binding(name, rolebinding)
    except ApiException as apiException:
        KubernetesCreateException('rolebinding', apiException)

    logger.info("Rolebinding {} created".format(name))
    return response 
Example #17
Source File: test_kubernetes_tools.py    From paasta with Apache License 2.0 6 votes vote down vote up
def test_get_annotations_for_kubernetes_service(
    mock_kube_deploy_config, has_persistent_volumes, expected_annotations
):
    mock_client = mock.Mock()
    mock_client.deployments.read_namespaced_stateful_set.return_value = V1StatefulSet(
        metadata=V1ObjectMeta(annotations={"I-am": "stateful_set"})
    )
    mock_client.deployments.read_namespaced_deployment.return_value = V1Deployment(
        metadata=V1ObjectMeta(annotations={"I-am": "deployment"})
    )
    mock_kube_deploy_config.get_sanitised_deployment_name.return_value = (
        "fake_k8s_service"
    )
    mock_kube_deploy_config.get_persistent_volumes.return_value = has_persistent_volumes
    mock_kube_deploy_config.format_kubernetes_app.return_value = mock.Mock()
    annotations = get_annotations_for_kubernetes_service(
        mock_client, mock_kube_deploy_config
    )
    assert annotations == expected_annotations 
Example #18
Source File: k8s.py    From nephos with Apache License 2.0 6 votes vote down vote up
def ns_create(namespace):
    """Create K8S namespace.

    Args:
        namespace (str): Name of namespace.
    """
    try:
        ns_read(namespace)
    except ApiException:
        ns = client.V1Namespace()
        ns.metadata = client.V1ObjectMeta(name=namespace)
        api.create_namespace(ns)
        logging.info(f'Created namespace "{namespace}"')
        logging.debug(pretty_print(json.dumps(ns.metadata, default=str)))


# TODO: Can we be more precise with the return type annotation? 
Example #19
Source File: test_clusterinit.py    From CPU-Manager-for-Kubernetes with Apache License 2.0 6 votes vote down vote up
def test_clusterinit_update_pod_with_init_container():
    pod_passed = k8sclient.V1Pod(
        metadata=k8sclient.V1ObjectMeta(annotations={}),
        spec=k8sclient.V1PodSpec(containers=[
            k8sclient.V1Container(name="cmk")
        ]),
        status=k8sclient.V1PodStatus()).to_dict()
    cmd = "cmd"
    cmk_img = "cmk_img"
    cmk_img_pol = "policy"
    args = "argument"
    clusterinit.update_pod_with_init_container(pod_passed, cmd, cmk_img,
                                               cmk_img_pol,
                                               args)
    pods = json.loads(pod_passed["metadata"]["annotations"][
                          "pod.beta.kubernetes.io/init-containers"])
    assert len(pods) == 1
    assert pods[0]["name"] == cmd
    assert pods[0]["image"] == cmk_img
    assert pods[0]["imagePullPolicy"] == cmk_img_pol
    assert args in pods[0]["args"]

    second_cmd = "cmd2"
    second_img = cmk_img
    second_img_pol = "Always"
    second_args = ["arg1", "arg2"]
    clusterinit.update_pod_with_init_container(pod_passed, second_cmd,
                                               second_img,
                                               second_img_pol,
                                               second_args)

    pods = json.loads(pod_passed["metadata"]["annotations"][
                          "pod.beta.kubernetes.io/init-containers"])
    assert len(pods) == 2 
Example #20
Source File: job_crud.py    From python with Apache License 2.0 6 votes vote down vote up
def create_job_object():
    # Configureate Pod template container
    container = client.V1Container(
        name="pi",
        image="perl",
        command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"])
    # Create and configurate a spec section
    template = client.V1PodTemplateSpec(
        metadata=client.V1ObjectMeta(labels={"app": "pi"}),
        spec=client.V1PodSpec(restart_policy="Never", containers=[container]))
    # Create the specification of deployment
    spec = client.V1JobSpec(
        template=template,
        backoff_limit=4)
    # Instantiate the job object
    job = client.V1Job(
        api_version="batch/v1",
        kind="Job",
        metadata=client.V1ObjectMeta(name=JOB_NAME),
        spec=spec)

    return job 
Example #21
Source File: test_base.py    From node with Apache License 2.0 6 votes vote down vote up
def create_service(self, name, app, ns, port, svc_type="NodePort", traffic_policy="Local", cluster_ip=None, ipv6=False):
        service = client.V1Service(
            metadata=client.V1ObjectMeta(
                name=name,
                labels={"name": name},
            ),
            spec={
                "ports": [{"port": port}],
                "selector": {"app": app},
                "type": svc_type,
                "externalTrafficPolicy": traffic_policy,
            }
        )
        if cluster_ip:
          service.spec["clusterIP"] = cluster_ip
        if ipv6:
          service.spec["ipFamily"] = "IPv6"

        api_response = self.cluster.create_namespaced_service(
            body=service,
            namespace=ns,
        )
        logger.debug("Additional Service created. status='%s'" % str(api_response.status)) 
Example #22
Source File: ingress_create.py    From python with Apache License 2.0 6 votes vote down vote up
def create_service():
    core_v1_api = client.CoreV1Api()
    body = client.V1Service(
        api_version="v1",
        kind="Service",
        metadata=client.V1ObjectMeta(
            name="service-example"
        ),
        spec=client.V1ServiceSpec(
            selector={"app": "deployment"},
            ports=[client.V1ServicePort(
                port=5678,
                target_port=5678
            )]
        )
    )
    # Creation of the Deployment in specified namespace
    # (Can replace "default" with a namespace you may have created)
    core_v1_api.create_namespaced_service(namespace="default", body=body) 
Example #23
Source File: install_aws_secret.py    From aws-eks-deep-learning-benchmark with Apache License 2.0 6 votes vote down vote up
def install_aws_secret(api_client, namespace, secret_name, aws_access_key_id, aws_secret_access_key):
  """Install AWS secret on the cluster.
  Return:
    secret: Secret for AWS credentials
  """
  logging.info("Install AWS secret %s", secret_name)

  corev1_api = k8s_client.CoreV1Api(api_client)
  try:
    secret = k8s_client.V1Secret()
    secret.metadata = k8s_client.V1ObjectMeta(name=secret_name)
    secret.type = "Opaque"
    secret.data = {
      "AWS_ACCESS_KEY_ID": aws_access_key_id,
      "AWS_SECRET_ACCESS_KEY": aws_secret_access_key
      }

    corev1_api.create_namespaced_secret(namespace, secret)
  except rest.ApiException as e:
    # Status appears to be a string.
    if e.status == 409:
      logging.info("Github token has already been installed")
    else:
      raise 
Example #24
Source File: kubernetes.py    From training_results_v0.6 with Apache License 2.0 6 votes vote down vote up
def create_job_manifest(envs, commands, name, image, template_file):
    if template_file is not None:
        with open( template_file ) as f:
            job=yaml.safe_load(f)
            job["metadata"]["name"]=name
            job["spec"]["template"]["metadata"]["labels"]["app"]=name
            job["spec"]["template"]["spec"]["containers"][0]["image"]=image
            job["spec"]["template"]["spec"]["containers"][0]["command"]=commands
            job["spec"]["template"]["spec"]["containers"][0]["name"]=name
            job["spec"]["template"]["spec"]["containers"][0]["env"]=envs
            job["spec"]["template"]["spec"]["containers"][0]["command"]=commands
    else:
        container=client.V1Container(image=image, command=commands, name=name, env=envs)
        pod_temp=client.V1PodTemplateSpec(
                spec=client.V1PodSpec(restart_policy="OnFailure", containers=[container]),
                metadata=client.V1ObjectMeta(name=name, labels={"app":name})
                )
        job=client.V1Job(
                api_version="batch/v1",
                kind="Job",
                spec=client.V1JobSpec(template=pod_temp),
                metadata=client.V1ObjectMeta(name=name)
                )
    return job 
Example #25
Source File: clusterinit.py    From CPU-Manager-for-Kubernetes with Apache License 2.0 6 votes vote down vote up
def update_mutatingwebhookconfiguration(config, name, app, webhook_name, cert,
                                        service, path, namespace,
                                        failure_policy):
    config.metadata = k8sclient.V1ObjectMeta()
    config.metadata.name = name
    config.metadata.labels = {"app": app}
    client_config = k8sclient.AdmissionregistrationV1beta1WebhookClientConfig(
        ca_bundle=cert,
        service=k8sclient.AdmissionregistrationV1beta1ServiceReference(
            name=service,
            namespace=namespace,
            path=path))
    webhook = k8sclient.V1beta1Webhook(name=webhook_name,
                                       client_config=client_config,
                                       failure_policy=failure_policy)
    webhook.rules = [{
        "apiGroups": [""],
        "apiVersions": ["v1"],
        "operations": ["CREATE"],
        "resources": ["pods"]
    }]
    config.webhooks = [webhook] 
Example #26
Source File: kubernetes.py    From training_results_v0.6 with Apache License 2.0 6 votes vote down vote up
def create_job_manifest(envs, commands, name, image, template_file):
    if template_file is not None:
        with open( template_file ) as f:
            job=yaml.safe_load(f)
            job["metadata"]["name"]=name
            job["spec"]["template"]["metadata"]["labels"]["app"]=name
            job["spec"]["template"]["spec"]["containers"][0]["image"]=image
            job["spec"]["template"]["spec"]["containers"][0]["command"]=commands
            job["spec"]["template"]["spec"]["containers"][0]["name"]=name
            job["spec"]["template"]["spec"]["containers"][0]["env"]=envs
            job["spec"]["template"]["spec"]["containers"][0]["command"]=commands
    else:
        container=client.V1Container(image=image, command=commands, name=name, env=envs)
        pod_temp=client.V1PodTemplateSpec(
                spec=client.V1PodSpec(restart_policy="OnFailure", containers=[container]),
                metadata=client.V1ObjectMeta(name=name, labels={"app":name})
                )
        job=client.V1Job(
                api_version="batch/v1",
                kind="Job",
                spec=client.V1JobSpec(template=pod_temp),
                metadata=client.V1ObjectMeta(name=name)
                )
    return job 
Example #27
Source File: conftest.py    From kubetest with GNU General Public License v3.0 6 votes vote down vote up
def simple_service():
    """Return the Kubernetes config matching the simple-service.yaml manifest."""
    return client.V1Service(
        api_version='v1',
        kind='Service',
        metadata=client.V1ObjectMeta(
            name='my-service'
        ),
        spec=client.V1ServiceSpec(
            selector={
                'app': 'MyApp'
            },
            ports=[
                client.V1ServicePort(
                    protocol='TCP',
                    port=80,
                    target_port=9376
                )
            ]
        )
    ) 
Example #28
Source File: KubernetesResources.py    From k8s-mongo-operator with GNU Affero General Public License v3.0 5 votes vote down vote up
def createService(cls, cluster_object: V1MongoClusterConfiguration) -> client.V1Service:
        """
        Creates a service model object.
        :param cluster_object: The cluster object from the YAML file.
        :return: The service object.
        """
        # Parse cluster data object.
        name = cluster_object.metadata.name

        # Create service.
        return client.V1Service(
            metadata=client.V1ObjectMeta(
                name=name,
                namespace=cluster_object.metadata.namespace,
                labels=cls.createDefaultLabels(name),
            ),
            spec=client.V1ServiceSpec(
                cluster_ip="None",  # create headless service, no load-balancing and a single service IP
                selector=cls.createDefaultLabels(name),
                ports=[client.V1ServicePort(
                    name="mongod",
                    port=cls.MONGO_PORT,
                    protocol="TCP"
                )],
            ),
        ) 
Example #29
Source File: k8s.py    From nephos with Apache License 2.0 5 votes vote down vote up
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}") 
Example #30
Source File: clusterinit.py    From CPU-Manager-for-Kubernetes with Apache License 2.0 5 votes vote down vote up
def update_configmap(configmap, name, data):
    configmap.metadata = k8sclient.V1ObjectMeta()
    configmap.metadata.name = name
    configmap.data = data