Skip to content

Commit

Permalink
reconcile a slice of additional tag IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanderp3 committed Dec 6, 2023
1 parent 442c206 commit 0cdddd4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 29 deletions.
73 changes: 49 additions & 24 deletions pkg/controller/vsphere/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/vsphere/session/tag_ids_caching_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:")
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0cdddd4

Please sign in to comment.