-
Notifications
You must be signed in to change notification settings - Fork 158
Add HorizontalPodAutoScaler Processor #171
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
Open
DheerajKN
wants to merge
8
commits into
arttor:main
Choose a base branch
from
DheerajKN:add_hpa
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
896861e
Add HorizontalPodAutoScaler Processor
DheerajKN f05ecfa
Go Formatting fixes
DheerajKN cd0f1c1
Re ran the examples to correctly generate them
DheerajKN f389b4a
Review Comments
DheerajKN 16dae23
Recreate examples
DheerajKN e180dcb
Modify pdb name
DheerajKN 5af57dc
Fixes
DheerajKN c56db40
Rerun the script
DheerajKN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| apiVersion: autoscaling/v1 | ||
| kind: HorizontalPodAutoscaler | ||
| metadata: | ||
| name: {{ include "app.fullname" . }}-hpa | ||
| labels: | ||
| {{- include "app.labels" . | nindent 4 }} | ||
| spec: | ||
| scaleTargetRef: | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| name: myapp | ||
| minReplicas: {{ .Values.hpa.minReplicas }} | ||
| maxReplicas: {{ .Values.hpa.maxReplicas }} | ||
| targetCPUUtilizationPercentage: {{ .Values.hpa.targetCPUUtilizationPercentage }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| apiVersion: autoscaling/v1 | ||
| kind: HorizontalPodAutoscaler | ||
| metadata: | ||
| name: {{ include "operator.fullname" . }}-app-hpa | ||
| labels: | ||
| {{- include "operator.labels" . | nindent 4 }} | ||
| spec: | ||
| scaleTargetRef: | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| name: myapp | ||
| minReplicas: {{ .Values.appHpa.minReplicas }} | ||
| maxReplicas: {{ .Values.appHpa.maxReplicas }} | ||
| targetCPUUtilizationPercentage: {{ .Values.appHpa.targetCPUUtilizationPercentage }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package horizontalpodautoscaler | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "io" | ||
|
|
||
| "github.com/arttor/helmify/pkg/processor" | ||
|
|
||
| "github.com/arttor/helmify/pkg/helmify" | ||
| yamlformat "github.com/arttor/helmify/pkg/yaml" | ||
| "github.com/iancoleman/strcase" | ||
| autoscalingv1 "k8s.io/api/autoscaling/v1" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "sigs.k8s.io/yaml" | ||
| ) | ||
|
|
||
| const ( | ||
| hpaTempSpec = ` | ||
| spec: | ||
| scaleTargetRef: | ||
| %[1]s | ||
| minReplicas: {{ .Values.%[2]s.minReplicas }} | ||
| maxReplicas: {{ .Values.%[2]s.maxReplicas }} | ||
| targetCPUUtilizationPercentage: {{ .Values.%[2]s.targetCPUUtilizationPercentage }}` | ||
| ) | ||
|
|
||
| var hpaGVC = schema.GroupVersionKind{ | ||
| Group: "autoscaling", | ||
| Version: "v1", | ||
| Kind: "HorizontalPodAutoscaler", | ||
| } | ||
|
|
||
| // New creates processor for k8s Service resource. | ||
| func New() helmify.Processor { | ||
| return &hpa{} | ||
| } | ||
|
|
||
| type hpa struct{} | ||
|
|
||
| // Process k8s Service object into template. Returns false if not capable of processing given resource type. | ||
| func (r hpa) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured) (bool, helmify.Template, error) { | ||
| if obj.GroupVersionKind() != hpaGVC { | ||
| return false, nil, nil | ||
| } | ||
| hpa := autoscalingv1.HorizontalPodAutoscaler{} | ||
| err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &hpa) | ||
| if err != nil { | ||
| return true, nil, fmt.Errorf("%w: unable to cast to hpa", err) | ||
| } | ||
| spec := hpa.Spec | ||
| values := helmify.Values{} | ||
|
|
||
| meta, err := processor.ProcessObjMeta(appMeta, obj) | ||
| if err != nil { | ||
| return true, nil, err | ||
| } | ||
|
|
||
| name := appMeta.TrimName(obj.GetName()) | ||
| nameCamel := strcase.ToLowerCamel(name) | ||
|
|
||
| scaleTargetRef, err := yaml.Marshal(hpa.Spec.ScaleTargetRef) | ||
| if err != nil { | ||
| return true, nil, fmt.Errorf("error marshaling scaleTargetRef: %w", err) | ||
| } | ||
| scaleTargetRef = yamlformat.Indent(scaleTargetRef, 4) | ||
| scaleTargetRef = bytes.TrimRight(scaleTargetRef, "\n ") | ||
|
|
||
| if spec.MinReplicas != nil && *spec.MinReplicas != 0 { | ||
| _, err := values.Add(*spec.MinReplicas, nameCamel, "minReplicas") | ||
| if err != nil { | ||
| return true, nil, err | ||
| } | ||
| } | ||
|
|
||
| if spec.MaxReplicas != 0 { | ||
| _, err := values.Add(spec.MaxReplicas, nameCamel, "maxReplicas") | ||
| if err != nil { | ||
| return true, nil, err | ||
| } | ||
| } | ||
|
|
||
| if spec.TargetCPUUtilizationPercentage != nil && *spec.TargetCPUUtilizationPercentage != 0 { | ||
| _, err := values.Add(*spec.TargetCPUUtilizationPercentage, nameCamel, "targetCPUUtilizationPercentage") | ||
| if err != nil { | ||
| return true, nil, err | ||
| } | ||
| } | ||
|
|
||
| res := meta + fmt.Sprintf(hpaTempSpec, scaleTargetRef, nameCamel) | ||
| return true, &result{ | ||
| name: name, | ||
| data: res, | ||
| values: values, | ||
| }, nil | ||
| } | ||
|
|
||
| type result struct { | ||
| name string | ||
| data string | ||
| values helmify.Values | ||
| } | ||
|
|
||
| func (r *result) Filename() string { | ||
| return r.name + ".yaml" | ||
| } | ||
|
|
||
| func (r *result) Values() helmify.Values { | ||
| return r.values | ||
| } | ||
|
|
||
| func (r *result) Write(writer io.Writer) error { | ||
| _, err := writer.Write([]byte(r.data)) | ||
| return err | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package horizontalpodautoscaler | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/arttor/helmify/pkg/metadata" | ||
|
|
||
| "github.com/arttor/helmify/internal" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| const hpaYaml = `apiVersion: autoscaling/v1 | ||
| kind: HorizontalPodAutoscaler | ||
| metadata: | ||
| name: myapp-hpa | ||
| spec: | ||
| scaleTargetRef: | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| name: myapp | ||
| minReplicas: 2 | ||
| maxReplicas: 10 | ||
| targetCPUUtilizationPercentage: 80` | ||
|
|
||
| func Test_hpa_Process(t *testing.T) { | ||
| var testInstance hpa | ||
|
|
||
| t.Run("processed", func(t *testing.T) { | ||
| obj := internal.GenerateObj(hpaYaml) | ||
| processed, tt, err := testInstance.Process(&metadata.Service{}, obj) | ||
| _ = tt.Write(os.Stdout) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, true, processed) | ||
| }) | ||
| t.Run("skipped", func(t *testing.T) { | ||
| obj := internal.TestNs | ||
| processed, _, err := testInstance.Process(&metadata.Service{}, obj) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, false, processed) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.