From 335fc06971f09bf737d72a1b7c9c2c2ba418af0b Mon Sep 17 00:00:00 2001 From: Chethan Venkatesh Date: Fri, 27 Sep 2024 15:54:06 -0700 Subject: [PATCH] Add guest cluster labels on Supervisor PVC objects --- pkg/csi/service/wcpguest/controller.go | 12 +++-- pkg/csi/service/wcpguest/controller_helper.go | 3 +- pkg/syncer/pvcsi_fullsync.go | 49 +++++++++++++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/pkg/csi/service/wcpguest/controller.go b/pkg/csi/service/wcpguest/controller.go index e8c18139b1..4c84332717 100644 --- a/pkg/csi/service/wcpguest/controller.go +++ b/pkg/csi/service/wcpguest/controller.go @@ -81,6 +81,8 @@ type controller struct { vmWatcher *cache.ListWatch supervisorNamespace string tanzukubernetesClusterUID string + tanzukubernetesClusterName string + guestClusterDist string } // New creates a CNS controller @@ -99,6 +101,8 @@ func (c *controller) Init(config *commonconfig.Config, version string) error { return err } c.tanzukubernetesClusterUID = config.GC.TanzuKubernetesClusterUID + c.tanzukubernetesClusterName = config.GC.TanzuKubernetesClusterName + c.guestClusterDist = config.GC.ClusterDistribution c.restClientConfig = k8s.GetRestClientConfigForSupervisor(ctx, config.GC.Endpoint, config.GC.Port) c.supervisorClient, err = k8s.NewSupervisorClient(ctx, c.restClientConfig) if err != nil { @@ -311,13 +315,15 @@ func (c *controller) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequ if err != nil { if errors.IsNotFound(err) { diskSize := strconv.FormatInt(volSizeMB, 10) + "Mi" - var annotations map[string]string + labels := make(map[string]string) + annotations := make(map[string]string) + key := fmt.Sprintf("%s/%s", c.tanzukubernetesClusterName, c.guestClusterDist) + labels[key] = c.tanzukubernetesClusterUID if commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.TKGsHA) && req.AccessibilityRequirements != nil && (commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.WorkloadDomainIsolationFSS) || !isFileVolumeRequest) { // Generate volume topology requirement annotation. - annotations = make(map[string]string) topologyAnnotation, err := generateGuestClusterRequestedTopologyJSON(req.AccessibilityRequirements.Preferred) if err != nil { msg := fmt.Sprintf("failed to generate accessibility topology for pvc with name: %s "+ @@ -328,7 +334,7 @@ func (c *controller) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequ annotations[common.AnnGuestClusterRequestedTopology] = topologyAnnotation } claim := getPersistentVolumeClaimSpecWithStorageClass(supervisorPVCName, c.supervisorNamespace, - diskSize, supervisorStorageClass, getAccessMode(accessMode), annotations, volumeSnapshotName) + diskSize, supervisorStorageClass, getAccessMode(accessMode), annotations, labels, volumeSnapshotName) log.Debugf("PVC claim spec is %+v", spew.Sdump(claim)) pvc, err = c.supervisorClient.CoreV1().PersistentVolumeClaims(c.supervisorNamespace).Create( ctx, claim, metav1.CreateOptions{}) diff --git a/pkg/csi/service/wcpguest/controller_helper.go b/pkg/csi/service/wcpguest/controller_helper.go index 8baa15092b..400ce3111f 100644 --- a/pkg/csi/service/wcpguest/controller_helper.go +++ b/pkg/csi/service/wcpguest/controller_helper.go @@ -213,12 +213,13 @@ func getAccessMode(accessMode csi.VolumeCapability_AccessMode_Mode) v1.Persisten // getPersistentVolumeClaimSpecWithStorageClass return the PersistentVolumeClaim spec with specified storage class func getPersistentVolumeClaimSpecWithStorageClass(pvcName string, namespace string, diskSize string, storageClassName string, pvcAccessMode v1.PersistentVolumeAccessMode, annotations map[string]string, - volumeSnapshotName string) *v1.PersistentVolumeClaim { + labels map[string]string, volumeSnapshotName string) *v1.PersistentVolumeClaim { claim := &v1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{ Name: pvcName, Namespace: namespace, Annotations: annotations, + Labels: labels, }, Spec: v1.PersistentVolumeClaimSpec{ AccessModes: []v1.PersistentVolumeAccessMode{ diff --git a/pkg/syncer/pvcsi_fullsync.go b/pkg/syncer/pvcsi_fullsync.go index 369b37e240..d7670dae7b 100644 --- a/pkg/syncer/pvcsi_fullsync.go +++ b/pkg/syncer/pvcsi_fullsync.go @@ -19,6 +19,7 @@ package syncer import ( "context" "encoding/json" + "fmt" "reflect" "time" @@ -150,6 +151,14 @@ func PvcsiFullSync(ctx context.Context, metadataSyncer *metadataSyncInformer) er } } + // Set csi.vsphere-volume labels on the Supervisor PVC which is + // requested from TKC Cluster + err = setGuestClusterDetailsOnSupervisorPVC(ctx, metadataSyncer, supervisorNamespace) + if err != nil { + log.Errorf("FullSync: Failed to set Guest Cluster labels on SupervisorPVC. Err: %v", err) + return err + } + log.Infof("FullSync: End") return nil } @@ -355,6 +364,46 @@ func GenerateVolumeNodeAffinity(accessibleTopology []*csi.Topology) *v1.VolumeNo } } +func setGuestClusterDetailsOnSupervisorPVC(ctx context.Context, metadataSyncer *metadataSyncInformer, + supervisorNamespace string) error { + log := logger.GetLogger(ctx) + log.Debugf("FullSync: Querying guest cluster API server for all PV objects.") + pvList, err := getPVsInBoundAvailableOrReleased(ctx, metadataSyncer) + if err != nil { + log.Errorf("FullSync: Failed to get PVs from guest cluster. Err: %v", err) + return err + } + for _, pv := range pvList { + if pv.Spec.ClaimRef != nil && pv.Status.Phase == v1.VolumeBound { + svPVC, err := metadataSyncer.supervisorClient.CoreV1().PersistentVolumeClaims(supervisorNamespace). + Get(ctx, pv.Spec.CSI.VolumeHandle, metav1.GetOptions{}) + if err != nil { + msg := fmt.Sprintf("failed to retrieve supervisor PVC %q in %q namespace. Error: %+v", + pv.Spec.CSI.VolumeHandle, supervisorNamespace, err) + log.Error(msg) + continue + } + key := fmt.Sprintf("%s/%s", metadataSyncer.configInfo.Cfg.GC.TanzuKubernetesClusterName, + metadataSyncer.configInfo.Cfg.GC.ClusterDistribution) + if _, ok := svPVC.Labels[key]; !ok { + if svPVC.Labels == nil { + svPVC.Labels = make(map[string]string) + } + svPVC.Labels[key] = metadataSyncer.configInfo.Cfg.GC.TanzuKubernetesClusterUID + _, err = metadataSyncer.supervisorClient.CoreV1().PersistentVolumeClaims(supervisorNamespace).Update( + ctx, svPVC, metav1.UpdateOptions{}) + if err != nil { + msg := fmt.Sprintf("failed to update supervisor PVC: %q with guest cluster labels in %q namespace. Error: %+v", + pv.Spec.CSI.VolumeHandle, supervisorNamespace, err) + log.Error(msg) + continue + } + } + } + } + return nil +} + // compareCnsVolumeMetadatas compares input cnsvolumemetadata objects // and returns false if their labels are not deeply equal. func compareCnsVolumeMetadatas(guestObject *cnsvolumemetadatav1alpha1.CnsVolumeMetadataSpec,