Skip to content

Commit c647c59

Browse files
author
xdonggao
committed
fix(application): fix failed and sync failed check
1 parent 9258bbe commit c647c59

File tree

7 files changed

+77
-35
lines changed

7 files changed

+77
-35
lines changed

Diff for: api/application/types.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -226,21 +226,18 @@ const (
226226
type AppPhase string
227227

228228
const (
229-
// ChartFetchedFailed means the chart to which the App
230-
// refers could not be fetched.
231-
AppPhaseChartFetchFailed AppPhase = "ChartFetchFailed"
232-
233229
// Installing means the installation for the App is running.
234230
AppPhaseInstalling AppPhase = "Installing"
231+
// InstallFailed means the installation for the App failed.
232+
AppPhaseInstallFailed AppPhase = "InstallFailed"
235233
// Upgrading means the upgrade for the App is running.
236234
AppPhaseUpgrading AppPhase = "Upgrading"
237235
// Succeeded means the dry-run, installation, or upgrade for the
238236
// App succeeded.
239237
AppPhaseSucceeded AppPhase = "Succeeded"
240-
// Failed means the installation or upgrade for the App
238+
// Failed means the upgrade for the App
241239
// failed.
242-
AppPhaseFailed AppPhase = "Failed"
243-
240+
AppPhaseUpgradFailed AppPhase = "UpgradFailed"
244241
// RollingBack means a rollback for the App is running.
245242
AppPhaseRollingBack AppPhase = "RollingBack"
246243
// RolledBack means the App has been rolled back.

Diff for: api/application/v1/types.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -226,21 +226,18 @@ const (
226226
type AppPhase string
227227

228228
const (
229-
// ChartFetchedFailed means the chart to which the App
230-
// refers could not be fetched.
231-
AppPhaseChartFetchFailed AppPhase = "ChartFetchFailed"
232-
233229
// Installing means the installation for the App is running.
234230
AppPhaseInstalling AppPhase = "Installing"
231+
// InstallFailed means the installation for the App failed.
232+
AppPhaseInstallFailed AppPhase = "InstallFailed"
235233
// Upgrading means the upgrade for the App is running.
236234
AppPhaseUpgrading AppPhase = "Upgrading"
237235
// Succeeded means the dry-run, installation, or upgrade for the
238236
// App succeeded.
239237
AppPhaseSucceeded AppPhase = "Succeeded"
240-
// Failed means the installation or upgrade for the App
238+
// Failed means the upgrade for the App
241239
// failed.
242-
AppPhaseFailed AppPhase = "Failed"
243-
240+
AppPhaseUpgradFailed AppPhase = "UpgradFailed"
244241
// RollingBack means a rollback for the App is running.
245242
AppPhaseRollingBack AppPhase = "RollingBack"
246243
// RolledBack means the App has been rolled back.

Diff for: pkg/application/controller/app/action/install.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package action
2020

2121
import (
2222
"context"
23+
"fmt"
2324

2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526
applicationv1 "tkestack.io/tke/api/application/v1"
@@ -31,6 +32,7 @@ import (
3132
applicationprovider "tkestack.io/tke/pkg/application/provider/application"
3233
"tkestack.io/tke/pkg/application/util"
3334
chartpath "tkestack.io/tke/pkg/application/util/chartpath/v1"
35+
"tkestack.io/tke/pkg/util/log"
3436
)
3537

3638
// Install installs a chart archive
@@ -51,7 +53,22 @@ func Install(ctx context.Context,
5153
}
5254
destfile, err := Pull(ctx, applicationClient, platformClient, app, repo, updateStatusFunc)
5355
if err != nil {
54-
return nil, err
56+
newStatus := app.Status.DeepCopy()
57+
if updateStatusFunc != nil {
58+
if app.Status.Phase == applicationv1.AppPhaseInstallFailed {
59+
log.Error(fmt.Sprintf("install app failed, helm pull err: %s", err.Error()))
60+
// delayed retry, queue.AddRateLimited does not meet the demand
61+
return app, nil
62+
}
63+
newStatus.Phase = applicationv1.AppPhaseInstallFailed
64+
newStatus.Message = "fetch chart failed"
65+
newStatus.Reason = err.Error()
66+
newStatus.LastTransitionTime = metav1.Now()
67+
_, updateStatusErr := updateStatusFunc(ctx, app, &app.Status, newStatus)
68+
if updateStatusErr != nil {
69+
return nil, updateStatusErr
70+
}
71+
}
5572
}
5673

5774
newApp, err := applicationClient.Apps(app.Namespace).Get(ctx, app.Name, metav1.GetOptions{})
@@ -82,7 +99,12 @@ func Install(ctx context.Context,
8299
newStatus := newApp.Status.DeepCopy()
83100
var updateStatusErr error
84101
if err != nil {
85-
newStatus.Phase = applicationv1.AppPhaseFailed
102+
if app.Status.Phase == applicationv1.AppPhaseInstallFailed {
103+
log.Error(fmt.Sprintf("install app failed, helm install err: %s", err.Error()))
104+
// delayed retry, queue.AddRateLimited does not meet the demand
105+
return app, nil
106+
}
107+
newStatus.Phase = applicationv1.AppPhaseInstallFailed
86108
newStatus.Message = "install app failed"
87109
newStatus.Reason = err.Error()
88110
newStatus.LastTransitionTime = metav1.Now()

Diff for: pkg/application/controller/app/action/pull.go

-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package action
2121
import (
2222
"context"
2323

24-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2524
applicationv1 "tkestack.io/tke/api/application/v1"
2625
applicationversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/application/v1"
2726
platformversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1"
@@ -51,16 +50,5 @@ func Pull(ctx context.Context,
5150
destfile, err := client.Pull(&helmaction.PullOptions{
5251
ChartPathOptions: chartPathBasicOptions,
5352
})
54-
if updateStatusFunc != nil {
55-
newStatus := app.Status.DeepCopy()
56-
if err != nil {
57-
newStatus.Phase = applicationv1.AppPhaseChartFetchFailed
58-
newStatus.Message = "fetch chart failed"
59-
newStatus.Reason = err.Error()
60-
newStatus.LastTransitionTime = metav1.Now()
61-
updateStatusFunc(ctx, app, &app.Status, newStatus)
62-
return destfile, err
63-
}
64-
}
6553
return destfile, err
6654
}

Diff for: pkg/application/controller/app/action/upgrade.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package action
2020

2121
import (
2222
"context"
23+
"fmt"
2324

2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526
applicationv1 "tkestack.io/tke/api/application/v1"
@@ -31,6 +32,7 @@ import (
3132
applicationprovider "tkestack.io/tke/pkg/application/provider/application"
3233
"tkestack.io/tke/pkg/application/util"
3334
chartpath "tkestack.io/tke/pkg/application/util/chartpath/v1"
35+
"tkestack.io/tke/pkg/util/log"
3436
)
3537

3638
// Upgrade upgrade a helm release
@@ -52,6 +54,19 @@ func Upgrade(ctx context.Context,
5254

5355
destfile, err := Pull(ctx, applicationClient, platformClient, app, repo, updateStatusFunc)
5456
if err != nil {
57+
newStatus := app.Status.DeepCopy()
58+
if updateStatusFunc != nil {
59+
if app.Status.Phase == applicationv1.AppPhaseUpgradFailed {
60+
log.Error(fmt.Sprintf("upgrade app failed, helm pull err: %s", err.Error()))
61+
// delayed retry, queue.AddRateLimited does not meet the demand
62+
return app, nil
63+
}
64+
newStatus.Phase = applicationv1.AppPhaseUpgradFailed
65+
newStatus.Message = "fetch chart failed"
66+
newStatus.Reason = err.Error()
67+
newStatus.LastTransitionTime = metav1.Now()
68+
updateStatusFunc(ctx, app, &app.Status, newStatus)
69+
}
5570
return nil, err
5671
}
5772

@@ -85,7 +100,12 @@ func Upgrade(ctx context.Context,
85100
newStatus := newApp.Status.DeepCopy()
86101
var updateStatusErr error
87102
if err != nil {
88-
newStatus.Phase = applicationv1.AppPhaseFailed
103+
if app.Status.Phase == applicationv1.AppPhaseUpgradFailed {
104+
log.Error(fmt.Sprintf("upgrade app failed, helm upgrade err: %s", err.Error()))
105+
// delayed retry, queue.AddRateLimited does not meet the demand
106+
return app, nil
107+
}
108+
newStatus.Phase = applicationv1.AppPhaseUpgradFailed
89109
newStatus.Message = "upgrade app failed"
90110
newStatus.Reason = err.Error()
91111
newStatus.LastTransitionTime = metav1.Now()

Diff for: pkg/application/controller/app/app_controller.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ func (c *Controller) needsUpdate(old *applicationv1.App, new *applicationv1.App)
157157
return true
158158
}
159159

160+
if new.Status.Phase == applicationv1.AppPhaseSyncFailed ||
161+
new.Status.Phase == applicationv1.AppPhaseInstallFailed ||
162+
new.Status.Phase == applicationv1.AppPhaseUpgradFailed ||
163+
new.Status.Phase == applicationv1.AppPhaseSucceeded ||
164+
new.Status.Phase == applicationv1.AppPhaseTerminating {
165+
return true
166+
}
167+
160168
return false
161169
}
162170

@@ -326,11 +334,13 @@ func (c *Controller) handlePhase(ctx context.Context, key string, cachedApp *cac
326334
return c.updateStatus(ctx, app, &app.Status, newStatus)
327335
}
328336
return action.Upgrade(ctx, c.client.ApplicationV1(), c.platformClient, app, c.repo, c.updateStatus)
337+
case applicationv1.AppPhaseInstallFailed:
338+
return action.Install(ctx, c.client.ApplicationV1(), c.platformClient, app, c.repo, c.updateStatus)
329339
case applicationv1.AppPhaseSucceeded:
330340
c.startAppHealthCheck(ctx, key)
331341
// sync release status
332342
return c.syncAppFromRelease(ctx, cachedApp, app)
333-
case applicationv1.AppPhaseFailed:
343+
case applicationv1.AppPhaseUpgradFailed:
334344
return action.Upgrade(ctx, c.client.ApplicationV1(), c.platformClient, app, c.repo, c.updateStatus)
335345
case applicationv1.AppPhaseRollingBack:
336346
if app.Status.RollbackRevision > 0 {
@@ -341,18 +351,16 @@ func (c *Controller) handlePhase(ctx context.Context, key string, cachedApp *cac
341351
return c.syncAppFromRelease(ctx, cachedApp, app)
342352
case applicationv1.AppPhaseRollbackFailed:
343353
break
344-
case applicationv1.AppPhaseChartFetchFailed:
345-
break
346354
case applicationv1.AppPhaseSyncFailed:
347-
break
355+
return c.syncAppFromRelease(ctx, cachedApp, app)
348356
default:
349357
break
350358
}
351359
return app, nil
352360
}
353361

354362
func (c *Controller) syncAppFromRelease(ctx context.Context, cachedApp *cachedApp, app *applicationv1.App) (*applicationv1.App, error) {
355-
if app.Status.Phase == applicationv1.AppPhaseSucceeded && hasSynced(app) {
363+
if hasSynced(app) {
356364
return app, nil
357365
}
358366
defer func() {
@@ -363,6 +371,11 @@ func (c *Controller) syncAppFromRelease(ctx context.Context, cachedApp *cachedAp
363371
newStatus := app.Status.DeepCopy()
364372
rels, err := action.List(ctx, c.client.ApplicationV1(), c.platformClient, app)
365373
if err != nil {
374+
if app.Status.Phase == applicationv1.AppPhaseSyncFailed {
375+
log.Error(fmt.Sprintf("sync app failed, helm list failed, err: %s", err.Error()))
376+
// delayed retry, queue.AddRateLimited does not meet the demand
377+
return app, nil
378+
}
366379
newStatus.Phase = applicationv1.AppPhaseSyncFailed
367380
newStatus.Message = "sync app failed"
368381
newStatus.Reason = err.Error()
@@ -371,6 +384,11 @@ func (c *Controller) syncAppFromRelease(ctx context.Context, cachedApp *cachedAp
371384
}
372385
rel, found := helmutil.Filter(rels, app.Spec.TargetNamespace, app.Spec.Name)
373386
if !found {
387+
if app.Status.Phase == applicationv1.AppPhaseSyncFailed {
388+
log.Error(fmt.Sprintf("sync app failed, release not found: %s/%s", app.Spec.TargetNamespace, app.Spec.Name))
389+
// delayed retry, queue.AddRateLimited does not meet the demand
390+
return app, nil
391+
}
374392
newStatus.Phase = applicationv1.AppPhaseSyncFailed
375393
newStatus.Message = "sync app failed"
376394
newStatus.Reason = fmt.Sprintf("release not found: %s/%s", app.Spec.TargetNamespace, app.Spec.Name)

Diff for: pkg/application/controller/app/deletion/app_resources_deleter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func deleteApplication(ctx context.Context,
289289
repo appconfig.RepoConfiguration) error {
290290
_, err := action.Uninstall(ctx, deleter.applicationClient, deleter.platformClient, app, repo)
291291
if err != nil {
292-
if strings.Contains(err.Error(), "release: not found") {
292+
if strings.Contains(err.Error(), "release: not found") || errors.IsNotFound(err) {
293293
log.Warn(err.Error())
294294
return nil
295295
}

0 commit comments

Comments
 (0)