kubestellar: bug: no validation is present in bindingpolicy CRDs for label selectors

Describe the bug

It is currently possible to create bindingpolicies with invalid labels in label selector. That can be detected only looking at the kubestellar-controller-manager logs:

E0301 13:34:57.225776 1 controller.go:424] error matching selectors: values[0][name]: Invalid value: "edgeplatform-test-wec1, edgeplatform-test-wec2": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue', or 'my_value', or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')

Steps To Reproduce

  1. create a binding policy as follows:
apiVersion: control.kubestellar.io/v1alpha1
kind: Placement
metadata:
  name: nvidia-gpu-placement
spec:
  clusterSelectors:
  - matchLabels:
      name: edgeplatform-test-wec1, edgeplatform-test-wec2
  downsync:
  - objectSelectors:
    - matchLabels:
        app.kubernetes.io/part-of: wec-gpu
  wantSingletonReportedState: true
  1. look at kubestellar-controller-manager logs

Expected Behavior

Should validate and prevent user to create such binding policy.

Additional Context

A similar experiment with a deployment shows that there is validation for label selectors:

Apply a deployment as follows:

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

(⎈|kind-kubeflex:N/A)$ k apply -f ~/Desktop/test1.yml 
The Deployment "nginx-deployment" is invalid: 
* spec.selector.matchLabels: Invalid value: "nginx, nginx2": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue',  or 'my_value',  or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')
* spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string{"app":"nginx, nginx2"}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: invalid label selector

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 20 (20 by maintainers)

Most upvoted comments

For example, we might add the following.

pattern: "(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?"

Spent past two hours trying this.

The issue I ran into was that using this regex (which is exactly the same as the one in the error message in the opening commit) doesn’t stop the creation of a bindingpolicy with bad matchLabels values.

ubuntu@ip-172-31-5-54:kubestellar$ . ~/debug/kubestellar/validation/update_crd.sh 
customresourcedefinition.apiextensions.k8s.io/bindingpolicies.control.kubestellar.io configured
pattern: (([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?
type: string
ubuntu@ip-172-31-5-54:kubestellar$ kc create -f ~/debug/kubestellar/validation/bp_with_bad_matchLabels.yaml 
bindingpolicy.control.kubestellar.io/nvidia-gpu-placement created
ubuntu@ip-172-31-5-54:kubestellar$ kc get bp nvidia-gpu-placement -oyaml | yq .spec.clusterSelectors
- matchLabels:
    name: edgeplatform-test-wec1, edgeplatform-test-wec2

Until I dig into Swagger documentation https://swagger.io/docs/specification/data-models/data-types/, and noticed that the example shows pattern: '^\d{3}-\d{2}-\d{4}$' and the text says Without ^…$, pattern works as a partial match. Then I realized there are subtle diff between the matching in the opening comment and the matching here. So turns out that

                        pattern: "^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$"

stops the creation of the bad bp:

ubuntu@ip-172-31-5-54:kubestellar$ . ~/debug/kubestellar/validation/update_crd.sh 
customresourcedefinition.apiextensions.k8s.io/bindingpolicies.control.kubestellar.io configured
pattern: ^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$
type: string
ubuntu@ip-172-31-5-54:kubestellar$ kc create -f ~/debug/kubestellar/validation/bp_with_bad_matchLabels.yaml 
The BindingPolicy "nvidia-gpu-placement" is invalid: spec.clusterSelectors[0].matchLabels.name: Invalid value: "edgeplatform-test-wec1, edgeplatform-test-wec2": spec.clusterSelectors[0].matchLabels.name in body should match '^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$'

So (G) finally works. Will continue on tomorrow.