forked from operator-framework/operator-lifecycle-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
164 lines (147 loc) · 5.54 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
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
package main
import (
"context"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators"
"github.com/operator-framework/operator-lifecycle-manager/pkg/feature"
)
var (
copiedLabelDoesNotExist labels.Selector
)
func init() {
requirement, err := labels.NewRequirement(operatorsv1alpha1.CopiedLabelKey, selection.DoesNotExist, nil)
if err != nil {
panic(err)
}
copiedLabelDoesNotExist = labels.NewSelector().Add(*requirement)
}
func Manager(ctx context.Context, debug bool) (ctrl.Manager, error) {
ctrl.SetLogger(zap.New(zap.UseDevMode(debug)))
setupLog := ctrl.Log.WithName("setup").V(1)
scheme := runtime.NewScheme()
if err := metav1.AddMetaToScheme(scheme); err != nil {
return nil, err
}
if err := operators.AddToScheme(scheme); err != nil {
// ctrl.NewManager needs the Scheme to be populated
// up-front so that the NewCache implementation we
// provide can configure custom cache behavior on
// non-core types.
return nil, err
}
setupLog.Info("configuring manager")
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: "0", // TODO(njhale): Enable metrics on non-conflicting port (not 8080)
Cache: cache.Options{
ByObject: map[client.Object]cache.ByObject{
&appsv1.Deployment{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&corev1.Service{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&apiextensionsv1.CustomResourceDefinition{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&apiregistrationv1.APIService{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&corev1.ConfigMap{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&corev1.ServiceAccount{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&rbacv1.Role{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&rbacv1.RoleBinding{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&rbacv1.ClusterRole{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&rbacv1.ClusterRoleBinding{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&admissionregistrationv1.MutatingWebhookConfiguration{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&admissionregistrationv1.ValidatingWebhookConfiguration{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&operatorsv1alpha1.ClusterServiceVersion{}: {
Label: copiedLabelDoesNotExist,
},
},
},
})
if err != nil {
return nil, err
}
operatorConditionReconciler, err := operators.NewOperatorConditionReconciler(
mgr.GetClient(),
ctrl.Log.WithName("controllers").WithName("operatorcondition"),
mgr.GetScheme(),
)
if err != nil {
return nil, err
}
if err = operatorConditionReconciler.SetupWithManager(mgr); err != nil {
return nil, err
}
operatorConditionGeneratorReconciler, err := operators.NewOperatorConditionGeneratorReconciler(
mgr.GetClient(),
ctrl.Log.WithName("controllers").WithName("operatorcondition-generator"),
mgr.GetScheme(),
)
if err != nil {
return nil, err
}
if err = operatorConditionGeneratorReconciler.SetupWithManager(mgr); err != nil {
return nil, err
}
if feature.Gate.Enabled(feature.OperatorLifecycleManagerV1) {
// Setup a new controller to reconcile Operators
operatorReconciler, err := operators.NewOperatorReconciler(
mgr.GetClient(),
ctrl.Log.WithName("controllers").WithName("operator"),
mgr.GetScheme(),
)
if err != nil {
return nil, err
}
if err = operatorReconciler.SetupWithManager(mgr); err != nil {
return nil, err
}
adoptionReconciler, err := operators.NewAdoptionReconciler(
mgr.GetClient(),
ctrl.Log.WithName("controllers").WithName("adoption"),
mgr.GetScheme(),
)
if err != nil {
return nil, err
}
if err = adoptionReconciler.SetupWithManager(mgr); err != nil {
return nil, err
}
}
setupLog.Info("manager configured")
return mgr, nil
}