-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.go
313 lines (279 loc) · 10.9 KB
/
main.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//
// Copyright 2022 IBM Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"flag"
"os"
"strings"
olmv1 "github.com/operator-framework/api/pkg/operators/v1"
olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
operatorsv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1"
admv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/klog"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"github.com/IBM/controller-filtered-cache/filteredcache"
nssv1 "github.com/IBM/ibm-namespace-scope-operator/v4/api/v1"
ssv1 "github.com/IBM/ibm-secretshare-operator/api/v1"
odlm "github.com/IBM/operand-deployment-lifecycle-manager/v4/api/v1alpha1"
certmanagerv1 "github.com/ibm/ibm-cert-manager-operator/apis/cert-manager/v1"
operatorv3 "github.com/IBM/ibm-common-service-operator/v4/api/v3"
"github.com/IBM/ibm-common-service-operator/v4/controllers"
"github.com/IBM/ibm-common-service-operator/v4/controllers/bootstrap"
certmanagerv1controllers "github.com/IBM/ibm-common-service-operator/v4/controllers/cert-manager"
util "github.com/IBM/ibm-common-service-operator/v4/controllers/common"
"github.com/IBM/ibm-common-service-operator/v4/controllers/constant"
"github.com/IBM/ibm-common-service-operator/v4/controllers/goroutines"
commonservicewebhook "github.com/IBM/ibm-common-service-operator/v4/controllers/webhooks/commonservice"
operandrequestwebhook "github.com/IBM/ibm-common-service-operator/v4/controllers/webhooks/operandrequest"
// +kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(odlm.AddToScheme(scheme))
utilruntime.Must(nssv1.AddToScheme(scheme))
utilruntime.Must(ssv1.AddToScheme(scheme))
utilruntime.Must(operatorv3.AddToScheme(scheme))
utilruntime.Must(admv1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
utilruntime.Must(olmv1alpha1.AddToScheme(scheme))
utilruntime.Must(olmv1.AddToScheme(scheme))
utilruntime.Must(operatorsv1.AddToScheme(scheme))
utilruntime.Must(certmanagerv1.AddToScheme(scheme))
}
func main() {
klog.InitFlags(nil)
defer klog.Flush()
var metricsAddr string
var probeAddr string
var enableLeaderElection bool
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
watchNamespace := util.GetWatchNamespace()
gvkLabelMap := map[schema.GroupVersionKind]filteredcache.Selector{
corev1.SchemeGroupVersion.WithKind("ConfigMap"): {
LabelSelector: constant.CsManagedLabel,
},
corev1.SchemeGroupVersion.WithKind("Secret"): {
LabelSelector: constant.SecretWatchLabel,
},
}
clusterGVKList := []schema.GroupVersionKind{
{Group: "admissionregistration.k8s.io", Kind: "MutatingWebhookConfiguration", Version: "v1"},
{Group: "admissionregistration.k8s.io", Kind: "ValidatingWebhookConfiguration", Version: "v1"},
}
var NewCache cache.NewCacheFunc
watchNamespaceList := strings.Split(watchNamespace, ",")
NewCache = util.NewCSCache(clusterGVKList, gvkLabelMap, watchNamespaceList)
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
HealthProbeBindAddress: probeAddr,
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "be598e12.ibm.com",
NewCache: NewCache,
})
if err != nil {
klog.Errorf("Unable to start manager: %v", err)
os.Exit(1)
}
operatorNs, err := util.GetOperatorNamespace()
klog.Infof("Identifying Common Service Operator Role in the namespace %s", operatorNs)
if err != nil {
klog.Errorf("Failed to get operatorNs: %v", err)
os.Exit(1)
}
cpfsNs, err := bootstrap.IdentifyCPFSNs(mgr.GetAPIReader(), operatorNs)
if err != nil {
klog.Errorf("Failed to get Common Service deployed namespace: %v", err)
os.Exit(1)
}
// If Common Service Operator Namespace is not in the same as .spec.operatorNamespace(cpfsNs) in default CS CR,
// this Common Service Operator is not in the operatorNamespace(cpfsNs) under this tenant, and goes dormant.
if operatorNs == cpfsNs {
// Bootstrap for non-olm deploy
if os.Getenv("NO_OLM") == "true" {
bs, err := bootstrap.NewNonOLMBootstrap(mgr)
if err != nil {
klog.Errorf("Bootstrap failed: %v", err)
os.Exit(1)
}
if err := bs.CleanupWebhookResources(); err != nil {
klog.Errorf("Cleanup Webhook Resources failed: %v", err)
os.Exit(1)
}
klog.Infof("Setup commonservice manager")
if err = (&controllers.CommonServiceReconciler{
Bootstrap: bs,
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("commonservice-controller"),
}).SetupWithManager(mgr); err != nil {
klog.Errorf("Unable to create controller CommonService: %v", err)
os.Exit(1)
}
klog.Infof("Start go routines")
// Create CS CR
go goroutines.WaitToCreateCsCR(bs)
// Delete Keycloak Cert
go goroutines.CleanupResources(bs)
// check if cert-manager CRD does not exist, then skip cert-manager related controllers initialization
exist, err := bs.CheckCRD(constant.CertManagerAPIGroupVersionV1, "Certificate")
if err != nil {
klog.Errorf("Failed to check if cert-manager CRD exists: %v", err)
os.Exit(1)
}
if !exist && err == nil {
klog.Infof("cert-manager CRD does not exist, skip cert-manager related controllers initialization")
} else if exist && err == nil {
if err = (&certmanagerv1controllers.CertificateRefreshReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
klog.Error(err, "unable to create controller", "controller", "CertificateRefresh")
os.Exit(1)
}
if err = (&certmanagerv1controllers.PodRefreshReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
klog.Error(err, "unable to create controller", "controller", "PodRefresh")
os.Exit(1)
}
if err = (&certmanagerv1controllers.V1AddLabelReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
klog.Error(err, "unable to create controller", "controller", "V1AddLabel")
os.Exit(1)
}
}
} else {
// New bootstrap Object
bs, err := bootstrap.NewBootstrap(mgr)
if err != nil {
klog.Errorf("Bootstrap failed: %v", err)
os.Exit(1)
}
if err := bs.CleanupWebhookResources(); err != nil {
klog.Errorf("Cleanup Webhook Resources failed: %v", err)
os.Exit(1)
}
klog.Infof("Setup commonservice manager")
if err = (&controllers.CommonServiceReconciler{
Bootstrap: bs,
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("commonservice-controller"),
}).SetupWithManager(mgr); err != nil {
klog.Errorf("Unable to create controller CommonService: %v", err)
os.Exit(1)
}
klog.Infof("Start go routines")
// Update CS CR Status
go goroutines.UpdateCsCrStatus(bs)
// Create CS CR
go goroutines.WaitToCreateCsCR(bs)
// Delete Keycloak Cert
go goroutines.CleanupResources(bs)
// check if cert-manager CRD does not exist, then skip cert-manager related controllers initialization
exist, err := bs.CheckCRD(constant.CertManagerAPIGroupVersionV1, "Certificate")
if err != nil {
klog.Errorf("Failed to check if cert-manager CRD exists: %v", err)
os.Exit(1)
}
if !exist && err == nil {
klog.Infof("cert-manager CRD does not exist, skip cert-manager related controllers initialization")
} else if exist && err == nil {
if err = (&certmanagerv1controllers.CertificateRefreshReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
klog.Error(err, "unable to create controller", "controller", "CertificateRefresh")
os.Exit(1)
}
if err = (&certmanagerv1controllers.PodRefreshReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
klog.Error(err, "unable to create controller", "controller", "PodRefresh")
os.Exit(1)
}
if err = (&certmanagerv1controllers.V1AddLabelReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
klog.Error(err, "unable to create controller", "controller", "V1AddLabel")
os.Exit(1)
}
}
}
} else {
klog.Infof("Common Service Operator goes dormant in the namespace %s", operatorNs)
klog.Infof("Common Service Operator in the namespace %s takes charge of resource management", cpfsNs)
}
// Start up the webhook server
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&commonservicewebhook.Defaulter{
Client: mgr.GetClient(),
Reader: mgr.GetAPIReader(),
IsDormant: operatorNs != cpfsNs,
}).SetupWebhookWithManager(mgr); err != nil {
klog.Errorf("Unable to create CommonService webhook: %v", err)
os.Exit(1)
}
if err = (&operandrequestwebhook.Defaulter{
Client: mgr.GetClient(),
Reader: mgr.GetAPIReader(),
IsDormant: operatorNs != cpfsNs,
}).SetupWebhookWithManager(mgr); err != nil {
klog.Errorf("Unable to create OperandRequest webhook: %v", err)
os.Exit(1)
}
}
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
klog.Errorf("unable to set up health check: %v", err)
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
klog.Errorf("unable to set up ready check: %v", err)
os.Exit(1)
}
klog.Info("Starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
klog.Errorf("Problem running manager: %v", err)
os.Exit(1)
}
}