github.com/oam-dev/kubevela@v1.9.11/pkg/utils/app/operation.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package app
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/pkg/errors"
    23  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	k8stypes "k8s.io/apimachinery/pkg/types"
    26  	"k8s.io/client-go/util/retry"
    27  	"k8s.io/klog/v2"
    28  	"sigs.k8s.io/controller-runtime/pkg/client"
    29  
    30  	apicommon "github.com/oam-dev/kubevela/apis/core.oam.dev/common"
    31  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    32  	"github.com/oam-dev/kubevela/pkg/component"
    33  	"github.com/oam-dev/kubevela/pkg/controller/core.oam.dev/v1beta1/application"
    34  	"github.com/oam-dev/kubevela/pkg/controller/utils"
    35  	"github.com/oam-dev/kubevela/pkg/oam"
    36  )
    37  
    38  // ErrNotMatchRevision -
    39  var ErrNotMatchRevision = errors.Errorf("failed to find revision matching the application")
    40  
    41  // ErrPublishVersionNotChange -
    42  var ErrPublishVersionNotChange = errors.Errorf("the PublishVersion is not changed")
    43  
    44  // ErrRevisionNotChange -
    45  var ErrRevisionNotChange = errors.Errorf("the revision is not changed")
    46  
    47  // RevisionContextKey if this key is exit in ctx, we should use it preferentially
    48  var RevisionContextKey = "revision-context-key"
    49  
    50  // RollbackApplicationWithRevision make the exist application rollback to specified revision.
    51  // revisionCtx the context used to manage the application revision.
    52  func RollbackApplicationWithRevision(ctx context.Context, cli client.Client, appName, appNamespace, revisionName, publishVersion string) (*v1beta1.ApplicationRevision, *v1beta1.Application, error) {
    53  	revisionCtx, ok := ctx.Value(&RevisionContextKey).(context.Context)
    54  	if !ok {
    55  		revisionCtx = ctx
    56  	}
    57  	// check revision
    58  	revs, err := application.GetSortedAppRevisions(revisionCtx, cli, appName, appNamespace)
    59  	if err != nil {
    60  		return nil, nil, err
    61  	}
    62  	var matchedRev *v1beta1.ApplicationRevision
    63  	for _, rev := range revs {
    64  		if rev.Name == revisionName {
    65  			matchedRev = rev.DeepCopy()
    66  		}
    67  	}
    68  	if matchedRev == nil {
    69  		return nil, nil, ErrNotMatchRevision
    70  	}
    71  
    72  	app := &v1beta1.Application{}
    73  	if err := cli.Get(ctx, k8stypes.NamespacedName{Name: appName, Namespace: appNamespace}, app); err != nil {
    74  		return nil, nil, err
    75  	}
    76  
    77  	if appPV := oam.GetPublishVersion(app); appPV == publishVersion {
    78  		return nil, nil, ErrPublishVersionNotChange
    79  	}
    80  
    81  	if app != nil && app.Status.LatestRevision != nil && app.Status.LatestRevision.Name == revisionName {
    82  		return nil, nil, ErrRevisionNotChange
    83  	}
    84  
    85  	// freeze the application
    86  	appKey := client.ObjectKeyFromObject(app)
    87  	controllerRequirement, err := FreezeApplication(ctx, cli, app, func() {
    88  		app.Spec = matchedRev.Spec.Application.Spec
    89  		oam.SetPublishVersion(app, publishVersion)
    90  	})
    91  	if err != nil {
    92  		return nil, nil, errors.Wrapf(err, "failed to freeze application %s before update", appKey)
    93  	}
    94  
    95  	defer func() {
    96  		// unfreeze application
    97  		if err = UnfreezeApplication(ctx, cli, app, nil, controllerRequirement); err != nil {
    98  			klog.Errorf("failed to unfreeze application %s after update:%s", appKey, err.Error())
    99  		}
   100  	}()
   101  
   102  	// create new revision based on the matched revision
   103  	revName, revisionNum := utils.GetAppNextRevision(app)
   104  	matchedRev.Name = revName
   105  	oam.SetPublishVersion(matchedRev, publishVersion)
   106  	obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(matchedRev)
   107  	if err != nil {
   108  		return nil, nil, err
   109  	}
   110  	un := &unstructured.Unstructured{Object: obj}
   111  	component.ClearRefObjectForDispatch(un)
   112  	if err = cli.Create(revisionCtx, un); err != nil {
   113  		return nil, nil, errors.Wrapf(err, "failed to update application %s to create new revision %s", appKey, revName)
   114  	}
   115  
   116  	// update application status to point to the new revision
   117  	if err = retry.RetryOnConflict(retry.DefaultBackoff, func() error {
   118  		if err = cli.Get(ctx, appKey, app); err != nil {
   119  			return err
   120  		}
   121  		app.Status = apicommon.AppStatus{
   122  			LatestRevision: &apicommon.Revision{Name: revName, Revision: revisionNum, RevisionHash: matchedRev.GetLabels()[oam.LabelAppRevisionHash]},
   123  		}
   124  		return cli.Status().Update(ctx, app)
   125  	}); err != nil {
   126  		if delErr := cli.Delete(revisionCtx, un); delErr != nil {
   127  			klog.Warningf("failed to clear the new revision after failed to rollback application:%s:%s", appName, delErr.Error())
   128  		}
   129  		return nil, nil, errors.Wrapf(err, "failed to update application %s to use new revision %s", appKey, revName)
   130  	}
   131  	return matchedRev, app, nil
   132  }
   133  
   134  // FreezeApplication freeze application to disable the reconciling process for it
   135  func FreezeApplication(ctx context.Context, cli client.Client, app *v1beta1.Application, mutate func()) (string, error) {
   136  	return oam.GetControllerRequirement(app), _updateApplicationWithControllerRequirement(ctx, cli, app, mutate, "Disabled")
   137  }
   138  
   139  // UnfreezeApplication unfreeze application to enable the reconciling process for it
   140  func UnfreezeApplication(ctx context.Context, cli client.Client, app *v1beta1.Application, mutate func(), originalControllerRequirement string) error {
   141  	return _updateApplicationWithControllerRequirement(ctx, cli, app, mutate, originalControllerRequirement)
   142  }
   143  
   144  func _updateApplicationWithControllerRequirement(ctx context.Context, cli client.Client, app *v1beta1.Application, mutate func(), controllerRequirement string) error {
   145  	appKey := client.ObjectKeyFromObject(app)
   146  	return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
   147  		if err := cli.Get(ctx, appKey, app); err != nil {
   148  			return err
   149  		}
   150  		oam.SetControllerRequirement(app, controllerRequirement)
   151  		if mutate != nil {
   152  			mutate()
   153  		}
   154  		return cli.Update(ctx, app)
   155  	})
   156  }