-
Notifications
You must be signed in to change notification settings - Fork 175
/
init.go
411 lines (382 loc) · 16.1 KB
/
init.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
Copyright 2019 The Kubernetes Authors.
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 manager
import (
"context"
"fmt"
"path/filepath"
"reflect"
"time"
"github.com/fsnotify/fsnotify"
cnstypes "github.com/vmware/govmomi/cns/types"
apierrors "k8s.io/apimachinery/pkg/api/errors"
k8stypes "k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
cnsoperatorconfig "sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/cnsoperator/config"
internalapiscnsoperatorconfig "sigs.k8s.io/vsphere-csi-driver/v3/pkg/internalapis/cnsoperator/config"
csinodetopologyconfig "sigs.k8s.io/vsphere-csi-driver/v3/pkg/internalapis/csinodetopology/config"
cnsoperatorv1alpha1 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/cnsoperator"
cnsvolumemetadatav1alpha1 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/cnsoperator/cnsvolumemetadata/v1alpha1"
volumes "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/cns-lib/volume"
cnsvsphere "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/cns-lib/vsphere"
commonconfig "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/config"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common/commonco"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/logger"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/internalapis"
triggercsifullsyncv1alpha1 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/internalapis/cnsoperator/triggercsifullsync/v1alpha1"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/internalapis/csinodetopology"
csinodetopologyv1alpha1 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/internalapis/csinodetopology/v1alpha1"
k8s "sigs.k8s.io/vsphere-csi-driver/v3/pkg/kubernetes"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/syncer/cnsoperator/controller"
)
var (
// Use localhost and port for metrics
metricsHost = "0.0.0.0"
metricsPort int32 = 8383
)
type cnsOperator struct {
configInfo *commonconfig.ConfigurationInfo
coCommonInterface commonco.COCommonInterface
}
// InitCnsOperator initializes the Cns Operator.
func InitCnsOperator(ctx context.Context, clusterFlavor cnstypes.CnsClusterFlavor,
configInfo *commonconfig.ConfigurationInfo, coInitParams *interface{}) error {
log := logger.GetLogger(ctx)
log.Infof("Initializing CNS Operator")
cnsOperator := &cnsOperator{}
cnsOperator.configInfo = configInfo
var volumeManager volumes.Manager
if clusterFlavor == cnstypes.CnsClusterFlavorWorkload || clusterFlavor == cnstypes.CnsClusterFlavorVanilla {
var (
vCenter *cnsvsphere.VirtualCenter
err error
vcconfig *cnsvsphere.VirtualCenterConfig
)
if commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.MultiVCenterCSITopology) {
vcconfig, err = cnsvsphere.GetVirtualCenterConfig(ctx, cnsOperator.configInfo.Cfg)
if err != nil {
log.Errorf("failed to get VirtualCenterConfig. Err: %+v", err)
return err
}
vCenter, err = cnsvsphere.GetVirtualCenterInstanceForVCenterConfig(ctx, vcconfig, false)
if err != nil {
return err
}
} else {
vCenter, err = cnsvsphere.GetVirtualCenterInstance(ctx, cnsOperator.configInfo, false)
if err != nil {
return err
}
}
volumeManager, err = volumes.GetManager(ctx, vCenter, nil, false, false, false,
commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.ListViewPerf), clusterFlavor)
if err != nil {
return logger.LogNewErrorf(log, "failed to create an instance of volume manager. err=%v", err)
}
}
// Get a config to talk to the apiserver
restConfig, err := config.GetConfig()
if err != nil {
log.Errorf("failed to get Kubernetes config. Err: %+v", err)
return err
}
// Initialize the k8s orchestrator interface.
cnsOperator.coCommonInterface, err = commonco.GetContainerOrchestratorInterface(ctx, common.Kubernetes,
clusterFlavor, coInitParams)
if err != nil {
log.Errorf("failed to create CO agnostic interface. Err: %v", err)
return err
}
// TODO: Verify leader election for CNS Operator in multi-master mode
// Create CRD's for WCP flavor.
if clusterFlavor == cnstypes.CnsClusterFlavorWorkload {
// Create CnsNodeVmAttachment CRD
err = k8s.CreateCustomResourceDefinitionFromManifest(ctx, cnsoperatorconfig.EmbedCnsNodeVmAttachmentCRFile,
cnsoperatorconfig.EmbedCnsNodeVmAttachmentCRFileName)
if err != nil {
crdNameNodeVMAttachment := cnsoperatorv1alpha1.CnsNodeVMAttachmentPlural +
"." + cnsoperatorv1alpha1.SchemeGroupVersion.Group
log.Errorf("failed to create %q CRD. Err: %+v", crdNameNodeVMAttachment, err)
return err
}
// Create CnsVolumeMetadata CRD
err = k8s.CreateCustomResourceDefinitionFromManifest(ctx, cnsoperatorconfig.EmbedCnsVolumeMetadataCRFile,
cnsoperatorconfig.EmbedCnsVolumeMetadataCRFileName)
if err != nil {
crdKindVolumeMetadata := reflect.TypeOf(cnsvolumemetadatav1alpha1.CnsVolumeMetadata{}).Name()
log.Errorf("failed to create %q CRD. Err: %+v", crdKindVolumeMetadata, err)
return err
}
var stretchedSupervisor bool
if commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.TKGsHA) {
clusterComputeResourceMoIds, err := common.GetClusterComputeResourceMoIds(ctx)
if err != nil {
log.Errorf("failed to get clusterComputeResourceMoIds. err: %v", err)
return err
}
if len(clusterComputeResourceMoIds) > 1 {
stretchedSupervisor = true
}
}
if stretchedSupervisor {
log.Info("Observed stretchedSupervisor setup")
}
if !stretchedSupervisor ||
(stretchedSupervisor && commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.PodVMOnStretchedSupervisor)) {
// Create CnsRegisterVolume CRD from manifest.
log.Infof("Creating %q CRD", cnsoperatorv1alpha1.CnsRegisterVolumePlural)
err = k8s.CreateCustomResourceDefinitionFromManifest(ctx, cnsoperatorconfig.EmbedCnsRegisterVolumeCRFile,
cnsoperatorconfig.EmbedCnsRegisterVolumeCRFileName)
if err != nil {
log.Errorf("Failed to create %q CRD. Err: %+v", cnsoperatorv1alpha1.CnsRegisterVolumePlural, err)
return err
}
log.Infof("%q CRD is created successfully", cnsoperatorv1alpha1.CnsRegisterVolumePlural)
}
if !stretchedSupervisor {
if cnsOperator.coCommonInterface.IsFSSEnabled(ctx, common.FileVolume) {
// Create CnsFileAccessConfig CRD from manifest if file volume feature
// is enabled.
err = k8s.CreateCustomResourceDefinitionFromManifest(ctx, cnsoperatorconfig.EmbedCnsFileAccessConfigCRFile,
cnsoperatorconfig.EmbedCnsFileAccessConfigCRFileName)
if err != nil {
log.Errorf("Failed to create %q CRD. Err: %+v", cnsoperatorv1alpha1.CnsFileAccessConfigPlural, err)
return err
}
// Create FileVolumeClients CRD from manifest if file volume feature
// is enabled.
err = k8s.CreateCustomResourceDefinitionFromManifest(ctx,
internalapiscnsoperatorconfig.EmbedCnsFileVolumeClientFile,
internalapiscnsoperatorconfig.EmbedCnsFileVolumeClientFileName)
if err != nil {
log.Errorf("Failed to create %q CRD. Err: %+v", internalapis.CnsFileVolumeClientPlural, err)
return err
}
}
}
if !stretchedSupervisor ||
(stretchedSupervisor && commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.PodVMOnStretchedSupervisor)) {
// Clean up routine to cleanup successful CnsRegisterVolume instances.
log.Info("Starting go routine to cleanup successful CnsRegisterVolume instances.")
err = watcher(ctx, cnsOperator)
if err != nil {
log.Error("Failed to watch on config file for changes to CnsRegisterVolumesCleanupIntervalInMin. Error: %+v",
err)
return err
}
go func() {
for {
ctx, log = logger.GetNewContextWithLogger()
log.Infof("Triggering CnsRegisterVolume cleanup routine")
cleanUpCnsRegisterVolumeInstances(ctx, restConfig,
cnsOperator.configInfo.Cfg.Global.CnsRegisterVolumesCleanupIntervalInMin)
log.Infof("Completed CnsRegisterVolume cleanup")
for i := 1; i <= cnsOperator.configInfo.Cfg.Global.CnsRegisterVolumesCleanupIntervalInMin; i++ {
time.Sleep(time.Duration(1 * time.Minute))
}
}
}()
}
} else if clusterFlavor == cnstypes.CnsClusterFlavorVanilla {
// Create CSINodeTopology CRD.
err = k8s.CreateCustomResourceDefinitionFromManifest(ctx, csinodetopologyconfig.EmbedCSINodeTopologyFile,
csinodetopologyconfig.EmbedCSINodeTopologyFileName)
if err != nil {
log.Errorf("Failed to create %q CRD. Error: %+v", csinodetopology.CRDSingular, err)
return err
}
} else if clusterFlavor == cnstypes.CnsClusterFlavorGuest {
if cnsOperator.coCommonInterface.IsFSSEnabled(ctx, common.TKGsHA) {
// Create CSINodeTopology CRD.
err = k8s.CreateCustomResourceDefinitionFromManifest(ctx, csinodetopologyconfig.EmbedCSINodeTopologyFile,
csinodetopologyconfig.EmbedCSINodeTopologyFileName)
if err != nil {
log.Errorf("Failed to create %q CRD. Error: %+v", csinodetopology.CRDSingular, err)
return err
}
}
}
// Create a new operator to provide shared dependencies and start components
// Setting namespace to empty would let operator watch all namespaces.
mgr, err := manager.New(restConfig, manager.Options{
Namespace: "",
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
})
if err != nil {
log.Errorf("failed to create new Cns operator instance. Err: %+v", err)
return err
}
log.Info("Registering Components for Cns Operator")
// Setup Scheme for all resources for external APIs.
if err := cnsoperatorv1alpha1.AddToScheme(mgr.GetScheme()); err != nil {
log.Errorf("failed to set the scheme for Cns operator. Err: %+v", err)
return err
}
if err = csinodetopologyv1alpha1.AddToScheme(mgr.GetScheme()); err != nil {
log.Errorf("failed to add CSINodeTopology to scheme with error: %+v", err)
return err
}
if err = internalapis.AddToScheme(mgr.GetScheme()); err != nil {
log.Errorf("failed to add internalapis to scheme with error: %+v", err)
return err
}
// Setup all Controllers.
if err := controller.AddToManager(mgr, clusterFlavor, cnsOperator.configInfo, volumeManager); err != nil {
log.Errorf("failed to setup the controller for Cns operator. Err: %+v", err)
return err
}
log.Info("Starting Cns Operator")
// Start the operator.
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
log.Errorf("failed to start Cns operator. Err: %+v", err)
return err
}
return nil
}
// InitCommonModules initializes the common modules for all flavors.
func InitCommonModules(ctx context.Context, clusterFlavor cnstypes.CnsClusterFlavor,
coInitParams *interface{}) error {
ctx = logger.NewContextWithLogger(ctx)
log := logger.GetLogger(ctx)
var err error
if clusterFlavor == cnstypes.CnsClusterFlavorWorkload {
commonco.ContainerOrchestratorUtility, err = commonco.GetContainerOrchestratorInterface(ctx,
common.Kubernetes, cnstypes.CnsClusterFlavorWorkload, *coInitParams)
} else if clusterFlavor == cnstypes.CnsClusterFlavorVanilla {
commonco.ContainerOrchestratorUtility, err = commonco.GetContainerOrchestratorInterface(ctx,
common.Kubernetes, cnstypes.CnsClusterFlavorVanilla, *coInitParams)
} else if clusterFlavor == cnstypes.CnsClusterFlavorGuest {
commonco.ContainerOrchestratorUtility, err = commonco.GetContainerOrchestratorInterface(ctx,
common.Kubernetes, cnstypes.CnsClusterFlavorGuest, *coInitParams)
}
if err != nil {
log.Errorf("failed to create CO agnostic interface. Err: %v", err)
return err
}
if commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.TriggerCsiFullSync) {
log.Infof("Triggerfullsync feature enabled")
err := k8s.CreateCustomResourceDefinitionFromManifest(ctx, internalapiscnsoperatorconfig.EmbedTriggerCsiFullSync,
internalapiscnsoperatorconfig.EmbedTriggerCsiFullSyncName)
if err != nil {
log.Errorf("Failed to create %q CRD. Err: %+v", internalapis.TriggerCsiFullSyncPlural, err)
return err
}
// Get a config to talk to the apiserver.
restConfig, err := config.GetConfig()
if err != nil {
log.Errorf("failed to get Kubernetes config. Err: %+v", err)
return err
}
cnsOperatorClient, err := k8s.NewClientForGroup(ctx, restConfig, cnsoperatorv1alpha1.GroupName)
if err != nil {
log.Errorf("Failed to create CnsOperator client. Err: %+v", err)
return err
}
// Check if TriggerCsiFullSync instance is present. If not present,
// create the TriggerCsiFullSync instance with name "csifullsync".
// If present, update the TriggerCsiFullSync.Status.InProgress to false if
// a full sync is already running.
triggerCsiFullSyncInstance := &triggercsifullsyncv1alpha1.TriggerCsiFullSync{}
key := k8stypes.NamespacedName{Namespace: "", Name: common.TriggerCsiFullSyncCRName}
if err := cnsOperatorClient.Get(ctx, key, triggerCsiFullSyncInstance); err != nil {
if apierrors.IsNotFound(err) {
newtriggerCsiFullSyncInstance := triggercsifullsyncv1alpha1.CreateTriggerCsiFullSyncInstance()
if err := cnsOperatorClient.Create(ctx, newtriggerCsiFullSyncInstance); err != nil {
log.Errorf("Failed to create TriggerCsiFullSync instance: %q. Error: %v",
common.TriggerCsiFullSyncCRName, err)
return err
}
log.Infof("Created the a new instance of %q TriggerCsiFullSync instance as it was not found.",
common.TriggerCsiFullSyncCRName)
} else {
log.Errorf("Failed to get TriggerCsiFullSync instance: %q. Error: %v",
common.TriggerCsiFullSyncCRName, err)
return err
}
}
if triggerCsiFullSyncInstance.Status.InProgress {
log.Infof("Found %q instance with InProgress set to true on syncer startup. "+
"Resetting InProgress field to false as no full sync is currently running",
common.TriggerCsiFullSyncCRName)
triggerCsiFullSyncInstance.Status.InProgress = false
if err := cnsOperatorClient.Update(ctx, triggerCsiFullSyncInstance); err != nil {
log.Errorf("Failed to update TriggerCsiFullSync instance: %q with Status.InProgress set to false. "+
"Error: %v", common.TriggerCsiFullSyncCRName, err)
return err
}
}
}
return nil
}
// watcher watches on the vsphere.conf file mounted as secret within the syncer
// container.
func watcher(ctx context.Context, cnsOperator *cnsOperator) error {
log := logger.GetLogger(ctx)
cfgPath := commonconfig.GetConfigPath(ctx)
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Errorf("Failed to create fsnotify watcher. Err: %+v", err)
return err
}
go func() {
for {
log.Debugf("Waiting for event on fsnotify watcher")
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Debugf("fsnotify event: %q", event.String())
if event.Op&fsnotify.Remove == fsnotify.Remove {
log.Infof("Reloading Configuration")
err := reloadConfiguration(ctx, cnsOperator)
if err != nil {
log.Infof("Failed to reload configuration from: %q. Err: %+v", cfgPath, err)
} else {
log.Infof("Successfully reloaded configuration from: %q", cfgPath)
}
}
case err, ok := <-watcher.Errors:
if !ok {
log.Errorf("fsnotify error: %+v", err)
return
}
}
log.Debugf("fsnotify event processed")
}
}()
cfgDirPath := filepath.Dir(cfgPath)
log.Infof("Adding watch on path: %q", cfgDirPath)
err = watcher.Add(cfgDirPath)
if err != nil {
log.Errorf("Failed to watch on path: %q. err=%v", cfgDirPath, err)
}
return err
}
// reloadConfiguration reloads configuration from the secret, and cnsOperator
// with the latest configInfo.
func reloadConfiguration(ctx context.Context, cnsOperator *cnsOperator) error {
log := logger.GetLogger(ctx)
cfg, err := commonconfig.GetConfig(ctx)
if err != nil {
log.Errorf("Failed to read config. Error: %+v", err)
return err
}
cnsOperator.configInfo = &commonconfig.ConfigurationInfo{Cfg: cfg}
log.Infof("Reloaded the value for CnsRegisterVolumesCleanupIntervalInMin to %d",
cnsOperator.configInfo.Cfg.Global.CnsRegisterVolumesCleanupIntervalInMin)
return nil
}