deprecated node labels: make naming consistant and remove some unused args in funcs

This commit is contained in:
Paco Xu 2022-05-25 11:41:51 +08:00
parent db147b7d67
commit 234c33e8b8
9 changed files with 31 additions and 36 deletions

View File

@ -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`, `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. // 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] msg, ok := deprecatedNodeLabels[key]
return msg, ok return msg, ok
} }
@ -46,7 +46,7 @@ func GetWarningsForRuntimeClass(rc *node.RuntimeClass) []string {
if rc != nil && rc.Scheduling != nil && rc.Scheduling.NodeSelector != nil { if rc != nil && rc.Scheduling != nil && rc.Scheduling.NodeSelector != nil {
// use of deprecated node labels in scheduling's node affinity // use of deprecated node labels in scheduling's node affinity
for key := range rc.Scheduling.NodeSelector { 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)) warnings = append(warnings, fmt.Sprintf("%s: %s", field.NewPath("scheduling", "nodeSelector"), msg))
} }
} }
@ -55,9 +55,9 @@ func GetWarningsForRuntimeClass(rc *node.RuntimeClass) []string {
return warnings 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. // 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 { if nodeSelector == nil {
return nil return nil
} }
@ -65,7 +65,7 @@ func WarningsForNodeSelector(nodeSelector *metav1.LabelSelector, fieldPath *fiel
var warnings []string var warnings []string
// use of deprecated node labels in matchLabelExpressions // use of deprecated node labels in matchLabelExpressions
for i, expression := range nodeSelector.MatchExpressions { for i, expression := range nodeSelector.MatchExpressions {
if msg, deprecated := GetLabelDeprecatedMessage(expression.Key); deprecated { if msg, deprecated := GetNodeLabelDeprecatedMessage(expression.Key); deprecated {
warnings = append( warnings = append(
warnings, warnings,
fmt.Sprintf( fmt.Sprintf(
@ -80,19 +80,19 @@ func WarningsForNodeSelector(nodeSelector *metav1.LabelSelector, fieldPath *fiel
// use of deprecated node labels in matchLabels // use of deprecated node labels in matchLabels
for label := range nodeSelector.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)) warnings = append(warnings, fmt.Sprintf("%s: %s", fieldPath.Child("matchLabels").Child(label), msg))
} }
} }
return warnings return warnings
} }
// WarningsForNodeSelectorTerm checks match expressions of node selector term // GetWarningsForNodeSelectorTerm checks match expressions of node selector term
func WarningsForNodeSelectorTerm(nodeSelectorTerm api.NodeSelectorTerm, fieldPath *field.Path) []string { func GetWarningsForNodeSelectorTerm(nodeSelectorTerm api.NodeSelectorTerm, fieldPath *field.Path) []string {
var warnings []string var warnings []string
// use of deprecated node labels in matchLabelExpressions // use of deprecated node labels in matchLabelExpressions
for i, expression := range nodeSelectorTerm.MatchExpressions { for i, expression := range nodeSelectorTerm.MatchExpressions {
if msg, deprecated := GetLabelDeprecatedMessage(expression.Key); deprecated { if msg, deprecated := GetNodeLabelDeprecatedMessage(expression.Key); deprecated {
warnings = append( warnings = append(
warnings, warnings,
fmt.Sprintf( fmt.Sprintf(

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2022 The Kubernetes Authors. Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -17,8 +17,6 @@ limitations under the License.
package persistentvolume package persistentvolume
import ( import (
"context"
"k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/apimachinery/pkg/util/validation/field"
utilfeature "k8s.io/apiserver/pkg/util/feature" utilfeature "k8s.io/apiserver/pkg/util/feature"
nodeapi "k8s.io/kubernetes/pkg/api/node" nodeapi "k8s.io/kubernetes/pkg/api/node"
@ -47,7 +45,7 @@ func hasNodeExpansionSecrets(oldPVSpec *api.PersistentVolumeSpec) bool {
return false return false
} }
func GetWarningsForPersistentVolume(ctx context.Context, pv *api.PersistentVolume) []string { func GetWarningsForPersistentVolume(pv *api.PersistentVolume) []string {
if pv == nil { if pv == nil {
return nil return nil
} }
@ -61,7 +59,7 @@ func warningsForPersistentVolumeSpecAndMeta(fieldPath *field.Path, pvSpec *api.P
termFldPath := fieldPath.Child("spec", "nodeAffinity", "required", "nodeSelectorTerms") termFldPath := fieldPath.Child("spec", "nodeAffinity", "required", "nodeSelectorTerms")
// use of deprecated node labels in node affinity // use of deprecated node labels in node affinity
for i, term := range pvSpec.NodeAffinity.Required.NodeSelectorTerms { 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))...)
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2022 The Kubernetes Authors. Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@ limitations under the License.
package persistentvolume package persistentvolume
import ( import (
"context"
"reflect" "reflect"
"testing" "testing"
@ -170,7 +169,7 @@ func TestWarnings(t *testing.T) {
for _, tc := range testcases { for _, tc := range testcases {
t.Run("podspec_"+tc.name, func(t *testing.T) { 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...) expected := sets.NewString(tc.expected...)
for _, missing := range expected.Difference(actual).List() { for _, missing := range expected.Difference(actual).List() {
t.Errorf("missing: %s", missing) t.Errorf("missing: %s", missing)

View File

@ -85,7 +85,7 @@ func warningsForPodSpecAndMeta(fieldPath *field.Path, podSpec *api.PodSpec, meta
// use of deprecated node labels in selectors/affinity/topology // use of deprecated node labels in selectors/affinity/topology
for k := range podSpec.NodeSelector { 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)) 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 { if n.RequiredDuringSchedulingIgnoredDuringExecution != nil {
termFldPath := fieldPath.Child("spec", "affinity", "nodeAffinity", "requiredDuringSchedulingIgnoredDuringExecution", "nodeSelectorTerms") termFldPath := fieldPath.Child("spec", "affinity", "nodeAffinity", "requiredDuringSchedulingIgnoredDuringExecution", "nodeSelectorTerms")
for i, term := range n.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") preferredFldPath := fieldPath.Child("spec", "affinity", "nodeAffinity", "preferredDuringSchedulingIgnoredDuringExecution")
for i, term := range n.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 { 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( warnings = append(warnings, fmt.Sprintf(
"%s: %s is %s", "%s: %s is %s",
fieldPath.Child("spec", "topologySpreadConstraints").Index(i).Child("topologyKey"), fieldPath.Child("spec", "topologySpreadConstraints").Index(i).Child("topologyKey"),

View File

@ -17,7 +17,6 @@ limitations under the License.
package storage package storage
import ( import (
"context"
"fmt" "fmt"
"k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/apimachinery/pkg/util/validation/field"
@ -25,14 +24,14 @@ import (
"k8s.io/kubernetes/pkg/apis/storage" "k8s.io/kubernetes/pkg/apis/storage"
) )
func GetWarningsForStorageClass(ctx context.Context, sc *storage.StorageClass) []string { func GetWarningsForStorageClass(sc *storage.StorageClass) []string {
var warnings []string var warnings []string
if sc != nil && sc.AllowedTopologies != nil { if sc != nil && sc.AllowedTopologies != nil {
// use of deprecated node labels in allowedTopologies's matchLabelExpressions // use of deprecated node labels in allowedTopologies's matchLabelExpressions
for i, topo := range sc.AllowedTopologies { for i, topo := range sc.AllowedTopologies {
for j, expression := range topo.MatchLabelExpressions { 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)) 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 return warnings
} }
func GetWarningsForCSIStorageCapacity(ctx context.Context, csc *storage.CSIStorageCapacity) []string { func GetWarningsForCSIStorageCapacity(csc *storage.CSIStorageCapacity) []string {
if csc != nil { if csc != nil {
return nodeapi.WarningsForNodeSelector(csc.NodeTopology, field.NewPath("nodeTopology")) return nodeapi.GetWarningsForNodeSelector(csc.NodeTopology, field.NewPath("nodeTopology"))
} }
return nil return nil
} }

View File

@ -17,7 +17,6 @@ limitations under the License.
package storage package storage
import ( import (
"context"
"testing" "testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -77,7 +76,7 @@ func TestStorageClassWarnings(t *testing.T) {
for _, tc := range testcases { for _, tc := range testcases {
t.Run("podspec_"+tc.name, func(t *testing.T) { 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...) expected := sets.NewString(tc.expected...)
for _, missing := range expected.Difference(actual).List() { for _, missing := range expected.Difference(actual).List() {
t.Errorf("missing: %s", missing) t.Errorf("missing: %s", missing)
@ -158,7 +157,7 @@ func TestCSIStorageCapacityWarnings(t *testing.T) {
for _, tc := range testcases { for _, tc := range testcases {
t.Run("podspec_"+tc.name, func(t *testing.T) { 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...) expected := sets.NewString(tc.expected...)
for _, missing := range expected.Difference(actual).List() { for _, missing := range expected.Difference(actual).List() {
t.Errorf("missing: %s", missing) t.Errorf("missing: %s", missing)

View File

@ -78,7 +78,7 @@ func (persistentvolumeStrategy) Validate(ctx context.Context, obj runtime.Object
// WarningsOnCreate returns warnings for the creation of the given object. // WarningsOnCreate returns warnings for the creation of the given object.
func (persistentvolumeStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { 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. // 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. // WarningsOnUpdate returns warnings for the given update.
func (persistentvolumeStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 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 { func (persistentvolumeStrategy) AllowUnconditionalUpdate() bool {

View File

@ -57,7 +57,7 @@ func (csiStorageCapacityStrategy) Validate(ctx context.Context, obj runtime.Obje
// WarningsOnCreate returns warnings for the creation of the given object. // WarningsOnCreate returns warnings for the creation of the given object.
func (csiStorageCapacityStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { 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. // 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. // WarningsOnUpdate returns warnings for the given update.
func (csiStorageCapacityStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 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 { func (csiStorageCapacityStrategy) AllowUnconditionalUpdate() bool {

View File

@ -53,7 +53,7 @@ func (storageClassStrategy) Validate(ctx context.Context, obj runtime.Object) fi
// WarningsOnCreate returns warnings for the creation of the given object. // WarningsOnCreate returns warnings for the creation of the given object.
func (storageClassStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { 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. // 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. // WarningsOnUpdate returns warnings for the given update.
func (storageClassStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 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 { func (storageClassStrategy) AllowUnconditionalUpdate() bool {