diff --git a/controllers/apps/transformer_component_workload_upgrade.go b/controllers/apps/transformer_component_workload_upgrade.go index 18b0b64fe1a..5a6c3282f14 100644 --- a/controllers/apps/transformer_component_workload_upgrade.go +++ b/controllers/apps/transformer_component_workload_upgrade.go @@ -61,10 +61,11 @@ func (t *componentWorkloadUpgradeTransformer) Transform(ctx graph.TransformConte legacyFound := false // update pod & pvc & svc labels - objectList := []client.ObjectList{&corev1.PersistentVolumeClaimList{}, &corev1.ServiceList{}, &corev1.PodList{}} - ml := constant.GetComponentWellKnownLabels(transCtx.Cluster.Name, synthesizeComp.Name) + objectList := []client.ObjectList{&corev1.PersistentVolumeClaimList{}, &corev1.PodList{}, &corev1.ServiceList{}, &corev1.ConfigMapList{}} + ml := constant.GetComponentWellKnownLabels(synthesizeComp.ClusterName, synthesizeComp.Name) inNS := client.InNamespace(comp.Namespace) - defaultHeadlessSvc := constant.GenerateDefaultComponentHeadlessServiceName(transCtx.Cluster.Name, synthesizeComp.Name) + defaultHeadlessSvc := constant.GenerateDefaultComponentHeadlessServiceName(synthesizeComp.ClusterName, synthesizeComp.Name) + envCM := instanceset.GetEnvConfigMapName(constant.GenerateClusterComponentName(synthesizeComp.ClusterName, synthesizeComp.Name)) var revision string for _, list := range objectList { if err := graphCli.List(transCtx.Context, list, client.MatchingLabels(ml), inNS); err == nil { @@ -75,13 +76,34 @@ func (t *componentWorkloadUpgradeTransformer) Transform(ctx graph.TransformConte if _, ok := object.GetLabels()[instanceset.WorkloadsManagedByLabelKey]; ok { continue } - if _, ok := object.(*corev1.Service); ok && object.GetName() != defaultHeadlessSvc { + _, isSvc := object.(*corev1.Service) + if isSvc && object.GetName() != defaultHeadlessSvc { + continue + } + _, isCM := object.(*corev1.ConfigMap) + if isCM && object.GetName() != envCM { continue } legacyFound = true - object.SetOwnerReferences([]metav1.OwnerReference{}) + + // fix labels object.GetLabels()[instanceset.WorkloadsManagedByLabelKey] = workloads.Kind object.GetLabels()[instanceset.WorkloadsInstanceLabelKey] = comp.Name + + // fix labels, ownerReference and finalizer of Service and ConfigMap: + // assume all the OwnerReferences and Finalizers were set by the KubeBlocks. + // set them to empty and the InstanceSet Controller will fix them. + if isSvc || isCM { + object.SetOwnerReferences([]metav1.OwnerReference{}) + object.SetFinalizers([]string{}) + delete(object.GetLabels(), constant.AppManagedByLabelKey) + delete(object.GetLabels(), constant.AppNameLabelKey) + delete(object.GetLabels(), constant.AppComponentLabelKey) + delete(object.GetLabels(), constant.AppInstanceLabelKey) + delete(object.GetLabels(), constant.KBAppComponentLabelKey) + } + + // fix revision of Pods if _, ok := object.(*corev1.Pod); ok { if revision == "" { revision, err = buildRevision(synthesizeComp, compDef) @@ -123,7 +145,7 @@ func (t *componentWorkloadUpgradeTransformer) Transform(ctx graph.TransformConte if legacyFound { // set status.observedGeneration to zero to trigger a creation reconciliation loop of the component controller. comp.Status.ObservedGeneration = 0 - return errPrematureStopWithSetCompOwnership(transCtx.Component, dag, graphCli) + return graph.ErrPrematureStop } return nil } diff --git a/pkg/controller/instanceset/instance_util.go b/pkg/controller/instanceset/instance_util.go index 0b7f315addc..4ff57d33104 100644 --- a/pkg/controller/instanceset/instance_util.go +++ b/pkg/controller/instanceset/instance_util.go @@ -363,6 +363,16 @@ func copyAndMerge(oldObj, newObj client.Object) client.Object { } copyAndMergeSvc := func(oldSvc *corev1.Service, newSvc *corev1.Service) client.Object { + intctrlutil.MergeList(&newSvc.Finalizers, &oldSvc.Finalizers, func(finalizer string) func(string) bool { + return func(item string) bool { + return finalizer == item + } + }) + intctrlutil.MergeList(&newSvc.OwnerReferences, &oldSvc.OwnerReferences, func(reference metav1.OwnerReference) func(metav1.OwnerReference) bool { + return func(item metav1.OwnerReference) bool { + return reference.UID == item.UID + } + }) mergeMap(&newSvc.Annotations, &oldSvc.Annotations) mergeMap(&newSvc.Labels, &oldSvc.Labels) mergeMap(&newSvc.Spec.Selector, &oldSvc.Spec.Selector) @@ -377,6 +387,16 @@ func copyAndMerge(oldObj, newObj client.Object) client.Object { } copyAndMergeCm := func(oldCm, newCm *corev1.ConfigMap) client.Object { + intctrlutil.MergeList(&newCm.Finalizers, &oldCm.Finalizers, func(finalizer string) func(string) bool { + return func(item string) bool { + return finalizer == item + } + }) + intctrlutil.MergeList(&newCm.OwnerReferences, &oldCm.OwnerReferences, func(reference metav1.OwnerReference) func(metav1.OwnerReference) bool { + return func(item metav1.OwnerReference) bool { + return reference.UID == item.UID + } + }) oldCm.Data = newCm.Data oldCm.BinaryData = newCm.BinaryData return oldCm