From 01519369f2d9b4c2c21b8157987da1d9f994918d Mon Sep 17 00:00:00 2001 From: Ian Booth Date: Mon, 22 Apr 2024 10:09:50 +1000 Subject: [PATCH] Do not render empty pod affinity info --- .../provider/application/constraints.go | 25 +++++++++++-------- .../provider/application/constraints_test.go | 12 +++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/caas/kubernetes/provider/application/constraints.go b/caas/kubernetes/provider/application/constraints.go index b3120f7c45f..68450f3d13c 100644 --- a/caas/kubernetes/provider/application/constraints.go +++ b/caas/kubernetes/provider/application/constraints.go @@ -159,13 +159,15 @@ func processNodeAffinity(pod *core.PodSpec, affinityLabels map[string]string) er } var nodeSelectorTerm core.NodeSelectorTerm updateSelectorTerms(&nodeSelectorTerm, affinityTags) - if pod.Affinity == nil { - pod.Affinity = &core.Affinity{} - } - pod.Affinity.NodeAffinity = &core.NodeAffinity{ - RequiredDuringSchedulingIgnoredDuringExecution: &core.NodeSelector{ - NodeSelectorTerms: []core.NodeSelectorTerm{nodeSelectorTerm}, - }, + if len(nodeSelectorTerm.MatchExpressions) > 0 { + if pod.Affinity == nil { + pod.Affinity = &core.Affinity{} + } + pod.Affinity.NodeAffinity = &core.NodeAffinity{ + RequiredDuringSchedulingIgnoredDuringExecution: &core.NodeSelector{ + NodeSelectorTerms: []core.NodeSelectorTerm{nodeSelectorTerm}, + }, + } } return nil } @@ -240,12 +242,12 @@ func processPodAffinity(pod *core.PodSpec, affinityLabels map[string]string) err affinityTerm.TopologyKey = topologyKey } } - if pod.Affinity == nil { - pod.Affinity = &core.Affinity{} - } var affinityTerm core.PodAffinityTerm updateAffinityTerm(&affinityTerm, affinityTags) if len(affinityTerm.LabelSelector.MatchExpressions) > 0 || affinityTerm.TopologyKey != "" { + if pod.Affinity == nil { + pod.Affinity = &core.Affinity{} + } pod.Affinity.PodAffinity = &core.PodAffinity{ RequiredDuringSchedulingIgnoredDuringExecution: []core.PodAffinityTerm{affinityTerm}, } @@ -254,6 +256,9 @@ func processPodAffinity(pod *core.PodSpec, affinityLabels map[string]string) err var antiAffinityTerm core.PodAffinityTerm updateAffinityTerm(&antiAffinityTerm, antiAffinityTags) if len(antiAffinityTerm.LabelSelector.MatchExpressions) > 0 || antiAffinityTerm.TopologyKey != "" { + if pod.Affinity == nil { + pod.Affinity = &core.Affinity{} + } pod.Affinity.PodAntiAffinity = &core.PodAntiAffinity{ RequiredDuringSchedulingIgnoredDuringExecution: []core.PodAffinityTerm{antiAffinityTerm}, } diff --git a/caas/kubernetes/provider/application/constraints_test.go b/caas/kubernetes/provider/application/constraints_test.go index ce39bb93c58..814e9a2863f 100644 --- a/caas/kubernetes/provider/application/constraints_test.go +++ b/caas/kubernetes/provider/application/constraints_test.go @@ -69,6 +69,8 @@ func (s *applyConstraintsSuite) TestPodAffinityJustTopologyKey(c *gc.C) { TopologyKey: "foo", }}, }) + c.Assert(pod.Affinity.PodAntiAffinity, gc.IsNil) + c.Assert(pod.Affinity.NodeAffinity, gc.IsNil) } func (s *applyConstraintsSuite) TestAffinityPod(c *gc.C) { @@ -94,6 +96,8 @@ func (s *applyConstraintsSuite) TestAffinityPod(c *gc.C) { }, }}, }) + c.Assert(pod.Affinity.PodAntiAffinity, gc.IsNil) + c.Assert(pod.Affinity.NodeAffinity, gc.IsNil) } func (s *applyConstraintsSuite) TestPodAffinityAll(c *gc.C) { @@ -135,6 +139,8 @@ func (s *applyConstraintsSuite) TestAntiPodAffinityJustTopologyKey(c *gc.C) { TopologyKey: "foo", }}, }) + c.Assert(pod.Affinity.PodAffinity, gc.IsNil) + c.Assert(pod.Affinity.NodeAffinity, gc.IsNil) } func (s *applyConstraintsSuite) TestAntiPodAffinity(c *gc.C) { @@ -160,6 +166,8 @@ func (s *applyConstraintsSuite) TestAntiPodAffinity(c *gc.C) { }, }}, }) + c.Assert(pod.Affinity.PodAffinity, gc.IsNil) + c.Assert(pod.Affinity.NodeAffinity, gc.IsNil) } func (s *applyConstraintsSuite) TestAntiPodAffinityAll(c *gc.C) { @@ -186,6 +194,8 @@ func (s *applyConstraintsSuite) TestAntiPodAffinityAll(c *gc.C) { TopologyKey: "foo", }}, }) + c.Assert(pod.Affinity.PodAffinity, gc.IsNil) + c.Assert(pod.Affinity.NodeAffinity, gc.IsNil) } func (s *applyConstraintsSuite) TestNodeAntiAffinity(c *gc.C) { @@ -210,4 +220,6 @@ func (s *applyConstraintsSuite) TestNodeAntiAffinity(c *gc.C) { }}, }, }) + c.Assert(pod.Affinity.PodAffinity, gc.IsNil) + c.Assert(pod.Affinity.PodAntiAffinity, gc.IsNil) }