Skip to content

Commit

Permalink
Fixes session active check with datacenter scoped TKG admin account
Browse files Browse the repository at this point in the history
  • Loading branch information
laozc committed Aug 16, 2023
1 parent 1a94554 commit c0e6df0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 14 additions & 0 deletions pkg/session/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package session
import (
"fmt"
"strings"

"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
)

const (
Expand All @@ -37,3 +40,14 @@ func (e unidentifiedVCenterVersion) Error() string {
func IsUnidentifiedVCenterVersion(err error) bool {
return strings.HasPrefix(err.Error(), errString)
}

func HasNoPermission(err error) bool {
if soap.IsSoapFault(err) {
soapError := soap.ToSoapFault(err)
if _, ok := soapError.VimFault().(types.NoPermission); ok {
return true
}
}

return false
}
14 changes: 13 additions & 1 deletion pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/vmware/govmomi/vapi/tags"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
ctrl "sigs.k8s.io/controller-runtime"

Expand Down Expand Up @@ -133,7 +134,18 @@ func GetOrCreate(ctx context.Context, params *Params) (*Session, error) {

vimSessionActive, err := s.SessionManager.SessionIsActive(ctx)
if err != nil {
logger.Error(err, "unable to check if vim session is active")
if HasNoPermission(err) && s.datacenter != nil {
var dc mo.Datacenter
err = s.Client.RetrieveOne(ctx, s.datacenter.Reference(), nil, &dc)
vimSessionActive = err == nil
if vimSessionActive {
logger.Info("vim session is active as the datacenter is found")
}
}

if err != nil {
logger.Error(err, "unable to check if vim session is active")
}
}

tagManagerSession, err := s.TagManager.Session(ctx)
Expand Down

0 comments on commit c0e6df0

Please sign in to comment.