From 7d3d818f93e7a9f0308ec796bdb0bdc699673ca1 Mon Sep 17 00:00:00 2001 From: Tiger Kaovilai Date: Mon, 28 Aug 2023 10:09:37 -0400 Subject: [PATCH] Handle 1.27 k8s job label changes per https://github.com/kubernetes/kubernetes/blob/0e86fa5115cb79a04bdb949d135e35a31ce806f2/CHANGELOG/CHANGELOG-1.27.md?plain=1#L1768 Signed-off-by: Tiger Kaovilai --- pkg/discovery/helper.go | 8 ++++++++ pkg/restore/job_action.go | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/pkg/discovery/helper.go b/pkg/discovery/helper.go index fa84de1f96..f52fc7225f 100644 --- a/pkg/discovery/helper.go +++ b/pkg/discovery/helper.go @@ -312,3 +312,11 @@ func (h *helper) ServerVersion() *version.Info { defer h.lock.RUnlock() return h.serverVersion } + +func ServerVersion(logger logrus.FieldLogger) (*version.Info, error) { + discoveryHelper, err := NewHelper(&discovery.DiscoveryClient{}, logger) + if err != nil { + return nil, err + } + return discoveryHelper.ServerVersion(), nil +} diff --git a/pkg/restore/job_action.go b/pkg/restore/job_action.go index fbaf30b249..66af7a7a80 100644 --- a/pkg/restore/job_action.go +++ b/pkg/restore/job_action.go @@ -17,15 +17,23 @@ limitations under the License. package restore import ( + "strconv" + "github.com/pkg/errors" "github.com/sirupsen/logrus" batchv1api "k8s.io/api/batch/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" + "github.com/vmware-tanzu/velero/pkg/discovery" "github.com/vmware-tanzu/velero/pkg/plugin/velero" ) +const ( + legacyControllerUIDLabel = "controller-uid" // <=1.27 This still exists in 1.27 for backward compatibility, maybe remove in 1.28? + controllerUIDLabel = "batch.kubernetes.io/controller-uid" // >=1.27 https://github.com/kubernetes/kubernetes/pull/114930#issuecomment-1384667494 +) + type JobAction struct { logger logrus.FieldLogger } @@ -46,10 +54,35 @@ func (a *JobAction) Execute(input *velero.RestoreItemActionExecuteInput) (*veler return nil, errors.WithStack(err) } + // get kube version + serverVersion, err := discovery.ServerVersion(a.logger) + if err != nil { + return nil, errors.WithStack(err) + } + // if kube version < 1.27, use legacy controller-uid label + majorVersion, err := strconv.Atoi(serverVersion.Major) + if err != nil { + return nil, errors.WithStack(err) + } + minorVersion, err := strconv.Atoi(serverVersion.Minor) + if err != nil { + return nil, errors.WithStack(err) + } + deleteLegacyControllerUIDLabel := false + if majorVersion < 1 || (majorVersion == 1 && minorVersion <= 27) { + deleteLegacyControllerUIDLabel = true + } + if job.Spec.Selector != nil { - delete(job.Spec.Selector.MatchLabels, "controller-uid") + delete(job.Spec.Selector.MatchLabels, controllerUIDLabel) + if deleteLegacyControllerUIDLabel { + delete(job.Spec.Selector.MatchLabels, legacyControllerUIDLabel) + } + } + delete(job.Spec.Template.ObjectMeta.Labels, controllerUIDLabel) + if deleteLegacyControllerUIDLabel { + delete(job.Spec.Template.ObjectMeta.Labels, legacyControllerUIDLabel) } - delete(job.Spec.Template.ObjectMeta.Labels, "controller-uid") res, err := runtime.DefaultUnstructuredConverter.ToUnstructured(job) if err != nil {