Commit edca6284 authored by Shinya Maeda's avatar Shinya Maeda
Browse files

Merge branch 'feature/cronjobs' into 'master'

feat(cronjob): add cronjob support

See merge request gitlab-org/cluster-integration/auto-deploy-image!294
parents 0e05052f bf05714e
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@
| application.track             |             | `stable`                           |
| application.tier              |             | `web`                              |
| application.migrateCommand    | If present, this variable will run as a shell command within an application Container as a Helm pre-upgrade Hook. Intended to run migration commands. | `nil` |
| application.initializeCommand | If present, this variable will run as shell command within an application Container as a Helm post-install Hook. Intended to run database initialization commands. When set, the Deployment resource will be skipped.| `nil` |
| application.initializeCommand | If present, this variable will run as shell command within an application Container as a Helm post-install Hook. Intended to run database initialization commands. When set, the Deployment and Cronjob resources will be skipped.| `nil` |
| application.secretName        | Pass in the name of a Secret which the deployment will [load all key-value pairs from the Secret as environment variables](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#configure-all-key-value-pairs-in-a-configmap-as-container-environment-variables) in the application container. | `nil` |
| application.secretChecksum    | Pass in the checksum of the secrets referenced by `application.secretName`. | `nil` |
| application.database_url      | If present, sets the `DATABASE_URL` environment variable. If postgres is enabled this will be autogenerated. | `nil` |
@@ -123,3 +123,12 @@
| persistence.volumes[].claim.accessMode   | The access mode of the PVC. | `ReadWriteOnce` |
| persistence.volumes[].claim.size         | The storage size of the PVC. | `8Gi` |
| persistence.volumes[].claim.storageClass | (Optional) The storage class of the PVC. If not specified, it falls back to the default storage class from the provider. | `nil` |
| cronjobs                            | Define your jobs in this section, an example of the definition can be found in values.yaml | `nil` |
| cronjob.job.failedJobsHistoryLimit          | This field specify how many failed jobs are kept | `1` |
| cronjob.job.startingDeadlineSeconds         | If a CronJob controller cannot start a job run on its schedule, it will keep retrying until the value (In seconds) is reached. | `300` |
| cronjob.job.successfulJobsHistoryLimit      | This field specify how many completed jobs are kept | `1` |
| cronjob.job.concurrencyPolicy               | If `cronjob.concurrencyPolicy` is set to Forbid and a CronJob was attempted to be scheduled when there was a previous schedule still running, then it would count as missed. | `Forbid` |
| cronjob.job.restartPolicy                   | Possible values: `Always`, `OnFailure` and `Never` | `OnFailure` |
| cronjob.job.livenessProbe           | If defined, enables livenessProbe in the cronjob. If not defined, it uses top-level `livenessProbe` setting to the job. (To see details about the default probes check values.yaml) | |
| cronjob.job.readinessProbe           | If defined, enables readinessProbe in the cronjob. If not defined, it uses top-level `readinessProbe` setting to the job. (To see details about the default probes check values.yaml) | |
| cronjob.activeDeadlineSeconds           | Alternative to terminate a Job: Once a Job reaches `activeDeadlineSeconds` value, all of its running Pods are terminated and the Job status will become `type: Failed` with `reason: DeadlineExceeded` | `nil` |
+14 −0
Original line number Diff line number Diff line
@@ -33,6 +33,20 @@ We truncate at 63 chars because some Kubernetes name fields are limited to this
{{- $trackableName | trimSuffix "-stable" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Templates for cronjob
*/}}

{{- define "cronjobimagename" -}}
{{- if hasKey .job "image" -}}
{{-   if and (hasKey .job.image "repository") (hasKey .job.image "tag") -}}
{{- printf "%s:%s" .job.image.repository .job.image.tag -}}
{{-   end -}}
{{- else -}}
{{- printf "%s:%s" .glob.image.repository .glob.image.tag -}}
{{- end -}}
{{- end -}}

{{/*
Get a hostname from URL
*/}}
+165 −0
Original line number Diff line number Diff line
{{- if and (not .Values.application.initializeCommand) .Values.cronjobs -}}
apiVersion: v1
kind: List
items:
{{- range $jobName, $jobConfig:= .Values.cronjobs }}
{{- if $.Capabilities.APIVersions.Has "batch/v1" }}
- apiVersion: "batch/v1"
{{- else }}
- apiVersion: "batch/v1beta1"
{{- end }}
  kind: CronJob
  metadata:
    name: "{{ template "trackableappname" $ }}-{{ $jobName}}"
    annotations:
      {{ if $.Values.gitlab.app }}app.gitlab.com/app: {{ $.Values.gitlab.app | quote }}{{ end }}
      {{ if $.Values.gitlab.env }}app.gitlab.com/env: {{ $.Values.gitlab.env | quote }}{{ end }}
    labels:
      track: "{{ $.Values.application.track }}"
      tier: "{{ $.Values.application.tier }}"
      {{ include "sharedlabels" $ | nindent 6 }}
  spec:
    concurrencyPolicy: {{ default "Forbid" $jobConfig.concurrencyPolicy }}
    failedJobsHistoryLimit: {{ default 1 $jobConfig.failedJobsHistoryLimit }}
    startingDeadlineSeconds: {{ default 300 $jobConfig.startingDeadlineSeconds }}
    schedule: {{ $jobConfig.schedule | quote }}
    successfulJobsHistoryLimit: {{ default 1 $jobConfig.successfulJobsHistoryLimit }}
    jobTemplate:
      spec:
        {{- if $jobConfig.activeDeadlineSeconds }}
        activeDeadlineSeconds: {{ $jobConfig.activeDeadlineSeconds }}
        {{- end }}
        template:
          metadata:
            annotations:
              checksum/application-secrets: "{{ $.Values.application.secretChecksum }}"
              {{ if $.Values.gitlab.app }}app.gitlab.com/app: {{ $.Values.gitlab.app | quote }}{{ end }}
              {{ if $.Values.gitlab.env }}app.gitlab.com/env: {{ $.Values.gitlab.env | quote }}{{ end }}
              {{- if $.Values.podAnnotations }}
              {{ toYaml $.Values.podAnnotations | nindent 12 }}
              {{- end }}
            labels:
              app: {{ template "appname" $ }}
              release: {{ $.Release.Name }}
              track: "{{ $.Values.application.track }}"
              tier: "{{ $.Values.application.tier }}"
          spec:
            imagePullSecrets:
              {{ toYaml $.Values.image.secrets | nindent 14 }}
            restartPolicy: {{ default "OnFailure" $jobConfig.restartPolicy }}
            {{- with $nodeSelectorConfig := default $.Values.nodeSelector $jobConfig.nodeSelector -}}
            {{- if $nodeSelectorConfig  }}
            nodeSelector:
            {{ toYaml $nodeSelectorConfig | nindent 14 }}
            {{- end }}
            {{- end }}
            {{- with $tolerationsConfig := default $.Values.tolerations $jobConfig.tolerations -}}
            {{- if $tolerationsConfig }}
            tolerations:
            {{ toYaml $tolerationsConfig | nindent 14 }}
            {{- end }}
            {{- end }}
            {{- with $affinityConfig := default $.Values.affinity $jobConfig.affinity -}}
            {{- if $affinityConfig  }}
            affinity:
            {{ toYaml $affinityConfig | nindent 14 }}
            {{- end }}
            {{- end }}
            containers:
            - name: {{ $.Chart.Name }}
              image: "{{ template "cronjobimagename" (dict "job" . "glob" $.Values) }}"
              imagePullPolicy: {{ $.Values.image.pullPolicy }}
              {{- if $jobConfig.command }}
              command: 
              {{- range $jobConfig.command }}
              - {{ . }}
              {{- end }}
              {{- end }}
              {{- if $jobConfig.command }}
              args: 
              {{- range $jobConfig.args }}
              - {{ . }}
              {{- end }}
              {{- end }}
              {{- if $.Values.application.secretName }}
              envFrom:
              - secretRef:
                  name: {{ $.Values.application.secretName }}
              {{- end }}
              env:
              {{- if $.Values.postgresql.managed }}
              - name: POSTGRES_USER
                valueFrom:
                  secretKeyRef:
                    name: app-postgres
                    key: username
              - name: POSTGRES_PASSWORD
                valueFrom:
                  secretKeyRef:
                    name: app-postgres
                    key: password
              - name: POSTGRES_HOST
                valueFrom:
                  secretKeyRef:
                    name: app-postgres
                    key: privateIP
              {{- end }}
              {{- if $.Values.application.database_url }}
              - name: DATABASE_URL
                value: {{ $.Values.application.database_url | quote }}
              {{- end }}
              - name: GITLAB_ENVIRONMENT_NAME
                value: {{ $.Values.gitlab.envName | quote }}
              - name: GITLAB_ENVIRONMENT_URL
                value: {{ $.Values.gitlab.envURL | quote }}
              ports:
              - name: "{{ $.Values.service.name }}"
                containerPort: {{ $.Values.service.internalPort }}
              {{- with $livenessProbeConfig := default $.Values.livenessProbe $jobConfig.livenessProbe }}
              {{- if $livenessProbeConfig }}
              livenessProbe:
              {{- if eq $livenessProbeConfig.probeType "httpGet" }}
                httpGet:
                  path: {{ $livenessProbeConfig.path }}
                  scheme: {{ $livenessProbeConfig.scheme }}
                  port: {{ default $.Values.service.internalPort $livenessProbeConfig.port }}
              {{- else if eq $livenessProbeConfig.probeType "tcpSocket" }}
                tcpSocket:
                  port: {{ default $.Values.service.internalPort $.Values.service.internalPort }}
              {{- else if eq $livenessProbeConfig.probeType "exec" }}
                exec:
                  command:
                    {{ toYaml $livenessProbeConfig.command | nindent 18 }}
              {{- end }}
                initialDelaySeconds: {{ $livenessProbeConfig.initialDelaySeconds }}
                timeoutSeconds: {{  $livenessProbeConfig.timeoutSeconds }}
                failureThreshold: {{ $livenessProbeConfig.failureThreshold }}
                periodSeconds: {{ $livenessProbeConfig.periodSeconds }}
              {{- end }}
              {{- end }}
              {{- with $readinessProbe := default $.Values.readinessProbe  $jobConfig.readinessProbe }}
              {{- if $readinessProbe  }}
              readinessProbe:
                {{- if eq $readinessProbe.probeType "httpGet" }}
                httpGet:
                  path: {{ $readinessProbe.path }}
                  scheme: {{ $readinessProbe.scheme }}
                  port: {{ default $.Values.service.internalPort $readinessProbe.port }}
                {{- else if eq $readinessProbe.probeType "tcpSocket" }}
                tcpSocket:
                  port: {{ default $.Values.service.internalPort $readinessProbe.port }}
                {{- else if eq $readinessProbe.probeType "exec" }}
                exec:
                  command:
                    {{ toYaml $readinessProbe.command | nindent 18 }}
                {{- end }}
                initialDelaySeconds: {{ $readinessProbe.initialDelaySeconds }}
                timeoutSeconds: {{ $readinessProbe.timeoutSeconds }}
                failureThreshold: {{ $readinessProbe.failureThreshold }}
                periodSeconds: {{ $readinessProbe.periodSeconds }}
              {{- end }}
              {{- end }}
              resources:
                {{ toYaml $.Values.resources | nindent 16 }}
{{- end -}}
{{- end -}}
+615 −0

File added.

Preview size limit exceeded, changes collapsed.

+54 −0
Original line number Diff line number Diff line
@@ -77,6 +77,12 @@ type workerDeploymentTestCase struct {
	ExpectedResources      coreV1.ResourceRequirements
}

type cronjobTestCase struct {
	ExpectedName     string
	ExpectedCmd      []string
	ExpectedSchedule string
}

type workerDeploymentSelectorTestCase struct {
	ExpectedName     string
	ExpectedSelector *metav1.LabelSelector
@@ -159,3 +165,51 @@ func workerReadinessProbe() *coreV1.Probe {
		TimeoutSeconds:      0,
	}
}

func execReadinessProbe() *coreV1.Probe {
	return &coreV1.Probe{
		Handler: coreV1.Handler{
			Exec: &coreV1.ExecAction{
				Command: []string{"echo", "hello"},
			},
		},
		InitialDelaySeconds: 0,
		TimeoutSeconds:      0,
	}
}

func execLivenessProbe() *coreV1.Probe {
	return &coreV1.Probe{
		Handler: coreV1.Handler{
			Exec: &coreV1.ExecAction{
				Command: []string{"echo", "hello"},
			},
		},
		InitialDelaySeconds: 0,
		TimeoutSeconds:      0,
	}
}

func tcpLivenessProbe() *coreV1.Probe {
	return &coreV1.Probe{
		Handler: coreV1.Handler{
			TCPSocket: &coreV1.TCPSocketAction{
				Port: intstr.IntOrString{IntVal: 5000},
			},
		},
		InitialDelaySeconds: 0,
		TimeoutSeconds:      0,
	}
}

func tcpReadinessProbe() *coreV1.Probe {
	return &coreV1.Probe{
		Handler: coreV1.Handler{
			TCPSocket: &coreV1.TCPSocketAction{
				Port: intstr.IntOrString{IntVal: 5000},
			},
		},
		InitialDelaySeconds: 0,
		TimeoutSeconds:      0,
	}
}
Loading