-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
287 lines (261 loc) · 8.84 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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"flag"
"os"
"time"
helmv2 "github.com/fluxcd/helm-controller/api/v2"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2"
"github.com/fluxcd/pkg/runtime/events"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
sourcev1beta2 "github.com/fluxcd/source-controller/api/v1beta2"
glog "gopkg.in/op/go-logging.v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/dynamic"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
//+kubebuilder:scaffold:imports
"github.com/open-component-model/ocm-controller/api/v1alpha1"
"github.com/open-component-model/ocm-controller/controllers"
"github.com/open-component-model/ocm-controller/pkg/oci"
"github.com/open-component-model/ocm-controller/pkg/ocm"
"github.com/open-component-model/ocm-controller/pkg/snapshot"
)
const controllerName = "ocm-controller"
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(v1alpha1.AddToScheme(scheme))
utilruntime.Must(sourcev1.AddToScheme(scheme))
utilruntime.Must(sourcev1beta2.AddToScheme(scheme))
utilruntime.Must(kustomizev1.AddToScheme(scheme))
utilruntime.Must(helmv2.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
func main() {
var (
metricsAddr string
eventsAddr string
enableLeaderElection bool
probeAddr string
ociRegistryAddr string
ociRegistryCertSecretName string
ociRegistryInsecureSkipVerify bool
ociRegistryNamespace string
)
flag.StringVar(
&metricsAddr,
"metrics-bind-address",
":9443",
"The address the metric endpoint binds to.",
)
flag.StringVar(&eventsAddr, "events-addr", "", "The address of the events receiver.")
flag.StringVar(
&probeAddr,
"health-probe-bind-address",
":8081",
"The address the probe endpoint binds to.",
)
flag.StringVar(
&ociRegistryAddr,
"oci-registry-addr",
":5000",
"The address of the OCI registry.",
)
flag.StringVar(
&ociRegistryCertSecretName,
"certificate-secret-name",
v1alpha1.DefaultRegistryCertificateSecretName,
"",
)
flag.StringVar(
&ociRegistryNamespace,
"oci-registry-namespace",
"ocm-system",
"The namespace in which the registry is running in.",
)
flag.BoolVar(
&ociRegistryInsecureSkipVerify,
"oci-registry-insecure-skip-verify",
false,
"Skip verification of the certificate that the registry is using.",
)
flag.BoolVar(&enableLeaderElection, "leader-elect", 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)))
// 2024-05-11 :
// We're setting the go-log leve because it is used by yqlib
// If we ever make it possible to set the log level for the service
// , I mean stop hard coding the log level, then we will need to remember
// to set it both for controller-runtime ( i.e. zap ) and yqlib ( i.e. go-log )
glog.SetLevel(glog.WARNING, "yq-lib")
restConfig := ctrl.GetConfigOrDie()
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
},
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "f8b21459.ocm.software",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
if v, found := os.LookupEnv("OCI_REGISTRY_LOCALHOST"); found {
ociRegistryAddr = v
}
setupManagers(ociRegistryAddr, mgr, ociRegistryNamespace, ociRegistryCertSecretName, ociRegistryInsecureSkipVerify, restConfig, eventsAddr)
//+kubebuilder:scaffold:builder
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}
ctx := ctrl.SetupSignalHandler()
setupLog.Info("starting manager")
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
func setupManagers(
ociRegistryAddr string,
mgr manager.Manager,
ociRegistryNamespace, ociRegistryCertSecretName string,
ociRegistryInsecureSkipVerify bool,
restConfig *rest.Config,
eventsAddr string,
) {
cache := oci.NewClient(
ociRegistryAddr,
oci.WithClient(mgr.GetClient()),
oci.WithNamespace(ociRegistryNamespace),
oci.WithCertificateSecret(ociRegistryCertSecretName),
oci.WithInsecureSkipVerify(ociRegistryInsecureSkipVerify),
)
ocmClient := ocm.NewClient(mgr.GetClient(), cache)
snapshotWriter := snapshot.NewOCIWriter(mgr.GetClient(), cache, mgr.GetScheme())
dynClient, err := dynamic.NewForConfig(restConfig)
if err != nil {
setupLog.Error(err, "unable to get dynamic config client", "controller", "ocm-controller")
os.Exit(1)
}
var eventsRecorder *events.Recorder
if eventsRecorder, err = events.NewRecorder(mgr, ctrl.Log, eventsAddr, controllerName); err != nil {
setupLog.Error(err, "unable to create event recorder")
os.Exit(1)
}
if err = (&controllers.ComponentVersionReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
EventRecorder: eventsRecorder,
OCMClient: ocmClient,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ComponentVersion")
os.Exit(1)
}
if err = (&controllers.SnapshotReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
EventRecorder: eventsRecorder,
RegistryServiceName: ociRegistryAddr,
Cache: cache,
InsecureSkipVerify: ociRegistryInsecureSkipVerify,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Snapshot")
os.Exit(1)
}
if err = (&controllers.ResourceReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
EventRecorder: eventsRecorder,
OCMClient: ocmClient,
Cache: cache,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Resource")
os.Exit(1)
}
mutationReconciler := controllers.MutationReconcileLooper{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
OCMClient: ocmClient,
DynamicClient: dynClient,
Cache: cache,
SnapshotWriter: snapshotWriter,
}
if err = (&controllers.LocalizationReconciler{
Client: mgr.GetClient(),
DynamicClient: dynClient,
Scheme: mgr.GetScheme(),
EventRecorder: eventsRecorder,
ReconcileInterval: time.Hour,
RetryInterval: time.Minute,
OCMClient: ocmClient,
Cache: cache,
MutationReconciler: mutationReconciler,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Localization")
os.Exit(1)
}
if err = (&controllers.ConfigurationReconciler{
Client: mgr.GetClient(),
DynamicClient: dynClient,
Scheme: mgr.GetScheme(),
EventRecorder: eventsRecorder,
ReconcileInterval: time.Hour,
RetryInterval: time.Minute,
OCMClient: ocmClient,
Cache: cache,
MutationReconciler: mutationReconciler,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Configuration")
os.Exit(1)
}
if err = (&controllers.FluxDeployerReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
EventRecorder: eventsRecorder,
ReconcileInterval: time.Hour,
RetryInterval: time.Minute,
DynamicClient: dynClient,
RegistryServiceName: ociRegistryAddr,
CertSecretName: ociRegistryCertSecretName,
Cache: cache,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FluxDeployer")
os.Exit(1)
}
if err = (&controllers.ResourcePipelineReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
OCMClient: ocmClient,
EventRecorder: eventsRecorder,
SnapshotWriter: snapshotWriter,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ResourcePipeline")
os.Exit(1)
}
}