kubernetes - where is the official api documentation that says whether matchLabels is mandatory or not

6/10/2019

Today I was going through some documentation and discussions about matchLabels statement that is a part of a Deployment (or other objects) in Kubernetes. Example below:

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

In some discussions, I saw that depending on the version of the API it could be optional or mandatory to use this selector.

Ref:

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

What is the purpose of a kubernetes deployment pod selector?

But I can't see any official documentation where it is stated explicitly if the usage of this selector was mandatory or not for a specific version of a Kubernetes API. Do you know of any official documentation where it is stated whether or not it is mandatory to use matchLabels selector?

I have checked these links out but I did not bump into an official statement: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#deploymentspec-v1beta2-apps

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

-- honor
kubernetes

1 Answer

6/10/2019

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

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.

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"`

The lack of +optional above this line tells you it's mandatory. It matches up with the error message you'll get if you try to make a deployment without one.

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

error: error validating "STDIN": in io.k8s.api.apps.v1.DeploymentSpec; if you choose to ignore these errors, turn validation off with --validate=false. error validating data: ValidationError(Deployment.spec): missing required field "selector"

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"`
}
-- eamon1234
Source: StackOverflow