forked from verrazzano/verrazzano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
187 lines (159 loc) · 6.48 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
// Copyright (c) 2020, 2021, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package main
import (
"flag"
"os"
"github.com/verrazzano/verrazzano/pkg/log"
clustersv1alpha1 "github.com/verrazzano/verrazzano/platform-operator/apis/clusters/v1alpha1"
installv1alpha1 "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1"
clusterscontroller "github.com/verrazzano/verrazzano/platform-operator/controllers/clusters"
vzcontroller "github.com/verrazzano/verrazzano/platform-operator/controllers/verrazzano"
"github.com/verrazzano/verrazzano/platform-operator/internal/certificate"
config2 "github.com/verrazzano/verrazzano/platform-operator/internal/config"
"github.com/verrazzano/verrazzano/platform-operator/internal/netpolicy"
"go.uber.org/zap"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
kzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/source"
// +kubebuilder:scaffold:imports
)
var scheme = runtime.NewScheme()
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = installv1alpha1.AddToScheme(scheme)
_ = clustersv1alpha1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}
func main() {
// config will hold the entire operator config
config := config2.Get()
flag.StringVar(&config.MetricsAddr, "metrics-addr", config.MetricsAddr, "The address the metric endpoint binds to.")
flag.BoolVar(&config.LeaderElectionEnabled, "enable-leader-election", config.LeaderElectionEnabled,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&config.CertDir, "cert-dir", config.CertDir, "The directory containing tls.crt and tls.key.")
flag.BoolVar(&config.WebhooksEnabled, "enable-webhooks", config.WebhooksEnabled,
"Enable webhooks for the operator")
flag.BoolVar(&config.WebhookValidationEnabled, "enable-webhook-validation", config.WebhookValidationEnabled,
"Enable webhooks validation for the operator")
flag.BoolVar(&config.InitWebhooks, "init-webhooks", config.InitWebhooks,
"Initialize webhooks for the operator")
flag.StringVar(&config.VerrazzanoRootDir, "vz-root-dir", config.VerrazzanoRootDir,
"Specify the root directory of verrazzano (used for development)")
// Add the zap logger flag set to the CLI.
opts := kzap.Options{}
opts.BindFlags(flag.CommandLine)
flag.Parse()
kzap.UseFlagOptions(&opts)
log.InitLogs(opts)
// Save the config as immutable from this point on.
config2.Set(config)
setupLog := zap.S()
// initWebhooks flag is set when called from an initContainer. This allows the certs to be setup for the
// validatingWebhookConfiguration resource before the operator container runs.
if config.InitWebhooks {
setupLog.Info("Setting up certificates for webhook")
caCert, err := certificate.CreateWebhookCertificates(config.CertDir)
if err != nil {
setupLog.Errorf("unable to setup certificates for webhook: %v", err)
os.Exit(1)
}
config, err := ctrl.GetConfig()
if err != nil {
setupLog.Errorf("unable to get kubeconfig: %v", err)
os.Exit(1)
}
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
setupLog.Errorf("unable to get clientset: %v", err)
os.Exit(1)
}
setupLog.Info("Updating webhook configuration")
err = certificate.UpdateValidatingnWebhookConfiguration(kubeClient, caCert)
if err != nil {
setupLog.Errorf("unable to update validation webhook configuration: %v", err)
os.Exit(1)
}
client, err := client.New(config, client.Options{})
if err != nil {
setupLog.Errorf("unable to get client: %v", err)
os.Exit(1)
}
setupLog.Info("Creating or updating network policies")
opResult, err := netpolicy.CreateOrUpdateNetworkPolicies(kubeClient, client)
if err != nil {
setupLog.Errorf("unable to create or update network policies: %v", err)
os.Exit(1)
}
setupLog.Infof("Network policy operation result: %s", opResult)
return
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: config.MetricsAddr,
Port: 9443,
LeaderElection: config.LeaderElectionEnabled,
LeaderElectionID: "3ec4d290.verrazzano.io",
})
if err != nil {
setupLog.Errorf("unable to start manager: %v", err)
os.Exit(1)
}
// Setup the reconciler
_, dryRun := os.LookupEnv("VZ_DRY_RUN") // If this var is set, the install jobs are no-ops
reconciler := vzcontroller.Reconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
DryRun: dryRun,
}
if err = reconciler.SetupWithManager(mgr); err != nil {
setupLog.Errorf("unable to create controller: %v", err)
os.Exit(1)
}
// Watch for the secondary resource (Job).
if err := reconciler.Controller.Watch(&source.Kind{Type: &batchv1.Job{}},
&handler.EnqueueRequestForOwner{OwnerType: &installv1alpha1.Verrazzano{}, IsController: true}); err != nil {
setupLog.Errorf("unable to set watch for Job resource: %v", err)
os.Exit(1)
}
// Setup the validation webhook
if config.WebhooksEnabled {
setupLog.Info("Setting up Verrazzano webhook with manager")
if err = (&installv1alpha1.Verrazzano{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Errorf("unable to setup webhook with manager: %v", err)
os.Exit(1)
}
mgr.GetWebhookServer().CertDir = config.CertDir
}
// Setup the reconciler for VerrazzanoManagedCluster objects
if err = (&clusterscontroller.VerrazzanoManagedClusterReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "VerrazzanoManagedCluster")
os.Exit(1)
}
// Setup the validation webhook
if config.WebhooksEnabled {
setupLog.Info("Setting up VerrazzanoManagedCluster webhook with manager")
if err = (&clustersv1alpha1.VerrazzanoManagedCluster{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Errorf("unable to setup webhook with manager: %v", err)
os.Exit(1)
}
mgr.GetWebhookServer().CertDir = config.CertDir
}
// +kubebuilder:scaffold:builder
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Errorf("problem running manager: %v", err)
os.Exit(1)
}
}