Skip to content

Commit

Permalink
Do not render empty pod affinity info
Browse files Browse the repository at this point in the history
  • Loading branch information
wallyworld committed Apr 22, 2024
1 parent 405bb6b commit 0151936
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
25 changes: 15 additions & 10 deletions caas/kubernetes/provider/application/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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},
}
Expand All @@ -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},
}
Expand Down
12 changes: 12 additions & 0 deletions caas/kubernetes/provider/application/constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
}

0 comments on commit 0151936

Please sign in to comment.