-
Notifications
You must be signed in to change notification settings - Fork 217
/
controller.go
223 lines (194 loc) · 7.46 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Package cluster provides controllers for managing clusters: status changes, importing, bootstrapping. (fleetcontroller)
package cluster
import (
"context"
"os"
"sort"
"time"
"github.com/sirupsen/logrus"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/controllers/clusterregistration"
"github.com/rancher/fleet/pkg/durations"
fleetcontrollers "github.com/rancher/fleet/pkg/generated/controllers/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/summary"
corecontrollers "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/pkg/kv"
"github.com/rancher/wrangler/pkg/name"
"github.com/rancher/wrangler/pkg/relatedresource"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
type handler struct {
clusters fleetcontrollers.ClusterController
clusterCache fleetcontrollers.ClusterCache
clusterGroups fleetcontrollers.ClusterGroupCache
bundleDeployment fleetcontrollers.BundleDeploymentCache
namespaceCache corecontrollers.NamespaceCache
namespaces corecontrollers.NamespaceController
gitRepos fleetcontrollers.GitRepoCache
clusterRegistrations fleetcontrollers.ClusterRegistrationController
}
type repoKey struct {
repo string
ns string
}
func Register(ctx context.Context,
bundleDeployment fleetcontrollers.BundleDeploymentController,
clusterGroups fleetcontrollers.ClusterGroupCache,
clusters fleetcontrollers.ClusterController,
gitRepos fleetcontrollers.GitRepoCache,
namespaces corecontrollers.NamespaceController,
clusterRegistrations fleetcontrollers.ClusterRegistrationController) {
h := &handler{
clusterGroups: clusterGroups,
clusterCache: clusters.Cache(),
clusters: clusters,
bundleDeployment: bundleDeployment.Cache(),
namespaceCache: namespaces.Cache(),
namespaces: namespaces,
gitRepos: gitRepos,
clusterRegistrations: clusterRegistrations,
}
clusters.OnChange(ctx, "managed-cluster-trigger", h.ensureNSDeleted)
fleetcontrollers.RegisterClusterStatusHandler(ctx,
clusters,
"Processed",
"managed-cluster",
h.OnClusterChanged)
relatedresource.Watch(ctx, "managed-cluster", h.findClusters(namespaces.Cache()), clusters, bundleDeployment)
}
func (h *handler) ensureNSDeleted(key string, obj *fleet.Cluster) (*fleet.Cluster, error) {
if obj == nil {
logrus.Debugf("Cluster %s deleted, enqueue cluster namespace deletion", key)
h.namespaces.Enqueue(clusterNamespace(kv.Split(key, "/")))
}
return obj, nil
}
func (h *handler) findClusters(namespaces corecontrollers.NamespaceCache) relatedresource.Resolver {
return func(namespace, _ string, obj runtime.Object) ([]relatedresource.Key, error) {
if _, ok := obj.(*fleet.BundleDeployment); !ok {
return nil, nil
}
ns, err := namespaces.Get(namespace)
if err != nil {
return nil, nil
}
clusterNS := ns.Annotations[fleet.ClusterNamespaceAnnotation]
clusterName := ns.Annotations[fleet.ClusterAnnotation]
if clusterNS == "" || clusterName == "" {
return nil, nil
}
logrus.Debugf("Enqueueing cluster %s/%s for bundledeployment %s/%s", clusterNS, clusterName, namespace, obj.(*fleet.BundleDeployment).Name)
return []relatedresource.Key{
{
Namespace: clusterNS,
Name: clusterName,
},
}, nil
}
}
// clusterNamespace returns the cluster namespace name
// for a given cluster name, e.g.:
// "cluster-fleet-local-cluster-294db1acfa77-d9ccf852678f"
func clusterNamespace(clusterNamespace, clusterName string) string {
return name.SafeConcatName("cluster",
clusterNamespace,
clusterName,
clusterregistration.KeyHash(clusterNamespace+"::"+clusterName))
}
func (h *handler) OnClusterChanged(cluster *fleet.Cluster, status fleet.ClusterStatus) (fleet.ClusterStatus, error) {
logrus.Debugf("OnClusterChanged for cluster status %s, checking cluster registration, updating status from bundledeployments, gitrepos", cluster.Name)
if cluster.DeletionTimestamp != nil {
clusterRegistrations, err := h.clusterRegistrations.List(cluster.Namespace, metav1.ListOptions{})
if err != nil {
return status, err
}
for _, clusterRegistration := range clusterRegistrations.Items {
if clusterRegistration.Status.ClusterName == cluster.Name {
err := h.clusterRegistrations.Delete(clusterRegistration.Namespace, clusterRegistration.Name, &metav1.DeleteOptions{})
if err == nil {
logrus.Debugf("deleted leftover ClusterRegistration (%s) for cluster: %s", clusterRegistration.Name, cluster.Name)
} else if !apierrors.IsNotFound(err) {
return status, err
}
}
}
return status, nil
}
if status.Namespace == "" {
status.Namespace = clusterNamespace(cluster.Namespace, cluster.Name)
}
bundleDeployments, err := h.bundleDeployment.List(status.Namespace, labels.Everything())
if err != nil {
return status, err
}
status.DesiredReadyGitRepos = 0
status.ReadyGitRepos = 0
status.ResourceCounts = fleet.GitRepoResourceCounts{}
status.Summary = fleet.BundleSummary{}
sort.Slice(bundleDeployments, func(i, j int) bool {
return bundleDeployments[i].Name < bundleDeployments[j].Name
})
repos := map[repoKey]bool{}
for _, app := range bundleDeployments {
state := summary.GetDeploymentState(app)
summary.IncrementState(&status.Summary, app.Name, state, summary.MessageFromDeployment(app), app.Status.ModifiedStatus, app.Status.NonReadyStatus)
status.Summary.DesiredReady++
repo := app.Labels[fleet.RepoLabel]
ns := app.Labels[fleet.BundleNamespaceLabel]
if repo != "" && ns != "" {
repos[repoKey{repo: repo, ns: ns}] = (state == fleet.Ready) || repos[repoKey{repo: repo, ns: ns}]
}
}
allReady := true
for repo, ready := range repos {
gitrepo, err := h.gitRepos.Get(repo.ns, repo.repo)
if err == nil {
summary.IncrementResourceCounts(&status.ResourceCounts, gitrepo.Status.ResourceCounts)
status.DesiredReadyGitRepos++
if ready {
status.ReadyGitRepos++
} else {
allReady = false
}
}
}
if allReady && status.ResourceCounts.Ready != status.ResourceCounts.DesiredReady {
logrus.Debugf("Cluster %s/%s is not ready because not all gitrepos are ready: %d/%d, enqueue cluster again",
cluster.Namespace, cluster.Name, status.ResourceCounts.Ready, status.ResourceCounts.DesiredReady)
// Counts from gitrepo are out of sync with bundleDeployment state
// just retry in a number of seconds as there no great way to trigger an event that
// doesn't cause a loop
delay := durations.DefaultClusterEnqueueDelay
if customDelay, err := time.ParseDuration(os.Getenv("FLEET_CLUSTER_ENQUEUE_DELAY")); err == nil {
delay = customDelay
}
h.clusters.EnqueueAfter(cluster.Namespace, cluster.Name, delay)
}
summary.SetReadyConditions(&status, "Bundle", status.Summary)
return status, h.createNamespace(cluster, status)
}
func (h *handler) createNamespace(cluster *fleet.Cluster, status fleet.ClusterStatus) error {
_, err := h.namespaceCache.Get(status.Namespace)
if apierrors.IsNotFound(err) {
_, err = h.namespaces.Create(&v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: status.Namespace,
Labels: map[string]string{
fleet.ManagedLabel: "true",
},
Annotations: map[string]string{
fleet.ClusterNamespaceAnnotation: cluster.Namespace,
fleet.ClusterAnnotation: cluster.Name,
},
},
})
}
if apierrors.IsAlreadyExists(err) {
return nil
}
return err
}