1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-15 17:50:49 +00:00

Merge pull request #1233 from marquiz/release-0.13

[release-0.13] nfd-master: add validation of label names and values
This commit is contained in:
Kubernetes Prow Robot 2023-05-31 05:19:45 -07:00 committed by GitHub
commit 4f4cd46f13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View file

@ -368,7 +368,9 @@ func TestSetLabels(t *testing.T) {
"kubernetes.io/feature-4": "val-4",
"sub.ns.kubernetes.io/feature-5": "val-5",
vendorFeatureLabel: "val-6",
vendorProfileLabel: " val-7"}
vendorProfileLabel: "val-7",
"--invalid-name--": "valid-val",
"valid-name": "--invalid-val--"}
expectedPatches := []apihelper.JsonPatch{
apihelper.NewJsonPatch("add", "/metadata/annotations", instance+"."+nfdv1alpha1.WorkerVersionAnnotation, workerVer),
apihelper.NewJsonPatch("add", "/metadata/annotations",

View file

@ -39,6 +39,7 @@ import (
corev1 "k8s.io/api/core/v1"
k8sQuantity "k8s.io/apimachinery/pkg/api/resource"
k8sLabels "k8s.io/apimachinery/pkg/labels"
k8svalidation "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"k8s.io/klog/v2"
@ -462,6 +463,12 @@ func (m *nfdMaster) filterFeatureLabels(labels Labels) (Labels, ExtendedResource
// Add possibly missing default ns
label := addNs(label, nfdv1alpha1.FeatureLabelNs)
//Validate label name
if errs := k8svalidation.IsQualifiedName(label); len(errs) > 0 {
klog.Errorf("ignoring label %q, invalid name: %s", label, strings.Join(errs, "; "))
continue
}
ns, name := splitNs(label)
// Check label namespace, filter out if ns is not whitelisted
@ -481,6 +488,12 @@ func (m *nfdMaster) filterFeatureLabels(labels Labels) (Labels, ExtendedResource
klog.Errorf("%s (%s) does not match the whitelist (%s) and will not be published.", name, label, m.config.LabelWhiteList.Regexp.String())
continue
}
// Validate the label value
if errs := k8svalidation.IsValidLabelValue(value); len(errs) > 0 {
klog.Errorf("ignoring label %q, invalid value %q: %s", label, value, strings.Join(errs, "; "))
continue
}
outLabels[label] = value
}