This repository is currently being migrated. It's locked while the migration is in progress.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
272 lines (235 loc) · 9.56 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
package main
import (
"context"
"errors"
"flag"
"os"
"path/filepath"
"strings"
"time"
// 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/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/klog/v2"
"github.com/ondat/operator-toolkit/declarative/loader"
"github.com/ondat/operator-toolkit/telemetry/export"
"github.com/ondat/operator-toolkit/webhook/cert"
"go.uber.org/zap/zapcore"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
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/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsv1 "github.com/ondat/metrics-exporter/api/config.storageos.com/v1"
configstorageoscomv1 "github.com/storageos/operator/api/config.storageos.com/v1"
storageoscomv1 "github.com/storageos/operator/api/v1"
"github.com/storageos/operator/controllers"
whctrlr "github.com/storageos/operator/controllers/webhook"
"github.com/storageos/operator/internal/distro"
"github.com/storageos/operator/internal/version"
// +kubebuilder:scaffold:imports
)
const (
// podNamespace is the operator's pod namespace environment variable.
podNamespace = "POD_NAMESPACE"
// disableConfigWatch is the operator's disable config watch environment variable.
disableConfigWatch = "DISABLE_CONFIG_WATCH"
)
var (
shutDownperiod = 5 * time.Second
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
var SupportedMinKubeVersion string
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(storageoscomv1.AddToScheme(scheme))
utilruntime.Must(configstorageoscomv1.AddToScheme(scheme))
utilruntime.Must(metricsv1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}
// +kubebuilder:rbac:groups=admissionregistration.k8s.io,resources=mutatingwebhookconfigurations;validatingwebhookconfigurations,verbs=*
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=configmaps/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="coordination.k8s.io",resources=leases,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="extensions",resources=podsecuritypolicies,verbs=use
func main() {
ctx, cancel := context.WithCancel(ctrl.SetupSignalHandler())
defer func() {
cancel()
time.Sleep(shutDownperiod)
os.Exit(1)
}()
var configFile string
flag.StringVar(&configFile, "config", "",
"The controller will load its initial configuration from this file. "+
"Omit this flag to use the default configuration values. "+
"Command-line flags override configuration from this file.")
var leaderRenewSeconds uint
flag.UintVar(&leaderRenewSeconds, "leader-renew-seconds", 10, "Leader renewal frequency")
var opts zap.Options
opts.BindFlags(flag.CommandLine)
flag.Parse()
// Configure logger.
f := func(ec *zapcore.EncoderConfig) {
ec.TimeKey = "timestamp"
ec.EncodeTime = zapcore.RFC3339NanoTimeEncoder
}
encoderOpts := func(o *zap.Options) {
o.EncoderConfigOptions = append(o.EncoderConfigOptions, f)
}
zapLogger := zap.New(zap.UseFlagOptions(&opts), zap.StacktraceLevel(zapcore.PanicLevel), encoderOpts)
ctrl.SetLogger(zapLogger)
klog.SetLogger(zapLogger)
// Setup telemetry.
telemetryShutdown, err := export.InstallJaegerExporter("storageos-operator")
if err != nil {
setupLog.Error(err, "unable to setup telemetry exporter")
os.Exit(1)
}
defer telemetryShutdown()
currentNS := os.Getenv(podNamespace)
if len(currentNS) == 0 {
setupLog.Error(errors.New("current namespace not found"), "failed to get current namespace")
os.Exit(1)
}
renewDeadline := time.Duration(leaderRenewSeconds) * time.Second
leaseDuration := time.Duration(int(1.2*float64(leaderRenewSeconds))) * time.Second
leaderRetryDuration := renewDeadline / 2
// Load controller manager configuration and create manager options from
// it.
serialExecutionStrategy := false
ctrlConfig := configstorageoscomv1.OperatorConfig{}
options := ctrl.Options{
Scheme: scheme,
LeaderElection: true,
LeaderElectionID: "storageos-operator-leader-imewimw",
LeaderElectionNamespace: currentNS,
LeaderElectionResourceLock: resourcelock.LeasesResourceLock,
RenewDeadline: &renewDeadline,
LeaseDuration: &leaseDuration,
RetryPeriod: &leaderRetryDuration,
}
if configFile != "" {
var err error
cfg := ctrl.ConfigFile()
options, err = options.AndFrom(cfg.AtPath(configFile).OfKind(&ctrlConfig))
if err != nil {
setupLog.Error(err, "unable to load the config file")
os.Exit(1)
}
// get the execution strategy from the config file
serialExecutionStrategy = cfg.DeepCopyObject().(*configstorageoscomv1.OperatorConfig).SerialExecutionStrategy
}
defaultRestConfig := ctrl.GetConfigOrDie()
defaultKubeClient := kubernetes.NewForConfigOrDie(defaultRestConfig)
restConfig := ctrl.GetConfigOrDie()
restConfig.Timeout = time.Minute
timeoutKubeClient := kubernetes.NewForConfigOrDie(restConfig)
// Get Kubernetes version.
kubeVersion, err := timeoutKubeClient.Discovery().ServerVersion()
if err != nil {
setupLog.Error(err, "unable to get Kubernetes version")
os.Exit(1)
}
// Validate Kubernetes version
if SupportedMinKubeVersion != "" && !version.IsSupported(kubeVersion.String(), SupportedMinKubeVersion) {
setupLog.Error(errors.New("unsupported Kubernetes version"), "current version of Kubernetes is lower than required minimum version", "supported", SupportedMinKubeVersion, "current", kubeVersion.String())
os.Exit(1)
}
// Get Kubernetes distro.
kubeDistro := distro.DetermineDistribution(kubeVersion.String())
mgr, err := ctrl.NewManager(defaultRestConfig, options)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
// Create an uncached client to be used in the certificate manager
// and ConfigMap watcher.
// NOTE: Cached client from manager can't be used here because the cache is
// uninitialized at this point.
cli, err := client.New(mgr.GetConfig(), client.Options{Scheme: mgr.GetScheme()})
if err != nil {
setupLog.Error(err, "failed to create raw client")
os.Exit(1)
}
// Configure the certificate manager.
certOpts := cert.Options{
CertRefreshInterval: ctrlConfig.WebhookCertRefreshInterval.Duration,
Service: &admissionregistrationv1.ServiceReference{
Name: ctrlConfig.WebhookServiceName,
Namespace: currentNS,
},
Client: cli,
SecretRef: &types.NamespacedName{Name: ctrlConfig.WebhookSecretRef, Namespace: currentNS},
ValidatingWebhookConfigRefs: []types.NamespacedName{{Name: ctrlConfig.ValidatingWebhookConfigRef}},
}
// Create certificate manager without manager to start the provisioning
// immediately.
// NOTE: Certificate Manager implements nonLeaderElectionRunnable interface
// but since the webhook server is also a nonLeaderElectionRunnable, they
// start at the same time, resulting in a race condition where sometimes
// the certificates aren't available when the webhook server starts. By
// passing nil instead of the manager, the certificate manager is not
// managed by the controller manager. It starts immediately, in a blocking
// fashion, ensuring that the cert is created before the webhook server
// starts.
if err := cert.NewManager(nil, certOpts); err != nil {
setupLog.Error(err, "unable to provision certificate")
os.Exit(1)
}
// Set channel based on K8s version. If that channel does not exist, use default channelDir 'stable'.
cleanKubeVersion := version.CleanupVersion(kubeVersion.String())
channel := version.MajorMinor(cleanKubeVersion)
_, err = os.Stat(filepath.Join(loader.DefaultChannelDir, channel))
if err != nil {
channel = loader.DefaultChannelName
}
dcw := os.Getenv(disableConfigWatch)
if dcw == "" || strings.ToLower(dcw) == "false" {
if err = controllers.NewConfigReconciler(mgr).
SetupWithManager(mgr, currentNS); err != nil {
setupLog.Error(err, "unable to create controller",
"controller", "ConfigReconciler")
os.Exit(1)
}
}
if err = controllers.NewStorageOSClusterReconciler(mgr).
SetupWithManager(mgr, defaultKubeClient.AppsV1(), defaultKubeClient.PolicyV1(), cleanKubeVersion, kubeDistro, channel, serialExecutionStrategy); err != nil {
setupLog.Error(err, "unable to create controller",
"controller", "StorageOSCluster")
os.Exit(1)
}
// Create and set up admission webhook controller.
clusterWh, err := whctrlr.NewStorageOSClusterWebhook(mgr.GetClient(), mgr.GetScheme())
if err != nil {
setupLog.Error(err, "unable to create admission webhook controller",
"controller", clusterWh.CtrlName)
os.Exit(1)
}
if err := clusterWh.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to setup webhook controller with manager",
"controller", clusterWh.CtrlName)
os.Exit(1)
}
// +kubebuilder:scaffold:builder
if err := mgr.AddHealthzCheck("health", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("check", 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)
}
}