-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
474 lines (390 loc) · 13.8 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
Copyright 2021.
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 (
"context"
"flag"
"fmt"
"net/url"
"os"
"strconv"
"strings"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/session/cache"
"github.com/vmware/govmomi/vapi/rest"
"github.com/vmware/govmomi/vapi/tags"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"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"
"sigs.k8s.io/controller-runtime/pkg/manager"
topologyv1 "vkubeviewer/api/v1"
"vkubeviewer/controllers"
//+kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(topologyv1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
// createDatastoreInfo creates DatastoreInfo CR in current k8s cluster
func createDatastoreInfo(ctx context.Context, mgr manager.Manager, vim25client *vim25.Client) error {
m := view.NewManager(vim25client)
vds, err := m.CreateContainerView(ctx, vim25client.ServiceContent.RootFolder, []string{"Datastore"}, true)
if err != nil {
msg := fmt.Sprintf("unable to create container view for Datastore: error %s", err)
setupLog.Info(msg)
} else {
msg := fmt.Sprintf("succeed to create container view for Datastore")
setupLog.Info(msg)
}
defer vds.Destroy(ctx)
var dss []mo.Datastore
err = vds.Retrieve(ctx, []string{"Datastore"}, []string{"name"}, &dss)
if err != nil {
msg := fmt.Sprintf("unable to retrieve Datastore info: error %s", err)
setupLog.Info(msg)
} else {
msg := fmt.Sprintf("succeed to retrieve Datastore info")
setupLog.Info(msg)
}
// ------------
// Create DatastoreInfo with K8s CRD
// ------------
c := mgr.GetClient()
for _, ds := range dss {
datastore := &topologyv1.DatastoreInfo{
TypeMeta: metav1.TypeMeta{Kind: "DatastoreInfo", APIVersion: "topology.vkubeviewer.com/v1"},
ObjectMeta: metav1.ObjectMeta{Name: strings.ToLower(ds.Name), Namespace: "default"},
Spec: topologyv1.DatastoreInfoSpec{Datastore: ds.Name},
Status: topologyv1.DatastoreInfoStatus{},
}
if err := c.Create(ctx, datastore); err != nil {
setupLog.Error(err, "unable to create Datastore")
} else {
msg := fmt.Sprintf("Create DatastoreInfo object %s", ds.Name)
setupLog.Info(msg)
}
}
return err
}
// createFCDInfo creates FCDInfo CR in current k8s cluster
func createFCDInfo(ctx context.Context, mgr manager.Manager, vim25client *vim25.Client) error {
var k8spv = controllers.ListK8sPV()
var err error
c := mgr.GetClient()
for index, fcd := range k8spv {
fcdindo := &topologyv1.FCDInfo{
TypeMeta: metav1.TypeMeta{Kind: "FCDInfo", APIVersion: "topology.vkubeviewer.com/v1"},
ObjectMeta: metav1.ObjectMeta{Name: "fcd" + strconv.Itoa(index), Namespace: "default"},
Spec: topologyv1.FCDInfoSpec{PVId: fcd},
Status: topologyv1.FCDInfoStatus{},
}
if err := c.Create(ctx, fcdindo); err != nil {
setupLog.Error(err, "unable to create FCDInfo object")
} else {
msg := fmt.Sprintf("Create FCDInfo object %s", fcd)
setupLog.Info(msg)
}
}
return err
}
// createHostInfo creates HostInfo CR in current k8s cluster
func createHostInfo(ctx context.Context, mgr manager.Manager, vim25client *vim25.Client) error {
m := view.NewManager(vim25client)
vhosts, err := m.CreateContainerView(ctx, vim25client.ServiceContent.RootFolder, []string{"HostSystem"}, true)
if err != nil {
msg := fmt.Sprintf("unable to create container view for HostSystem: error %s", err)
setupLog.Info(msg)
} else {
msg := fmt.Sprintf("succeed to create container view for HostSystem")
setupLog.Info(msg)
}
defer vhosts.Destroy(ctx)
var hosts []mo.HostSystem
err = vhosts.Retrieve(ctx, []string{"HostSystem"}, []string{"name"}, &hosts)
if err != nil {
msg := fmt.Sprintf("unable to retrieve HostSystem info: error %s", err)
setupLog.Info(msg)
} else {
msg := fmt.Sprintf("succeed to retrieve HostSystem info")
setupLog.Info(msg)
}
// ------------
// Create DatastoreInfo with K8s CRD
// ------------
c := mgr.GetClient()
for _, host := range hosts {
hostinfo := &topologyv1.HostInfo{
TypeMeta: metav1.TypeMeta{Kind: "HostInfo", APIVersion: "topology.vkubeviewer.com/v1"},
ObjectMeta: metav1.ObjectMeta{Name: strings.ToLower(host.Name), Namespace: "default"},
Spec: topologyv1.HostInfoSpec{Hostname: host.Name},
Status: topologyv1.HostInfoStatus{},
}
if err := c.Create(ctx, hostinfo); err != nil {
setupLog.Error(err, "unable to create Host")
} else {
msg := fmt.Sprintf("Create HostInfo object %s", host.Name)
setupLog.Info(msg)
}
}
return err
}
// createNodeInfo creates NodeInfo CR in current k8s cluster
func createNodeInfo(ctx context.Context, mgr manager.Manager, vim25client *vim25.Client) error {
var k8snodes = controllers.ListK8sNodes()
// ------------
// Create DatastoreInfo with K8s CRD
// ------------
c := mgr.GetClient()
var err error
for _, n := range k8snodes {
node := &topologyv1.NodeInfo{
TypeMeta: metav1.TypeMeta{Kind: "NodeInfo", APIVersion: "topology.vkubeviewer.com/v1"},
ObjectMeta: metav1.ObjectMeta{Name: strings.ToLower(n), Namespace: "default"},
Spec: topologyv1.NodeInfoSpec{Nodename: n},
Status: topologyv1.NodeInfoStatus{},
}
if err := c.Create(ctx, node); err != nil {
setupLog.Error(err, "unable to create Node")
return err
} else {
msg := fmt.Sprintf("Create NodeInfo object %s", n)
setupLog.Info(msg)
}
}
return err
}
// createTagInfo creates TagInfo CR in current k8s cluster
func createTagInfo(ctx context.Context, mgr manager.Manager, restclient *rest.Client) error {
tm := tags.NewManager(restclient)
// ------------
// Create DatastoreInfo with K8s CRD
// ------------
tags, err := tm.GetTags(ctx)
if err != nil {
msg := fmt.Sprintf("unable to retrieve Tag Lists : error %s", err)
setupLog.Info(msg)
} else {
msg := fmt.Sprintf("succeed to retrieve Tag Lists")
setupLog.Info(msg)
}
c := mgr.GetClient()
for index, tag := range tags {
tag := &topologyv1.TagInfo{
TypeMeta: metav1.TypeMeta{Kind: "TagInfo", APIVersion: "topology.vkubeviewer.com/v1"},
ObjectMeta: metav1.ObjectMeta{Name: "tag" + strconv.Itoa(index), Namespace: "default"},
Spec: topologyv1.TagInfoSpec{Tagname: tag.Name},
Status: topologyv1.TagInfoStatus{},
}
if err := c.Create(ctx, tag); err != nil {
setupLog.Error(err, "unable to create TagInfo object")
} else {
msg := fmt.Sprintf("Create TagInfo object %s", tag.Name)
setupLog.Info(msg)
}
}
return err
}
// vlogin logins with vSphere with two Clients we use in the operator
// vim25.Client, the most usable one, connects SOAP service
// rest.Client extends soap.Client to support JSON encoding
func vlogin(ctx context.Context, vc, user, pwd string) (*vim25.Client, *rest.Client, error) {
// This section allows for insecure govmomi logins
var insecure bool
flag.BoolVar(&insecure, "insecure", true, "ignore any vCenter TLS cert validation error")
// Create a vSphere/vCenter client
// The govmomi client requires a URL object, u.
// You cannot use a string representation of the vCenter URL.
// soap.ParseURL provides the correct object format.
u, err := soap.ParseURL(vc)
if u == nil {
setupLog.Error(err, "Unable to parse URL. Are required environment variables set?", "controller", "NodeInfo")
os.Exit(1)
}
if err != nil {
setupLog.Error(err, "URL parsing not successful", "controller", "NodeInfo")
os.Exit(1)
}
u.User = url.UserPassword(user, pwd)
// Session cache example taken from https://github.com/vmware/govmomi/blob/master/examples/examples.go
// Share govc's session cache
s := &cache.Session{
URL: u,
Insecure: true,
}
// Create new vim25 client
vim25client := new(vim25.Client)
// Login using client vim25client and cache s
err = s.Login(ctx, vim25client, nil)
if err != nil {
setupLog.Error(err, "Vkubeviewer: vim25 login not successful", "manager", "Vkubeviewer")
os.Exit(1)
}
// Create new rest client
restclient := rest.NewClient(vim25client)
err = restclient.Login(ctx, u.User)
if err != nil {
setupLog.Error(err, "Vkubeviewer: rest login not successful", "controller", "Vkubeviewer")
os.Exit(1)
}
return vim25client, restclient, nil
}
func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "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, "leader-elect", 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)))
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "982940d6.vkubeviewer.com",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
// Retrieve vCenter URL, username and password from environment variables
// These are provided via the manager manifest when controller is deployed
vc := os.Getenv("GOVMOMI_URL")
user := os.Getenv("GOVMOMI_USERNAME")
pwd := os.Getenv("GOVMOMI_PASSWORD")
// Create context, and get vSphere session information
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
vim25client, restclient, err := vlogin(ctx, vc, user, pwd)
if err != nil {
setupLog.Error(err, "unable to get login session to vSphere")
os.Exit(1)
} else {
setupLog.Info("succeed to get login session to vSphere")
}
finder := find.NewFinder(vim25client, true)
// find and set the default datacenter
dc, err := finder.DefaultDatacenter(ctx)
if err != nil {
setupLog.Error(err, "Manager: Could not get default datacenter")
} else {
finder.SetDatacenter(dc)
}
err = createDatastoreInfo(ctx, mgr, vim25client)
if err != nil {
setupLog.Error(err, "Manager: Could not create DatastorInfo")
}
err = createHostInfo(ctx, mgr, vim25client)
if err != nil {
setupLog.Error(err, "Manager: Could not create HostInfo")
}
err = createNodeInfo(ctx, mgr, vim25client)
if err != nil {
setupLog.Error(err, "Manager: Could not create NodeInfo")
}
err = createTagInfo(ctx, mgr, restclient)
if err != nil {
setupLog.Error(err, "Manager: Could not create TagInfo")
}
err = createFCDInfo(ctx, mgr, vim25client)
if err != nil {
setupLog.Error(err, "Manager: Could not create FCDInfo")
}
//Modified Reconcile call
if err = (&controllers.FCDInfoReconciler{
Client: mgr.GetClient(),
VC: vim25client,
Finder: finder,
Log: ctrl.Log.WithName("controllers").WithName("FCDInfo"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FCDInfo")
os.Exit(1)
}
if err = (&controllers.NodeInfoReconciler{
Client: mgr.GetClient(),
VC_vim25: vim25client,
VC_rest: restclient,
Log: ctrl.Log.WithName("controllers").WithName("NodeInfo"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "NodeInfo")
os.Exit(1)
}
if err = (&controllers.HostInfoReconciler{
Client: mgr.GetClient(),
VC: vim25client,
Log: ctrl.Log.WithName("controllers").WithName("HostInfo"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "HostInfo")
os.Exit(1)
}
if err = (&controllers.DatastoreInfoReconciler{
Client: mgr.GetClient(),
VC: vim25client,
Log: ctrl.Log.WithName("controllers").WithName("DatastoreInfo"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DatastoreInfo")
os.Exit(1)
}
if err = (&controllers.TagInfoReconciler{
Client: mgr.GetClient(),
VC_vim25: vim25client,
VC_rest: restclient,
Log: ctrl.Log.WithName("controllers").WithName("TagInfo"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "TagInfo")
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)
}
}