-
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
10c81ce
commit e3b4886
Showing
10 changed files
with
278 additions
and
3 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
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,176 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"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" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"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/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
const ( | ||
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).Named("Maintenance Mode"). | ||
Watches( | ||
&ramenv1alpha1.MaintenanceMode{}, | ||
&handler.EnqueueRequestForObject{}, | ||
builder.WithPredicates(generationChangePredicate), | ||
). | ||
Watches( | ||
&v1alpha1.StorageClaim{}, | ||
&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=ocs.openshift.io,resources=storageclients;storageclaims,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") | ||
|
||
mapClientNameToObj := map[string]*v1alpha1.StorageClient{} | ||
|
||
maintenanceModes := &ramenv1alpha1.MaintenanceModeList{} | ||
if err := r.list(maintenanceModes); err != nil { | ||
r.log.Error(err, "failed to list the Maintenance Modes") | ||
return reconcile.Result{}, err | ||
} | ||
|
||
for i := range maintenanceModes.Items { | ||
mm := &maintenanceModes.Items[i] | ||
sc := &v1alpha1.StorageClaim{} | ||
// MMode's TargetID is replicationID, which in our case is storageClaim name | ||
sc.Name = mm.Spec.TargetID | ||
err := r.get(sc) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
clientName := sc.Spec.StorageClient | ||
if mapClientNameToObj[clientName] == nil { | ||
storageClient := &v1alpha1.StorageClient{} | ||
storageClient.Name = clientName | ||
err := r.get(storageClient) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
mapClientNameToObj[clientName] = storageClient | ||
} | ||
if mapClientNameToObj[clientName].Status.InMaintenanceMode { | ||
err := r.updateStatusCompletedForMM(mm) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
} | ||
|
||
storageClients := &v1alpha1.StorageClientList{} | ||
if err := r.list(storageClients); err != nil { | ||
r.log.Error(err, "failed to list the Storage Clients") | ||
return reconcile.Result{}, err | ||
} | ||
|
||
for i := range storageClients.Items { | ||
storageClient := &storageClients.Items[i] | ||
if mapClientNameToObj[storageClient.Name] != nil && !storageClient.Status.InMaintenanceMode { | ||
if err := r.sendRequest(storageClient, true); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} else if mapClientNameToObj[storageClient.Name] == nil && storageClient.Status.InMaintenanceMode { | ||
if err := r.sendRequest(storageClient, false); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
func (r *MaintenanceModeReconciler) sendRequest(storageClient *v1alpha1.StorageClient, enable bool) error { | ||
providerClient, err := providerclient.NewProviderClient( | ||
r.ctx, | ||
storageClient.Spec.StorageProviderEndpoint, | ||
10*time.Second, | ||
) | ||
if err != nil { | ||
return 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, enable) | ||
if err != nil { | ||
return fmt.Errorf("failed to Request maintenance mode: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (r *MaintenanceModeReconciler) updateStatusCompletedForMM(maintenanceMode *ramenv1alpha1.MaintenanceMode) error { | ||
// 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, | ||
}, | ||
) | ||
|
||
statusErr := r.Client.Status().Update(r.ctx, maintenanceMode) | ||
if statusErr != nil { | ||
r.log.Error(statusErr, "Failed to update MaintenanceMode status", "MaintenanceMode", maintenanceMode.Name) | ||
return statusErr | ||
} | ||
return nil | ||
} | ||
|
||
func (r *MaintenanceModeReconciler) list(obj client.ObjectList, opts ...client.ListOption) error { | ||
return r.List(r.ctx, obj, opts...) | ||
} | ||
|
||
func (r *MaintenanceModeReconciler) get(obj client.Object, opts ...client.GetOption) error { | ||
return r.Get(r.ctx, client.ObjectKeyFromObject(obj), 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
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
2 changes: 2 additions & 0 deletions
2
vendor/github.com/red-hat-storage/ocs-client-operator/api/v1alpha1/storageclient_types.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.