kubernetes: Can't update Jobs, field is immutable

What happened: When I updating Job objects I see the next error:

The Job "test-job" is invalid: spec.template: Invalid value: core.PodTemplateSpec{ObjectMeta:v1.ObjectMeta{Name:"", GenerateName:"", Namespace:"", SelfLink:"", UID:"", ResourceVersion:"", Generation:0, CreationTimestamp:v1.Time{Time:time.Time{wall:0x0, ext:0, loc:(*time.Location)(nil)}}, DeletionTimestamp:(*v1.Time)(nil), DeletionGracePeriodSeconds:(*int64)(nil), Labels:map[string]string{"controller-uid":"e50402b2-2476-4ceb-a82f-749c044a6182", "job-name":"test-job"}, Annotations:map[string]string(nil), OwnerReferences:[]v1.OwnerReference(nil), Finalizers:[]string(nil), ClusterName:"", ManagedFields:[]v1.ManagedFieldsEntry(nil)}, Spec:core.PodSpec{Volumes:[]core.Volume(nil), InitContainers:[]core.Container(nil), Containers:[]core.Container{core.Container{Name:"test", Image:"alpine:3.11", Command:[]string{"sleep", "2"}, Args:[]string(nil), WorkingDir:"", Ports:[]core.ContainerPort(nil), EnvFrom:[]core.EnvFromSource(nil), Env:[]core.EnvVar(nil), Resources:core.ResourceRequirements{Limits:core.ResourceList(nil), Requests:core.ResourceList(nil)}, VolumeMounts:[]core.VolumeMount(nil), VolumeDevices:[]core.VolumeDevice(nil), LivenessProbe:(*core.Probe)(nil), ReadinessProbe:(*core.Probe)(nil), StartupProbe:(*core.Probe)(nil), Lifecycle:(*core.Lifecycle)(nil), TerminationMessagePath:"/dev/termination-log", TerminationMessagePolicy:"File", ImagePullPolicy:"Always", SecurityContext:(*core.SecurityContext)(nil), Stdin:false, StdinOnce:false, TTY:false}}, EphemeralContainers:[]core.EphemeralContainer(nil), RestartPolicy:"OnFailure", TerminationGracePeriodSeconds:(*int64)(0xc00d249bb8), ActiveDeadlineSeconds:(*int64)(nil), DNSPolicy:"ClusterFirst", NodeSelector:map[string]string(nil), ServiceAccountName:"", AutomountServiceAccountToken:(*bool)(nil), NodeName:"", SecurityContext:(*core.PodSecurityContext)(0xc009f10300), ImagePullSecrets:[]core.LocalObjectReference(nil), Hostname:"", Subdomain:"", Affinity:(*core.Affinity)(nil), SchedulerName:"default-scheduler", Tolerations:[]core.Toleration(nil), HostAliases:[]core.HostAlias(nil), PriorityClassName:"", Priority:(*int32)(nil), PreemptionPolicy:(*core.PreemptionPolicy)(nil), DNSConfig:(*core.PodDNSConfig)(nil), ReadinessGates:[]core.PodReadinessGate(nil), RuntimeClassName:(*string)(nil), Overhead:core.ResourceList(nil), EnableServiceLinks:(*bool)(nil), TopologySpreadConstraints:[]core.TopologySpreadConstraint(nil)}}: field is immutable

What you expected to happen: Job object is updated

How to reproduce it (as minimally and precisely as possible):

kubectl apply -f - <<EOT
apiVersion: batch/v1
kind: Job
metadata:
  name: "test-job"
spec:
  template:
    spec:
      restartPolicy: OnFailure
      containers:
      - name: test
        image: alpine:3.11
        imagePullPolicy: Always
        command: [ 'sleep', '1' ]
EOT
# job.batch/test-job created
kubectl apply -f - <<EOT
apiVersion: batch/v1
kind: Job
metadata:
  name: "test-job"
spec:
  template:
    spec:
      restartPolicy: OnFailure
      containers:
      - name: test
        image: alpine:3.11
        imagePullPolicy: Always
        command: [ 'sleep', '2' ]
EOT
# The Job "test-job" is invalid: spec.template: Invalid value: core.PodTemplateSpec{ObjectMeta:v1.ObjectMeta{Name:"", GenerateName:"", Namespace:"", SelfLink:"", UID:"", ResourceVersion:"", Generation:0, CreationTimestamp:v1.Time{Time:time.Time{wall:0x0, ext:0, loc:(*time.Location)(nil)}}, DeletionTimestamp:(*v1.Time)(nil), DeletionGracePeriodSeconds:(*int64)(nil), Labels:map[string]string{"controller-uid":"e50402b2-2476-4ceb-a82f-749c044a6182", "job-name":"test-job"}, Annotations:map[string]string(nil), OwnerReferences:[]v1.OwnerReference(nil), Finalizers:[]string(nil), ClusterName:"", ManagedFields:[]v1.ManagedFieldsEntry(nil)}, Spec:core.PodSpec{Volumes:[]core.Volume(nil), InitContainers:[]core.Container(nil), Containers:[]core.Container{core.Container{Name:"test", Image:"alpine:3.11", Command:[]string{"sleep", "2"}, Args:[]string(nil), WorkingDir:"", Ports:[]core.ContainerPort(nil), EnvFrom:[]core.EnvFromSource(nil), Env:[]core.EnvVar(nil), Resources:core.ResourceRequirements{Limits:core.ResourceList(nil), Requests:core.ResourceList(nil)}, VolumeMounts:[]core.VolumeMount(nil), VolumeDevices:[]core.VolumeDevice(nil), LivenessProbe:(*core.Probe)(nil), ReadinessProbe:(*core.Probe)(nil), StartupProbe:(*core.Probe)(nil), Lifecycle:(*core.Lifecycle)(nil), TerminationMessagePath:"/dev/termination-log", TerminationMessagePolicy:"File", ImagePullPolicy:"Always", SecurityContext:(*core.SecurityContext)(nil), Stdin:false, StdinOnce:false, TTY:false}}, EphemeralContainers:[]core.EphemeralContainer(nil), RestartPolicy:"OnFailure", TerminationGracePeriodSeconds:(*int64)(0xc00d249bb8), ActiveDeadlineSeconds:(*int64)(nil), DNSPolicy:"ClusterFirst", NodeSelector:map[string]string(nil), ServiceAccountName:"", AutomountServiceAccountToken:(*bool)(nil), NodeName:"", SecurityContext:(*core.PodSecurityContext)(0xc009f10300), ImagePullSecrets:[]core.LocalObjectReference(nil), Hostname:"", Subdomain:"", Affinity:(*core.Affinity)(nil), SchedulerName:"default-scheduler", Tolerations:[]core.Toleration(nil), HostAliases:[]core.HostAlias(nil), PriorityClassName:"", Priority:(*int32)(nil), PreemptionPolicy:(*core.PreemptionPolicy)(nil), DNSConfig:(*core.PodDNSConfig)(nil), ReadinessGates:[]core.PodReadinessGate(nil), RuntimeClassName:(*string)(nil), Overhead:core.ResourceList(nil), EnableServiceLinks:(*bool)(nil), TopologySpreadConstraints:[]core.TopologySpreadConstraint(nil)}}: field is immutable

Anything else we need to know?: no

Environment:

  • Kubernetes version (use kubectl version): v1.17.4, v1.18.0
  • Cloud provider or hardware configuration:
  • OS (e.g: cat /etc/os-release): Ubuntu 18.04.2 LTS
  • Kernel (e.g. uname -a): 4.15.0-54-generic
  • Install tools: kubeadm
  • Network plugin and version (if this is a network-related bug):
  • Others:

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 20
  • Comments: 15 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Or you could simply delete your job.

kubectl delete job <name>

For people wondering how to possibly workaround this, check out this link: https://kubernetes.io/docs/concepts/workloads/controllers/job/#ttl-mechanism-for-finished-jobs

This is not a bug, you can’t change the template after the job has been created. /close

The existing job need to be deleted, because the template section in the job is immutable or not updatable. so you have 2 following options.

  1. Always create a new job with a unique name, so it leaves the old jobs and creates a new one - every time if you include the version of image would be sensible.
  2. Automatic cleaning of jobs (more info check here) - The job property ttlSecondsAfterFinished allows helps to delete the job automatically after a sepecified period. for eg:
    apiVersion: batch/v1
    kind: Job
    metadata:
      name: pi-with-ttl
    spec:
      ttlSecondsAfterFinished: 100
      template:
        spec:
          containers:
          - name: pi
            image: perl
            command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
          restartPolicy: Never

@3deep5me i found this ArgoCD Hook Policy, which helped in my case: i need to run DB migration as a Job, before the rest of the app is deployed and delete the Job after it’s done, so I’ve added to the Job’s annotations:

apiVersion: batch/v1
kind: Job
metadata:
  annotations:
    "argocd.argoproj.io/hook": PreSync
    "argocd.argoproj.io/hook-delete-policy": HookSucceeded

Does anyone have any idea how to workaround this in an argocd case? If you use ttlSecondsAfterFinished or a helm-hook. ArgoCD just recreates the resource 😅. Is there a reason why it is not allowed to update spec.Template? Or is it just a technical limitation?

This also happens when you do kubeclt replace --force - which is confusing. Why wouldn’t a replace work in this case and what is --force doing if it can’t actually force it to work?

@soltysh I realise this is an old thread, but what about the case where we have a job using a configmap generated via a configMapGenerator? - everytime you try to kustomize … | apply a new map is generated, all the other resources update just fine, but theres no way I know of to pin a job to it’s original map.
hints and tips welcomed

Thanks! I know its complaining at a high level but i don’t like the fact to involve argocd spefics in my manifests. Its something which would not work in a non argocd deployment.