mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-04 15:05:20 +00:00
Merge pull request #111441 from denkensk/respect-topology
Respect PodTopologySpread after rolling upgrades
This commit is contained in:
@@ -545,6 +545,7 @@ func dropDisabledFields(
|
||||
|
||||
dropDisabledTopologySpreadConstraintsFields(podSpec, oldPodSpec)
|
||||
dropDisabledNodeInclusionPolicyFields(podSpec, oldPodSpec)
|
||||
dropDisabledMatchLabelKeysField(podSpec, oldPodSpec)
|
||||
}
|
||||
|
||||
// dropDisabledTopologySpreadConstraintsFields removes disabled fields from PodSpec related
|
||||
@@ -618,6 +619,31 @@ func dropDisabledNodeInclusionPolicyFields(podSpec, oldPodSpec *api.PodSpec) {
|
||||
}
|
||||
}
|
||||
|
||||
// dropDisabledMatchLabelKeysField removes disabled fields from PodSpec related
|
||||
// to MatchLabelKeys only if it is not already used by the old spec.
|
||||
func dropDisabledMatchLabelKeysField(podSpec, oldPodSpec *api.PodSpec) {
|
||||
if !utilfeature.DefaultFeatureGate.Enabled(features.MatchLabelKeysInPodTopologySpread) && !matchLabelKeysInUse(oldPodSpec) {
|
||||
for i := range podSpec.TopologySpreadConstraints {
|
||||
podSpec.TopologySpreadConstraints[i].MatchLabelKeys = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// matchLabelKeysInUse returns true if the pod spec is non-nil
|
||||
// and has MatchLabelKeys field set in TopologySpreadConstraints.
|
||||
func matchLabelKeysInUse(podSpec *api.PodSpec) bool {
|
||||
if podSpec == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, c := range podSpec.TopologySpreadConstraints {
|
||||
if len(c.MatchLabelKeys) > 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// nodeAffinityPolicyInUse returns true if the pod spec is non-nil and has NodeAffinityPolicy field set
|
||||
// in TopologySpreadConstraints
|
||||
func nodeAffinityPolicyInUse(podSpec *api.PodSpec) bool {
|
||||
|
||||
@@ -1814,3 +1814,138 @@ func TestDropNodeInclusionPolicyFields(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDropDisabledMatchLabelKeysField(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
enabled bool
|
||||
podSpec *api.PodSpec
|
||||
oldPodSpec *api.PodSpec
|
||||
wantPodSpec *api.PodSpec
|
||||
}{
|
||||
{
|
||||
name: "feature disabled, both pods don't use MatchLabelKeys fields",
|
||||
enabled: false,
|
||||
oldPodSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{},
|
||||
},
|
||||
podSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{},
|
||||
},
|
||||
wantPodSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "feature disabled, only old pod uses MatchLabelKeys field",
|
||||
enabled: false,
|
||||
oldPodSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{
|
||||
{MatchLabelKeys: []string{"foo"}},
|
||||
},
|
||||
},
|
||||
podSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{},
|
||||
},
|
||||
wantPodSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "feature disabled, only current pod uses MatchLabelKeys field",
|
||||
enabled: false,
|
||||
oldPodSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{},
|
||||
},
|
||||
podSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{
|
||||
{MatchLabelKeys: []string{"foo"}},
|
||||
},
|
||||
},
|
||||
wantPodSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{{}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "feature disabled, both pods use MatchLabelKeys fields",
|
||||
enabled: false,
|
||||
oldPodSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{
|
||||
{MatchLabelKeys: []string{"foo"}},
|
||||
},
|
||||
},
|
||||
podSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{
|
||||
{MatchLabelKeys: []string{"foo"}},
|
||||
},
|
||||
},
|
||||
wantPodSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{
|
||||
{MatchLabelKeys: []string{"foo"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "feature enabled, only old pod uses MatchLabelKeys field",
|
||||
enabled: true,
|
||||
oldPodSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{
|
||||
{MatchLabelKeys: []string{"foo"}},
|
||||
},
|
||||
},
|
||||
podSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{},
|
||||
},
|
||||
wantPodSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "feature enabled, only current pod uses MatchLabelKeys field",
|
||||
enabled: true,
|
||||
oldPodSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{},
|
||||
},
|
||||
podSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{
|
||||
{MatchLabelKeys: []string{"foo"}},
|
||||
},
|
||||
},
|
||||
wantPodSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{
|
||||
{MatchLabelKeys: []string{"foo"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "feature enabled, both pods use MatchLabelKeys fields",
|
||||
enabled: false,
|
||||
oldPodSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{
|
||||
{MatchLabelKeys: []string{"foo"}},
|
||||
},
|
||||
},
|
||||
podSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{
|
||||
{MatchLabelKeys: []string{"foo"}},
|
||||
},
|
||||
},
|
||||
wantPodSpec: &api.PodSpec{
|
||||
TopologySpreadConstraints: []api.TopologySpreadConstraint{
|
||||
{MatchLabelKeys: []string{"foo"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.MatchLabelKeysInPodTopologySpread, test.enabled)()
|
||||
|
||||
dropDisabledFields(test.podSpec, nil, test.oldPodSpec, nil)
|
||||
if diff := cmp.Diff(test.wantPodSpec, test.podSpec); diff != "" {
|
||||
t.Errorf("unexpected pod spec (-want, +got):\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user