-
Notifications
You must be signed in to change notification settings - Fork 23
/
manager.go
115 lines (100 loc) · 3.92 KB
/
manager.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
// Copyright (c) Red Hat, Inc.
// Copyright Contributors to the Open Cluster Management project
package clusterdeployment
import (
"context"
"strings"
workv1 "open-cluster-management.io/api/work/v1"
"github.com/stolostron/managedcluster-import-controller/pkg/constants"
"github.com/stolostron/managedcluster-import-controller/pkg/helpers"
"github.com/stolostron/managedcluster-import-controller/pkg/source"
hivev1 "github.com/openshift/hive/apis/hive/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
const controllerName = "clusterdeployment-controller"
// Add creates a new managedcluster controller and adds it to the Manager.
// The Manager will set fields on the Controller and Start it when the Manager is Started.
func Add(mgr manager.Manager, clientHolder *helpers.ClientHolder, informerHolder *source.InformerHolder) (string, error) {
err := ctrl.NewControllerManagedBy(mgr).Named(controllerName).
WithOptions(controller.Options{
MaxConcurrentReconciles: helpers.GetMaxConcurrentReconciles(),
}).
Watches( // watch the clusterdeployment
&hivev1.ClusterDeployment{},
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) []reconcile.Request {
return []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Namespace: o.GetNamespace(),
Name: o.GetNamespace(),
},
},
}
}),
).
WatchesRawSource( // watch the import secret
source.NewImportSecretSource(informerHolder.ImportSecretInformer),
&source.ManagedClusterResourceEventHandler{},
builder.WithPredicates(predicate.Funcs{
GenericFunc: func(e event.GenericEvent) bool { return false },
DeleteFunc: func(e event.DeleteEvent) bool { return false },
CreateFunc: func(e event.CreateEvent) bool { return true },
UpdateFunc: func(e event.UpdateEvent) bool {
new, okNew := e.ObjectNew.(*corev1.Secret)
old, okOld := e.ObjectOld.(*corev1.Secret)
if okNew && okOld {
return !equality.Semantic.DeepEqual(old.Data, new.Data)
}
return false
},
}),
).
WatchesRawSource( // watch the klusterlet manifest works
source.NewKlusterletWorkSource(informerHolder.KlusterletWorkInformer),
&source.ManagedClusterResourceEventHandler{},
builder.WithPredicates(predicate.Funcs{
GenericFunc: func(e event.GenericEvent) bool { return false },
DeleteFunc: func(e event.DeleteEvent) bool { return false },
CreateFunc: func(e event.CreateEvent) bool {
workName := e.Object.GetName()
// only watch klusterlet manifest works
if !strings.HasSuffix(workName, constants.KlusterletCRDsSuffix) &&
!strings.HasSuffix(workName, constants.KlusterletSuffix) {
return false
}
return true
},
UpdateFunc: func(e event.UpdateEvent) bool {
workName := e.ObjectNew.GetName()
// only watch klusterlet manifest works
if !strings.HasSuffix(workName, constants.KlusterletCRDsSuffix) &&
!strings.HasSuffix(workName, constants.KlusterletSuffix) {
return false
}
new, okNew := e.ObjectNew.(*workv1.ManifestWork)
old, okOld := e.ObjectOld.(*workv1.ManifestWork)
if okNew && okOld {
return !helpers.ManifestsEqual(new.Spec.Workload.Manifests, old.Spec.Workload.Manifests)
}
return false
},
}),
).
Complete(NewReconcileClusterDeployment(
clientHolder.RuntimeClient,
clientHolder.KubeClient,
informerHolder,
helpers.NewEventRecorder(clientHolder.KubeClient, controllerName)))
return controllerName, err
}