Skip to content

Commit

Permalink
deprecated node labels: make naming consistant and remove some unused…
Browse files Browse the repository at this point in the history
… args in funcs
  • Loading branch information
pacoxu committed May 25, 2022
1 parent db147b7 commit 234c33e
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 36 deletions.
20 changes: 10 additions & 10 deletions pkg/api/node/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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))
}
}
Expand All @@ -55,17 +55,17 @@ 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
}

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(
Expand All @@ -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(
Expand Down
8 changes: 3 additions & 5 deletions pkg/api/persistentvolume/util.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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))...)
}
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/api/persistentvolume/util_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,7 +17,6 @@ limitations under the License.
package persistentvolume

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/pod/warnings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand All @@ -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"),
Expand Down
9 changes: 4 additions & 5 deletions pkg/api/storage/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@ limitations under the License.
package storage

import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/util/validation/field"
nodeapi "k8s.io/kubernetes/pkg/api/node"
"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))
}
}
Expand All @@ -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
}
5 changes: 2 additions & 3 deletions pkg/api/storage/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package storage

import (
"context"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/core/persistentvolume/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/storage/csistoragecapacity/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/storage/storageclass/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down

0 comments on commit 234c33e

Please sign in to comment.