Skip to content

Commit

Permalink
fix(application): app status is Terminating, update will delete it (#…
Browse files Browse the repository at this point in the history
…2271)

Co-authored-by: xdonggao <xdonggao@tencent.com>
  • Loading branch information
GaoXiaodong and xdonggao committed May 8, 2023
1 parent 46034f5 commit ecb5eef
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
5 changes: 0 additions & 5 deletions api/application/v1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ func SetDefaults_App(obj *App) {
}

func SetDefaults_AppSpec(obj *AppSpec) {
if obj.Finalizers == nil || len(obj.Finalizers) == 0 {
obj.Finalizers = []FinalizerName{
AppFinalize,
}
}
if obj.Values.Values == nil || len(obj.Values.Values) == 0 {
obj.Values.Values = make([]string, 0)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/application/registry/application/storage/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (rs *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObj
}
}

if !reflect.DeepEqual(oldApp.Spec, app.Spec) && app.Status.Phase != applicationapi.AppPhaseRolledBack {
if !reflect.DeepEqual(oldApp.Spec, app.Spec) && app.Status.Phase != applicationapi.AppPhaseRolledBack && oldApp.Status.Phase != applicationapi.AppPhaseTerminating {
app.Status.Phase = applicationapi.AppPhaseUpgrading
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/application/registry/application/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func NewStorage(optsGetter genericregistry.RESTOptionsGetter,
CreateStrategy: strategy,
UpdateStrategy: strategy,
DeleteStrategy: strategy,

ShouldDeleteDuringUpdate: applicationtrategy.ShouldDeleteDuringUpdate,
}
store.TableConvertor = printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(AddHandlers)}
options := &genericregistry.StoreOptions{
Expand Down Expand Up @@ -198,7 +200,7 @@ func (r *GenericREST) Delete(ctx context.Context, name string, deleteValidation
return nil, false, err
}

if app.DeletionTimestamp.IsZero() {
if app.DeletionTimestamp.IsZero() || app.Status.Phase != applicationapi.AppPhaseTerminating {
key, err := r.Store.KeyFunc(ctx, name)
if err != nil {
return nil, false, err
Expand Down
19 changes: 16 additions & 3 deletions pkg/application/registry/application/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ package application
import (
"context"
"fmt"

apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
genericregistry "k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/generic/registry"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/apiserver/pkg/storage"
"k8s.io/apiserver/pkg/storage/names"
Expand Down Expand Up @@ -104,6 +104,10 @@ func (s *Strategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
app.ObjectMeta.GenerateName = "app-"
app.Generation = 1

app.Spec.Finalizers = []application.FinalizerName{
application.AppFinalize,
}

if app.Spec.Chart.TenantID == "" {
app.Spec.Chart.TenantID = app.Spec.TenantID
}
Expand Down Expand Up @@ -151,11 +155,11 @@ func (Strategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) [

// GetAttrs returns labels and fields of a given object for filtering purposes.
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
application, ok := obj.(*application.App)
app, ok := obj.(*application.App)
if !ok {
return nil, nil, fmt.Errorf("not a application")
}
return application.ObjectMeta.Labels, ToSelectableFields(application), nil
return app.ObjectMeta.Labels, ToSelectableFields(app), nil
}

// MatchApplication returns a generic matcher for a given label and field selector.
Expand Down Expand Up @@ -188,6 +192,15 @@ func ToSelectableFields(app *application.App) fields.Set {
return genericregistry.MergeFieldsSets(objectMetaFieldsSet, specificFieldsSet)
}

func ShouldDeleteDuringUpdate(ctx context.Context, key string, obj, existing runtime.Object) bool {
app, ok := obj.(*application.App)
if !ok {
log.Errorf("unexpected object, key:%s", key)
return false
}
return len(app.Spec.Finalizers) == 0 && registry.ShouldDeleteDuringUpdate(ctx, key, obj, existing)
}

// StatusStrategy implements verification logic for status of app request.
type StatusStrategy struct {
*Strategy
Expand Down

0 comments on commit ecb5eef

Please sign in to comment.