Generate work permanent id in webhook

Signed-off-by: whitewindmills <jayfantasyhjh@gmail.com>
This commit is contained in:
whitewindmills 2024-05-10 19:01:12 +08:00
parent 4ccffccc70
commit 93a04cbf0d
2 changed files with 4 additions and 8 deletions

View File

@ -20,7 +20,6 @@ import (
"context"
"fmt"
"github.com/google/uuid"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@ -72,9 +71,6 @@ func CreateOrUpdateWork(client client.Client, workMeta metav1.ObjectMeta, resour
runtimeObject.Spec = work.Spec
runtimeObject.Labels = util.DedupeAndMergeLabels(runtimeObject.Labels, work.Labels)
runtimeObject.Annotations = util.DedupeAndMergeAnnotations(runtimeObject.Annotations, work.Annotations)
if util.GetLabelValue(runtimeObject.Labels, workv1alpha2.WorkPermanentIDLabel) == "" {
runtimeObject.Labels = util.DedupeAndMergeLabels(runtimeObject.Labels, map[string]string{workv1alpha2.WorkPermanentIDLabel: uuid.New().String()})
}
return nil
})
return err

View File

@ -51,6 +51,10 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
}
klog.V(2).Infof("Mutating the work(%s/%s) for request: %s", work.Namespace, work.Name, req.Operation)
if util.GetLabelValue(work.Labels, workv1alpha2.WorkPermanentIDLabel) == "" {
util.MergeLabel(work, workv1alpha2.WorkPermanentIDLabel, uuid.New().String())
}
var manifests []workv1alpha1.Manifest
for _, manifest := range work.Spec.Workload.Manifests {
@ -86,10 +90,6 @@ func (a *MutatingAdmission) Handle(_ context.Context, req admission.Request) adm
return admission.Errored(http.StatusInternalServerError, err)
}
if util.GetLabelValue(work.Labels, workv1alpha2.WorkPermanentIDLabel) == "" {
util.MergeLabel(work, workv1alpha2.WorkPermanentIDLabel, uuid.New().String())
}
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledBytes)
}