Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[main] HPA creation enabled via options #2092

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/TektonConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
106 changes: 51 additions & 55 deletions pkg/reconciler/common/transformer_additional_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}