Skip to content

Commit

Permalink
fix: cpl latest should handle helm operation
Browse files Browse the repository at this point in the history
Signed-off-by: Abirdcfly <[email protected]>
  • Loading branch information
Abirdcfly committed Oct 31, 2023
1 parent 6796675 commit c28972a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
25 changes: 22 additions & 3 deletions controllers/componentplan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"strconv"
"strings"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -482,12 +483,30 @@ func (r *ComponentPlanReconciler) updateLatest(ctx context.Context, logger logr.
logger.Error(err, "Failed to get last release")
return
}
relVersion := -1
var parseDescription bool
var relNs, relName, relUID string
if rel == nil {
logger.Info("no release found, just skip update latest")
return
logger.Info("no release found")
} else {
relVersion = rel.Version
if rel.Info != nil && rel.Info.Status == release.StatusDeployed && !strings.HasPrefix(rel.Info.Description, "Rollback ") {
// descrpiton match, the chart is exactly installed by this componentplan
relNs, relName, relUID, _, _ = helm.ParseDescription(rel.Info.Description)
parseDescription = true
}
}
for _, cur := range list.Items {
if latestShouldBe := cur.Status.InstalledRevision == rel.Version; cur.Status.Latest == nil || *cur.Status.Latest != latestShouldBe {
var latestShouldBe bool
if relVersion == cur.Status.InstalledRevision {
latestShouldBe = true
}
if parseDescription {
if relNs != cur.GetNamespace() || relName != cur.GetName() || relUID != string(cur.GetUID()) {
latestShouldBe = false
}
}
if cur.Status.Latest == nil || *cur.Status.Latest != latestShouldBe {
newCur := cur.DeepCopy()
newCur.Status.Latest = pointer.Bool(latestShouldBe)
if err := r.Status().Patch(ctx, newCur, client.MergeFrom(&cur)); err != nil {
Expand Down
31 changes: 20 additions & 11 deletions pkg/helm/cpl_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,28 @@ func (c *CoreHelmWrapper) Uninstall(ctx context.Context) (err error) {
if rel == nil {
return nil
}
// version match, the chart is exactly installed by this componentplan
if rel.Version == c.cpl.Status.InstalledRevision {
return c.uninstall(ctx)
}
// descrpiton match, the chart is exactly installed by this componentplan
if ns, name, uid, _, _ := ParseDescription(rel.Info.Description); ns == c.cpl.Namespace && name == c.cpl.Name && uid == string(c.cpl.GetUID()) {
return c.uninstall(ctx)
}
// when installing/upgrading/rollingback, description will be setted by helm, so we compare helm release version and component status
if c.cpl.Status.InstalledRevision+1 == rel.Version {
if c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonInstalling) || c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonUpgrading) || c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonRollingBack) {
var parseDescription bool
var relNs, relName, relUID string
if rel.Info != nil && rel.Info.Status == release.StatusDeployed && !strings.HasPrefix(rel.Info.Description, "Rollback ") {
// descrpiton match, the chart is exactly installed by this componentplan
relNs, relName, relUID, _, _ = ParseDescription(rel.Info.Description)
parseDescription = true
}
if parseDescription {
if c.cpl.Namespace == relNs || c.cpl.Name == relName || string(c.cpl.GetUID()) == relUID {
return c.uninstall(ctx)
}
} else {
// version match, the chart is exactly installed by this componentplan
if rel.Version == c.cpl.Status.InstalledRevision {
return c.uninstall(ctx)
}
// when installing/upgrading/rollingback, description will be setted by helm, so we compare helm release version and component status
if c.cpl.Status.InstalledRevision+1 == rel.Version {
if c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonInstalling) || c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonUpgrading) || c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonRollingBack) {
return c.uninstall(ctx)
}
}
}
return nil
}
Expand Down

0 comments on commit c28972a

Please sign in to comment.