-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
controllers: new controller for maintenance mode
Signed-off-by: Rewant Soni <[email protected]>
- Loading branch information
1 parent
7190d9f
commit 30c26d7
Showing
12 changed files
with
333 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"time" | ||
|
||
"github.com/red-hat-storage/ocs-client-operator/api/v1alpha1" | ||
|
||
"github.com/go-logr/logr" | ||
ramenv1alpha1 "github.com/ramendr/ramen/api/v1alpha1" | ||
providerclient "github.com/red-hat-storage/ocs-operator/services/provider/api/v4/client" | ||
storagev1 "k8s.io/api/storage/v1" | ||
kerrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/builder" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
const ( | ||
maintenanceModeFinalizer = "ocs-client-operator.ocs.openshift.io/maintenance-mode" | ||
ramenReplicationIDLabel = "ramendr.openshift.io/replicationID" | ||
MaintenanceModeCRDName = "maintenancemodes.ramendr.openshift.io" | ||
) | ||
|
||
// MaintenanceModeReconciler reconciles a ClusterVersion object | ||
type MaintenanceModeReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
|
||
log logr.Logger | ||
ctx context.Context | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *MaintenanceModeReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
generationChangePredicate := predicate.GenerationChangedPredicate{} | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&ramenv1alpha1.MaintenanceMode{}, builder.WithPredicates(generationChangePredicate)). | ||
Watches( | ||
&storagev1.StorageClass{}, | ||
&handler.EnqueueRequestForObject{}, | ||
builder.WithPredicates(generationChangePredicate), | ||
). | ||
Watches( | ||
&v1alpha1.StorageClient{}, | ||
&handler.EnqueueRequestForObject{}, | ||
builder.WithPredicates(generationChangePredicate), | ||
). | ||
Complete(r) | ||
} | ||
|
||
//+kubebuilder:rbac:groups=ramendr.openshift.io,resources=maintenancemodes,verbs=get;list;update;create;watch;delete | ||
//+kubebuilder:rbac:groups=ramendr.openshift.io,resources=maintenancemodes/status,verbs=get;update;patch | ||
//+kubebuilder:rbac:groups=ramendr.openshift.io,resources=maintenancemodes/finalizers,verbs=update | ||
//+kubebuilder:rbac:groups=ocs.openshift.io,resources=storageclients,verbs=get;list;watch | ||
//+kubebuilder:rbac:groups=storage.k8s.io,resources=storageclasses,verbs=get;list;watch | ||
|
||
func (r *MaintenanceModeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
r.ctx = ctx | ||
r.log = log.FromContext(ctx, "MaintenanceMode", req) | ||
r.log.Info("Reconciling MaintenanceMode") | ||
|
||
maintenanceMode := &ramenv1alpha1.MaintenanceMode{} | ||
maintenanceMode.Name = req.Name | ||
if err := r.get(maintenanceMode); err != nil { | ||
if kerrors.IsNotFound(err) { | ||
r.log.Info("Maintenance Mode resource not found. Ignoring since object might be deleted.") | ||
return reconcile.Result{}, nil | ||
} | ||
r.log.Error(err, "failed to get the Maintenance Mode") | ||
return reconcile.Result{}, err | ||
} | ||
|
||
// find the storageClass with the targetID | ||
storageClassList := &storagev1.StorageClassList{} | ||
err := r.list(storageClassList, client.MatchingLabels{ramenReplicationIDLabel: maintenanceMode.Spec.TargetID}) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
if len(storageClassList.Items) != 1 { | ||
return ctrl.Result{}, fmt.Errorf("failed to find storageClass for maintenance mode") | ||
} | ||
|
||
storageClass := &storageClassList.Items[0] | ||
|
||
// find the storageClient | ||
// TODO: when storageClass is owned by storageClient, move to find the Client by ownerRef | ||
storageClient := &v1alpha1.StorageClient{} | ||
storageClient.Name = storageClass.GetLabels()[storageClientLabel] | ||
err = r.get(storageClient) | ||
if err != nil { | ||
return ctrl.Result{}, fmt.Errorf("failed to get the storage client: %v", err) | ||
} | ||
|
||
result, reconcileErr := r.reconcilePhases(maintenanceMode, storageClient) | ||
|
||
// Apply status changes to the StorageClient | ||
statusErr := r.Client.Status().Update(ctx, maintenanceMode) | ||
if statusErr != nil { | ||
r.log.Error(statusErr, "Failed to update MaintenanceMode status.") | ||
} | ||
if reconcileErr != nil { | ||
err = reconcileErr | ||
} else if statusErr != nil { | ||
err = statusErr | ||
} | ||
return result, err | ||
} | ||
|
||
func (r *MaintenanceModeReconciler) reconcilePhases(maintenanceMode *ramenv1alpha1.MaintenanceMode, storageClient *v1alpha1.StorageClient) (ctrl.Result, error) { | ||
|
||
// ensure finalizer | ||
if controllerutil.AddFinalizer(maintenanceMode, maintenanceModeFinalizer) { | ||
r.log.Info("finalizer missing on the Maintenance Mode resource, adding...") | ||
if err := r.Client.Update(r.ctx, maintenanceMode); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
|
||
if !storageClient.Status.InMaintenanceMode { | ||
providerClient, err := providerclient.NewProviderClient( | ||
r.ctx, | ||
storageClient.Spec.StorageProviderEndpoint, | ||
10*time.Second, | ||
) | ||
if err != nil { | ||
return reconcile.Result{}, fmt.Errorf( | ||
"failed to create provider client with endpoint %v: %v", | ||
storageClient.Spec.StorageProviderEndpoint, | ||
err, | ||
) | ||
} | ||
// Close client-side connections. | ||
defer providerClient.Close() | ||
|
||
_, err = providerClient.RequestMaintenanceMode(r.ctx, storageClient.Status.ConsumerID, true) | ||
if err != nil { | ||
return ctrl.Result{}, fmt.Errorf("failed to Request maintenance mode: %v", err) | ||
} | ||
maintenanceMode.Status.State = ramenv1alpha1.MModeStateUnknown | ||
return ctrl.Result{Requeue: true}, nil | ||
} | ||
|
||
// Ramen reads the State and Conditions in order to determine that the MaintenanceMode is Completed | ||
maintenanceMode.Status.State = ramenv1alpha1.MModeStateCompleted | ||
maintenanceMode.Status.ObservedGeneration = maintenanceMode.Generation | ||
meta.SetStatusCondition(&maintenanceMode.Status.Conditions, | ||
metav1.Condition{ | ||
Type: string(ramenv1alpha1.MModeConditionFailoverActivated), | ||
ObservedGeneration: maintenanceMode.Generation, | ||
Reason: string(ramenv1alpha1.MModeStateCompleted), | ||
Status: metav1.ConditionTrue, | ||
}, | ||
) | ||
|
||
if !maintenanceMode.GetDeletionTimestamp().IsZero() { | ||
// deletion phase | ||
if storageClient.Statusgit.InMaintenanceMode { | ||
providerClient, err := providerclient.NewProviderClient( | ||
r.ctx, | ||
storageClient.Spec.StorageProviderEndpoint, | ||
10*time.Second, | ||
) | ||
if err != nil { | ||
return reconcile.Result{}, fmt.Errorf( | ||
"failed to create provider client with endpoint %v: %v", | ||
storageClient.Spec.StorageProviderEndpoint, | ||
err, | ||
) | ||
} | ||
// Close client-side connections. | ||
defer providerClient.Close() | ||
|
||
_, err = providerClient.RequestMaintenanceMode(r.ctx, storageClient.Status.ConsumerID, false) | ||
if err != nil { | ||
return ctrl.Result{}, fmt.Errorf("failed to disable maintenance mode: %v", err) | ||
} | ||
return ctrl.Result{Requeue: true}, nil | ||
} | ||
|
||
// remove finalizer | ||
if controllerutil.RemoveFinalizer(maintenanceMode, maintenanceModeFinalizer) { | ||
if err := r.Client.Update(r.ctx, maintenanceMode); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
r.log.Info("finalizer removed successfully") | ||
} | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
func (r *MaintenanceModeReconciler) get(obj client.Object, opts ...client.GetOption) error { | ||
return r.Get(r.ctx, client.ObjectKeyFromObject(obj), obj, opts...) | ||
} | ||
|
||
func (r *MaintenanceModeReconciler) list(obj client.ObjectList, opts ...client.ListOption) error { | ||
return r.List(r.ctx, obj, opts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.