Skip to content

Commit

Permalink
fix: upgrade rsm-env object (#7681)
Browse files Browse the repository at this point in the history
(cherry picked from commit b765afe)
  • Loading branch information
free6om authored Jul 1, 2024
1 parent 8fecb13 commit c4797b9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
34 changes: 28 additions & 6 deletions controllers/apps/transformer_component_workload_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/controller/instanceset/instance_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit c4797b9

Please sign in to comment.