From b3ac0c68da2ad2b364e5b940ae0904853605941f Mon Sep 17 00:00:00 2001 From: Jeeva Kandasamy Date: Tue, 2 Apr 2024 15:53:10 +0530 Subject: [PATCH] HPA creation enabled via options Signed-off-by: Jeeva Kandasamy --- docs/TektonConfig.md | 3 +- .../test-additional-options-test-hpa.yaml | 58 +++++----- .../common/transformer_additional_options.go | 106 +++++++++--------- 3 files changed, 80 insertions(+), 87 deletions(-) diff --git a/docs/TektonConfig.md b/docs/TektonConfig.md index 7136e3d829..0911c0a37c 100644 --- a/docs/TektonConfig.md +++ b/docs/TektonConfig.md @@ -628,8 +628,7 @@ The following fields are supported in `StatefulSet` * `args` - appends given args with existing arguments. **NOTE: THIS OPERATION DO NOT REPLACE EXISTING ARGS** #### HorizontalPodAutoscalers -Supports to update the existing HorizontalPodAutoscaler(HPA) ~~also supports to create new HPA.~~ -There is open [issue](https://github.com/tektoncd/operator/issues/2002) on `options` transformer. Until resolve the issue create HPA feature will not be available. +Supports to update the existing HorizontalPodAutoscaler(HPA) also supports to create new HPA. The following fields are supported in `HorizontalPodAutoscaler` (aka HPA) * `metadata` diff --git a/pkg/reconciler/common/testdata/test-additional-options-test-hpa.yaml b/pkg/reconciler/common/testdata/test-additional-options-test-hpa.yaml index 648bf32990..5dabfe0784 100644 --- a/pkg/reconciler/common/testdata/test-additional-options-test-hpa.yaml +++ b/pkg/reconciler/common/testdata/test-additional-options-test-hpa.yaml @@ -110,33 +110,31 @@ status: currentMetrics: null desiredReplicas: 0 -# disabled HPA creation until we resolve the bug -# BUG: https://github.com/tektoncd/operator/issues/2002 -# --- -# apiVersion: autoscaling/v2 -# kind: HorizontalPodAutoscaler -# metadata: -# creationTimestamp: null -# annotations: -# foo: bar -# labels: -# foo: bar -# name: new-hpa -# namespace: tekton-pipelines -# spec: -# scaleTargetRef: -# apiVersion: apps/v1 -# kind: Deployment -# name: foo -# minReplicas: 2 -# maxReplicas: 5 -# metrics: -# - resource: -# name: cpu -# target: -# averageUtilization: 100 -# type: Utilization -# type: Resource -# status: -# currentMetrics: null -# desiredReplicas: 0 +--- +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + creationTimestamp: null + annotations: + foo: bar + labels: + foo: bar + name: new-hpa + namespace: tekton-pipelines +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: foo + minReplicas: 2 + maxReplicas: 5 + metrics: + - resource: + name: cpu + target: + averageUtilization: 100 + type: Utilization + type: Resource +status: + currentMetrics: null + desiredReplicas: 0 diff --git a/pkg/reconciler/common/transformer_additional_options.go b/pkg/reconciler/common/transformer_additional_options.go index a8a0d76c2a..ed54e7a82b 100644 --- a/pkg/reconciler/common/transformer_additional_options.go +++ b/pkg/reconciler/common/transformer_additional_options.go @@ -71,17 +71,15 @@ func ExecuteAdditionalOptionsTransformer(ctx context.Context, manifest *mf.Manif return err } - // disabled HPA creation until we resolve the bug - // BUG: https://github.com/tektoncd/operator/issues/2002 // create HorizontalPodAutoscaler, if not found in the existing manifest - // extraHPAs, err := ot.createHorizontalPodAutoscalers(manifest, targetNamespace, additionalOptions) - // if err != nil { - // return err - // } - // // update into the manifests - // if err = ot.addInToManifest(manifest, extraHPAs); err != nil { - // return err - // } + extraHPAs, err := ot.createHorizontalPodAutoscalers(manifest, targetNamespace, additionalOptions) + if err != nil { + return err + } + // update into the manifests + if err = ot.addInToManifest(manifest, extraHPAs); err != nil { + return err + } return nil } @@ -630,48 +628,46 @@ func (ot *OptionsTransformer) updateHorizontalPodAutoscalers(u *unstructured.Uns return nil } -// disabled HPA creation until we resolve the bug -// BUG: https://github.com/tektoncd/operator/issues/2002 -// func (ot *OptionsTransformer) createHorizontalPodAutoscalers(manifest *mf.Manifest, targetNamespace string, additionalOptions v1alpha1.AdditionalOptions) ([]unstructured.Unstructured, error) { -// newHPAs := []unstructured.Unstructured{} -// existingHPAs := manifest.Filter(mf.Any(mf.ByKind(KindHorizontalPodAutoscaler))) -// for hpaName, newHPA := range additionalOptions.HorizontalPodAutoscalers { -// found := false -// for _, resource := range existingHPAs.Resources() { -// if resource.GetName() == hpaName { -// found = true -// break -// } -// } -// if found { -// continue -// } -// -// // update name -// newHPA.SetName(hpaName) -// -// // update the namespace to targetNamespace -// newHPA.SetNamespace(targetNamespace) -// -// // update kind -// if newHPA.TypeMeta.Kind == "" { -// newHPA.TypeMeta.Kind = KindHorizontalPodAutoscaler -// } -// -// // update api version -// if newHPA.TypeMeta.APIVersion == "" { -// newHPA.TypeMeta.APIVersion = autoscalingv2.SchemeGroupVersion.String() -// } -// -// // convert hpa to unstructured object -// obj, err := apimachineryRuntime.DefaultUnstructuredConverter.ToUnstructured(&newHPA) -// if err != nil { -// return nil, err -// } -// u := unstructured.Unstructured{} -// u.SetUnstructuredContent(obj) -// newHPAs = append(newHPAs, u) -// } -// -// return newHPAs, nil -// } +func (ot *OptionsTransformer) createHorizontalPodAutoscalers(manifest *mf.Manifest, targetNamespace string, additionalOptions v1alpha1.AdditionalOptions) ([]unstructured.Unstructured, error) { + newHPAs := []unstructured.Unstructured{} + existingHPAs := manifest.Filter(mf.Any(mf.ByKind(KindHorizontalPodAutoscaler))) + for hpaName, newHPA := range additionalOptions.HorizontalPodAutoscalers { + found := false + for _, resource := range existingHPAs.Resources() { + if resource.GetName() == hpaName { + found = true + break + } + } + if found { + continue + } + + // update name + newHPA.SetName(hpaName) + + // update the namespace to targetNamespace + newHPA.SetNamespace(targetNamespace) + + // update kind + if newHPA.TypeMeta.Kind == "" { + newHPA.TypeMeta.Kind = KindHorizontalPodAutoscaler + } + + // update api version + if newHPA.TypeMeta.APIVersion == "" { + newHPA.TypeMeta.APIVersion = autoscalingv2.SchemeGroupVersion.String() + } + + // convert hpa to unstructured object + obj, err := apimachineryRuntime.DefaultUnstructuredConverter.ToUnstructured(&newHPA) + if err != nil { + return nil, err + } + u := unstructured.Unstructured{} + u.SetUnstructuredContent(obj) + newHPAs = append(newHPAs, u) + } + + return newHPAs, nil +}