No need to propagate ttlsecondsafterfinished

Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
This commit is contained in:
chaunceyjiang 2022-08-01 15:15:17 +08:00
parent 616033fc0e
commit 83c91ac909
2 changed files with 21 additions and 4 deletions

View File

@ -13,7 +13,7 @@ import (
// RemoveIrrelevantField used to remove fields that generated by kube-apiserver and no need(or can't) propagate to
// member clusters.
func RemoveIrrelevantField(workload *unstructured.Unstructured) error {
func RemoveIrrelevantField(workload *unstructured.Unstructured, extraHooks ...func(*unstructured.Unstructured)) error {
// populated by the kubernetes.
unstructured.RemoveNestedField(workload.Object, "metadata", "creationTimestamp")
@ -61,9 +61,10 @@ func RemoveIrrelevantField(workload *unstructured.Unstructured) error {
if err != nil {
return err
}
if job.Spec.ManualSelector == nil || !*job.Spec.ManualSelector {
return removeGenerateSelectorOfJob(workload)
if err = removeGenerateSelectorOfJob(workload); err != nil {
return err
}
}
}
@ -81,6 +82,9 @@ func RemoveIrrelevantField(workload *unstructured.Unstructured) error {
}
}
for i := range extraHooks {
extraHooks[i](workload)
}
return nil
}
@ -119,3 +123,16 @@ func removeGenerateSelectorOfJob(workload *unstructured.Unstructured) error {
}
return nil
}
// RemoveJobTTLSeconds removes the '.spec.ttlSecondsAfterFinished' from a Job.
// The reason for removing it is that the Job propagated by Karmada probably be automatically deleted
// from member clusters(by 'ttl-after-finished' controller in member clusters). That will cause a conflict if
// Karmada tries to re-create it. See https://github.com/karmada-io/karmada/issues/2197 for more details.
//
// It is recommended to enable the `ttl-after-finished` controller in the Karmada control plane.
// See https://karmada.io/docs/administrator/configuration/configure-controllers#ttl-after-finished for more details.
func RemoveJobTTLSeconds(workload *unstructured.Unstructured) {
if workload.GetKind() == util.JobKind {
unstructured.RemoveNestedField(workload.Object, "spec", "ttlSecondsAfterFinished")
}
}

View File

@ -43,7 +43,7 @@ func (a *MutatingAdmission) Handle(ctx context.Context, req admission.Request) a
return admission.Errored(http.StatusInternalServerError, err)
}
err = prune.RemoveIrrelevantField(workloadObj)
err = prune.RemoveIrrelevantField(workloadObj, prune.RemoveJobTTLSeconds)
if err != nil {
klog.Errorf("Failed to remove irrelevant field for work(%s): %v", work.Name, err)
return admission.Errored(http.StatusInternalServerError, err)