-
Notifications
You must be signed in to change notification settings - Fork 58
/
discovery_controller.go
229 lines (203 loc) · 6.87 KB
/
discovery_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
224
225
226
227
228
229
package addon
import (
"context"
"fmt"
"strings"
"time"
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/resource/resourcemerge"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1"
addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1"
clientset "open-cluster-management.io/api/client/cluster/clientset/versioned"
clusterv1informer "open-cluster-management.io/api/client/cluster/informers/externalversions/cluster/v1"
clusterv1listers "open-cluster-management.io/api/client/cluster/listers/cluster/v1"
)
const (
addOnFeaturePrefix = "feature.open-cluster-management.io/addon-"
addOnStatusAvailable = "available"
addOnStatusUnhealthy = "unhealthy"
addOnStatusUnreachable = "unreachable"
)
// addOnFeatureDiscoveryController monitors ManagedCluster and its ManagedClusterAddOns on hub and
// create/update/delete labels of the ManagedCluster to reflect the status of addons.
type addOnFeatureDiscoveryController struct {
clusterClient clientset.Interface
clusterLister clusterv1listers.ManagedClusterLister
addOnLister addonlisterv1alpha1.ManagedClusterAddOnLister
recorder events.Recorder
}
// NewAddOnFeatureDiscoveryController returns an instance of addOnFeatureDiscoveryController
func NewAddOnFeatureDiscoveryController(
clusterClient clientset.Interface,
clusterInformer clusterv1informer.ManagedClusterInformer,
addOnInformers addoninformerv1alpha1.ManagedClusterAddOnInformer,
recorder events.Recorder,
) factory.Controller {
c := &addOnFeatureDiscoveryController{
clusterClient: clusterClient,
clusterLister: clusterInformer.Lister(),
addOnLister: addOnInformers.Lister(),
recorder: recorder,
}
return factory.New().
WithInformersQueueKeyFunc(
func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
},
clusterInformer.Informer()).
WithInformersQueueKeyFunc(
func(obj runtime.Object) string {
key, _ := cache.MetaNamespaceKeyFunc(obj)
return key
},
addOnInformers.Informer()).
WithSync(c.sync).
ResyncEvery(10*time.Minute).
ToController("AddOnFeatureDiscoveryController", recorder)
}
func (c *addOnFeatureDiscoveryController) sync(ctx context.Context, syncCtx factory.SyncContext) error {
// The value of queueKey might be
// 1) equal to the default queuekey. It is triggered by resync every 10 minutes;
// 2) in format: namespace/name. It indicates the event source is a ManagedClusterAddOn;
// 3) in format: name. It indicates the event source is a ManagedCluster;
queueKey := syncCtx.QueueKey()
namespace, name, err := cache.SplitMetaNamespaceKey(queueKey)
if err != nil {
utilruntime.HandleError(err)
return nil
}
switch {
case queueKey == factory.DefaultQueueKey:
// handle resync
clusters, err := c.clusterLister.List(labels.Everything())
if err != nil {
return err
}
for _, cluster := range clusters {
syncCtx.Queue().Add(cluster.Name)
}
return nil
case len(namespace) > 0:
// sync a particular addon
return c.syncAddOn(ctx, namespace, name)
default:
// sync the cluster
return c.syncCluster(ctx, name)
}
}
func (c *addOnFeatureDiscoveryController) syncAddOn(ctx context.Context, clusterName, addOnName string) error {
klog.V(4).Infof("Reconciling addOn %q", addOnName)
labels := map[string]string{}
addOn, err := c.addOnLister.ManagedClusterAddOns(clusterName).Get(addOnName)
switch {
case errors.IsNotFound(err):
// addon is deleted
key := fmt.Sprintf("%s%s-", addOnFeaturePrefix, addOnName)
labels[key] = ""
case err != nil:
return err
case !addOn.DeletionTimestamp.IsZero():
key := fmt.Sprintf("%s%s-", addOnFeaturePrefix, addOnName)
labels[key] = ""
default:
key := fmt.Sprintf("%s%s", addOnFeaturePrefix, addOn.Name)
labels[key] = getAddOnLabelValue(addOn)
}
cluster, err := c.clusterLister.Get(clusterName)
if errors.IsNotFound(err) {
// no cluster, it could be deleted
return nil
}
if err != nil {
return fmt.Errorf("unable to find cluster with name %q: %w", clusterName, err)
}
// no work if cluster is deleting
if !cluster.DeletionTimestamp.IsZero() {
return nil
}
// merge labels
modified := false
cluster = cluster.DeepCopy()
resourcemerge.MergeMap(&modified, &cluster.Labels, labels)
// no work if the cluster labels have no change
if !modified {
return nil
}
// otherwise, update cluster
_, err = c.clusterClient.ClusterV1().ManagedClusters().Update(ctx, cluster, metav1.UpdateOptions{})
return err
}
func (c *addOnFeatureDiscoveryController) syncCluster(ctx context.Context, clusterName string) error {
// sync all addon labels on the managed cluster
cluster, err := c.clusterLister.Get(clusterName)
if errors.IsNotFound(err) {
// cluster is deleted
return nil
}
if err != nil {
return err
}
// Do not update addon label if cluster is deleting
if !cluster.DeletionTimestamp.IsZero() {
return nil
}
// build labels for existing addons
addOnLabels := map[string]string{}
addOns, err := c.addOnLister.ManagedClusterAddOns(clusterName).List(labels.Everything())
if err != nil {
return fmt.Errorf("unable to list addOns of cluster %q: %w", clusterName, err)
}
for _, addOn := range addOns {
// addon is deleting
if !addOn.DeletionTimestamp.IsZero() {
continue
}
key := fmt.Sprintf("%s%s", addOnFeaturePrefix, addOn.Name)
addOnLabels[key] = getAddOnLabelValue(addOn)
}
// remove addon lable if its corresponding addon no longer exists
for key := range cluster.Labels {
if !strings.HasPrefix(key, addOnFeaturePrefix) {
continue
}
if _, ok := addOnLabels[key]; !ok {
addOnLabels[fmt.Sprintf("%s-", key)] = ""
}
}
// merge labels
modified := false
cluster = cluster.DeepCopy()
resourcemerge.MergeMap(&modified, &cluster.Labels, addOnLabels)
// no work if the cluster labels have no change
if !modified {
return nil
}
// otherwise, update cluster
_, err = c.clusterClient.ClusterV1().ManagedClusters().Update(ctx, cluster, metav1.UpdateOptions{})
return err
}
func getAddOnLabelValue(addOn *addonv1alpha1.ManagedClusterAddOn) string {
availableCondition := meta.FindStatusCondition(addOn.Status.Conditions, addonv1alpha1.ManagedClusterAddOnConditionAvailable)
if availableCondition == nil {
return addOnStatusUnreachable
}
switch availableCondition.Status {
case metav1.ConditionTrue:
return addOnStatusAvailable
case metav1.ConditionFalse:
return addOnStatusUnhealthy
default:
return addOnStatusUnreachable
}
}