Skip to content

Commit

Permalink
feat(hpa): add hpa dedicated labels and annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Grégory Sanchez <gregory.sanchez@icloud.com>
Signed-off-by: Grégory SANCHEZ <gregory.sanchez@icloud.com>
  • Loading branch information
chubchubsancho committed Jun 14, 2024
1 parent 21e17d8 commit 87c70da
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ Here is an overview of all new **experimental** features:

### Fixes

- **General**: Fix reconcile HPA when ScaledObject annotations or labels is updated ([#5468](https://github.com/kedacore/keda/issues/5468))
- **General**: Scalers are properly closed after being refreshed ([#5806](https://github.com/kedacore/keda/issues/5806))
- **MongoDB Scaler**: MongoDB url parses correctly `+srv` scheme ([#5760](https://github.com/kedacore/keda/issues/5760))
- **General**: Fix reconcile HPA when ScaledObject annotations or labels is updated ([#5468](https://github.com/kedacore/keda/issues/5468))

### Deprecations

Expand Down
8 changes: 8 additions & 0 deletions apis/keda/v1alpha1/scaledobject_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ type HorizontalPodAutoscalerConfig struct {
Behavior *autoscalingv2.HorizontalPodAutoscalerBehavior `json:"behavior,omitempty"`
// +optional
Name string `json:"name,omitempty"`
// Those labels are added to the HorizontalPodAutoscaler created by KEDA
// and reconciled by KEDA on change
// +optional
Labels map[string]string `json:"labels,omitempty"`
// Those annotations are added to the HorizontalPodAutoscaler created by KEDA
// and reconciled by KEDA on change
// +optional
Annotations map[string]string `json:"annotations,omitempty"`
}

// ScaleTarget holds the reference to the scale target Object
Expand Down
14 changes: 14 additions & 0 deletions config/crd/bases/keda.sh_scaledobjects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ spec:
description: HorizontalPodAutoscalerConfig specifies horizontal
scale config
properties:
annotations:
additionalProperties:
type: string
description: |-
Those annotations are added to the HorizontalPodAutoscaler created by KEDA
and reconciled by KEDA on change
type: object
behavior:
description: |-
HorizontalPodAutoscalerBehavior configures the scaling behavior of the target
Expand Down Expand Up @@ -197,6 +204,13 @@ spec:
type: integer
type: object
type: object
labels:
additionalProperties:
type: string
description: |-
Those labels are added to the HorizontalPodAutoscaler created by KEDA
and reconciled by KEDA on change
type: object
name:
type: string
type: object
Expand Down
21 changes: 18 additions & 3 deletions controllers/keda/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
kedacontrollerutil "github.com/kedacore/keda/v2/controllers/keda/util"
"github.com/kedacore/keda/v2/pkg/scaling/executor"
kedastatus "github.com/kedacore/keda/v2/pkg/status"
version "github.com/kedacore/keda/v2/version"
"github.com/kedacore/keda/v2/version"
)

// createAndDeployNewHPA creates and deploy HPA in the cluster for specified ScaledObject
Expand Down Expand Up @@ -95,9 +95,24 @@ func (r *ScaledObjectReconciler) newHPAForScaledObject(ctx context.Context, logg
"app.kubernetes.io/part-of": scaledObject.Name,
"app.kubernetes.io/managed-by": "keda-operator",
}
for key, value := range scaledObject.ObjectMeta.Labels {
for key, value := range scaledObject.Labels {
labels[key] = value
}
if scaledObject.Spec.Advanced.HorizontalPodAutoscalerConfig.Labels != nil {
for key, value := range scaledObject.Spec.Advanced.HorizontalPodAutoscalerConfig.Labels {
labels[key] = value
}
}

annotations := map[string]string{}
for key, value := range scaledObject.Annotations {
annotations[key] = value
}
if scaledObject.Spec.Advanced.HorizontalPodAutoscalerConfig.Annotations != nil {
for key, value := range scaledObject.Spec.Advanced.HorizontalPodAutoscalerConfig.Annotations {
annotations[key] = value
}
}

minReplicas := scaledObject.GetHPAMinReplicas()
maxReplicas := scaledObject.GetHPAMaxReplicas()
Expand Down Expand Up @@ -130,7 +145,7 @@ func (r *ScaledObjectReconciler) newHPAForScaledObject(ctx context.Context, logg
Name: getHPAName(scaledObject),
Namespace: scaledObject.Namespace,
Labels: labels,
Annotations: scaledObject.Annotations,
Annotations: annotations,
},
TypeMeta: metav1.TypeMeta{
APIVersion: "v2",
Expand Down
2 changes: 0 additions & 2 deletions controllers/keda/scaledobject_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ func (r *ScaledObjectReconciler) SetupWithManager(mgr ctrl.Manager, options cont
kedacontrollerutil.PausedReplicasPredicate{},
kedacontrollerutil.ScaleObjectReadyConditionPredicate{},
predicate.GenerationChangedPredicate{},
predicate.LabelChangedPredicate{},
predicate.AnnotationChangedPredicate{},
),
)).
WithEventFilter(util.IgnoreOtherNamespaces()).
Expand Down
32 changes: 24 additions & 8 deletions controllers/keda/scaledobject_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1454,19 +1454,24 @@ var _ = Describe("ScaledObjectController", func() {
// Create the ScaledObject without specifying name.
so := &kedav1alpha1.ScaledObject{
ObjectMeta: metav1.ObjectMeta{
Name: soName,
Namespace: "default",
Annotations: map[string]string{
"annotation-email": "email@example.com",
"annotation-url": "https://example.com",
},
Name: soName,
Namespace: "default",
Annotations: map[string]string{},
},
Spec: kedav1alpha1.ScaledObjectSpec{
ScaleTargetRef: &kedav1alpha1.ScaleTarget{
Name: deploymentName,
},
Advanced: &kedav1alpha1.AdvancedConfig{
HorizontalPodAutoscalerConfig: &kedav1alpha1.HorizontalPodAutoscalerConfig{},
HorizontalPodAutoscalerConfig: &kedav1alpha1.HorizontalPodAutoscalerConfig{
Annotations: map[string]string{
"annotation-email": "email@example.com",
"annotation-url": "https://example.com",
},
Labels: map[string]string{
"label-key": "label-value",
},
},
},
Triggers: []kedav1alpha1.ScaleTriggers{
{
Expand Down Expand Up @@ -1494,8 +1499,19 @@ var _ = Describe("ScaledObjectController", func() {
err = k8sClient.Get(context.Background(), types.NamespacedName{Name: soName, Namespace: "default"}, so)
Expect(err).ToNot(HaveOccurred())
annotations := make(map[string]string)
labels := make(map[string]string)
annotations["new-annotation"] = "new-annotation-value"
so.SetAnnotations(annotations)
labels["new-label"] = "new-label-value"
so := &kedav1alpha1.ScaledObject{
Spec: kedav1alpha1.ScaledObjectSpec{
Advanced: &kedav1alpha1.AdvancedConfig{
HorizontalPodAutoscalerConfig: &kedav1alpha1.HorizontalPodAutoscalerConfig{
Annotations: annotations,
Labels: labels,
},
},
},
}
return k8sClient.Update(context.Background(), so)
}).WithTimeout(1 * time.Minute).WithPolling(10 * time.Second).ShouldNot(HaveOccurred())
testLogger.Info("annotation is set")
Expand Down

0 comments on commit 87c70da

Please sign in to comment.