【问题标题】:kubernetes - where is the official api documentation that says whether matchLabels is mandatory or notkubernetes - 说明 matchLabels 是否是强制性的官方 api 文档在哪里
【发布时间】:2019-06-11 17:37:18
【问题描述】:

今天我正在浏览一些关于 matchLabels 语句的文档和讨论,该语句是 Kubernetes 中部署(或其他对象)的一部分。示例如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

在一些讨论中,我看到根据 API 的版本,使用此选择器可能是可选的或强制的。

参考:

https://github.com/helm/charts/issues/7680

What is the purpose of a kubernetes deployment pod selector?

但是我看不到任何官方文档明确说明对于特定版本的 Kubernetes API 是否强制使用此选择器。您是否知道任何官方文档说明是否必须使用 matchLabels 选择器?

我已经检查了这些链接,但我没有遇到官方声明: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#deploymentspec-v1beta2-apps

https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

【问题讨论】:

    标签: kubernetes


    【解决方案1】:

    kubectl explain deploy.spec.selector --api-version=apps/v1

    pod 的标签选择器。已选择 pod 的现有 ReplicaSet 经过 这将是受此部署影响的那些。它必须与 pod 匹配 模板的标签。

    https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/api/apps/v1/types.go#L276-L279

    Selector *metav1.LabelSelector `json:"selector" protobuf:"bytes,2,opt,name=selector"`
    

    此行上方缺少+optional 表明它是强制性的。如果您尝试在没有部署的情况下进行部署,它与您将收到的错误消息相匹配。

    cat <<EOF | kubectl apply -f -
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx
     spec:
       replicas: 1
       template:
         spec:
           containers:
           - image: nginx
             imagePullPolicy: Always
             name: nginx
           dnsPolicy: ClusterFirst
           restartPolicy: Always
    EOF
    

    错误:验证“STDIN”时出错: 在 io.k8s.api.apps.v1.DeploymentSpec;如果你选择忽略这些 错误,使用 --validate=false 关闭验证。验证数据时出错:ValidationError(Deployment.spec):缺失 必填字段“选择器”

    https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go#L1076-L1085

    type LabelSelector struct {
        // matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels
        // map is equivalent to an element of matchExpressions, whose key field is "key", the
        // operator is "In", and the values array contains only "value". The requirements are ANDed.
        // +optional
        MatchLabels map[string]string `json:"matchLabels,omitempty" protobuf:"bytes,1,rep,name=matchLabels"`
        // matchExpressions is a list of label selector requirements. The requirements are ANDed.
        // +optional
        MatchExpressions []LabelSelectorRequirement `json:"matchExpressions,omitempty" protobuf:"bytes,2,rep,name=matchExpressions"`
    }
    

    【讨论】:

    • 您好,谢谢。但这并没有说明 matchLabel 的使用是否是强制性的。
    • 太棒了。但是您上面提到的错误消息告诉我,实际上可以在不指定此选择器的情况下抑制验证并创建部署。我知道代码中没有“// +可选”注释意味着它不是可选参数,但仍然不是可靠的证据,因为它只是一个注释。
    • 我不知道 GoLong,所以我不得不做一点快速阅读。显然,对于结构中不需要(可选)的字段,我们使用“,omitempty”标签。观察您在第一个链接中提到的代码,所有可选字段都有这个“,omitempty”标签,但“选择器”没有这个标签。这实际上是“选择器”不是可选的可靠证明。现在,我们在代码中看到了证明。虽然我一直在寻找官方文档,但这也一样好。答案已接受。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2012-03-21
    • 2021-02-03
    • 1970-01-01
    相关资源
    最近更新 更多