Skip to content

Commit

Permalink
Add tkc cluster annotation on Supervisor PVC objects
Browse files Browse the repository at this point in the history
  • Loading branch information
chethanv28 committed Sep 28, 2024
1 parent 94709a7 commit d971931
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/csi/service/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ const (
// AnnVolumeAccessibleTopology is the annotation set by the supervisor cluster on PVC
AnnVolumeAccessibleTopology = "csi.vsphere.volume-accessible-topology"

// AnnTanzuGuestClusterOwner is the annotation key to set TanzuClusterID as value
// on the PVC in supervisor cluster
AnnTanzuGuestClusterOwner = "csi.vsphere.tanzu-kubernetes-cluster"

// PVtoBackingDiskObjectIdSupportedVCenterMajor is the minimum major version of vCenter
// on which PV to BackingDiskObjectId mapping feature is supported.
PVtoBackingDiskObjectIdSupportedVCenterMajor int = 7
Expand Down
28 changes: 26 additions & 2 deletions pkg/csi/service/wcpguest/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type controller struct {
vmWatcher *cache.ListWatch
supervisorNamespace string
tanzukubernetesClusterUID string
tanzukubernetesClusterName string
}

// New creates a CNS controller
Expand All @@ -99,6 +100,7 @@ func (c *controller) Init(config *commonconfig.Config, version string) error {
return err
}
c.tanzukubernetesClusterUID = config.GC.TanzuKubernetesClusterUID
c.tanzukubernetesClusterName = config.GC.TanzuKubernetesClusterName
c.restClientConfig = k8s.GetRestClientConfigForSupervisor(ctx, config.GC.Endpoint, config.GC.Port)
c.supervisorClient, err = k8s.NewSupervisorClient(ctx, c.restClientConfig)
if err != nil {
Expand Down Expand Up @@ -311,13 +313,35 @@ 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
annotations := make(map[string]string)
//
// Function to generate annotation key with an index if it already exists
addAnnotationWithIndex := func(prefix, uid string, annotations map[string]string) {
index := 1
key := fmt.Sprintf("%s-%d", prefix, index)

// Check if key with current index exists, and increment until it's unique
for {
if _, exists := annotations[key]; !exists {
break
}
index++
key = fmt.Sprintf("%s-%d", prefix, index)
}

// Assign the value to the unique key
annotations[key] = c.tanzukubernetesClusterUID
}

// Adding the annotation with index check
annotationKey := c.tanzukubernetesClusterName + c.tanzukubernetesClusterUID
addAnnotationWithIndex(commmon.AnnTanzuGuestClusterOwner, annotationKey, annotations)

if commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.TKGsHA) &&
req.AccessibilityRequirements != nil &&
(commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.WorkloadDomainIsolation) ||
!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 "+
Expand Down
66 changes: 66 additions & 0 deletions pkg/syncer/pvcsi_fullsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package syncer

import (
"context"
"fmt"
"encoding/json"
"reflect"
"time"

"github.com/container-storage-interface/spec/lib/go/csi"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/strategicpatch"
Expand Down Expand Up @@ -150,6 +152,14 @@ func PvcsiFullSync(ctx context.Context, metadataSyncer *metadataSyncInformer) er
}
}

// Set csi.vsphere.tanzu-kubernetes-cluster annotation on the Supervisor PVC which is
// requested from TKC Cluster
err = setTanzuClusterIDOnSupervisorPVC(ctx, metadataSyncer, supervisorNamespace)
if err != nil {
log.Errorf("FullSync: Failed to create CnsVolumeMetadataList from guest cluster. Err: %v", err)
return err
}

log.Infof("FullSync: End")
return nil
}
Expand Down Expand Up @@ -355,6 +365,62 @@ func GenerateVolumeNodeAffinity(accessibleTopology []*csi.Topology) *v1.VolumeNo
}
}

func setTanzuClusterIDOnSupervisorPVC(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
}
if svPVC.Annotations[common.AnnTanzuGuestClusterOwner] != "" {
// Function to generate annotation key with an index if it already exists
addAnnotationWithIndex := func(prefix, uid string, annotations map[string]string) {
index := 1
key := fmt.Sprintf("%s-%d", prefix, index)

// Check if key with current index exists, and increment until it's unique
for {
if _, exists := annotations[key]; !exists {
break
}
index++
key = fmt.Sprintf("%s-%d", prefix, index)
}

// Assign the value to the unique key
annotations[key] = c.tanzukubernetesClusterUID
}
// Adding the annotation with index check
annotationKey := metadataSyncer.configInfo.Cfg.GC.TanzuKubernetesClusterName
+ metadataSyncer.configInfo.Cfg.GC.TanzuKubernetesClusterUID

addAnnotationWithIndex(common.AnnTanzuGuestClusterOwner, annotationKey, svPVC.Annotations)
_, err = metadataSyncer.supervisorClient.CoreV1().PersistentVolumeClaims(supervisorNamespace).Update(
ctx, svPVC, metav1.UpdateOptions{})
if err != nil {
msg := fmt.Sprintf("failed to update supervisor PVC: %q with annoation %q in %q namespace. Error: %+v",
pv.Spec.CSI.VolumeHandle, common.AnnTanzuGuestClusterOwner, 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,
Expand Down

0 comments on commit d971931

Please sign in to comment.