Skip to content

Commit

Permalink
feat: support excluded sync resources
Browse files Browse the repository at this point in the history
Signed-off-by: KubeKyrie <shaolong.qin@daocloud.io>
  • Loading branch information
KubeKyrie committed Dec 14, 2023
1 parent 810c6c4 commit b83f1d1
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 9 deletions.
19 changes: 19 additions & 0 deletions kustomize/crds/cluster.clusterpedia.io_clustersyncresources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ spec:
type: object
spec:
properties:
excludedSyncResources:
items:
properties:
group:
type: string
resources:
items:
type: string
minItems: 1
type: array
versions:
items:
type: string
type: array
required:
- group
- resources
type: object
type: array
syncResources:
items:
properties:
Expand Down
19 changes: 19 additions & 0 deletions kustomize/crds/cluster.clusterpedia.io_pediaclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ spec:
certData:
format: byte
type: string
excludedSyncResources:
items:
properties:
group:
type: string
resources:
items:
type: string
minItems: 1
type: array
versions:
items:
type: string
type: array
required:
- group
- resources
type: object
type: array
keyData:
format: byte
type: string
Expand Down
26 changes: 26 additions & 0 deletions pkg/generated/openapi/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 16 additions & 6 deletions pkg/synchromanager/clustersynchro/cluster_synchro.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ type ClusterSynchro struct {
storageResourceVersions map[schema.GroupVersionResource]map[string]interface{}
storageResourceSynchros sync.Map

syncResources atomic.Value // []clusterv1alpha2.ClusterGroupResources
setSyncResourcesCh chan struct{}
resourceNegotiator *ResourceNegotiator
groupResourceStatus atomic.Value // *GroupResourceStatus
syncResources atomic.Value // []clusterv1alpha2.ClusterGroupResources
excludedSyncResources atomic.Value // []clusterv1alpha2.ClusterGroupResources
setSyncResourcesCh chan struct{}
resourceNegotiator *ResourceNegotiator
groupResourceStatus atomic.Value // *GroupResourceStatus

runningCondition atomic.Value // metav1.Condition
healthyCondition atomic.Value // metav1.Condition
Expand Down Expand Up @@ -278,9 +279,12 @@ func (s *ClusterSynchro) Shutdown(updateStatus bool) {
<-s.closed
}

func (s *ClusterSynchro) SetResources(syncResources []clusterv1alpha2.ClusterGroupResources, syncAllCustomResources bool) {
func (s *ClusterSynchro) SetResources(syncResources, excludedSyncResources []clusterv1alpha2.ClusterGroupResources, syncAllCustomResources bool) {
s.syncResources.Store(syncResources)
s.resourceNegotiator.SetSyncAllCustomResources(syncAllCustomResources)
if excludedSyncResources != nil {
s.excludedSyncResources.Store(excludedSyncResources)
}

s.resetSyncResources()
}
Expand Down Expand Up @@ -315,7 +319,13 @@ func (s *ClusterSynchro) refreshSyncResources() {
if syncResources == nil {
return
}
groupResourceStatus, storageResourceSyncConfigs := s.resourceNegotiator.NegotiateSyncResources(syncResources)

excludedSyncResources := make([]clusterv1alpha2.ClusterGroupResources, 0)
loadValue := s.excludedSyncResources.Load()
if loadValue != nil {
excludedSyncResources = loadValue.([]clusterv1alpha2.ClusterGroupResources)
}
groupResourceStatus, storageResourceSyncConfigs := s.resourceNegotiator.NegotiateSyncResources(syncResources, excludedSyncResources)

lastGroupResourceStatus := s.groupResourceStatus.Load().(*GroupResourceStatus)
deleted := groupResourceStatus.Merge(lastGroupResourceStatus)
Expand Down
38 changes: 36 additions & 2 deletions pkg/synchromanager/clustersynchro/resource_negotiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (negotiator *ResourceNegotiator) SetSyncAllCustomResources(sync bool) {
negotiator.syncAllCustomResources = sync
}

func (negotiator *ResourceNegotiator) NegotiateSyncResources(syncResources []clusterv1alpha2.ClusterGroupResources) (*GroupResourceStatus, map[schema.GroupVersionResource]syncConfig) {
func (negotiator *ResourceNegotiator) NegotiateSyncResources(syncResources, excludedSyncResources []clusterv1alpha2.ClusterGroupResources) (*GroupResourceStatus, map[schema.GroupVersionResource]syncConfig) {
var syncAllResources bool
var watchKubeVersion, watchAggregatorResourceTypes bool
for i, syncResource := range syncResources {
Expand Down Expand Up @@ -96,8 +96,24 @@ func (negotiator *ResourceNegotiator) NegotiateSyncResources(syncResources []clu
continue
}

// remove excludedSyncResources, groupResources.Versions or excludedGroupResources.Versions may be wildcard
availableVersions := supportedVersions
for _, excludedGroupResources := range excludedSyncResources {
excludedResources := sets.New(excludedGroupResources.Resources...)
if groupResources.Group != excludedGroupResources.Group {
continue
}
if excludedResources.Has("*") {
availableVersions = []string{}
break
}
if excludedResources.Has(resource) {
availableVersions = getNewAvailableVersions(availableVersions, excludedGroupResources.Versions)
}
}

syncGK := schema.GroupKind{Group: syncGR.Group, Kind: apiResource.Kind}
syncVersions, isLegacyResource, err := negotiateSyncVersions(syncGK, groupResources.Versions, supportedVersions)
syncVersions, isLegacyResource, err := negotiateSyncVersions(syncGK, groupResources.Versions, availableVersions)
if err != nil {
klog.InfoS("Skip resource sync", "cluster", negotiator.name, "resource", resource, "reason", err)
continue
Expand Down Expand Up @@ -412,3 +428,21 @@ func (set GVRSet) Insert(gvrs ...schema.GroupVersionResource) {
set[gvr] = struct{}{}
}
}

func getNewAvailableVersions(supportedVersions, excludedVersions []string) []string {
if len(excludedVersions) == 0 {
return []string{}
}
excludedVersionSet := sets.New(excludedVersions...)
if excludedVersionSet.Has("*") {
return []string{}
}

res := make([]string, 0)
for _, version := range supportedVersions {
if !excludedVersionSet.Has(version) {
res = append(res, version)
}
}
return res
}
4 changes: 3 additions & 1 deletion pkg/synchromanager/clustersynchro_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ func (manager *Manager) reconcileCluster(cluster *clusterv1alpha2.PediaCluster)

var warnMsg string
syncResources := cluster.Spec.SyncResources
excludedSyncResources := cluster.Spec.ExcludedSyncResources
if refName := cluster.Spec.SyncResourcesRefName; refName != "" {
if ref, err := manager.clusterSyncResourcesLister.Get(refName); err != nil {
if !apierrors.IsNotFound(err) {
Expand All @@ -354,6 +355,7 @@ func (manager *Manager) reconcileCluster(cluster *clusterv1alpha2.PediaCluster)
warnMsg = "Warning: sync resource ref is not found"
} else {
syncResources = append(syncResources, ref.Spec.SyncResources...)
excludedSyncResources = append(excludedSyncResources, ref.Spec.ExcludedSyncResources...)
}
}

Expand Down Expand Up @@ -439,7 +441,7 @@ func (manager *Manager) reconcileCluster(cluster *clusterv1alpha2.PediaCluster)
manager.synchrolock.Unlock()
}

synchro.SetResources(syncResources, cluster.Spec.SyncAllCustomResources)
synchro.SetResources(syncResources, excludedSyncResources, cluster.Spec.SyncAllCustomResources)
return controller.NoRequeueResult
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ type ClusterSpec struct {
// +required
SyncResources []ClusterGroupResources `json:"syncResources"`

// +optional
ExcludedSyncResources []ClusterGroupResources `json:"excludedSyncResources"`

// +optional
SyncAllCustomResources bool `json:"syncAllCustomResources,omitempty"`

Expand Down Expand Up @@ -242,6 +245,8 @@ type ClusterSyncResources struct {
type ClusterSyncResourcesSpec struct {
// +required
SyncResources []ClusterGroupResources `json:"syncResources"`
// +optional
ExcludedSyncResources []ClusterGroupResources `json:"excludedSyncResources"`
}

// +kubebuilder:object:root=true
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b83f1d1

Please sign in to comment.