Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP : Add tkc cluster annotation on Supervisor PVC objects #3071

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(common.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
65 changes: 65 additions & 0 deletions pkg/syncer/pvcsi_fullsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package syncer
import (
"context"
"encoding/json"
"fmt"
"reflect"
"time"

Expand Down Expand Up @@ -150,6 +151,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 +364,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] = metadataSyncer.configInfo.Cfg.GC.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