Skip to content

Commit

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

Fixes: #KATA-2902

Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
  • Loading branch information
bpradipt committed Apr 23, 2024
1 parent 19c93b8 commit 0bc95ba
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
34 changes: 33 additions & 1 deletion controllers/mc_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 @@ -48,7 +53,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 @@ -78,8 +83,10 @@ func (r *KataConfigOpenShiftReconciler) newMCForCR(machinePool string) (*mcfgv1.
}

if r.FeatureGatesStatus[featuregates.LayeredImageDeployment] {
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 @@ -151,3 +158,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
}
59 changes: 52 additions & 7 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 @@ -930,17 +931,61 @@ func (r *KataConfigOpenShiftReconciler) processKataConfigInstallRequest() (ctrl.
wasMcJustCreated := false

// If featuregate LayeredImageDeployment is enabled then create the image based MC
// TBD: Handling switching from image based deployment to extension and vice versa
// If it's disabled, create the extension based MC

if r.FeatureGatesStatus[featuregates.LayeredImageDeployment] {
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
// When we first time enter this condition the PreviousMethod will be empty
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
}
// We don't handle any updates to the ImageBasedDeployment configuration
r.Log.Info("LayeredImageDeployment is enabled, no updates to the configuration will be handled")

} 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
// When we first time enter this condition the PreviousMethod will be empty
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 0bc95ba

Please sign in to comment.