Skip to content

Commit

Permalink
Handle rollback of deployment based on featuregate
Browse files Browse the repository at this point in the history
This commit handles changes to the deployment method
extension to image or vice-versa

Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
  • Loading branch information
bpradipt committed Apr 9, 2024
1 parent d2589c1 commit 6469646
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
34 changes: 33 additions & 1 deletion controllers/imagedeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const (
image_mc_name = "50-enable-sandboxed-containers-image"
)

type DeploymentState struct {
PreviousMethod string
PreviousMcName string
}

// If the first return value is 'true' it means that the MC was just created
// by this call, 'false' means that it's already existed. As usual, the first
// return value is only valid if the second one is nil.
Expand All @@ -42,7 +47,7 @@ func (r *KataConfigOpenShiftReconciler) createImageMc(machinePool string) (bool,

err = r.Client.Create(context.TODO(), mc)
if err != nil {
r.Log.Error(err, "Failed to create a new MachineConfig ", "mc.Name", mc.Name)
r.Log.Info("Failed to create a new MachineConfig ", "mc.Name", mc.Name, "err", err)
return dummy, err
}
r.Log.Info("MachineConfig successfully created", "mc.Name", mc.Name)
Expand Down Expand Up @@ -72,8 +77,10 @@ func (r *KataConfigOpenShiftReconciler) newMCForCR(machinePool string) (*mcfgv1.
}

if r.FeatureGatesStatus[featuregates.ImageBasedDeployment] {
r.Log.Info("Creating MachineConfig for Custom Resource with image")
return r.newImageMCForCR(machinePool, icb)
} else {
r.Log.Info("Creating MachineConfig for Custom Resource with extension")
return r.newExtensionMCForCR(machinePool, icb)
}

Expand Down Expand Up @@ -145,3 +152,28 @@ func (r *KataConfigOpenShiftReconciler) newImageMCForCR(machinePool string, icb

return &mc, nil
}

// Delete the MachineConfig object
func (r *KataConfigOpenShiftReconciler) deleteMc(mcName string) error {
r.Log.Info("Deleting MachineConfig", "mc.Name", mcName)
mc := &mcfgv1.MachineConfig{}
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: mcName}, mc)
if err != nil {
if k8serrors.IsNotFound(err) || k8serrors.IsGone(err) {
r.Log.Info("MachineConfig not found. Most likely it was already deleted.")
return nil
}
r.Log.Info("failed to retrieve MachineConfig", "err", err)
return err
}

r.Log.Info("deleting MachineConfig")
err = r.Client.Delete(context.TODO(), mc)
if err != nil {
r.Log.Error(err, "Failed to delete MachineConfig")
return err
}

r.Log.Info("MachineConfig successfully deleted")
return nil
}
53 changes: 47 additions & 6 deletions controllers/openshift_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type KataConfigOpenShiftReconciler struct {
kataConfig *kataconfigurationv1.KataConfig
FeatureGates *featuregates.FeatureGates
FeatureGatesStatus featuregates.FeatureGateStatus
deploymentState DeploymentState
}

const (
Expand Down Expand Up @@ -934,14 +935,54 @@ func (r *KataConfigOpenShiftReconciler) processKataConfigInstallRequest() (ctrl.
// TBD: Handling switching from image based deployment to extension and vice versa

if r.FeatureGatesStatus[featuregates.ImageBasedDeployment] {
wasMcJustCreated, err = r.createImageMc(machinePool)
if err != nil {
return ctrl.Result{Requeue: true}, nil
r.Log.Info("PreviousMcName and PreviousMethod, ImageBasedDeployment=true",
"PreviousMcName", r.deploymentState.PreviousMcName, "PreviousMethod", r.deploymentState.PreviousMethod)
// Check if the previous deployment method was different
if r.deploymentState.PreviousMethod != "image" {
// Rollback the previous deployment
if r.deploymentState.PreviousMcName != "" {
// Delete the previous MachineConfig
err := r.deleteMc(r.deploymentState.PreviousMcName)
if err != nil {
r.Log.Info("Error deleting previous MachineConfig", "MachineConfig",
r.deploymentState.PreviousMcName, "err", err)
return ctrl.Result{Requeue: true}, err
}
}
wasMcJustCreated, err = r.createImageMc(machinePool)
if err != nil {
r.Log.Info("Error creating image MachineConfig for Image based deployment", "err", err)
return ctrl.Result{Requeue: true}, err
}
// Update deployment state
r.deploymentState.PreviousMethod = "image"
r.deploymentState.PreviousMcName = image_mc_name
}

} else {
wasMcJustCreated, err = r.createExtensionMc(machinePool)
if err != nil {
return ctrl.Result{Requeue: true}, nil
r.Log.Info("PreviousMcName and PreviousMethod, ImageBasedDeployment=false",
"PreviousMcName", r.deploymentState.PreviousMcName, "PreviousMethod", r.deploymentState.PreviousMethod)
// Check if the previous deployment method was different
if r.deploymentState.PreviousMethod != "extension" {
// Rollback the previous deployment
if r.deploymentState.PreviousMcName != "" {
// Delete the previous MachineConfig
err := r.deleteMc(r.deploymentState.PreviousMcName)
if err != nil {
r.Log.Info("Error deleting previous MachineConfig", "MachineConfig",
r.deploymentState.PreviousMcName, "err", err)
return ctrl.Result{Requeue: true}, err
}
}
wasMcJustCreated, err = r.createExtensionMc(machinePool)
if err != nil {
r.Log.Info("Error creating extension MachineConfig for Extension based deployment", "err", err)
return ctrl.Result{Requeue: true}, err
}

// Update deployment state
r.deploymentState.PreviousMethod = "extension"
r.deploymentState.PreviousMcName = extension_mc_name
}
}

Expand Down

0 comments on commit 6469646

Please sign in to comment.