github.com/verrazzano/verrazzano@v1.7.1/platform-operator/controllers/module/component-handler/upgrade/upgrade_handler.go (about)

     1  // Copyright (c) 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package upgrade
     5  
     6  import (
     7  	moduleapi "github.com/verrazzano/verrazzano-modules/module-operator/apis/platform/v1alpha1"
     8  	modulestatus "github.com/verrazzano/verrazzano-modules/module-operator/controllers/module/status"
     9  	"github.com/verrazzano/verrazzano-modules/pkg/controller/result"
    10  	"github.com/verrazzano/verrazzano-modules/pkg/controller/spi/handlerspi"
    11  	vzerrors "github.com/verrazzano/verrazzano/pkg/controller/errors"
    12  	vzapi "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1"
    13  	"github.com/verrazzano/verrazzano/platform-operator/constants"
    14  	"github.com/verrazzano/verrazzano/platform-operator/controllers/module/component-handler/common"
    15  )
    16  
    17  type ComponentHandler struct{}
    18  
    19  var (
    20  	_ handlerspi.StateMachineHandler = &ComponentHandler{}
    21  )
    22  
    23  func NewHandler() handlerspi.StateMachineHandler {
    24  	return &ComponentHandler{}
    25  }
    26  
    27  // GetWorkName returns the work name
    28  func (h ComponentHandler) GetWorkName() string {
    29  	return "upgrade"
    30  }
    31  
    32  // IsWorkNeeded returns true if upgrade is needed
    33  func (h ComponentHandler) IsWorkNeeded(ctx handlerspi.HandlerContext) (bool, result.Result) {
    34  	module := ctx.CR.(*moduleapi.Module)
    35  	needUpgrade := module.Spec.Version != "" && (module.Spec.Version != module.Status.LastSuccessfulVersion)
    36  	return needUpgrade, result.NewResult()
    37  }
    38  
    39  // CheckDependencies checks if the dependencies are ready
    40  func (h ComponentHandler) CheckDependencies(ctx handlerspi.HandlerContext) result.Result {
    41  	return common.CheckDependencies(ctx, string(constants.UpgradeOperation), moduleapi.ReadyReasonUpgradeStarted)
    42  }
    43  
    44  // PreWorkUpdateStatus updates the status for the pre-work state
    45  func (h ComponentHandler) PreWorkUpdateStatus(ctx handlerspi.HandlerContext) result.Result {
    46  	module := ctx.CR.(*moduleapi.Module)
    47  
    48  	// Update the Verrazzano component status
    49  	nsn, err := common.GetVerrazzanoNSN(ctx)
    50  	if err != nil {
    51  		return result.NewResultShortRequeueDelayWithError(err)
    52  	}
    53  	sd := common.StatusData{
    54  		Vznsn:    *nsn,
    55  		CondType: vzapi.CondUpgradeStarted,
    56  		CompName: module.Spec.ModuleName,
    57  		Msg:      string(vzapi.CondUpgradeStarted),
    58  		Ready:    false,
    59  	}
    60  	res := common.UpdateVerrazzanoComponentStatus(ctx, sd)
    61  	if res.ShouldRequeue() {
    62  		return res
    63  	}
    64  
    65  	// Update the module status
    66  	return modulestatus.UpdateReadyConditionReconciling(ctx, module, moduleapi.ReadyReasonUpgradeStarted)
    67  }
    68  
    69  // PreWork does the pre-work
    70  func (h ComponentHandler) PreWork(ctx handlerspi.HandlerContext) result.Result {
    71  	module := ctx.CR.(*moduleapi.Module)
    72  
    73  	compCtx, comp, err := common.GetComponentAndContext(ctx, constants.UpgradeOperation)
    74  	if err != nil {
    75  		return result.NewResultShortRequeueDelayWithError(err)
    76  	}
    77  
    78  	// Do the pre-upgrade
    79  	if err := comp.PreUpgrade(compCtx); err != nil {
    80  		if !vzerrors.IsRetryableError(err) {
    81  			modulestatus.UpdateReadyConditionFailed(ctx, module, moduleapi.ReadyReasonUpgradeStarted, err.Error())
    82  		}
    83  		return result.NewResultShortRequeueDelayWithError(err)
    84  	}
    85  	return result.NewResult()
    86  }
    87  
    88  // DoWorkUpdateStatus updates the status for the work state
    89  func (h ComponentHandler) DoWorkUpdateStatus(ctx handlerspi.HandlerContext) result.Result {
    90  	return result.NewResult()
    91  }
    92  
    93  // DoWork upgrades the module using Helm
    94  func (h ComponentHandler) DoWork(ctx handlerspi.HandlerContext) result.Result {
    95  	module := ctx.CR.(*moduleapi.Module)
    96  
    97  	compCtx, comp, err := common.GetComponentAndContext(ctx, constants.UpgradeOperation)
    98  	if err != nil {
    99  		return result.NewResultShortRequeueDelayWithError(err)
   100  	}
   101  
   102  	if err := comp.Upgrade(compCtx); err != nil {
   103  		if !vzerrors.IsRetryableError(err) {
   104  			modulestatus.UpdateReadyConditionFailed(ctx, module, moduleapi.ReadyReasonUpgradeStarted, err.Error())
   105  		}
   106  		return result.NewResultShortRequeueDelayWithError(err)
   107  	}
   108  	return result.NewResult()
   109  }
   110  
   111  // IsWorkDone indicates whether a module is upgraded and ready
   112  func (h ComponentHandler) IsWorkDone(ctx handlerspi.HandlerContext) (bool, result.Result) {
   113  	compCtx, comp, err := common.GetComponentAndContext(ctx, constants.UpgradeOperation)
   114  	if err != nil {
   115  		return false, result.NewResultShortRequeueDelayWithError(err)
   116  	}
   117  
   118  	ready := comp.IsReady(compCtx)
   119  	return ready, result.NewResult()
   120  }
   121  
   122  // PostWorkUpdateStatus does the post-work status update
   123  func (h ComponentHandler) PostWorkUpdateStatus(ctx handlerspi.HandlerContext) result.Result {
   124  	return result.NewResult()
   125  }
   126  
   127  // PostWork does installation pre-work
   128  func (h ComponentHandler) PostWork(ctx handlerspi.HandlerContext) result.Result {
   129  	module := ctx.CR.(*moduleapi.Module)
   130  
   131  	compCtx, comp, err := common.GetComponentAndContext(ctx, constants.UpgradeOperation)
   132  	if err != nil {
   133  		return result.NewResultShortRequeueDelayWithError(err)
   134  	}
   135  	if err := comp.PostUpgrade(compCtx); err != nil {
   136  		if !vzerrors.IsRetryableError(err) {
   137  			modulestatus.UpdateReadyConditionFailed(ctx, module, moduleapi.ReadyReasonUpgradeStarted, err.Error())
   138  		}
   139  		return result.NewResultShortRequeueDelayWithError(err)
   140  	}
   141  	return result.NewResult()
   142  }
   143  
   144  // WorkCompletedUpdateStatus updates the status to completed
   145  func (h ComponentHandler) WorkCompletedUpdateStatus(ctx handlerspi.HandlerContext) result.Result {
   146  	module := ctx.CR.(*moduleapi.Module)
   147  
   148  	// Update the Verrazzano component status
   149  	nsn, err := common.GetVerrazzanoNSN(ctx)
   150  	if err != nil {
   151  		return result.NewResultShortRequeueDelayWithError(err)
   152  	}
   153  	sd := common.StatusData{
   154  		Vznsn:       *nsn,
   155  		CondType:    vzapi.CondUpgradeComplete,
   156  		CompName:    module.Spec.ModuleName,
   157  		CompVersion: module.Spec.Version,
   158  		Msg:         string(vzapi.CondUpgradeComplete),
   159  		Ready:       true,
   160  	}
   161  	res := common.UpdateVerrazzanoComponentStatus(ctx, sd)
   162  	if res.ShouldRequeue() {
   163  		return res
   164  	}
   165  
   166  	// Update the module status
   167  	return modulestatus.UpdateReadyConditionSucceeded(ctx, module, moduleapi.ReadyReasonUpgradeSucceeded)
   168  }