This repository has been archived by the owner on Nov 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
214 lines (189 loc) · 7.96 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
// 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"
"github.com/fluxcd/source-controller/api/v1beta2"
gitv1alpha1 "github.com/open-component-model/git-controller/apis/delivery/v1alpha1"
gitmpasv1alpha1 "github.com/open-component-model/git-controller/apis/mpas/v1alpha1"
v1alpha12 "github.com/open-component-model/ocm-controller/api/v1alpha1"
"github.com/open-component-model/ocm-controller/pkg/oci"
"github.com/open-component-model/ocm-controller/pkg/snapshot"
replicationv1 "github.com/open-component-model/replication-controller/api/v1alpha1"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/record"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
mpasprojv1alpha1 "github.com/open-component-model/mpas-project-controller/api/v1alpha1"
"github.com/fluxcd/pkg/runtime/events"
//+kubebuilder:scaffold:imports
mpasv1alpha1 "github.com/open-component-model/mpas-product-controller/api/v1alpha1"
"github.com/open-component-model/mpas-product-controller/controllers"
"github.com/open-component-model/mpas-product-controller/pkg/deployers/kubernetes"
"github.com/open-component-model/mpas-product-controller/pkg/ocm"
"github.com/open-component-model/mpas-product-controller/pkg/validators/gitea"
"github.com/open-component-model/mpas-product-controller/pkg/validators/github"
)
const (
controllerName = "mpas-product-controller"
defaultMpasSystemNamespace = "mpas-system"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(replicationv1.AddToScheme(scheme))
utilruntime.Must(mpasv1alpha1.AddToScheme(scheme))
utilruntime.Must(mpasprojv1alpha1.AddToScheme(scheme))
utilruntime.Must(v1alpha12.AddToScheme(scheme))
utilruntime.Must(gitv1alpha1.AddToScheme(scheme))
utilruntime.Must(v1beta2.AddToScheme(scheme))
utilruntime.Must(gitmpasv1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
func main() {
var (
metricsAddr string
eventsAddr string
enableLeaderElection bool
probeAddr string
ociRegistryAddr string
mpasSystemNamespace string
ociRegistryCertSecretName string
)
flag.StringVar(&ociRegistryCertSecretName, "certificate-secret-name", "ocm-registry-tls-certs", "")
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&eventsAddr, "events-bind-address", "", "The address the event endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&ociRegistryAddr, "oci-registry-addr", ":5000", "The address of the OCI registry.")
flag.StringVar(
&mpasSystemNamespace,
"mpas-system-namespace",
defaultMpasSystemNamespace,
"The namespace in which this controller is running in. This namespace is used to locate Project objects.",
)
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
const metricsPort = 9443
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: metricsPort,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "b3469b71.ocm.software",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
eventRecorder := mustSetupEventRecorder(mgr, eventsAddr, controllerName)
ctx := ctrl.SetupSignalHandler()
cache := oci.NewClient(ociRegistryAddr,
oci.WithClient(mgr.GetClient()),
oci.WithNamespace(mpasSystemNamespace),
oci.WithCertificateSecret(ociRegistryCertSecretName),
)
snapshotWriter := snapshot.NewOCIWriter(mgr.GetClient(), cache, mgr.GetScheme())
ocmClient := ocm.NewClient(mgr.GetClient())
if err = (&controllers.ProductDeploymentGeneratorReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
OCMClient: ocmClient,
SnapshotWriter: snapshotWriter,
MpasSystemNamespace: mpasSystemNamespace,
EventRecorder: mgr.GetEventRecorderFor("product-deployment-generator-reconciler"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ProductDeploymentGenerator")
os.Exit(1)
}
if err = (&controllers.ProductDeploymentReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
MpasSystemNamespace: mpasSystemNamespace,
EventRecorder: mgr.GetEventRecorderFor("product-deployment-reconciler"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ProductDeployment")
os.Exit(1)
}
if err = (&controllers.ProductDeploymentPipelineReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
MpasSystemNamespace: mpasSystemNamespace,
EventRecorder: mgr.GetEventRecorderFor("product-deployment-pipeline-reconciler"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ProductDeploymentPipeline")
os.Exit(1)
}
if err = (&controllers.TargetReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ControllerName: controllerName,
EventRecorder: eventRecorder,
}).SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Target")
os.Exit(1)
}
kubeDeployer := kubernetes.NewDeployer(mgr.GetClient(), mgr.GetScheme(), nil)
if err = (&controllers.ProductDeploymentPipelineScheduler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
MpasSystemNamespace: mpasSystemNamespace,
Deployer: kubeDeployer,
EventRecorder: mgr.GetEventRecorderFor("product-deployment-pipeline-scheduler"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create scheduler", "scheduler", "ProductDeploymentScheduler")
os.Exit(1)
}
giteaValidator := gitea.NewValidator(mgr.GetClient(), nil)
githubValidator := github.NewValidator(mgr.GetClient(), giteaValidator)
if err = (&controllers.ValidationReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
EventRecorder: mgr.GetEventRecorderFor("validation-reconciler"),
MpasSystemNamespace: mpasSystemNamespace,
Validator: githubValidator,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Validation")
os.Exit(1)
}
//+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)
}
setupLog.Info("starting manager")
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
func mustSetupEventRecorder(mgr ctrl.Manager, eventsAddr, controllerName string) record.EventRecorder {
eventRecorder, err := events.NewRecorder(mgr, ctrl.Log, eventsAddr, controllerName)
if err != nil {
setupLog.Error(err, "unable to create event recorder")
os.Exit(1)
}
return eventRecorder
}