spring-cloud / spring-cloud-deployer-kubernetes

The Spring Cloud Deployer implementation for Kubernetes
Apache License 2.0
157 stars 98 forks source link

JobAnnotations are not being applied to Schedules #395

Closed yoep closed 4 years ago

yoep commented 4 years ago

We're using Service Mesh within our cluster and need to apply some annotations on scheduled Pods by the CronJob within Kubernetes. When configuring jobAnnotations under spring.cloud.scheduler.kubernetes, they're not being applied to the created Schedule by the KubernetesScheduler. When configuring jobAnnotations under spring.cloud.deployer.kubernetes, they're being correctly applied to the Task by KubernetesTaskLauncher.

Example configuration:

spring:
  cloud:
    scheduler:
      kubernetes:
        jobAnnotations: "sidecar.istio.io/inject:true"

Expected result:

kind: CronJob
apiVersion: batch/v1beta1
metadata:
  name: example-schedule
  labels:
    spring-cronjob-id: example-schedule
spec:
  schedule: '0,30 6-19 * * 1-5'
  concurrencyPolicy: Allow
  suspend: false
  jobTemplate:
    metadata:
      creationTimestamp: null
    spec:
      template:
        metadata:
          creationTimestamp: null
          annotations:
            sidecar.istio.io/inject: 'true'
        spec:
          containers:
            - name: example
              image: busybox
              env:
                - name: SPRING_CLOUD_APPLICATION_GUID
                  valueFrom:
                    fieldRef:
                      apiVersion: v1
                      fieldPath: metadata.uid
              terminationMessagePath: /dev/termination-log
              terminationMessagePolicy: File

Actual result:

kind: CronJob
apiVersion: batch/v1beta1
metadata:
  name: example-schedule
  labels:
    spring-cronjob-id: example-schedule
spec:
  schedule: '0,30 6-19 * * 1-5'
  concurrencyPolicy: Allow
  suspend: false
  jobTemplate:
    metadata:
      creationTimestamp: null
    spec:
      template:
        metadata:
          creationTimestamp: null
        spec:
          containers:
            - name: example
              image: busybox
              env:
                - name: SPRING_CLOUD_APPLICATION_GUID
                  valueFrom:
                    fieldRef:
                      apiVersion: v1
                      fieldPath: metadata.uid
              terminationMessagePath: /dev/termination-log
              terminationMessagePolicy: File
chrisjs commented 4 years ago

already covered under https://github.com/spring-cloud/spring-cloud-deployer-kubernetes/issues/389

ilayaperumalg commented 4 years ago

@yoep Now that the changes are merged you should be able to see the job specific annotations set.

Also, remember that you don't have to set spring.cloud.scheduler properties explicitly as the task platform account properties are expected to be applied into both deployer and scheduler during the SCDF server startup.

Thanks!

yoep commented 3 years ago

Hi @ilayaperumalg

It seems like it's still not working correctly for schedules when using global scheduler config with jobAnnotations. When applying the following scheduler global config, it doesn't get applied to the job spec within the scheduled CronJob:

The following configuration is applied to the configuration of the dataflow server (not individual tasks):

spring:
  cloud:
    dataflow:
      task:
        platform:
          kubernetes:
            accounts:
              default:
                jobAnnotations: sidecar.istio.io/inject:true,sidecar.istio.io/rewriteAppHTTPProbers:true
    kubernetes:
      enabled: true
    deployer:
      kubernetes:
        podAnnotations: sidecar.istio.io/inject:true,sidecar.istio.io/rewriteAppHTTPProbers:true
        jobAnnotations: sidecar.istio.io/inject:true,sidecar.istio.io/rewriteAppHTTPProbers:true
    scheduler:
      kubernetes:
        jobAnnotations: sidecar.istio.io/inject:true,sidecar.istio.io/rewriteAppHTTPProbers:true

It results in the following CronJob resource:

kind: CronJob
apiVersion: batch/v1beta1
metadata:
  name: example-schedule
  annotations:
    sidecar.istio.io/inject: 'true'
    sidecar.istio.io/rewriteAppHTTPProbers: 'true'
spec:
  schedule: 15 0/3 * * 1-5
  concurrencyPolicy: Allow
  suspend: false
  jobTemplate:
    metadata:
      creationTimestamp: null
    spec:
      template:
        metadata:
          creationTimestamp: null
        spec:
          restartPolicy: Never
          containers:
            - name: example
              ...

As you can see above, the annotations get applied to the metadata.annotations of the CronJob, but not to the spec.jobTemplate.metadata.annotations of the jobTemplate (which is what I expect to actually happen).

We're using spring-cloud-deployer-kubernetes version 2.5.0.

Kind regards, yoep