Skip to content

Commit 7af55b6

Browse files
committed
Include VM MoID in its logger
Use the VM's Status.UniqueID if set. Generally this will always be set on an existing VM. While here, move setting the govmomi types.ID into a helper function, and do a TODO of including our generated reconcile IDs in the logger if the orginal context do not have a controller-runtime ReconcileID.
1 parent a1cc17d commit 7af55b6

File tree

6 files changed

+116
-90
lines changed

6 files changed

+116
-90
lines changed

pkg/context/vc_context.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package context
6+
7+
import (
8+
"context"
9+
"math/rand"
10+
"strings"
11+
12+
"github.com/go-logr/logr"
13+
vimtypes "github.com/vmware/govmomi/vim25/types"
14+
"sigs.k8s.io/controller-runtime/pkg/client"
15+
"sigs.k8s.io/controller-runtime/pkg/controller"
16+
)
17+
18+
func getOpID(ctx context.Context, obj client.Object, operation string) (string, string) {
19+
opID := strings.Join([]string{"vmoperator", obj.GetName(), operation}, "-")
20+
21+
if recID := controller.ReconcileIDFromContext(ctx); len(recID) >= 8 {
22+
id := string(recID[:8])
23+
return opID + "-" + id, ""
24+
}
25+
26+
// Generate our own internal ID.
27+
const charset = "0123456789abcdef"
28+
buf := make([]byte, 8)
29+
for i := range buf {
30+
idx := rand.Intn(len(charset)) //nolint:gosec
31+
buf[i] = charset[idx]
32+
}
33+
id := string(buf)
34+
35+
return opID + "-" + id, id
36+
}
37+
38+
// WithVCOpID sets the vimtypes.ID in the context so that it is passed along to VC,
39+
// so we can easily correlate VMOP and VC logs.
40+
func WithVCOpID(ctx context.Context, obj client.Object, operation string) context.Context {
41+
vcOpID, intRecID := getOpID(ctx, obj, operation)
42+
if intRecID != "" {
43+
// If the context did not have a controller-runtime reconcile ID, include the one
44+
// that we generated in the context's logger. The underlying reconcile ID context
45+
// value type is not exported by controller-runtime so we cannot set that specific
46+
// reconcile type.
47+
if logger, err := logr.FromContext(ctx); err == nil {
48+
ctx = logr.NewContext(ctx, logger.WithValues("opID", intRecID))
49+
}
50+
}
51+
return context.WithValue(ctx, vimtypes.ID{}, vcOpID)
52+
}

pkg/context/virtualmachine_context.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
vimtypes "github.com/vmware/govmomi/vim25/types"
1515

1616
vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha5"
17+
pkglog "github.com/vmware-tanzu/vm-operator/pkg/log"
1718
)
1819

1920
type vmContextKey uint8
@@ -33,6 +34,20 @@ type VirtualMachineContext struct {
3334
MoVM mo.VirtualMachine
3435
}
3536

37+
func NewVirtualMachineContext(ctx context.Context, vm *vmopv1.VirtualMachine) VirtualMachineContext {
38+
logger := pkglog.FromContextOrDefault(ctx)
39+
if vm.Status.UniqueID != "" {
40+
logger = logger.WithValues("moID", vm.Status.UniqueID)
41+
ctx = logr.NewContext(ctx, logger)
42+
}
43+
44+
return VirtualMachineContext{
45+
Context: ctx,
46+
Logger: logger,
47+
VM: vm,
48+
}
49+
}
50+
3651
func (v VirtualMachineContext) String() string {
3752
if v.VM == nil {
3853
return ""

pkg/providers/vsphere/vmprovider.go

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"encoding/json"
1010
"fmt"
11-
"math/rand"
1211
"strings"
1312
"sync"
1413
"sync/atomic"
@@ -24,7 +23,6 @@ import (
2423
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2524
apierrorsutil "k8s.io/apimachinery/pkg/util/errors"
2625
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
27-
"sigs.k8s.io/controller-runtime/pkg/controller"
2826
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2927
"sigs.k8s.io/yaml"
3028

@@ -509,25 +507,6 @@ func (vs *vSphereVMProvider) UpdateContentLibraryItem(ctx context.Context, itemI
509507
return contentLibraryProvider.UpdateLibraryItem(ctx, itemID, newName, newDescription)
510508
}
511509

512-
func (vs *vSphereVMProvider) getOpID(ctx context.Context, obj ctrlclient.Object, operation string) string {
513-
var id string
514-
515-
if recID := controller.ReconcileIDFromContext(ctx); recID != "" {
516-
id = string(recID[:8])
517-
} else {
518-
const charset = "0123456789abcdef"
519-
buf := make([]byte, 8)
520-
for i := range buf {
521-
idx := rand.Intn(len(charset)) //nolint:gosec
522-
buf[i] = charset[idx]
523-
}
524-
id = string(buf)
525-
// TODO: Add this id as our own reconcile ID type?
526-
}
527-
528-
return strings.Join([]string{"vmoperator", obj.GetName(), operation, id}, "-")
529-
}
530-
531510
func (vs *vSphereVMProvider) getVM(
532511
vmCtx pkgctx.VirtualMachineContext,
533512
client *vcclient.Client,
@@ -627,15 +606,10 @@ func (vs *vSphereVMProvider) GetTasksByActID(ctx context.Context, vm *vmopv1.Vir
627606
taskManager := task.NewManager(vcClient.VimClient())
628607
filterSpec := vimtypes.TaskFilterSpec{}
629608
if vm != nil {
630-
vmCtx := pkgctx.VirtualMachineContext{
631-
Context: context.WithValue(
632-
ctx,
633-
vimtypes.ID{},
634-
vs.getOpID(ctx, vm, "GetCloneTasksForVM"),
635-
),
636-
Logger: logger,
637-
VM: vm,
638-
}
609+
vmCtx := pkgctx.NewVirtualMachineContext(
610+
pkgctx.WithVCOpID(ctx, vm, "getCloneTasksForVM"),
611+
vm,
612+
)
639613

640614
vcVM, err := vs.getVM(vmCtx, vcClient, true)
641615
if err != nil {

pkg/providers/vsphere/vmprovider_vm.go

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,10 @@ func (vs *vSphereVMProvider) createOrUpdateVirtualMachine(
183183
return nil, providers.ErrReconcileInProgress
184184
}
185185

186-
vmCtx := pkgctx.VirtualMachineContext{
187-
Context: context.WithValue(
188-
ctx,
189-
vimtypes.ID{},
190-
vs.getOpID(ctx, vm, "createOrUpdateVM"),
191-
),
192-
Logger: logger,
193-
VM: vm,
194-
}
186+
vmCtx := pkgctx.NewVirtualMachineContext(
187+
pkgctx.WithVCOpID(ctx, vm, "createOrUpdateVM"),
188+
vm,
189+
)
195190

196191
client, err := vs.getVcClient(vmCtx)
197192
if err != nil {
@@ -329,11 +324,10 @@ func (vs *vSphereVMProvider) CleanupVirtualMachine(
329324
return providers.ErrReconcileInProgress
330325
}
331326

332-
vmCtx := pkgctx.VirtualMachineContext{
333-
Context: context.WithValue(ctx, vimtypes.ID{}, vs.getOpID(ctx, vm, "cleanupVM")),
334-
Logger: pkglog.FromContextOrDefault(ctx),
335-
VM: vm,
336-
}
327+
vmCtx := pkgctx.NewVirtualMachineContext(
328+
pkgctx.WithVCOpID(ctx, vm, "cleanupVM"),
329+
vm,
330+
)
337331

338332
client, err := vs.getVcClient(vmCtx)
339333
if err != nil {
@@ -372,11 +366,10 @@ func (vs *vSphereVMProvider) DeleteVirtualMachine(
372366
return providers.ErrReconcileInProgress
373367
}
374368

375-
vmCtx := pkgctx.VirtualMachineContext{
376-
Context: context.WithValue(ctx, vimtypes.ID{}, vs.getOpID(ctx, vm, "deleteVM")),
377-
Logger: pkglog.FromContextOrDefault(ctx),
378-
VM: vm,
379-
}
369+
vmCtx := pkgctx.NewVirtualMachineContext(
370+
pkgctx.WithVCOpID(ctx, vm, "deleteVM"),
371+
vm,
372+
)
380373

381374
client, err := vs.getVcClient(vmCtx)
382375
if err != nil {
@@ -464,11 +457,10 @@ func (vs *vSphereVMProvider) PublishVirtualMachine(
464457
"vmName", vm.NamespacedName(), "clName", fmt.Sprintf("%s/%s", cl.Namespace, cl.Name))
465458
ctx = logr.NewContext(ctx, logger)
466459

467-
vmCtx := pkgctx.VirtualMachineContext{
468-
Context: context.WithValue(ctx, vimtypes.ID{}, fmt.Sprintf("%s-%s", vs.getOpID(ctx, vm, "publishVM"), actID)),
469-
Logger: logger,
470-
VM: vm,
471-
}
460+
vmCtx := pkgctx.NewVirtualMachineContext(
461+
pkgctx.WithVCOpID(ctx, vm, "publishVM-"+actID),
462+
vm,
463+
)
472464

473465
client, err := vs.getVcClient(ctx)
474466
if err != nil {
@@ -521,11 +513,10 @@ func (vs *vSphereVMProvider) GetVirtualMachineGuestHeartbeat(
521513
logger := pkglog.FromContextOrDefault(ctx).WithValues("vmName", vm.NamespacedName())
522514
ctx = logr.NewContext(ctx, logger)
523515

524-
vmCtx := pkgctx.VirtualMachineContext{
525-
Context: context.WithValue(ctx, vimtypes.ID{}, vs.getOpID(ctx, vm, "heartbeat")),
526-
Logger: logger,
527-
VM: vm,
528-
}
516+
vmCtx := pkgctx.NewVirtualMachineContext(
517+
pkgctx.WithVCOpID(ctx, vm, "heartbeat"),
518+
vm,
519+
)
529520

530521
client, err := vs.getVcClient(vmCtx)
531522
if err != nil {
@@ -553,11 +544,10 @@ func (vs *vSphereVMProvider) GetVirtualMachineProperties(
553544
logger := pkglog.FromContextOrDefault(ctx).WithValues("vmName", vm.NamespacedName())
554545
ctx = logr.NewContext(ctx, logger)
555546

556-
vmCtx := pkgctx.VirtualMachineContext{
557-
Context: context.WithValue(ctx, vimtypes.ID{}, vs.getOpID(ctx, vm, "properties")),
558-
Logger: logger,
559-
VM: vm,
560-
}
547+
vmCtx := pkgctx.NewVirtualMachineContext(
548+
pkgctx.WithVCOpID(ctx, vm, "properties"),
549+
vm,
550+
)
561551

562552
client, err := vs.getVcClient(vmCtx)
563553
if err != nil {
@@ -616,11 +606,10 @@ func (vs *vSphereVMProvider) GetVirtualMachineWebMKSTicket(
616606
logger := pkglog.FromContextOrDefault(ctx).WithValues("vmName", vm.NamespacedName())
617607
ctx = logr.NewContext(ctx, logger)
618608

619-
vmCtx := pkgctx.VirtualMachineContext{
620-
Context: context.WithValue(ctx, vimtypes.ID{}, vs.getOpID(ctx, vm, "webconsole")),
621-
Logger: logger,
622-
VM: vm,
623-
}
609+
vmCtx := pkgctx.NewVirtualMachineContext(
610+
pkgctx.WithVCOpID(ctx, vm, "webconsole"),
611+
vm,
612+
)
624613

625614
client, err := vs.getVcClient(vmCtx)
626615
if err != nil {
@@ -647,11 +636,10 @@ func (vs *vSphereVMProvider) GetVirtualMachineHardwareVersion(
647636
logger := pkglog.FromContextOrDefault(ctx).WithValues("vmName", vm.NamespacedName())
648637
ctx = logr.NewContext(ctx, logger)
649638

650-
vmCtx := pkgctx.VirtualMachineContext{
651-
Context: context.WithValue(ctx, vimtypes.ID{}, vs.getOpID(ctx, vm, "hardware-version")),
652-
Logger: logger,
653-
VM: vm,
654-
}
639+
vmCtx := pkgctx.NewVirtualMachineContext(
640+
pkgctx.WithVCOpID(ctx, vm, "hardware-version"),
641+
vm,
642+
)
655643

656644
client, err := vs.getVcClient(vmCtx)
657645
if err != nil {

pkg/providers/vsphere/vmprovider_vm_group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (vs *vSphereVMProvider) PlaceVirtualMachineGroup(
4545
group *vmopv1.VirtualMachineGroup,
4646
groupPlacements []providers.VMGroupPlacement) error {
4747

48-
ctx = context.WithValue(ctx, vimtypes.ID{}, vs.getOpID(ctx, group, "groupPlacement"))
48+
ctx = pkgctx.WithVCOpID(ctx, group, "groupPlacement")
4949

5050
vcClient, err := vs.getVcClient(ctx)
5151
if err != nil {

pkg/providers/vsphere/vmprovider_vm_snapshot.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,10 @@ func (vs *vSphereVMProvider) DeleteSnapshot(
4949
logger := pkglog.FromContextOrDefault(ctx).WithValues("vmName", vm.NamespacedName())
5050
ctx = logr.NewContext(ctx, logger)
5151

52-
vmCtx := pkgctx.VirtualMachineContext{
53-
Context: context.WithValue(ctx, vimtypes.ID{}, vs.getOpID(ctx, vm, "deleteSnapshot")),
54-
Logger: logger,
55-
VM: vm,
56-
}
52+
vmCtx := pkgctx.NewVirtualMachineContext(
53+
pkgctx.WithVCOpID(ctx, vm, "deleteSnapshot"),
54+
vm,
55+
)
5756

5857
client, err := vs.getVcClient(ctx)
5958
if err != nil {
@@ -93,11 +92,10 @@ func (vs *vSphereVMProvider) GetSnapshotSize(
9392
logger := pkglog.FromContextOrDefault(ctx).WithValues("vmName", vm.NamespacedName())
9493
ctx = logr.NewContext(ctx, logger)
9594

96-
vmCtx := pkgctx.VirtualMachineContext{
97-
Context: context.WithValue(ctx, vimtypes.ID{}, vs.getOpID(ctx, vm, "getSnapshotSize")),
98-
Logger: logger,
99-
VM: vm,
100-
}
95+
vmCtx := pkgctx.NewVirtualMachineContext(
96+
pkgctx.WithVCOpID(ctx, vm, "getSnapshotSize"),
97+
vm,
98+
)
10199

102100
client, err := vs.getVcClient(ctx)
103101
if err != nil {
@@ -131,11 +129,10 @@ func (vs *vSphereVMProvider) SyncVMSnapshotTreeStatus(ctx context.Context, vm *v
131129
logger := pkglog.FromContextOrDefault(ctx).WithValues("vmName", vm.NamespacedName())
132130
ctx = logr.NewContext(ctx, logger)
133131

134-
vmCtx := pkgctx.VirtualMachineContext{
135-
Context: context.WithValue(ctx, vimtypes.ID{}, vs.getOpID(ctx, vm, "syncVMSnapshotTreeStatus")),
136-
Logger: logger,
137-
VM: vm,
138-
}
132+
vmCtx := pkgctx.NewVirtualMachineContext(
133+
pkgctx.WithVCOpID(ctx, vm, "syncVMSnapshotTreeStatus"),
134+
vm,
135+
)
139136

140137
client, err := vs.getVcClient(ctx)
141138
if err != nil {

0 commit comments

Comments
 (0)