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
- 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
- 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)
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
matchLabelsvalues.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 saysWithout ^…$, 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 thatstops the creation of the bad bp:
So (G) finally works. Will continue on tomorrow.