Skip to content

Commit

Permalink
controller: render topologySpreadConstraints custom labelSelectors
Browse files Browse the repository at this point in the history
  • Loading branch information
morpheu committed Jun 16, 2023
1 parent 94661ad commit bfe08d3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
17 changes: 17 additions & 0 deletions internal/controllers/util/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
// - rpaasinstance.spec.ingress.annotations
// - rpaasinstance.spec.podTemplate.affinity.podAffinity
// - rpaasinstance.spec.podTemplate.affinity.podAntiAffinity
// - rpaasinstance.spec.podTemplate.topologySpreadConstraints
func RenderCustomValues(instance *v1alpha1.RpaasInstance) error {
if instance == nil {
return nil
Expand All @@ -36,6 +37,10 @@ func RenderCustomValues(instance *v1alpha1.RpaasInstance) error {
return err
}

if err := renderTopologySpreadConstraints(instance); err != nil {
return err
}

return renderAffinityCustomValues(instance)
}

Expand Down Expand Up @@ -73,6 +78,18 @@ func renderIngressCustomAnnotations(instance *v1alpha1.RpaasInstance) error {
return nil
}

func renderTopologySpreadConstraints(instance *v1alpha1.RpaasInstance) error {
if len(instance.Spec.PodTemplate.TopologySpreadConstraints) < 1 {
return nil
}
for i := range instance.Spec.PodTemplate.TopologySpreadConstraints {
if err := renderLabelSelector(instance, instance.Spec.PodTemplate.TopologySpreadConstraints[i].LabelSelector); err != nil {
return err
}
}
return nil
}

func renderAffinityCustomValues(instance *v1alpha1.RpaasInstance) error {
if instance.Spec.PodTemplate.Affinity == nil {
return nil
Expand Down
38 changes: 37 additions & 1 deletion internal/controllers/util/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,44 @@ func TestRenderCustomValues(t *testing.T) {
},
},
},
}

"with custom topology spread constraints": {
instance: &v1alpha1.RpaasInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
Namespace: "rpaasv2",
},
Spec: v1alpha1.RpaasInstanceSpec{
PodTemplate: nginxv1alpha1.NginxPodTemplateSpec{
TopologySpreadConstraints: []corev1.TopologySpreadConstraint{
{MaxSkew: 1, TopologyKey: "zone", WhenUnsatisfiable: corev1.ScheduleAnyway, LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{"example-key1": `{{ .Namespace }}`},
}}, {MaxSkew: 1, TopologyKey: "host", WhenUnsatisfiable: corev1.ScheduleAnyway, LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{"example-key2": `{{ .Name }}`},
}},
},
},
},
},
expected: &v1alpha1.RpaasInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
Namespace: "rpaasv2",
},
Spec: v1alpha1.RpaasInstanceSpec{
PodTemplate: nginxv1alpha1.NginxPodTemplateSpec{
TopologySpreadConstraints: []corev1.TopologySpreadConstraint{
{MaxSkew: 1, TopologyKey: "zone", WhenUnsatisfiable: corev1.ScheduleAnyway, LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{"example-key1": "rpaasv2"},
}}, {MaxSkew: 1, TopologyKey: "host", WhenUnsatisfiable: corev1.ScheduleAnyway, LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{"example-key2": "my-instance"},
}},
},
},
},
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
err := util.RenderCustomValues(tt.instance)
Expand Down

0 comments on commit bfe08d3

Please sign in to comment.