Skip to content

Commit fe6f6db

Browse files
authored
Merge pull request kubebb#391 from Abirdcfly/latest
fix: cpl latest should handle helm operation
2 parents 4efbb44 + c28972a commit fe6f6db

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

controllers/componentplan_controller.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323
"strconv"
24+
"strings"
2425
"time"
2526

2627
"github.com/go-logr/logr"
@@ -482,12 +483,30 @@ func (r *ComponentPlanReconciler) updateLatest(ctx context.Context, logger logr.
482483
logger.Error(err, "Failed to get last release")
483484
return
484485
}
486+
relVersion := -1
487+
var parseDescription bool
488+
var relNs, relName, relUID string
485489
if rel == nil {
486-
logger.Info("no release found, just skip update latest")
487-
return
490+
logger.Info("no release found")
491+
} else {
492+
relVersion = rel.Version
493+
if rel.Info != nil && rel.Info.Status == release.StatusDeployed && !strings.HasPrefix(rel.Info.Description, "Rollback ") {
494+
// descrpiton match, the chart is exactly installed by this componentplan
495+
relNs, relName, relUID, _, _ = helm.ParseDescription(rel.Info.Description)
496+
parseDescription = true
497+
}
488498
}
489499
for _, cur := range list.Items {
490-
if latestShouldBe := cur.Status.InstalledRevision == rel.Version; cur.Status.Latest == nil || *cur.Status.Latest != latestShouldBe {
500+
var latestShouldBe bool
501+
if relVersion == cur.Status.InstalledRevision {
502+
latestShouldBe = true
503+
}
504+
if parseDescription {
505+
if relNs != cur.GetNamespace() || relName != cur.GetName() || relUID != string(cur.GetUID()) {
506+
latestShouldBe = false
507+
}
508+
}
509+
if cur.Status.Latest == nil || *cur.Status.Latest != latestShouldBe {
491510
newCur := cur.DeepCopy()
492511
newCur.Status.Latest = pointer.Bool(latestShouldBe)
493512
if err := r.Status().Patch(ctx, newCur, client.MergeFrom(&cur)); err != nil {

pkg/helm/cpl_helm.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,28 @@ func (c *CoreHelmWrapper) Uninstall(ctx context.Context) (err error) {
100100
if rel == nil {
101101
return nil
102102
}
103-
// version match, the chart is exactly installed by this componentplan
104-
if rel.Version == c.cpl.Status.InstalledRevision {
105-
return c.uninstall(ctx)
106-
}
107-
// descrpiton match, the chart is exactly installed by this componentplan
108-
if ns, name, uid, _, _ := ParseDescription(rel.Info.Description); ns == c.cpl.Namespace && name == c.cpl.Name && uid == string(c.cpl.GetUID()) {
109-
return c.uninstall(ctx)
110-
}
111-
// when installing/upgrading/rollingback, description will be setted by helm, so we compare helm release version and component status
112-
if c.cpl.Status.InstalledRevision+1 == rel.Version {
113-
if c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonInstalling) || c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonUpgrading) || c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonRollingBack) {
103+
var parseDescription bool
104+
var relNs, relName, relUID string
105+
if rel.Info != nil && rel.Info.Status == release.StatusDeployed && !strings.HasPrefix(rel.Info.Description, "Rollback ") {
106+
// descrpiton match, the chart is exactly installed by this componentplan
107+
relNs, relName, relUID, _, _ = ParseDescription(rel.Info.Description)
108+
parseDescription = true
109+
}
110+
if parseDescription {
111+
if c.cpl.Namespace == relNs || c.cpl.Name == relName || string(c.cpl.GetUID()) == relUID {
114112
return c.uninstall(ctx)
115113
}
114+
} else {
115+
// version match, the chart is exactly installed by this componentplan
116+
if rel.Version == c.cpl.Status.InstalledRevision {
117+
return c.uninstall(ctx)
118+
}
119+
// when installing/upgrading/rollingback, description will be setted by helm, so we compare helm release version and component status
120+
if c.cpl.Status.InstalledRevision+1 == rel.Version {
121+
if c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonInstalling) || c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonUpgrading) || c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonRollingBack) {
122+
return c.uninstall(ctx)
123+
}
124+
}
116125
}
117126
return nil
118127
}

0 commit comments

Comments
 (0)