本文整理汇总了Python中kubernetes.client.V1LabelSelector方法的典型用法代码示例。如果您正苦于以下问题:Python client.V1LabelSelector方法的具体用法?Python client.V1LabelSelector怎么用?Python client.V1LabelSelector使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kubernetes.client
的用法示例。
在下文中一共展示了client.V1LabelSelector方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pod_disruption_budget_for_service_instance
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
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,
}
),
),
)
示例2: test_sanitize_for_config_hash
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
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
)
示例3: create_job_object
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
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
示例4: simple_deployment
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
def simple_deployment():
"""Return the Kubernetes config matching the simple-deployment.yaml manifest."""
return client.V1Deployment(
api_version='apps/v1',
kind='Deployment',
metadata=client.V1ObjectMeta(
name='nginx-deployment',
labels={
'app': 'nginx'
}
),
spec=client.V1DeploymentSpec(
replicas=3,
selector=client.V1LabelSelector(
match_labels={
'app': 'nginx'
}
),
template=client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(
labels={
'app': 'nginx'
}
),
spec=client.V1PodSpec(
containers=[
client.V1Container(
name='nginx',
image='nginx:1.7.9',
ports=[
client.V1ContainerPort(
container_port=80
)
]
)
]
)
)
)
)
示例5: simple_statefulset
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
def simple_statefulset():
"""Return the Kubernetes config matching the simple-statefulset.yaml manifest."""
return client.V1StatefulSet(
api_version='apps/v1',
kind='StatefulSet',
metadata=client.V1ObjectMeta(
name='postgres-statefulset',
labels={
'app': 'postgres'
}
),
spec=client.V1StatefulSetSpec(
replicas=3,
selector=client.V1LabelSelector(
match_labels={
'app': 'postgres'
}
),
service_name='simple-service',
template=client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(
labels={
'app': 'postgres'
}
),
spec=client.V1PodSpec(
containers=[
client.V1Container(
name='postgres',
image='postgres:9.6',
ports=[
client.V1ContainerPort(
container_port=5432
)
]
)
]
)
)
)
)
示例6: simple_daemonset
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
def simple_daemonset():
"""Return the Kubernetes config matching the simple-daemonset.yaml manifest."""
return client.V1DaemonSet(
api_version='apps/v1',
kind='DaemonSet',
metadata=client.V1ObjectMeta(
name='canal-daemonset',
labels={
'app': 'canal'
}
),
spec=client.V1DaemonSetSpec(
selector=client.V1LabelSelector(
match_labels={
'app': 'canal'
}
),
template=client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(
labels={
'app': 'canal'
}
),
spec=client.V1PodSpec(
containers=[
client.V1Container(
name='canal',
image='canal:3.7.2',
ports=[
client.V1ContainerPort(
container_port=9099
)
]
)
]
)
)
)
)
示例7: simple_replicaset
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
def simple_replicaset():
"""Return the Kubernetes config matching the simple-replicaset.yaml manifest."""
return client.V1ReplicaSet(
api_version='apps/v1',
kind='ReplicaSet',
metadata=client.V1ObjectMeta(
name='frontend',
labels={
'app': 'guestbook',
'tier': 'frontend',
},
),
spec=client.V1ReplicaSetSpec(
replicas=3,
selector=client.V1LabelSelector(
match_labels={
'tier': 'frontend',
},
),
template=client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(
labels={
'tier': 'frontend',
},
),
spec=client.V1PodSpec(
containers=[
client.V1Container(
name='php-redis',
image='gcr.io/google_samples/gb-frontend:v3',
),
],
),
),
),
)
示例8: test_get_autoscaling_metric_spec_http
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
def test_get_autoscaling_metric_spec_http(self):
# with http
config_dict = {
"min_instances": 1,
"max_instances": 3,
"autoscaling": {"metrics_provider": "http", "setpoint": 0.5},
}
mock_config = KubernetesDeploymentConfig( # type: ignore
service="service",
cluster="cluster",
instance="instance",
config_dict=config_dict,
branch_dict=None,
)
return_value = KubernetesDeploymentConfig.get_autoscaling_metric_spec(
mock_config, "fake_name", "cluster"
)
annotations = {"signalfx.com.custom.metrics": ""}
expected_res = V2beta1HorizontalPodAutoscaler(
kind="HorizontalPodAutoscaler",
metadata=V1ObjectMeta(
name="fake_name", namespace="paasta", annotations=annotations
),
spec=V2beta1HorizontalPodAutoscalerSpec(
max_replicas=3,
min_replicas=1,
metrics=[
V2beta1MetricSpec(
type="Pods",
pods=V2beta1PodsMetricSource(
metric_name="http",
target_average_value=0.5,
selector=V1LabelSelector(
match_labels={"paasta_cluster": "cluster"}
),
),
)
],
scale_target_ref=V2beta1CrossVersionObjectReference(
api_version="apps/v1", kind="Deployment", name="fake_name",
),
),
)
assert expected_res == return_value
示例9: test_get_autoscaling_metric_spec_uwsgi
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
def test_get_autoscaling_metric_spec_uwsgi(self):
config_dict = {
"min_instances": 1,
"max_instances": 3,
"autoscaling": {"metrics_provider": "uwsgi", "setpoint": 0.5},
}
mock_config = KubernetesDeploymentConfig( # type: ignore
service="service",
cluster="cluster",
instance="instance",
config_dict=config_dict,
branch_dict=None,
)
return_value = KubernetesDeploymentConfig.get_autoscaling_metric_spec(
mock_config, "fake_name", "cluster"
)
annotations = {"signalfx.com.custom.metrics": ""}
expected_res = V2beta1HorizontalPodAutoscaler(
kind="HorizontalPodAutoscaler",
metadata=V1ObjectMeta(
name="fake_name", namespace="paasta", annotations=annotations
),
spec=V2beta1HorizontalPodAutoscalerSpec(
max_replicas=3,
min_replicas=1,
metrics=[
V2beta1MetricSpec(
type="Pods",
pods=V2beta1PodsMetricSource(
metric_name="uwsgi",
target_average_value=0.5,
selector=V1LabelSelector(
match_labels={"paasta_cluster": "cluster"}
),
),
)
],
scale_target_ref=V2beta1CrossVersionObjectReference(
api_version="apps/v1", kind="Deployment", name="fake_name",
),
),
)
assert expected_res == return_value
示例10: init_deployment
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
def init_deployment(self, tosca_kube_obj, kube_obj_name):
"""Instantiate the deployment object"""
deployment_name = kube_obj_name
# Create a list of container, which made a Pod
containers = list()
for container_prop in tosca_kube_obj.containers:
limit_resource = self.init_resource_requirements(container_prop)
container = self.init_containers(
container_props=container_prop,
limit_resource=limit_resource,
name=deployment_name)
containers.append(container)
# Make a label with pattern {"selector": "deployment_name"}
if tosca_kube_obj.scaling_object:
scaling_name = tosca_kube_obj.scaling_object.scaling_name
update_label = self.config_labels(deployment_name=deployment_name,
scaling_name=scaling_name)
else:
update_label = self.config_labels(deployment_name=deployment_name)
if tosca_kube_obj.labels:
if 'selector' in update_label:
del update_label['selector']
update_label.update(tosca_kube_obj.labels)
labels = update_label
# Create and configure a spec section
pod_template = client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(
labels=labels, annotations=tosca_kube_obj.annotations),
spec=client.V1PodSpec(containers=containers))
# Create the specification of deployment
label_selector = client.V1LabelSelector(match_labels=labels)
deployment_spec = client.V1DeploymentSpec(
template=pod_template, selector=label_selector)
metadata = client.V1ObjectMeta(name=deployment_name, labels=labels)
# Instantiate the deployment object
deployment = client.V1Deployment(
api_version="apps/v1",
kind="Deployment",
metadata=metadata,
spec=deployment_spec)
return deployment
# init_hpa initializes Kubernetes Horizon Pod Auto-scaling object
示例11: _add_kubetest_labels
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
def _add_kubetest_labels(self) -> None:
"""Add a kubetest label to the StatefulSet object.
This allows kubetest to more easily and reliably search for and aggregate
API objects, such as getting the Pods for a StatefulSet.
The kubetest label key is "kubetest/<obj kind>" where the obj kind is
the lower-cased kind of the obj.
"""
self.klabel_key = 'kubetest/statefulset'
if self.obj.metadata.labels:
self.klabel_uid = self.obj.metadata.labels.get(self.klabel_key, None)
else:
self.klabel_uid = None
if not self.klabel_uid:
self.klabel_uid = str(uuid.uuid4())
# fixme: it would be nice to clean up this label setting logic a bit
# and possibly abstract it out to something more generalized, but
# that is difficult to do given the differences in object attributes
# Set the base metadata label
if self.obj.metadata is None:
self.obj.metadata = client.V1ObjectMeta()
if self.obj.metadata.labels is None:
self.obj.metadata.labels = {}
if self.klabel_key not in self.obj.metadata.labels:
self.obj.metadata.labels[self.klabel_key] = self.klabel_uid
# If no spec is set, there is nothing to set additional labels on
if self.obj.spec is None:
log.warning('statefulset spec not set - cannot set kubetest label')
return
# Set the selector label
if self.obj.spec.selector is None:
self.obj.spec.selector = client.V1LabelSelector()
if self.obj.spec.selector.match_labels is None:
self.obj.spec.selector.match_labels = {}
if self.klabel_key not in self.obj.spec.selector.match_labels:
self.obj.spec.selector.match_labels[self.klabel_key] = self.klabel_uid
# Set the template label
if self.obj.spec.template is None:
self.obj.spec.template = client.V1PodTemplateSpec()
if self.obj.spec.template.metadata is None:
self.obj.spec.template.metadata = client.V1ObjectMeta(labels={})
if self.klabel_key not in self.obj.spec.template.metadata.labels:
self.obj.spec.template.metadata.labels[self.klabel_key] = self.klabel_uid
示例12: _add_kubetest_labels
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
def _add_kubetest_labels(self) -> None:
"""Add a kubetest label to the Deployment object.
This allows kubetest to more easily and reliably search for and aggregate
API objects, such as getting the Pods for a Deployment.
The kubetest label key is "kubetest/<obj kind>" where the obj kind is
the lower-cased kind of the obj.
"""
self.klabel_key = 'kubetest/deployment'
if self.obj.metadata.labels:
self.klabel_uid = self.obj.metadata.labels.get(self.klabel_key, None)
else:
self.klabel_uid = None
if not self.klabel_uid:
self.klabel_uid = str(uuid.uuid4())
# fixme: it would be nice to clean up this label setting logic a bit
# and possibly abstract it out to something more generalized, but
# that is difficult to do given the differences in object attributes
# Set the base metadata label
if self.obj.metadata is None:
self.obj.metadata = client.V1ObjectMeta()
if self.obj.metadata.labels is None:
self.obj.metadata.labels = {}
if self.klabel_key not in self.obj.metadata.labels:
self.obj.metadata.labels[self.klabel_key] = self.klabel_uid
# If no spec is set, there is nothing to set additional labels on
if self.obj.spec is None:
log.warning('deployment spec not set - cannot set kubetest label')
return
# Set the selector label
if self.obj.spec.selector is None:
self.obj.spec.selector = client.V1LabelSelector()
if self.obj.spec.selector.match_labels is None:
self.obj.spec.selector.match_labels = {}
if self.klabel_key not in self.obj.spec.selector.match_labels:
self.obj.spec.selector.match_labels[self.klabel_key] = self.klabel_uid
# Set the template label
if self.obj.spec.template is None:
self.obj.spec.template = client.V1PodTemplateSpec()
if self.obj.spec.template.metadata is None:
self.obj.spec.template.metadata = client.V1ObjectMeta(labels={})
if self.klabel_key not in self.obj.spec.template.metadata.labels:
self.obj.spec.template.metadata.labels[self.klabel_key] = self.klabel_uid
示例13: _add_kubetest_labels
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
def _add_kubetest_labels(self) -> None:
"""Add a kubetest label to the DaemonSet object.
This allows kubetest to more easily and reliably search for and aggregate
API objects, such as getting the Pods for a DaemonSet.
The kubetest label key is "kubetest/<obj kind>" where the obj kind is
the lower-cased kind of the obj.
"""
self.klabel_key = 'kubetest/daemonset'
if self.obj.metadata.labels:
self.klabel_uid = self.obj.metadata.labels.get(self.klabel_key, None)
else:
self.klabel_uid = None
if not self.klabel_uid:
self.klabel_uid = str(uuid.uuid4())
# fixme: it would be nice to clean up this label setting logic a bit
# and possibly abstract it out to something more generalized, but
# that is difficult to do given the differences in object attributes
# Set the base metadata label
if self.obj.metadata is None:
self.obj.metadata = client.V1ObjectMeta()
if self.obj.metadata.labels is None:
self.obj.metadata.labels = {}
if self.klabel_key not in self.obj.metadata.labels:
self.obj.metadata.labels[self.klabel_key] = self.klabel_uid
# If no spec is set, there is nothing to set additional labels on
if self.obj.spec is None:
log.warning('daemonset spec not set - cannot set kubetest label')
return
# Set the selector label
if self.obj.spec.selector is None:
self.obj.spec.selector = client.V1LabelSelector()
if self.obj.spec.selector.match_labels is None:
self.obj.spec.selector.match_labels = {}
if self.klabel_key not in self.obj.spec.selector.match_labels:
self.obj.spec.selector.match_labels[self.klabel_key] = self.klabel_uid
# Set the template label
if self.obj.spec.template is None:
self.obj.spec.template = client.V1PodTemplateSpec()
if self.obj.spec.template.metadata is None:
self.obj.spec.template.metadata = client.V1ObjectMeta(labels={})
if self.klabel_key not in self.obj.spec.template.metadata.labels:
self.obj.spec.template.metadata.labels[self.klabel_key] = self.klabel_uid
示例14: __init__
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
def __init__(self, name=None, selector=None, labels=None, image_metadata=None,
namespace='default', create_in_cluster=False, from_template=None):
"""
Utility functions for kubernetes deployments.
:param name: str, name of the deployment
:param selector: Label selector for pods. Existing ReplicaSets whose pods are selected by
this will be the ones affected by this deployment. It must match the pod template's labels
:param labels: dict, dict of labels
:param image_metadata: ImageMetadata
:param namespace: str, name of the namespace
:param create_in_cluster: bool, if True deployment is created in Kubernetes cluster
:param from_template: str, deployment template, example:
- https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
"""
self.namespace = namespace
if (from_template is not None) and (name is not None or selector is not None or
labels is not None or image_metadata is not None):
raise ConuException(
'from_template cannot be passed to constructor at the same time with'
' name, selector, labels or image_metadata')
elif from_template is not None:
self.body = yaml.safe_load(from_template)
self.name = self.body['metadata']['name']
elif (name is not None and selector is not None and
labels is not None and image_metadata is not None):
self.name = name
self.pod = Pod.create(image_metadata)
self.spec = client.V1DeploymentSpec(
selector=client.V1LabelSelector(match_labels=selector),
template=client.V1PodTemplateSpec(metadata=client.V1ObjectMeta(labels=selector),
spec=self.pod.spec))
self.metadata = client.V1ObjectMeta(name=self.name, namespace=self.namespace,
labels=labels)
self.body = client.V1Deployment(spec=self.spec, metadata=self.metadata)
else:
raise ConuException(
'to create deployment you need to specify template or'
' properties: name, selector, labels, image_metadata')
self.api = get_apps_api()
if create_in_cluster:
self.create_in_cluster()
示例15: create_k8s_nfs_resources
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1LabelSelector [as 别名]
def create_k8s_nfs_resources(self) -> bool:
"""
Create NFS resources such as PV and PVC in Kubernetes.
"""
from kubernetes import client as k8sclient
pv_name = "nfs-ckpt-pv-{}".format(uuid.uuid4())
persistent_volume = k8sclient.V1PersistentVolume(
api_version="v1",
kind="PersistentVolume",
metadata=k8sclient.V1ObjectMeta(
name=pv_name,
labels={'app': pv_name}
),
spec=k8sclient.V1PersistentVolumeSpec(
access_modes=["ReadWriteMany"],
nfs=k8sclient.V1NFSVolumeSource(
path=self.params.path,
server=self.params.server
),
capacity={'storage': '10Gi'},
storage_class_name=""
)
)
k8s_api_client = k8sclient.CoreV1Api()
try:
k8s_api_client.create_persistent_volume(persistent_volume)
self.params.pv_name = pv_name
except k8sclient.rest.ApiException as e:
print("Got exception: %s\n while creating the NFS PV", e)
return False
pvc_name = "nfs-ckpt-pvc-{}".format(uuid.uuid4())
persistent_volume_claim = k8sclient.V1PersistentVolumeClaim(
api_version="v1",
kind="PersistentVolumeClaim",
metadata=k8sclient.V1ObjectMeta(
name=pvc_name
),
spec=k8sclient.V1PersistentVolumeClaimSpec(
access_modes=["ReadWriteMany"],
resources=k8sclient.V1ResourceRequirements(
requests={'storage': '10Gi'}
),
selector=k8sclient.V1LabelSelector(
match_labels={'app': self.params.pv_name}
),
storage_class_name=""
)
)
try:
k8s_api_client.create_namespaced_persistent_volume_claim(self.params.namespace, persistent_volume_claim)
self.params.pvc_name = pvc_name
except k8sclient.rest.ApiException as e:
print("Got exception: %s\n while creating the NFS PVC", e)
return False
return True