diff --git a/pkg/cloudprovider/provider/vsphere/helper.go b/pkg/cloudprovider/provider/vsphere/helper.go index 27b7ccf14..db08f9dbc 100644 --- a/pkg/cloudprovider/provider/vsphere/helper.go +++ b/pkg/cloudprovider/provider/vsphere/helper.go @@ -465,7 +465,12 @@ func attachTags(ctx context.Context, config *Config, vm *object.VirtualMachine) tagManager := tags.NewManager(restAPISession.Client) klog.V(3).Info("Attaching tags") for _, tag := range config.Tags { - if err := tagManager.AttachTag(ctx, tag.ID, vm.Reference()); err != nil { + tagID, err := determineTagID(ctx, tagManager, tag) + if err != nil { + return err + } + + if err := tagManager.AttachTag(ctx, tagID, vm.Reference()); err != nil { klog.V(3).Infof("Failed to attach tag %v. The tag was successfully deleted", tag) return fmt.Errorf("failed to attach tag to VM: %v %w", tag.Name, err) } @@ -487,7 +492,12 @@ func detachTags(ctx context.Context, config *Config, vm *object.VirtualMachine) } klog.V(3).Info("Deleting tags") for _, tag := range attachedTags { - err := tagManager.DetachTag(ctx, tag.ID, vm.Reference()) + tagID, err := determineTagID(ctx, tagManager, tag) + if err != nil { + return err + } + + err = tagManager.DetachTag(ctx, tagID, vm.Reference()) if err != nil { return fmt.Errorf("failed to delete tag: %v %w", tag, err) } @@ -495,3 +505,15 @@ func detachTags(ctx context.Context, config *Config, vm *object.VirtualMachine) return nil } + +func determineTagID(ctx context.Context, tagManager *tags.Manager, tag tags.Tag) (string, error) { + if tag.ID != "" { + return tag.ID, nil + } + + apiTag, err := tagManager.GetTagForCategory(ctx, tag.Name, tag.CategoryID) + if err != nil { + return "", fmt.Errorf("failed to retrieve tag: %v %w", tag.Name, err) + } + return apiTag.ID, nil +} diff --git a/pkg/cloudprovider/provider/vsphere/provider.go b/pkg/cloudprovider/provider/vsphere/provider.go index a7e47acae..481a2e519 100644 --- a/pkg/cloudprovider/provider/vsphere/provider.go +++ b/pkg/cloudprovider/provider/vsphere/provider.go @@ -226,11 +226,8 @@ func (p *provider) Validate(ctx context.Context, spec clusterv1alpha1.MachineSpe tagManager := tags.NewManager(restAPISession.Client) klog.V(3).Info("Found tags") for _, tag := range config.Tags { - if tag.ID == "" { - return fmt.Errorf("one of the tags id is empty") - } - if tag.Name == "" { - return fmt.Errorf("one of the tags name is empty") + if tag.ID == "" && tag.Name == "" { + return fmt.Errorf("either tag id or name must be specified") } if tag.CategoryID == "" { return fmt.Errorf("one of the tags category is empty") diff --git a/pkg/cloudprovider/provider/vsphere/types/types.go b/pkg/cloudprovider/provider/vsphere/types/types.go index b584cb37d..62331ee76 100644 --- a/pkg/cloudprovider/provider/vsphere/types/types.go +++ b/pkg/cloudprovider/provider/vsphere/types/types.go @@ -50,8 +50,8 @@ type RawConfig struct { // Tag represents vsphere tag. type Tag struct { Description string `json:"description,omitempty"` - ID string `json:"id"` - Name string `json:"name"` + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` CategoryID string `json:"categoryID"` }