forked from dell/dell-csi-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
309 lines (280 loc) · 10.2 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
/*
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"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
osruntime "runtime"
"strconv"
"strings"
"time"
"gopkg.in/yaml.v2"
operatorconfig "github.com/dell/dell-csi-operator/pkg/config"
"github.com/dell/dell-csi-operator/core"
"github.com/dell/dell-csi-operator/pkg/constants"
"github.com/dell/dell-csi-operator/pkg/resources"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
storagev1 "github.com/dell/dell-csi-operator/api/v1"
"github.com/dell/dell-csi-operator/controllers"
v1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
// +kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
// BaseKubernetesVersion - Kubernetes Version which the operator defaults to
const BaseKubernetesVersion = "v121"
// DefaultConfigFile - config.yaml is the default config file
const DefaultConfigFile = "config.yaml"
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(storagev1.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}
var log = logf.Log.WithName("cmd")
func printVersion() {
log.Info("Operator Version", "Version", core.SemVer, "Commit ID", core.CommitSha32, "Commit SHA", string(core.CommitTime.Format(time.RFC1123)))
log.Info(fmt.Sprintf("Go Version: %s", osruntime.Version()))
log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", osruntime.GOOS, osruntime.GOARCH))
//log.Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion.Version))
}
func kubeAPIServerVersion(configDirectory, configFile string) (storagev1.K8sVersion, error) {
kubeAPIServerVersion, err := resources.GetKubeAPIServerVersion()
if err != nil {
// Default to the base k8s version
log.Info(fmt.Sprintf("Failed to get KubeAPI Server versions. Defaulting to %s", BaseKubernetesVersion))
return storagev1.BaseK8sVersion, nil
}
majorVersion := kubeAPIServerVersion.Major
minorVersion := strings.TrimSuffix(kubeAPIServerVersion.Minor, "+")
kubeVersion := fmt.Sprintf("v%s%s", majorVersion, minorVersion)
log.Info(fmt.Sprintf("Kubernetes Version: %s", kubeVersion))
supportedVersionFilePath := filepath.Join(configDirectory, configFile)
file, err := os.Open(filepath.Clean(supportedVersionFilePath))
if err != nil {
log.Info(fmt.Sprintf("Failed to find the config file %s", configFile))
return "", fmt.Errorf("missing config file")
}
defer file.Close()
byteValue, err := ioutil.ReadAll(file)
if err != nil {
return "", err
}
configData := make(map[string]interface{})
err = yaml.Unmarshal(byteValue, &configData)
if err != nil {
return "", err
}
if versions, ok := configData["supportedK8sVersions"]; ok {
if reflect.TypeOf(versions).Kind() == reflect.Slice {
supportedVersions := reflect.ValueOf(versions)
found := false
for i := 0; i < supportedVersions.Len(); i++ {
if supportedVersions.Index(i).Elem().Interface().(string) == kubeVersion {
found = true
break
}
}
if !found {
minorVersionNumber, _ := strconv.Atoi(minorVersion)
tempKubeVersion := fmt.Sprintf("v%s%d", majorVersion, minorVersionNumber-1)
for i := 0; i < supportedVersions.Len(); i++ {
if supportedVersions.Index(i).Elem().Interface().(string) == tempKubeVersion {
log.Info(fmt.Sprintf("%s is not supported by dell-csi-operator. Falling back to config files for %s",
kubeVersion, tempKubeVersion))
kubeVersion = tempKubeVersion
found = true
break
}
}
if !found {
return "", fmt.Errorf("unsupported k8s version. can't run operator")
}
}
}
} else {
return "", fmt.Errorf("list of supported K8s versions missing from config file")
}
return storagev1.K8sVersion(kubeVersion), nil
}
func getOperatorConfig() operatorconfig.Config {
cfg := operatorconfig.Config{}
// Set the default retry count from constant
cfg.RetryCount = constants.RetryCount
// Get the environment variable config file
configFile := os.Getenv("X_CSI_OPERATOR_CONFIG_FILE")
if configFile == "" {
configFile = DefaultConfigFile
}
cfg.ConfigFile = configFile
// Get the environment variable config dir
configDir := os.Getenv("X_CSI_OPERATOR_CONFIG_DIR")
if configDir == "" {
// Set the config dir to the folder pkg/config
configDir = "driverconfig/"
} else {
// Check if the directory is empty
fileName := configDir + "/" + configFile
_, err := os.Open(filepath.Clean(fileName))
if err != nil {
// This means that the configmap is not mounted
// fall back to the local copy
log.Error(err, "Error reading file from the configmap mount")
log.Info("Falling back to local copy of config files")
configDir = "/etc/config/local/dell-csi-operator"
}
}
cfg.ConfigDirectory = configDir
kubeVersion, err := kubeAPIServerVersion(cfg.ConfigDirectory, cfg.ConfigFile)
if err != nil {
panic(err.Error())
}
cfg.KubeAPIServerVersion = kubeVersion
isOpenShift, err := resources.IsOpenshift()
if err != nil {
log.Error(err, "Failed to determine if it is an Openshift cluster. Assuming it is")
isOpenShift = true
}
cfg.IsOpenShift = isOpenShift
if cfg.IsOpenShift {
log.Info("Detected OpenShift API groups")
}
enabledDrivers := make([]storagev1.DriverType, 0)
enabledDriverEnvValue := os.Getenv("OPERATOR_DRIVERS")
if enabledDriverEnvValue == "" {
// if the environment variable is unset or set to empty, enabled all drivers
enabledDrivers = append(enabledDrivers, storagev1.PowerMax,
storagev1.Unity, storagev1.Isilon, storagev1.VXFlexOS, storagev1.PowerStore)
} else {
tempEnabledDrivers := strings.Split(enabledDriverEnvValue, ",")
for _, driver := range tempEnabledDrivers {
driverType := operatorconfig.GetDriverType(driver)
if driverType == storagev1.Unknown {
fmt.Println("Unknown driver type specified. Ignoring...")
continue
}
enabledDrivers = append(enabledDrivers, driverType)
}
}
cfg.EnabledDrivers = enabledDrivers
return cfg
}
func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
flag.StringVar(&metricsAddr, "metrics-addr", ":9999", "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)))
printVersion()
operatorConfig := getOperatorConfig()
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "7e980ba4.dell.com",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
if err = (&controllers.CSIPowerMaxReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("CSIPowerMax"),
Scheme: mgr.GetScheme(),
Config: operatorConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CSIPowerMax")
os.Exit(1)
}
if err = (&controllers.CSIPowerMaxRevProxyReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("CSIPowerMaxRevProxy"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CSIPowerMaxRevProxy")
os.Exit(1)
}
if err = (&controllers.CSIIsilonReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("CSIIsilon"),
Scheme: mgr.GetScheme(),
Config: operatorConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CSIIsilon")
os.Exit(1)
}
if err = (&controllers.CSIUnityReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("CSIUnity"),
Scheme: mgr.GetScheme(),
Config: operatorConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CSIUnity")
os.Exit(1)
}
if err = (&controllers.CSIVXFlexOSReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("CSIVXFlexOS"),
Scheme: mgr.GetScheme(),
Config: operatorConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CSIVXFlexOS")
os.Exit(1)
}
if err = (&controllers.CSIPowerStoreReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("CSIPowerStore"),
Scheme: mgr.GetScheme(),
Config: operatorConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CSIPowerStore")
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(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}