Skip to content

Commit

Permalink
fix: some cpl uninstall failed
Browse files Browse the repository at this point in the history
Signed-off-by: Abirdcfly <[email protected]>
  • Loading branch information
Abirdcfly authored and 0xff-dev committed Nov 1, 2023
1 parent 33aaa9f commit 4efbb44
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
22 changes: 18 additions & 4 deletions config/samples/example-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function deleteComponentPlan() {
componentPlanName=$2
helmReleaseShouldDelete=$3
START_TIME=$(date +%s)
helmReleaseName=$(kubectl get ComponentPlan -n${namespace} ${componentPlanName} --ignore-not-found=true -ojson | jq -r '.spec.name')
helmReleaseName=$(kubectl get ComponentPlan -n${namespace} ${componentPlanName%% *} --ignore-not-found=true -ojson | jq -r '.spec.name')
if [[ $helmReleaseName == "" ]]; then
info "componentPlan:${componentPlanName} has no release name"
kubectl get ComponentPlan -n${namespace} ${componentPlanName} -oyaml
Expand Down Expand Up @@ -257,7 +257,7 @@ function deleteComponentPlan() {
if [ $ELAPSED_TIME -gt $TimeoutSeconds ]; then
error "Timeout reached"
helm list -A -a
helm status -n ${namespace} ${componentPlanName}
helm status -n ${namespace} ${helmReleaseName}
exit 1
fi
sleep 30
Expand Down Expand Up @@ -428,6 +428,7 @@ docker tag kubebb/core:latest kubebb/core:example-e2e
kind load docker-image kubebb/core:example-e2e --name=$KindName
make deploy IMG="kubebb/core:example-e2e"
kubectl wait deploy -n kubebb-system kubebb-controller-manager --for condition=Available=True --timeout=$Timeout
sleep 10

info "3 try to verify that the common steps are valid"

Expand Down Expand Up @@ -479,6 +480,8 @@ sleep 10
kubectl patch subscription -n kubebb-system grafana-sample --type='json' -p='[{"op": "replace", "path": "/spec/componentPlanInstallMethod", "value": "auto"}]'
getPodImage "kubebb-system" "app.kubernetes.io/instance=grafana-sample,app.kubernetes.io/name=grafana" "192.168.1.1:5000/grafana-local/grafana"
getHelmRevision "kubebb-system" "grafana-sample" "1"
cplName=$(kubectl get cpl -n kubebb-system --no-headers=true | awk '{print $1}')
deleteComponentPlan "kubebb-system" $cplName "true"
kubectl delete -f config/samples/core_v1alpha1_grafana_subscription.yaml

info "5 try to verify that common use of componentPlan are valid"
Expand Down Expand Up @@ -510,8 +513,19 @@ waitPodReady "kubebb-system" "app.kubernetes.io/instance=my-nginx,app.kubernetes
getHelmRevision "kubebb-system" "my-nginx" "4"
validateComponentPlanStatusLatestValue "kubebb-system" "do-once-nginx-sample-15.1.0" "false"
validateComponentPlanStatusLatestValue "kubebb-system" "do-once-nginx-sample-15.0.2" "true"
deleteComponentPlan "kubebb-system" "do-once-nginx-sample-15.1.0" "false"
deleteComponentPlan "kubebb-system" "do-once-nginx-sample-15.0.2" "true"
rand=$(awk 'BEGIN{srand(); print rand()}')
if [ $(echo "$rand < 0.33" | bc) -eq 1 ]; then
info "remove older cpl first"
deleteComponentPlan "kubebb-system" "do-once-nginx-sample-15.0.2" "true"
deleteComponentPlan "kubebb-system" "do-once-nginx-sample-15.1.0" "true"
elif [ $(echo "$rand < 0.66" | bc) -eq 1 ]; then
info "remove newer cpl first"
deleteComponentPlan "kubebb-system" "do-once-nginx-sample-15.1.0" "false"
deleteComponentPlan "kubebb-system" "do-once-nginx-sample-15.0.2" "true"
else
info "remove these cpl both"
deleteComponentPlan "kubebb-system" "do-once-nginx-sample-15.1.0 do-once-nginx-sample-15.0.2" "true"
fi

info "5.5 Verify long running componentPlan install don not block others to install"
kubectl apply -f config/samples/core_v1alpha1_nginx_componentplan_long_ready.yaml
Expand Down
7 changes: 7 additions & 0 deletions pkg/helm/cpl_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ func (c *CoreHelmWrapper) InstallOrUpgrade(ctx context.Context, chartName string

// Uninstall installs a helm chart to the cluster
func (c *CoreHelmWrapper) Uninstall(ctx context.Context) (err error) {
defer func() {
if err != nil {
if strings.HasPrefix(err.Error(), "uninstallation completed with 1 error(s): uninstall: Failed to purge the release: release: not found") { //nolint:dupword
err = nil
}
}
}()
rel, err := c.GetLastRelease()
if err != nil {
return err
Expand Down
22 changes: 16 additions & 6 deletions pkg/helm/cpl_helm_release_workpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -236,6 +237,15 @@ func (w *baseWorker) GetResult() (rel *release.Release, isRuuing bool, err error
return w.release, w.isRunning, w.err
}

func (w *baseWorker) logKV() []interface{} {
res := []interface{}{"workername", w.name}
if w.plan != nil {
res = append(res, "planUID", string(w.plan.UID))
res = append(res, "planGeneration", strconv.FormatInt(w.plan.Generation, 10))
}
return res
}

type installWorker struct {
baseWorker
}
Expand All @@ -254,11 +264,11 @@ func newInstallWorker(ctx context.Context, name, chartName string, logger logr.L
subCtx, cancel := context.WithCancel(ctx)
w.cancel = cancel
go func() {
w.logger.V(1).Info("start install worker", "workername", w.name)
w.logger.V(1).Info("start install worker", w.logKV()...)
w.startTime = metav1.Now()
defer func() {
cost := time.Since(w.startTime.Time)
w.logger.V(1).Info(fmt.Sprintf("stop install worker, cost %s", cost), "workername", w.name)
w.logger.V(1).Info(fmt.Sprintf("stop install worker, cost %s", cost), w.logKV()...)
w.isRunning = false
}()
w.status = release.StatusPendingInstall
Expand Down Expand Up @@ -314,10 +324,10 @@ func newUnInstallWorker(ctx context.Context, name string, logger logr.Logger, pl
subCtx, cancel := context.WithCancel(ctx)
w.cancel = cancel
go func() {
w.logger.V(1).Info("start uninstall worker", "workername", w.name)
w.logger.V(1).Info("start uninstall worker", w.logKV()...)
startTime := metav1.Now()
defer func() {
w.logger.V(1).Info(fmt.Sprintf("stop uninstall worker, cost %s", time.Since(startTime.Time)), "workername", w.name)
w.logger.V(1).Info(fmt.Sprintf("stop uninstall worker, cost %s", time.Since(startTime.Time)), w.logKV()...)
w.isRunning = false
}()
w.status = release.StatusUninstalling
Expand Down Expand Up @@ -362,10 +372,10 @@ func newRollBackWorker(ctx context.Context, name string, logger logr.Logger, pla
subCtx, cancel := context.WithCancel(ctx)
w.cancel = cancel
go func() {
w.logger.V(1).Info("start rollback worker", "workername", w.name)
w.logger.V(1).Info("start rollback worker", w.logKV()...)
startTime := metav1.Now()
defer func() {
w.logger.V(1).Info(fmt.Sprintf("stop rollback worker, cost %s", time.Since(startTime.Time)), "workername", w.name)
w.logger.V(1).Info(fmt.Sprintf("stop rollback worker, cost %s", time.Since(startTime.Time)), w.logKV()...)
w.isRunning = false
}()
w.status = release.StatusUninstalling
Expand Down

0 comments on commit 4efbb44

Please sign in to comment.