Skip to content

Commit

Permalink
Use new govmomi rest error check
Browse files Browse the repository at this point in the history
  • Loading branch information
rikatz committed Dec 13, 2023
1 parent 2b1b1aa commit 3299124
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 42 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/vmware-tanzu/vm-operator/api v1.8.3-0.20231114230806-852c1641447a
github.com/vmware-tanzu/vm-operator/external/ncp v0.0.0-20211209213435-0f4ab286f64f
github.com/vmware-tanzu/vm-operator/external/tanzu-topology v0.0.0-20211209213435-0f4ab286f64f
github.com/vmware/govmomi v0.33.1
github.com/vmware/govmomi v0.34.0
golang.org/x/crypto v0.16.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/mod v0.14.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,8 @@ github.com/vmware-tanzu/vm-operator/external/ncp v0.0.0-20211209213435-0f4ab286f
github.com/vmware-tanzu/vm-operator/external/ncp v0.0.0-20211209213435-0f4ab286f64f/go.mod h1:5rqRJ9zGR+KnKbkGx373WgN8xJpvAj99kHnfoDYRO5I=
github.com/vmware-tanzu/vm-operator/external/tanzu-topology v0.0.0-20211209213435-0f4ab286f64f h1:wwYUf16/g8bLywQMQJB5VHbDtuf6aOFH24Ar2/yA7+I=
github.com/vmware-tanzu/vm-operator/external/tanzu-topology v0.0.0-20211209213435-0f4ab286f64f/go.mod h1:dfYrWS8DMRN+XZfhu8M4LVHmeGvYB29Ipd7j4uIq+mU=
github.com/vmware/govmomi v0.33.1 h1:qS2VpEBd/WLbzLO5McI6h5o5zaKsrezUxRY5r9jkW8A=
github.com/vmware/govmomi v0.33.1/go.mod h1:QuzWGiEMA/FYlu5JXKjytiORQoxv2hTHdS2lWnIqKMM=
github.com/vmware/govmomi v0.34.0 h1:Aun71BDf1t8r3jNeUWJ3ZM+7kHbIuuNzIuxRVo5LYYU=
github.com/vmware/govmomi v0.34.0/go.mod h1:qWWT6n9mdCr/T9vySsoUqcI04sSEj4CqHXxtk/Y+Los=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ=
Expand Down
17 changes: 6 additions & 11 deletions pkg/services/govmomi/clustermodules/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ package clustermodules

import (
"context"
"fmt"
"net/http"
"strings"

"github.com/vmware/govmomi/vapi/cluster"
"github.com/vmware/govmomi/vapi/rest"
"github.com/vmware/govmomi/vim25/types"
ctrl "sigs.k8s.io/controller-runtime"

"sigs.k8s.io/cluster-api-provider-vsphere/pkg/util"
)

// Provider exposes methods to interact with the cluster module vCenter API
Expand Down Expand Up @@ -74,7 +70,7 @@ func (cm *provider) DeleteModule(ctx context.Context, moduleUUID string) error {
log.Info("Deleting cluster module")

err := cm.manager.DeleteModule(ctx, moduleUUID)
if err != nil && !util.IsNotFoundError(err) {
if err != nil && !rest.IsStatusError(err, http.StatusNotFound) {
return err
}

Expand All @@ -83,23 +79,22 @@ func (cm *provider) DeleteModule(ctx context.Context, moduleUUID string) error {
}

// DoesModuleExist checks whether a module with a given name exists with the passed clusterRef and moduleUUID.
func (cm *provider) DoesModuleExist(ctx context.Context, moduleUUID string, clusterRef types.ManagedObjectReference) (bool, error) {
func (cm *provider) DoesModuleExist(ctx context.Context, moduleUUID string, _ types.ManagedObjectReference) (bool, error) {
log := ctrl.LoggerFrom(ctx)
log.V(4).Info("Checking if cluster module exists", "computeClusterRef", clusterRef)
log.V(4).Info("Checking if cluster module exists")

if moduleUUID == "" {
return false, nil
}

_, err := cm.manager.ListModuleMembers(ctx, moduleUUID)
if err == nil {
log.V(4).Info("Cluster module exists", "moduleUUID", moduleUUID)
log.V(4).Info("Cluster module exists")
return true, nil
}

// TODO: Once govmomi supports error checking for rest client, replace the below function
if strings.Contains(err.Error(), fmt.Sprintf("%d %s", http.StatusNotFound, http.StatusText(http.StatusNotFound))) {
log.V(4).Info("Cluster module doesn't exist", "moduleUUID", moduleUUID)
if rest.IsStatusError(err, http.StatusNotFound) {
log.V(4).Info("Cluster module doesn't exist")
return false, nil

Check warning on line 98 in pkg/services/govmomi/clustermodules/provider.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/govmomi/clustermodules/provider.go#L96-L98

Added lines #L96 - L98 were not covered by tests
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/services/govmomi/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import (
"context"
"encoding/base64"
"fmt"
"net/http"
"time"

"github.com/pkg/errors"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/pbm"
pbmTypes "github.com/vmware/govmomi/pbm/types"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vapi/rest"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -268,7 +270,7 @@ func (vms *VMService) DestroyVM(ctx context.Context, vmCtx *capvcontext.VMContex

provider := clustermodules.NewProvider(vmCtx.Session.TagManager.Client)
err := provider.RemoveMoRefFromModule(ctx, *vmCtx.ClusterModuleInfo, virtualMachineCtx.Ref)
if err != nil && !util.IsNotFoundError(err) {
if err != nil && !rest.IsStatusError(err, http.StatusNotFound) {

Check warning on line 273 in pkg/services/govmomi/service.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/govmomi/service.go#L273

Added line #L273 was not covered by tests
return reconcile.Result{}, vm, err
}
vmCtx.VSphereVM.Status.ModuleUUID = nil
Expand Down
27 changes: 0 additions & 27 deletions pkg/util/errors.go

This file was deleted.

0 comments on commit 3299124

Please sign in to comment.