Skip to content

Commit

Permalink
propogate labels from limitador to deployment template
Browse files Browse the repository at this point in the history
Signed-off-by: Laura Fitzgerald <[email protected]>
  • Loading branch information
laurafitzgerald committed Feb 10, 2025
1 parent 48cb256 commit b792015
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions controllers/limitador_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func (r *LimitadorReconciler) reconcileDeployment(ctx context.Context, limitador
reconcilers.DeploymentPortsMutator,
reconcilers.DeploymentLivenessProbeMutator,
reconcilers.DeploymentReadinessProbeMutator,
reconcilers.DeploymentTemplateLabelMutator,
)

// reconcile imagepullsecrets only when set in limitador CR
Expand Down
8 changes: 7 additions & 1 deletion pkg/limitador/k8s_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func Deployment(limitador *limitadorv1alpha1.Limitador, deploymentOptions Deploy
image = *limitador.Spec.Image
}

templateLabels := Labels(limitador)
if limitador.ObjectMeta.Labels != nil {
for key, value := range limitador.ObjectMeta.Labels {
templateLabels[key] = value
}
}
return &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
Expand All @@ -84,7 +90,7 @@ func Deployment(limitador *limitadorv1alpha1.Limitador, deploymentOptions Deploy
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: Labels(limitador),
Labels: templateLabels,
},
Spec: v1.PodSpec{
Affinity: limitador.Spec.Affinity,
Expand Down
36 changes: 34 additions & 2 deletions pkg/limitador/k8s_objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newTestLimitadorObj(name, namespace string, limits []limitadorv1alpha1.Rate
httpPort = int32(8000)
grpcPort = int32(8001)
)
return &limitadorv1alpha1.Limitador{
limitador := &limitadorv1alpha1.Limitador{
TypeMeta: metav1.TypeMeta{
Kind: "Limitador",
APIVersion: "limitador.kuadrant.io/v1alpha1",
Expand All @@ -58,6 +58,7 @@ func newTestLimitadorObj(name, namespace string, limits []limitadorv1alpha1.Rate
},
},
}
return limitador
}

func TestServiceName(t *testing.T) {
Expand All @@ -83,13 +84,44 @@ func TestDeployment(t *testing.T) {
})

t.Run("labels", func(subT *testing.T) {
limObjLabels := newTestLimitadorObj("some-name", "some-ns", nil)
limObjLabels.Labels = map[string]string{
"test-label-key": "test-label-value",
}
deployment := Deployment(limObjLabels, DeploymentOptions{})
assert.DeepEqual(subT, deployment.Labels,
map[string]string{
"app": "limitador",
"limitador-resource": "some-name",
})
assert.DeepEqual(subT, deployment.Spec.Template.Labels,
map[string]string{
"app": "limitador",
"limitador-resource": "some-name",
"test-label-key": "test-label-value",
})
assert.DeepEqual(subT, deployment.Spec.Selector.MatchLabels,
map[string]string{
"app": "limitador",
"limitador-resource": "some-name",
})
limObj := newTestLimitadorObj("some-name", "some-ns", nil)
deployment := Deployment(limObj, DeploymentOptions{})
deployment = Deployment(limObj, DeploymentOptions{})
assert.DeepEqual(subT, deployment.Labels,
map[string]string{
"app": "limitador",
"limitador-resource": "some-name",
})
assert.DeepEqual(subT, deployment.Spec.Template.Labels,
map[string]string{
"app": "limitador",
"limitador-resource": "some-name",
})
assert.DeepEqual(subT, deployment.Spec.Selector.MatchLabels,
map[string]string{
"app": "limitador",
"limitador-resource": "some-name",
})
})
t.Run("selector", func(subT *testing.T) {
limObj := newTestLimitadorObj("some-name", "some-ns", nil)
Expand Down
10 changes: 10 additions & 0 deletions pkg/reconcilers/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ func DeploymentMutator(opts ...DeploymentMutateFn) MutateFn {
}
}

func DeploymentTemplateLabelMutator(desired, existing *appsv1.Deployment) bool {
update := false
for key, val := range desired.Spec.Template.Labels {
if v, exists := existing.Spec.Template.Labels[key]; !exists || !reflect.DeepEqual(v, val) {
return true
}
}
return update
}

func DeploymentAffinityMutator(desired, existing *appsv1.Deployment) bool {
update := false
if !reflect.DeepEqual(existing.Spec.Template.Spec.Affinity, desired.Spec.Template.Spec.Affinity) {
Expand Down

0 comments on commit b792015

Please sign in to comment.