azure - I have a helm template which creates cronjob in AKS for scaling applications based on scheduler - Stack Overflow

admin2025-04-19  4

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

Share Improve this question edited Feb 5 at 7:09 Suresh Chikkam 3,6162 gold badges4 silver badges12 bronze badges Recognized by Microsoft Azure Collective asked Jan 28 at 16:30 sunil ssunil s 213 bronze badges 2
  • What is the value of $name...the only thing that could be is that $name has always the same value and the Cronjob gets overwritten o tried to recreate the same Cronjob 3 times – Hackerman Commented Jan 30 at 14:04
  • no, the $name loops through 3 components. – sunil s Commented Feb 3 at 9:32
Add a comment  | 

1 Answer 1

Reset to default 0

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.

  • For the CronJobs to be created for each component, you need to check that the enabled key is set to true for all components you want to scale. Like for example, in values.yaml
autoscale:
  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 * * *"
  • The loop should iterate through all the components and generate a CronJob for each enabled component. If only one CronJob is being created, it could mean that only the last valid component (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.

转载请注明原文地址:http://anycun.com/QandA/1745009439a90341.html