From 234c33e8b83335cacbf39fc374e2c4dca737b13f Mon Sep 17 00:00:00 2001 From: Paco Xu Date: Wed, 25 May 2022 11:41:51 +0800 Subject: [PATCH] deprecated node labels: make naming consistant and remove some unused args in funcs --- pkg/api/node/util.go | 20 +++++++++---------- pkg/api/persistentvolume/util.go | 8 +++----- pkg/api/persistentvolume/util_test.go | 5 ++--- pkg/api/pod/warnings.go | 8 ++++---- pkg/api/storage/util.go | 9 ++++----- pkg/api/storage/util_test.go | 5 ++--- .../core/persistentvolume/strategy.go | 4 ++-- .../storage/csistoragecapacity/strategy.go | 4 ++-- pkg/registry/storage/storageclass/strategy.go | 4 ++-- 9 files changed, 31 insertions(+), 36 deletions(-) diff --git a/pkg/api/node/util.go b/pkg/api/node/util.go index 166366dfb0d..fcc6dba4145 100644 --- a/pkg/api/node/util.go +++ b/pkg/api/node/util.go @@ -33,9 +33,9 @@ var deprecatedNodeLabels = map[string]string{ `beta.kubernetes.io/instance-type`: `deprecated since v1.17; use "node.kubernetes.io/instance-type" instead`, } -// GetLabelDeprecatedMessage returns the message for the deprecated label +// GetNodeLabelDeprecatedMessage returns the message for the deprecated node label // and a bool indicating if the label is deprecated. -func GetLabelDeprecatedMessage(key string) (string, bool) { +func GetNodeLabelDeprecatedMessage(key string) (string, bool) { msg, ok := deprecatedNodeLabels[key] return msg, ok } @@ -46,7 +46,7 @@ func GetWarningsForRuntimeClass(rc *node.RuntimeClass) []string { if rc != nil && rc.Scheduling != nil && rc.Scheduling.NodeSelector != nil { // use of deprecated node labels in scheduling's node affinity for key := range rc.Scheduling.NodeSelector { - if msg, deprecated := GetLabelDeprecatedMessage(key); deprecated { + if msg, deprecated := GetNodeLabelDeprecatedMessage(key); deprecated { warnings = append(warnings, fmt.Sprintf("%s: %s", field.NewPath("scheduling", "nodeSelector"), msg)) } } @@ -55,9 +55,9 @@ func GetWarningsForRuntimeClass(rc *node.RuntimeClass) []string { return warnings } -// WarningsForNodeSelector tests if any of the node selector requirements in the template is deprecated. +// GetWarningsForNodeSelector tests if any of the node selector requirements in the template is deprecated. // If there are deprecated node selector requirements in either match expressions or match labels, a warning is returned. -func WarningsForNodeSelector(nodeSelector *metav1.LabelSelector, fieldPath *field.Path) []string { +func GetWarningsForNodeSelector(nodeSelector *metav1.LabelSelector, fieldPath *field.Path) []string { if nodeSelector == nil { return nil } @@ -65,7 +65,7 @@ func WarningsForNodeSelector(nodeSelector *metav1.LabelSelector, fieldPath *fiel var warnings []string // use of deprecated node labels in matchLabelExpressions for i, expression := range nodeSelector.MatchExpressions { - if msg, deprecated := GetLabelDeprecatedMessage(expression.Key); deprecated { + if msg, deprecated := GetNodeLabelDeprecatedMessage(expression.Key); deprecated { warnings = append( warnings, fmt.Sprintf( @@ -80,19 +80,19 @@ func WarningsForNodeSelector(nodeSelector *metav1.LabelSelector, fieldPath *fiel // use of deprecated node labels in matchLabels for label := range nodeSelector.MatchLabels { - if msg, deprecated := GetLabelDeprecatedMessage(label); deprecated { + if msg, deprecated := GetNodeLabelDeprecatedMessage(label); deprecated { warnings = append(warnings, fmt.Sprintf("%s: %s", fieldPath.Child("matchLabels").Child(label), msg)) } } return warnings } -// WarningsForNodeSelectorTerm checks match expressions of node selector term -func WarningsForNodeSelectorTerm(nodeSelectorTerm api.NodeSelectorTerm, fieldPath *field.Path) []string { +// GetWarningsForNodeSelectorTerm checks match expressions of node selector term +func GetWarningsForNodeSelectorTerm(nodeSelectorTerm api.NodeSelectorTerm, fieldPath *field.Path) []string { var warnings []string // use of deprecated node labels in matchLabelExpressions for i, expression := range nodeSelectorTerm.MatchExpressions { - if msg, deprecated := GetLabelDeprecatedMessage(expression.Key); deprecated { + if msg, deprecated := GetNodeLabelDeprecatedMessage(expression.Key); deprecated { warnings = append( warnings, fmt.Sprintf( diff --git a/pkg/api/persistentvolume/util.go b/pkg/api/persistentvolume/util.go index a86df0ba4a5..29862d73d5b 100644 --- a/pkg/api/persistentvolume/util.go +++ b/pkg/api/persistentvolume/util.go @@ -1,5 +1,5 @@ /* -Copyright 2022 The Kubernetes Authors. +Copyright 2017 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -17,8 +17,6 @@ limitations under the License. package persistentvolume import ( - "context" - "k8s.io/apimachinery/pkg/util/validation/field" utilfeature "k8s.io/apiserver/pkg/util/feature" nodeapi "k8s.io/kubernetes/pkg/api/node" @@ -47,7 +45,7 @@ func hasNodeExpansionSecrets(oldPVSpec *api.PersistentVolumeSpec) bool { return false } -func GetWarningsForPersistentVolume(ctx context.Context, pv *api.PersistentVolume) []string { +func GetWarningsForPersistentVolume(pv *api.PersistentVolume) []string { if pv == nil { return nil } @@ -61,7 +59,7 @@ func warningsForPersistentVolumeSpecAndMeta(fieldPath *field.Path, pvSpec *api.P termFldPath := fieldPath.Child("spec", "nodeAffinity", "required", "nodeSelectorTerms") // use of deprecated node labels in node affinity for i, term := range pvSpec.NodeAffinity.Required.NodeSelectorTerms { - warnings = append(warnings, nodeapi.WarningsForNodeSelectorTerm(term, termFldPath.Index(i))...) + warnings = append(warnings, nodeapi.GetWarningsForNodeSelectorTerm(term, termFldPath.Index(i))...) } } diff --git a/pkg/api/persistentvolume/util_test.go b/pkg/api/persistentvolume/util_test.go index b143c1fef2e..1838c054c38 100644 --- a/pkg/api/persistentvolume/util_test.go +++ b/pkg/api/persistentvolume/util_test.go @@ -1,5 +1,5 @@ /* -Copyright 2022 The Kubernetes Authors. +Copyright 2018 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -17,7 +17,6 @@ limitations under the License. package persistentvolume import ( - "context" "reflect" "testing" @@ -170,7 +169,7 @@ func TestWarnings(t *testing.T) { for _, tc := range testcases { t.Run("podspec_"+tc.name, func(t *testing.T) { - actual := sets.NewString(GetWarningsForPersistentVolume(context.TODO(), tc.template)...) + actual := sets.NewString(GetWarningsForPersistentVolume(tc.template)...) expected := sets.NewString(tc.expected...) for _, missing := range expected.Difference(actual).List() { t.Errorf("missing: %s", missing) diff --git a/pkg/api/pod/warnings.go b/pkg/api/pod/warnings.go index 53c905f1d15..c0b000a68fa 100644 --- a/pkg/api/pod/warnings.go +++ b/pkg/api/pod/warnings.go @@ -85,7 +85,7 @@ func warningsForPodSpecAndMeta(fieldPath *field.Path, podSpec *api.PodSpec, meta // use of deprecated node labels in selectors/affinity/topology for k := range podSpec.NodeSelector { - if msg, deprecated := nodeapi.GetLabelDeprecatedMessage(k); deprecated { + if msg, deprecated := nodeapi.GetNodeLabelDeprecatedMessage(k); deprecated { warnings = append(warnings, fmt.Sprintf("%s: %s", fieldPath.Child("spec", "nodeSelector").Key(k), msg)) } } @@ -94,16 +94,16 @@ func warningsForPodSpecAndMeta(fieldPath *field.Path, podSpec *api.PodSpec, meta if n.RequiredDuringSchedulingIgnoredDuringExecution != nil { termFldPath := fieldPath.Child("spec", "affinity", "nodeAffinity", "requiredDuringSchedulingIgnoredDuringExecution", "nodeSelectorTerms") for i, term := range n.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms { - warnings = append(warnings, nodeapi.WarningsForNodeSelectorTerm(term, termFldPath.Index(i))...) + warnings = append(warnings, nodeapi.GetWarningsForNodeSelectorTerm(term, termFldPath.Index(i))...) } } preferredFldPath := fieldPath.Child("spec", "affinity", "nodeAffinity", "preferredDuringSchedulingIgnoredDuringExecution") for i, term := range n.PreferredDuringSchedulingIgnoredDuringExecution { - warnings = append(warnings, nodeapi.WarningsForNodeSelectorTerm(term.Preference, preferredFldPath.Index(i).Child("preference"))...) + warnings = append(warnings, nodeapi.GetWarningsForNodeSelectorTerm(term.Preference, preferredFldPath.Index(i).Child("preference"))...) } } for i, t := range podSpec.TopologySpreadConstraints { - if msg, deprecated := nodeapi.GetLabelDeprecatedMessage(t.TopologyKey); deprecated { + if msg, deprecated := nodeapi.GetNodeLabelDeprecatedMessage(t.TopologyKey); deprecated { warnings = append(warnings, fmt.Sprintf( "%s: %s is %s", fieldPath.Child("spec", "topologySpreadConstraints").Index(i).Child("topologyKey"), diff --git a/pkg/api/storage/util.go b/pkg/api/storage/util.go index 7c130feaf41..133d8b2b646 100644 --- a/pkg/api/storage/util.go +++ b/pkg/api/storage/util.go @@ -17,7 +17,6 @@ limitations under the License. package storage import ( - "context" "fmt" "k8s.io/apimachinery/pkg/util/validation/field" @@ -25,14 +24,14 @@ import ( "k8s.io/kubernetes/pkg/apis/storage" ) -func GetWarningsForStorageClass(ctx context.Context, sc *storage.StorageClass) []string { +func GetWarningsForStorageClass(sc *storage.StorageClass) []string { var warnings []string if sc != nil && sc.AllowedTopologies != nil { // use of deprecated node labels in allowedTopologies's matchLabelExpressions for i, topo := range sc.AllowedTopologies { for j, expression := range topo.MatchLabelExpressions { - if msg, deprecated := nodeapi.GetLabelDeprecatedMessage(expression.Key); deprecated { + if msg, deprecated := nodeapi.GetNodeLabelDeprecatedMessage(expression.Key); deprecated { warnings = append(warnings, fmt.Sprintf("%s: %s", field.NewPath("allowedTopologies").Index(i).Child("matchLabelExpressions").Index(j).Child("key"), msg)) } } @@ -42,9 +41,9 @@ func GetWarningsForStorageClass(ctx context.Context, sc *storage.StorageClass) [ return warnings } -func GetWarningsForCSIStorageCapacity(ctx context.Context, csc *storage.CSIStorageCapacity) []string { +func GetWarningsForCSIStorageCapacity(csc *storage.CSIStorageCapacity) []string { if csc != nil { - return nodeapi.WarningsForNodeSelector(csc.NodeTopology, field.NewPath("nodeTopology")) + return nodeapi.GetWarningsForNodeSelector(csc.NodeTopology, field.NewPath("nodeTopology")) } return nil } diff --git a/pkg/api/storage/util_test.go b/pkg/api/storage/util_test.go index 17c0fbb1645..5f34570487c 100644 --- a/pkg/api/storage/util_test.go +++ b/pkg/api/storage/util_test.go @@ -17,7 +17,6 @@ limitations under the License. package storage import ( - "context" "testing" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -77,7 +76,7 @@ func TestStorageClassWarnings(t *testing.T) { for _, tc := range testcases { t.Run("podspec_"+tc.name, func(t *testing.T) { - actual := sets.NewString(GetWarningsForStorageClass(context.TODO(), tc.template)...) + actual := sets.NewString(GetWarningsForStorageClass(tc.template)...) expected := sets.NewString(tc.expected...) for _, missing := range expected.Difference(actual).List() { t.Errorf("missing: %s", missing) @@ -158,7 +157,7 @@ func TestCSIStorageCapacityWarnings(t *testing.T) { for _, tc := range testcases { t.Run("podspec_"+tc.name, func(t *testing.T) { - actual := sets.NewString(GetWarningsForCSIStorageCapacity(context.TODO(), tc.template)...) + actual := sets.NewString(GetWarningsForCSIStorageCapacity(tc.template)...) expected := sets.NewString(tc.expected...) for _, missing := range expected.Difference(actual).List() { t.Errorf("missing: %s", missing) diff --git a/pkg/registry/core/persistentvolume/strategy.go b/pkg/registry/core/persistentvolume/strategy.go index a9e190f7964..e175a4097ad 100644 --- a/pkg/registry/core/persistentvolume/strategy.go +++ b/pkg/registry/core/persistentvolume/strategy.go @@ -78,7 +78,7 @@ func (persistentvolumeStrategy) Validate(ctx context.Context, obj runtime.Object // WarningsOnCreate returns warnings for the creation of the given object. func (persistentvolumeStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { - return pvutil.GetWarningsForPersistentVolume(ctx, obj.(*api.PersistentVolume)) + return pvutil.GetWarningsForPersistentVolume(obj.(*api.PersistentVolume)) } // Canonicalize normalizes the object after validation. @@ -109,7 +109,7 @@ func (persistentvolumeStrategy) ValidateUpdate(ctx context.Context, obj, old run // WarningsOnUpdate returns warnings for the given update. func (persistentvolumeStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { - return pvutil.GetWarningsForPersistentVolume(ctx, obj.(*api.PersistentVolume)) + return pvutil.GetWarningsForPersistentVolume(obj.(*api.PersistentVolume)) } func (persistentvolumeStrategy) AllowUnconditionalUpdate() bool { diff --git a/pkg/registry/storage/csistoragecapacity/strategy.go b/pkg/registry/storage/csistoragecapacity/strategy.go index 95df1945b4d..559b6bba04e 100644 --- a/pkg/registry/storage/csistoragecapacity/strategy.go +++ b/pkg/registry/storage/csistoragecapacity/strategy.go @@ -57,7 +57,7 @@ func (csiStorageCapacityStrategy) Validate(ctx context.Context, obj runtime.Obje // WarningsOnCreate returns warnings for the creation of the given object. func (csiStorageCapacityStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { - return storageutil.GetWarningsForCSIStorageCapacity(ctx, obj.(*storage.CSIStorageCapacity)) + return storageutil.GetWarningsForCSIStorageCapacity(obj.(*storage.CSIStorageCapacity)) } // Canonicalize normalizes the object after validation. @@ -81,7 +81,7 @@ func (csiStorageCapacityStrategy) ValidateUpdate(ctx context.Context, obj, old r // WarningsOnUpdate returns warnings for the given update. func (csiStorageCapacityStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { - return storageutil.GetWarningsForCSIStorageCapacity(ctx, obj.(*storage.CSIStorageCapacity)) + return storageutil.GetWarningsForCSIStorageCapacity(obj.(*storage.CSIStorageCapacity)) } func (csiStorageCapacityStrategy) AllowUnconditionalUpdate() bool { diff --git a/pkg/registry/storage/storageclass/strategy.go b/pkg/registry/storage/storageclass/strategy.go index c3b86b7ab11..7e833a06f4f 100644 --- a/pkg/registry/storage/storageclass/strategy.go +++ b/pkg/registry/storage/storageclass/strategy.go @@ -53,7 +53,7 @@ func (storageClassStrategy) Validate(ctx context.Context, obj runtime.Object) fi // WarningsOnCreate returns warnings for the creation of the given object. func (storageClassStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { - return storageutil.GetWarningsForStorageClass(ctx, obj.(*storage.StorageClass)) + return storageutil.GetWarningsForStorageClass(obj.(*storage.StorageClass)) } // Canonicalize normalizes the object after validation. @@ -75,7 +75,7 @@ func (storageClassStrategy) ValidateUpdate(ctx context.Context, obj, old runtime // WarningsOnUpdate returns warnings for the given update. func (storageClassStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { - return storageutil.GetWarningsForStorageClass(ctx, obj.(*storage.StorageClass)) + return storageutil.GetWarningsForStorageClass(obj.(*storage.StorageClass)) } func (storageClassStrategy) AllowUnconditionalUpdate() bool {