From 0cdddd4200da4ef2969eaffdfaa847d72938a29a Mon Sep 17 00:00:00 2001 From: Richard Vanderpool <49568690+rvanderp3@users.noreply.github.com> Date: Fri, 1 Dec 2023 15:48:43 -0500 Subject: [PATCH] reconcile a slice of additional tag IDs --- pkg/controller/vsphere/reconciler.go | 73 +++++++++++++------ .../vsphere/session/tag_ids_caching_client.go | 10 +-- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/pkg/controller/vsphere/reconciler.go b/pkg/controller/vsphere/reconciler.go index 97a11b50f..c0f1b03dc 100644 --- a/pkg/controller/vsphere/reconciler.go +++ b/pkg/controller/vsphere/reconciler.go @@ -250,7 +250,7 @@ func (r *Reconciler) update() error { Ref: vmRef, } - if err := vm.reconcileTags(r.Context, r.session, r.machine); err != nil { + if err := vm.reconcileTags(r.Context, r.session, r.machine, r.providerSpec); err != nil { metrics.RegisterFailedInstanceUpdate(&metrics.MachineLabels{ Name: r.machine.Name, Namespace: r.machine.Namespace, @@ -1314,25 +1314,28 @@ func (vm *virtualMachine) getPowerState() (types.VirtualMachinePowerState, error // reconcileTags ensures that the required tags are present on the virtual machine, eg the Cluster ID // that is used by the installer on cluster deletion to ensure ther are no leaked resources. -func (vm *virtualMachine) reconcileTags(ctx context.Context, sessionInstance *session.Session, machine *machinev1.Machine) error { +func (vm *virtualMachine) reconcileTags(ctx context.Context, sessionInstance *session.Session, machine *machinev1.Machine, providerSpec *machinev1.VSphereMachineProviderSpec) error { if err := sessionInstance.WithCachingTagsManager(vm.Context, func(c *session.CachingTagsManager) error { klog.Infof("%v: Reconciling attached tags", machine.GetName()) clusterID := machine.Labels[machinev1.MachineClusterIDLabel] - - attached, err := vm.checkAttachedTag(ctx, clusterID, c) - if err != nil { - return err - } - - if !attached { - klog.Infof("%v: Attaching %s tag to vm", machine.GetName(), clusterID) - // the tag should already be created by installer - if err := c.AttachTag(ctx, clusterID, vm.Ref); err != nil { + tagIDs := []string{clusterID} + tagIDs = append(tagIDs, providerSpec.TagIDs...) + klog.Infof("%v: Reconciling %s tags to vm", machine.GetName(), tagIDs) + for _, tagID := range tagIDs { + attached, err := vm.checkAttachedTag(ctx, tagID, c) + if err != nil { return err } - } + if !attached { + klog.Infof("%v: Attaching %s tag to vm", machine.GetName(), tagID) + // the tag should already be created by installer or the administrator + if err := c.AttachTag(ctx, tagID, vm.Ref); err != nil { + return err + } + } + } return nil }); err != nil { return err @@ -1359,9 +1362,16 @@ func (vm *virtualMachine) checkAttachedTag(ctx context.Context, tagName string, } for _, tag := range tags { - if tag.Name == tagName { - return true, nil + if session.IsName(tagName) { + if tag.Name == tagName { + return true, nil + } + } else { + if tag.ID == tagName { + return true, nil + } } + } return false, nil @@ -1377,22 +1387,37 @@ func tagToCategoryName(tagName string) string { } func (vm *virtualMachine) foundTag(ctx context.Context, tagName string, m *session.CachingTagsManager) (bool, error) { - tags, err := m.ListTagsForCategory(ctx, tagToCategoryName(tagName)) - if err != nil { - if isNotFoundErr(err) { - return false, nil + var tags []string + var err error + + if session.IsName(tagName) { + tags, err = m.ListTagsForCategory(ctx, tagToCategoryName(tagName)) + if err != nil { + if isNotFoundErr(err) { + return false, nil + } + return false, err } - return false, err + } else { + tags = []string{tagName} } - + klog.V(3).Infof("validating the presence of tags: %+v", tags) for _, id := range tags { tag, err := m.GetTag(ctx, id) if err != nil { return false, err } - - if tag.Name == tagName { - return true, nil + klog.V(3).Infof("checking tag: %+v", tag) + if session.IsName(tagName) { + if tag.Name == tagName { + klog.V(3).Infof("name tag: %+v", tag) + return true, nil + } + } else { + if tag.ID == tagName { + klog.V(3).Infof("URN tag: %+v", tag) + return true, nil + } } } diff --git a/pkg/controller/vsphere/session/tag_ids_caching_client.go b/pkg/controller/vsphere/session/tag_ids_caching_client.go index 0aac3e091..65a58e051 100644 --- a/pkg/controller/vsphere/session/tag_ids_caching_client.go +++ b/pkg/controller/vsphere/session/tag_ids_caching_client.go @@ -107,11 +107,11 @@ func newTagsCachingClient(tagsManager *tags.Manager, sessionKey string) *Caching } } -// isName returns true if the id is not an urn. +// IsName returns true if the id is not an urn. // this method came from vSphere sdk, // see https://github.com/vmware/govmomi/blob/a2fb82dc55a8eb00932233aa8028ce97140df784/vapi/tags/tags.go#L121 for // more context. -func isName(id string) bool { +func IsName(id string) bool { return !strings.HasPrefix(id, "urn:") } @@ -128,7 +128,7 @@ func isObjectNotFoundErr(err error) bool { // // In case if a tag was not found in vCenter, this would be cached for 12 hours and lookup won't happen till cache expiration. func (t *CachingTagsManager) GetTag(ctx context.Context, id string) (*tags.Tag, error) { - if !isName(id) { // if id is passed no cache check needed, use original GetTag method instantly + if !IsName(id) { // if id is passed no cache check needed, use original GetTag method instantly return t.Manager.GetTag(ctx, id) } @@ -181,7 +181,7 @@ func (t *CachingTagsManager) GetTag(ctx context.Context, id string) (*tags.Tag, // // In case if a tag was not found in vCenter, this would be cached for 12 hours and lookup won't happen till cache expiration. func (t *CachingTagsManager) GetCategory(ctx context.Context, id string) (*tags.Category, error) { - if !isName(id) { // if id is passed no cache check needed, use original GetTag method instantly + if !IsName(id) { // if id is passed no cache check needed, use original GetTag method instantly return t.Manager.GetCategory(ctx, id) } @@ -230,7 +230,7 @@ func (t *CachingTagsManager) GetCategory(ctx context.Context, id string) (*tags. // The id parameter can be a Category ID or Category Name. // Uses caching GetCategory method. func (t *CachingTagsManager) ListTagsForCategory(ctx context.Context, id string) ([]string, error) { - if isName(id) { + if IsName(id) { category, err := t.GetCategory(ctx, id) if err != nil { return nil, err