-
Notifications
You must be signed in to change notification settings - Fork 122
/
clusterresource_controller.go
107 lines (86 loc) · 3.02 KB
/
clusterresource_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package kubeapiserver
import (
"reflect"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/cache"
clusterv1alpha2 "github.com/clusterpedia-io/api/cluster/v1alpha2"
clusterinformer "github.com/clusterpedia-io/clusterpedia/pkg/generated/informers/externalversions/cluster/v1alpha2"
clusterlister "github.com/clusterpedia-io/clusterpedia/pkg/generated/listers/cluster/v1alpha2"
"github.com/clusterpedia-io/clusterpedia/pkg/kubeapiserver/discovery"
)
type ClusterResourceController struct {
clusterLister clusterlister.PediaClusterLister
restManager *RESTManager
discoveryManager *discovery.DiscoveryManager
clusterresources map[string]ResourceInfoMap
}
func NewClusterResourceController(restManager *RESTManager, discoveryManager *discovery.DiscoveryManager, informer clusterinformer.PediaClusterInformer) *ClusterResourceController {
controller := &ClusterResourceController{
clusterLister: informer.Lister(),
restManager: restManager,
discoveryManager: discoveryManager,
clusterresources: make(map[string]ResourceInfoMap),
}
informer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
controller.updateClusterResources(obj.(*clusterv1alpha2.PediaCluster))
},
UpdateFunc: func(_, obj interface{}) {
cluster := obj.(*clusterv1alpha2.PediaCluster)
if !cluster.DeletionTimestamp.IsZero() {
controller.removeCluster(cluster.Name)
return
}
controller.updateClusterResources(obj.(*clusterv1alpha2.PediaCluster))
},
DeleteFunc: func(obj interface{}) {
clusterName, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
return
}
controller.removeCluster(clusterName)
},
})
return controller
}
func (c *ClusterResourceController) updateClusterResources(cluster *clusterv1alpha2.PediaCluster) {
resources := ResourceInfoMap{}
for _, groupResources := range cluster.Status.SyncResources {
for _, resource := range groupResources.Resources {
if len(resource.SyncConditions) == 0 {
continue
}
versions := sets.NewString()
for _, cond := range resource.SyncConditions {
versions.Insert(cond.Version)
}
gr := schema.GroupResource{Group: groupResources.Group, Resource: resource.Name}
resources[gr] = resourceInfo{
Namespaced: resource.Namespaced,
Kind: resource.Kind,
Versions: versions,
}
}
}
currentResources := c.clusterresources[cluster.Name]
if reflect.DeepEqual(resources, currentResources) {
return
}
discoveryapis := c.restManager.LoadResources(resources)
c.discoveryManager.SetClusterGroupResource(cluster.Name, discoveryapis)
c.clusterresources[cluster.Name] = resources
}
func (c *ClusterResourceController) removeCluster(name string) {
if _, ok := c.clusterresources[name]; !ok {
return
}
c.discoveryManager.RemoveCluster(name)
delete(c.clusterresources, name)
}
type resourceInfo struct {
Namespaced bool
Kind string
Versions sets.String
}
type ResourceInfoMap map[schema.GroupResource]resourceInfo