Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ID an optional field for vSphere tags #1549

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions pkg/cloudprovider/provider/vsphere/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -487,11 +492,28 @@ 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)
}
}

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
}
7 changes: 2 additions & 5 deletions pkg/cloudprovider/provider/vsphere/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudprovider/provider/vsphere/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down