44 "context"
55 "encoding/json"
66 "fmt"
7+ "log/slog"
78 "time"
89
910 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -12,13 +13,11 @@ import (
1213 ramenv1alpha1 "github.com/ramendr/ramen/api/v1alpha1"
1314 multiclusterv1alpha1 "github.com/red-hat-storage/odf-multicluster-orchestrator/api/v1alpha1"
1415 "github.com/red-hat-storage/odf-multicluster-orchestrator/controllers/utils"
15- "k8s.io/apimachinery/pkg/api/errors"
1616 k8serrors "k8s.io/apimachinery/pkg/api/errors"
1717 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1818 "k8s.io/apimachinery/pkg/runtime"
1919 "k8s.io/apimachinery/pkg/runtime/schema"
2020 "k8s.io/apimachinery/pkg/types"
21- "k8s.io/klog/v2"
2221 workv1 "open-cluster-management.io/api/work/v1"
2322 ctrl "sigs.k8s.io/controller-runtime"
2423 "sigs.k8s.io/controller-runtime/pkg/builder"
@@ -46,87 +45,103 @@ const (
4645type DRPolicyReconciler struct {
4746 HubClient client.Client
4847 Scheme * runtime.Scheme
48+ Logger * slog.Logger
4949}
5050
5151// SetupWithManager sets up the controller with the Manager.
5252func (r * DRPolicyReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
53+ r .Logger .Info ("Setting up DRPolicyReconciler with manager" )
54+
5355 dpPredicate := utils .ComposePredicates (predicate.GenerationChangedPredicate {})
56+
5457 return ctrl .NewControllerManagedBy (mgr ).
5558 For (& ramenv1alpha1.DRPolicy {}, builder .WithPredicates (dpPredicate )).
5659 Complete (r )
5760}
5861
5962func (r * DRPolicyReconciler ) getMirrorPeerForClusterSet (ctx context.Context , clusterSet []string ) (* multiclusterv1alpha1.MirrorPeer , error ) {
63+ logger := r .Logger .With ("method" , "getMirrorPeerForClusterSet" , "ClusterSet" , clusterSet )
64+
6065 var mpList multiclusterv1alpha1.MirrorPeerList
6166 err := r .HubClient .List (ctx , & mpList )
6267 if err != nil {
63- klog .Error ("could not list mirrorpeers on hub" )
68+ logger .Error ("Could not list MirrorPeers on hub" , "error" , err )
6469 return nil , err
6570 }
6671
6772 if len (mpList .Items ) == 0 {
68- klog .Info ("no mirrorpeers found on hub yet" )
73+ logger .Info ("No MirrorPeers found on hub yet" )
6974 return nil , k8serrors .NewNotFound (schema.GroupResource {Group : multiclusterv1alpha1 .GroupVersion .Group , Resource : "MirrorPeer" }, "MirrorPeerList" )
7075 }
76+
7177 for _ , mp := range mpList .Items {
7278 if (mp .Spec .Items [0 ].ClusterName == clusterSet [0 ] && mp .Spec .Items [1 ].ClusterName == clusterSet [1 ]) ||
7379 (mp .Spec .Items [1 ].ClusterName == clusterSet [0 ] && mp .Spec .Items [0 ].ClusterName == clusterSet [1 ]) {
74- klog . Infof ( "found mirrorpeer %q for drpolicy " , mp .Name )
80+ logger . Info ( "Found MirrorPeer for DRPolicy" , "MirrorPeerName " , mp .Name )
7581 return & mp , nil
7682 }
7783 }
7884
79- klog .Info ("could not find any mirrorpeer for drpolicy " )
85+ logger .Info ("Could not find any MirrorPeer for DRPolicy " )
8086 return nil , k8serrors .NewNotFound (schema.GroupResource {Group : multiclusterv1alpha1 .GroupVersion .Group , Resource : "MirrorPeer" }, fmt .Sprintf ("ClusterSet-%s-%s" , clusterSet [0 ], clusterSet [1 ]))
8187}
88+
8289func (r * DRPolicyReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
83- klog .Infof ("running DRPolicy reconciler on hub cluster" )
84- // Fetch DRPolicy for given Request
90+ r .Logger .Info ("Running DRPolicy reconciler on hub cluster" , "RequestNamespace" , req .Namespace , "RequestName" , req .Name )
91+
92+ // Fetch DRPolicy for the given request
8593 var drpolicy ramenv1alpha1.DRPolicy
8694 err := r .HubClient .Get (ctx , req .NamespacedName , & drpolicy )
8795 if err != nil {
88- if errors .IsNotFound (err ) {
89- klog . Info ("Could not find DRPolicy . Ignoring since object must have been deleted" )
96+ if k8serrors .IsNotFound (err ) {
97+ r . Logger . Info ("DRPolicy not found . Ignoring since the object must have been deleted" , "RequestNamespace" , req . Namespace , "RequestName" , req . Name )
9098 return ctrl.Result {}, nil
9199 }
92- klog . Error (err , "Failed to get DRPolicy" )
100+ r . Logger . Error ("Failed to get DRPolicy" , "error" , err , "RequestNamespace" , req . Namespace , "RequestName" , req . Name )
93101 return ctrl.Result {}, err
94102 }
95103
96- // find mirrorpeer for clusterset for the storagecluster namespaces
104+ // Find MirrorPeer for clusterset for the storagecluster namespaces
97105 mirrorPeer , err := r .getMirrorPeerForClusterSet (ctx , drpolicy .Spec .DRClusters )
98106 if err != nil {
99107 if k8serrors .IsNotFound (err ) {
108+ r .Logger .Info ("MirrorPeer not found. Requeuing" , "DRClusters" , drpolicy .Spec .DRClusters )
100109 return ctrl.Result {RequeueAfter : time .Second * 10 }, nil
101110 }
102- klog . Error ("error occurred while trying to fetch MirrorPeer for given DRPolicy" )
111+ r . Logger . Error ("Error occurred while trying to fetch MirrorPeer for given DRPolicy" , "error" , err )
103112 return ctrl.Result {}, err
104113 }
105114
106115 if mirrorPeer .Spec .Type == multiclusterv1alpha1 .Async {
107116 clusterFSIDs := make (map [string ]string )
108- klog . Infof ("Fetching clusterFSIDs " )
117+ r . Logger . Info ("Fetching cluster FSIDs " )
109118 err = r .fetchClusterFSIDs (ctx , mirrorPeer , clusterFSIDs )
110119 if err != nil {
111- if errors .IsNotFound (err ) {
120+ if k8serrors .IsNotFound (err ) {
121+ r .Logger .Info ("Cluster FSIDs not found, requeuing" )
112122 return ctrl.Result {RequeueAfter : 10 * time .Second }, nil
113123 }
114- return ctrl.Result {}, fmt .Errorf ("an unknown error occured while fetching the cluster fsids, retrying again: %v" , err )
124+ r .Logger .Error ("An unknown error occurred while fetching the cluster FSIDs, retrying" , "error" , err )
125+ return ctrl.Result {}, fmt .Errorf ("an unknown error occurred while fetching the cluster FSIDs, retrying: %v" , err )
115126 }
116127
117128 err = r .createOrUpdateManifestWorkForVRC (ctx , mirrorPeer , & drpolicy , clusterFSIDs )
118129 if err != nil {
130+ r .Logger .Error ("Failed to create VolumeReplicationClass via ManifestWork" , "error" , err )
119131 return ctrl.Result {}, fmt .Errorf ("failed to create VolumeReplicationClass via ManifestWork: %v" , err )
120132 }
121133 }
122134
135+ r .Logger .Info ("Successfully reconciled DRPolicy" , "RequestNamespace" , req .Namespace , "RequestName" , req .Name )
123136 return ctrl.Result {}, nil
124137}
125138
126139func (r * DRPolicyReconciler ) createOrUpdateManifestWorkForVRC (ctx context.Context , mp * multiclusterv1alpha1.MirrorPeer , dp * ramenv1alpha1.DRPolicy , clusterFSIDs map [string ]string ) error {
140+ logger := r .Logger .With ("method" , "createOrUpdateManifestWorkForVRC" , "DRPolicy" , dp .Name , "MirrorPeer" , mp .Name )
127141
128142 replicationId , err := utils .CreateUniqueReplicationId (clusterFSIDs )
129143 if err != nil {
144+ logger .Error ("Failed to create unique replication ID" , "error" , err )
130145 return err
131146 }
132147
@@ -144,9 +159,9 @@ func (r *DRPolicyReconciler) createOrUpdateManifestWorkForVRC(ctx context.Contex
144159
145160 switch {
146161 case err == nil :
147- klog . Infof ( "%s already exists. updating... " , manifestWorkName )
148- case ! errors .IsNotFound (err ):
149- klog .Error (err , "failed to get ManifestWork: %s" , manifestWorkName )
162+ logger . Info ( "ManifestWork already exists, updating" , "ManifestWorkName " , manifestWorkName )
163+ case ! k8serrors .IsNotFound (err ):
164+ logger .Error ("Failed to get ManifestWork" , "ManifestWorkName" , manifestWorkName , "error" , err )
150165 return err
151166 }
152167
@@ -162,7 +177,8 @@ func (r *DRPolicyReconciler) createOrUpdateManifestWorkForVRC(ctx context.Contex
162177 labels [fmt .Sprintf (RamenLabelTemplate , "maintenancemodes" )] = "Failover"
163178 vrc := replicationv1alpha1.VolumeReplicationClass {
164179 TypeMeta : metav1.TypeMeta {
165- Kind : "VolumeReplicationClass" , APIVersion : "replication.storage.openshift.io/v1alpha1" ,
180+ Kind : "VolumeReplicationClass" ,
181+ APIVersion : "replication.storage.openshift.io/v1alpha1" ,
166182 },
167183 ObjectMeta : metav1.ObjectMeta {
168184 Name : vrcName ,
@@ -179,6 +195,7 @@ func (r *DRPolicyReconciler) createOrUpdateManifestWorkForVRC(ctx context.Contex
179195
180196 objJson , err := json .Marshal (vrc )
181197 if err != nil {
198+ logger .Error ("Failed to marshal VolumeReplicationClass to JSON" , "VolumeReplicationClass" , vrcName , "error" , err )
182199 return fmt .Errorf ("failed to marshal %v to JSON, error %w" , vrc , err )
183200 }
184201
@@ -210,7 +227,7 @@ func (r *DRPolicyReconciler) createOrUpdateManifestWorkForVRC(ctx context.Contex
210227 mw := workv1.ManifestWork {
211228 ObjectMeta : metav1.ObjectMeta {
212229 Name : manifestWorkName ,
213- Namespace : pr .ClusterName , //target cluster
230+ Namespace : pr .ClusterName ,
214231 OwnerReferences : []metav1.OwnerReference {
215232 {
216233 Kind : dp .Kind ,
@@ -221,6 +238,7 @@ func (r *DRPolicyReconciler) createOrUpdateManifestWorkForVRC(ctx context.Contex
221238 },
222239 },
223240 }
241+
224242 _ , err = controllerutil .CreateOrUpdate (ctx , r .HubClient , & mw , func () error {
225243 mw .Spec = workv1.ManifestWorkSpec {
226244 Workload : workv1.ManifestsTemplate {
@@ -231,35 +249,44 @@ func (r *DRPolicyReconciler) createOrUpdateManifestWorkForVRC(ctx context.Contex
231249 })
232250
233251 if err != nil {
234- klog .Error (err , "failed to create/update ManifestWork: %s" , manifestWorkName )
252+ logger .Error ("Failed to create/update ManifestWork" , "ManifestWorkName" , manifestWorkName , "error" , err )
235253 return err
236254 }
237255
238- klog . Infof ("ManifestWork created for %s " , vrcName )
256+ logger . Info ("ManifestWork created/updated successfully" , "ManifestWorkName" , manifestWorkName , "VolumeReplicationClassName " , vrcName )
239257 }
240258
241259 return nil
242260}
243261
244262func (r * DRPolicyReconciler ) fetchClusterFSIDs (ctx context.Context , peer * multiclusterv1alpha1.MirrorPeer , clusterFSIDs map [string ]string ) error {
263+ logger := r .Logger .With ("method" , "fetchClusterFSIDs" , "MirrorPeer" , peer .Name )
264+
245265 for _ , pr := range peer .Spec .Items {
246266 rookSecretName := utils .GetSecretNameByPeerRef (pr )
247- klog .Info ("Fetching rook secret " , "Secret Name:" , rookSecretName )
267+ logger .Info ("Fetching rook secret" , "SecretName" , rookSecretName , "ClusterName" , pr .ClusterName )
268+
248269 hs , err := utils .FetchSecretWithName (ctx , r .HubClient , types.NamespacedName {Name : rookSecretName , Namespace : pr .ClusterName })
249270 if err != nil {
250- if errors .IsNotFound (err ) {
251- klog .Infof ("could not find secret %q. will attempt to fetch it again after a delay" , rookSecretName )
271+ if k8serrors .IsNotFound (err ) {
272+ logger .Info ("Secret not found, will attempt to fetch again after a delay" , "SecretName" , rookSecretName , "ClusterName" , pr .ClusterName )
273+ return err
252274 }
275+ logger .Error ("Failed to fetch rook secret" , "SecretName" , rookSecretName , "ClusterName" , pr .ClusterName , "error" , err )
253276 return err
254277 }
255- klog .Info ("Unmarshalling rook secret " , "Secret Name:" , rookSecretName )
278+
279+ logger .Info ("Unmarshalling rook secret" , "SecretName" , rookSecretName , "ClusterName" , pr .ClusterName )
256280 rt , err := utils .UnmarshalHubSecret (hs )
257281 if err != nil {
258- klog .Error (err , "Failed to unmarshal rook secret" , "Secret " , rookSecretName )
282+ logger .Error ("Failed to unmarshal rook secret" , "SecretName " , rookSecretName , "ClusterName" , pr . ClusterName , "error" , err )
259283 return err
260284 }
285+
261286 clusterFSIDs [pr .ClusterName ] = rt .FSID
287+ logger .Info ("Successfully fetched FSID for cluster" , "ClusterName" , pr .ClusterName , "FSID" , rt .FSID )
262288 }
263289
290+ logger .Info ("Successfully fetched all cluster FSIDs" , "MirrorPeer" , peer .Name )
264291 return nil
265292}
0 commit comments