I have a helm template which creates cronjob in AKS for scaling applications based on scheduler. when running helm template, i can see three component cronjob listed.The code is moved to Git and deployed using Argo.
When i do argo sync , i see only the last component is active in cluster. rest are missing.
Cronjob template
{{- range $name, $config := .Values.autoscaleponents }}
{{- if $config.enabled }}
apiVersion: batch/v1
kind: CronJob
metadata:
name: scale-up-{{ $name | lower }}
namespace: {{ $.Values.autoscale.namespace }}
spec:
schedule: "{{ $config.scheduler_scale_up }}" # Run at midnight every day
jobTemplate:
spec:
template:
spec:
containers:
- name: scale-up-{{ $name | lower }}
image: bitnami/kubectl:latest # Lightweight image with kubectl
command:
- /bin/sh
- -c
- |
kubectl scale deployment {{ $name | lower }} --replicas={{ $config.maxReplicas }} -n {{ $.Values.autoscale.namespace }}
restartPolicy: OnFailure
serviceAccountName: scale-service-account
{{- end }}
{{- end }}
{{- end }}
autoscale:
enabled: false # Global toggle for autoscaling
namespace: ##set this accordingly to enable scaling feature for xx deployment
components:
A:
enabled: true # Enable autoscaling for A
minReplicas: 0
maxReplicas: 1
scheduler_scale_down: # 40 12 * * *
scheduler_scale_up:
B:
enabled: false # Enable autoscaling for B
minReplicas: 0
maxReplicas: 1
scheduler_scale_down: # 40 12 * * *
scheduler_scale_up:
C:
enabled: false # Enable autoscaling for C
minReplicas: 0
maxReplicas: 1
scheduler_scale_down: # 40 12 * * *
scheduler_scale_up:
i was expecting 3 cronjobs for 3 components passed in values.yaml, only 1 created
I have a helm template which creates cronjob in AKS for scaling applications based on scheduler. when running helm template, i can see three component cronjob listed.The code is moved to Git and deployed using Argo.
When i do argo sync , i see only the last component is active in cluster. rest are missing.
Cronjob template
{{- range $name, $config := .Values.autoscale.components }}
{{- if $config.enabled }}
apiVersion: batch/v1
kind: CronJob
metadata:
name: scale-up-{{ $name | lower }}
namespace: {{ $.Values.autoscale.namespace }}
spec:
schedule: "{{ $config.scheduler_scale_up }}" # Run at midnight every day
jobTemplate:
spec:
template:
spec:
containers:
- name: scale-up-{{ $name | lower }}
image: bitnami/kubectl:latest # Lightweight image with kubectl
command:
- /bin/sh
- -c
- |
kubectl scale deployment {{ $name | lower }} --replicas={{ $config.maxReplicas }} -n {{ $.Values.autoscale.namespace }}
restartPolicy: OnFailure
serviceAccountName: scale-service-account
{{- end }}
{{- end }}
{{- end }}
autoscale:
enabled: false # Global toggle for autoscaling
namespace: ##set this accordingly to enable scaling feature for xx deployment
components:
A:
enabled: true # Enable autoscaling for A
minReplicas: 0
maxReplicas: 1
scheduler_scale_down: # 40 12 * * *
scheduler_scale_up:
B:
enabled: false # Enable autoscaling for B
minReplicas: 0
maxReplicas: 1
scheduler_scale_down: # 40 12 * * *
scheduler_scale_up:
C:
enabled: false # Enable autoscaling for C
minReplicas: 0
maxReplicas: 1
scheduler_scale_down: # 40 12 * * *
scheduler_scale_up:
i was expecting 3 cronjobs for 3 components passed in values.yaml, only 1 created
In the above code provided, $name iterates over the components (A, B, C) in values.yaml, and you're expecting three separate CronJobs to be created.
enabled key is set to true for all components you want to scale. Like for example, in values.yamlautoscale:
components:
A:
enabled: true # Enable autoscaling for A
scheduler_scale_up: "0 0 * * *"
scheduler_scale_down: "0 12 * * *"
B:
enabled: true # Enable autoscaling for B
scheduler_scale_up: "0 2 * * *"
scheduler_scale_down: "0 14 * * *"
C:
enabled: true # Enable autoscaling for C
scheduler_scale_up: "0 4 * * *"
scheduler_scale_down: "0 16 * * *"
A, B, or C) is being processed, or the other components are not properly configured.{{- range $name, $config := .Values.autoscale.components }}
{{- if $config.enabled }}
# Debugging output
# This will show which component is being processed
{{- printf "Processing component: %s" $name | indent 4 }}
apiVersion: batch/v1
kind: CronJob
metadata:
name: scale-up-{{ $name | lower }}
namespace: {{ $.Values.autoscale.namespace }}
spec:
schedule: "{{ $config.scheduler_scale_up }}"
jobTemplate:
spec:
template:
spec:
containers:
- name: scale-up-{{ $name | lower }}
image: bitnami/kubectl:latest
command:
- /bin/sh
- -c
- |
kubectl scale deployment {{ $name | lower }} --replicas={{ $config.maxReplicas }} -n {{ $.Values.autoscale.namespace }}
restartPolicy: OnFailure
serviceAccountName: scale-service-account
{{- end }}
{{- end }}
Added --- at the end of each CronJob to properly separate multiple resources.
