-
Notifications
You must be signed in to change notification settings - Fork 216
/
controller.go
131 lines (111 loc) · 3.92 KB
/
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Package clustergroup provides a controller to update the ClusterGroup resource status.
package clustergroup
import (
"context"
"sort"
"github.com/sirupsen/logrus"
"github.com/rancher/fleet/internal/cmd/controller/summary"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
fleetcontrollers "github.com/rancher/fleet/pkg/generated/controllers/fleet.cattle.io/v1alpha1"
"github.com/rancher/wrangler/pkg/kv"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
type handler struct {
clusterGroupsCache fleetcontrollers.ClusterGroupCache
clusterGroups fleetcontrollers.ClusterGroupController
clusterCache fleetcontrollers.ClusterCache
clusters fleetcontrollers.ClusterController
}
func Register(ctx context.Context,
clusters fleetcontrollers.ClusterController,
clusterGroups fleetcontrollers.ClusterGroupController) {
h := &handler{
clusterGroupsCache: clusterGroups.Cache(),
clusterGroups: clusterGroups,
clusterCache: clusters.Cache(),
clusters: clusters,
}
fleetcontrollers.RegisterClusterGroupStatusHandler(ctx,
clusterGroups,
"Processed",
"cluster-group",
h.OnClusterGroup)
clusters.OnChange(ctx, "cluster-group-trigger", h.OnClusterChange)
}
func (h *handler) OnClusterChange(key string, cluster *fleet.Cluster) (*fleet.Cluster, error) {
if cluster == nil {
logrus.Debugf("Cluster '%s' was deleted, enqueue all cluster groups", key)
ns, _ := kv.Split(key, "/")
cgs, err := h.clusterGroupsCache.List(ns, labels.Everything())
if err != nil {
return nil, err
}
for _, cg := range cgs {
h.clusterGroups.Enqueue(cg.Namespace, cg.Name)
}
return cluster, nil
}
logrus.Debugf("Cluster '%s' changed, enqueue matching cluster groups", key)
cgs, err := h.clusterGroupsCache.List(cluster.Namespace, labels.Everything())
if err != nil {
return nil, err
}
for _, cg := range cgs {
if cg.Spec.Selector == nil {
continue
}
sel, err := metav1.LabelSelectorAsSelector(cg.Spec.Selector)
if err != nil {
logrus.Errorf("invalid selector on clustergroup %s/%s: %v", cg.Namespace, cg.Name, err)
continue
}
if sel.Matches(labels.Set(cluster.Labels)) {
h.clusterGroups.Enqueue(cg.Namespace, cg.Name)
}
// if cluster is removed from CG, need to reconcile if ClusterCount doesnt match
clusters, err := h.clusterCache.List(cg.Namespace, sel)
if err != nil {
logrus.Errorf("error fetching clusters in clustergroup %s%s: %v", cg.Namespace, cg.Name, err)
}
if cg.Status.ClusterCount != len(clusters) {
h.clusterGroups.Enqueue(cg.Namespace, cg.Name)
}
}
return cluster, nil
}
func (h *handler) OnClusterGroup(clusterGroup *fleet.ClusterGroup, status fleet.ClusterGroupStatus) (fleet.ClusterGroupStatus, error) {
var clusters []*fleet.Cluster
if clusterGroup.Spec.Selector != nil {
sel, err := metav1.LabelSelectorAsSelector(clusterGroup.Spec.Selector)
if err != nil {
return status, err
}
clusters, err = h.clusterCache.List(clusterGroup.Namespace, sel)
if err != nil {
return status, err
}
}
logrus.Debugf("ClusterGroupStatusHandler for '%s/%s', updating its status summary", clusterGroup.Namespace, clusterGroup.Name)
status.Summary = fleet.BundleSummary{}
status.ResourceCounts = fleet.GitRepoResourceCounts{}
status.ClusterCount = 0
status.NonReadyClusterCount = 0
status.NonReadyClusters = nil
sort.Slice(clusters, func(i, j int) bool {
return clusters[i].Name < clusters[j].Name
})
for _, cluster := range clusters {
summary.IncrementResourceCounts(&status.ResourceCounts, cluster.Status.ResourceCounts)
summary.Increment(&status.Summary, cluster.Status.Summary)
status.ClusterCount++
if !summary.IsReady(cluster.Status.Summary) {
status.NonReadyClusterCount++
if len(status.NonReadyClusters) < 10 {
status.NonReadyClusters = append(status.NonReadyClusters, cluster.Name)
}
}
}
summary.SetReadyConditions(&status, "Bundle", status.Summary)
return status, nil
}