Skip to content

Commit f76a5f0

Browse files
committed
capv: allow no auth to vcenter
The assisted installer has no connection to vCenter until day 2. Make sure that assets generation functions for vSphere if there is no connection to vCenter.
1 parent 3a6a99a commit f76a5f0

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

pkg/asset/installconfig/vsphere/metadata.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package vsphere
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"path"
8+
"strings"
79
"sync"
810

911
"github.com/vmware/govmomi/object"
12+
"github.com/vmware/govmomi/vim25/soap"
1013
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/session"
1114

1215
"github.com/openshift/installer/pkg/types/vsphere"
@@ -122,10 +125,33 @@ func (m *Metadata) unlockedSession(ctx context.Context, server string) (*session
122125
return m.sessions[server], err
123126
}
124127

128+
// unwrapToSoapFault is required because soapErrorFaul is not exported
129+
// and are unable to use errors.As()
130+
// https://github.com/vmware/govmomi/blob/main/vim25/soap/error.go#L38
131+
func unwrapToSoapFault(err error) error {
132+
if err != nil {
133+
if soapFault := soap.IsSoapFault(err); !soapFault {
134+
return unwrapToSoapFault(errors.Unwrap(err))
135+
}
136+
return err
137+
}
138+
return err
139+
}
140+
125141
// Networks populates VCenterContext and the ClusterNetworkMap based on the vCenter server url and the FailureDomains.
126142
func (m *Metadata) Networks(ctx context.Context, vcenter vsphere.VCenter, failureDomains []vsphere.FailureDomain) error {
127143
_, err := m.Session(ctx, vcenter.Server)
128144
if err != nil {
145+
// Defense against potential issues with assisted installer
146+
if soapErr := unwrapToSoapFault(err); soapErr != nil {
147+
soapFault := soap.ToSoapFault(soapErr)
148+
// The assisted installer provides bogus username and password
149+
// values. Only return the soap error (fault) if it matches incorrect username or password.
150+
if strings.Contains(soapFault.String, "Cannot complete login due to an incorrect user name or password") {
151+
return soapErr
152+
}
153+
}
154+
// if soapErr is nil then this is not a SOAP fault, return err
129155
return err
130156
}
131157

pkg/asset/machines/clusterapi.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package machines
33
import (
44
"context"
55
"fmt"
6+
"net"
67
"path/filepath"
78
"strings"
9+
"time"
810

911
"github.com/pkg/errors"
1012
"github.com/sirupsen/logrus"
13+
"github.com/vmware/govmomi/vim25/soap"
1114
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1215
"k8s.io/utils/ptr"
1316
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -301,10 +304,41 @@ func (c *ClusterAPI) Generate(dependencies asset.Parents) error {
301304
mpool.Set(pool.Platform.VSphere)
302305

303306
platform := ic.VSphere
307+
resolver := &net.Resolver{
308+
PreferGo: true,
309+
}
304310

305311
for _, v := range platform.VCenters {
306-
err := installConfig.VSphere.Networks(ctx, v, platform.FailureDomains)
312+
// Defense against potential issues with assisted installer
313+
// If the installer is unable to resolve vCenter there is a good possibility
314+
// that the installer's install-config has been provided with bogus values.
315+
316+
// Timeout context for Lookup
317+
ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
318+
defer cancel()
319+
320+
_, err := resolver.LookupHost(ctx, v.Server)
321+
if err != nil {
322+
logrus.Warnf("unable to resolve vSphere server %s", v.Server)
323+
return nil
324+
}
325+
326+
// Timeout context for Networks
327+
// vCenter APIs can be unreliable in performance, extended this context
328+
// timeout to 60 seconds.
329+
ctx, cancel = context.WithTimeout(context.TODO(), 60*time.Second)
330+
defer cancel()
331+
332+
err = installConfig.VSphere.Networks(ctx, v, platform.FailureDomains)
307333
if err != nil {
334+
// If we are receiving an error as a Soap Fault this is caused by
335+
// incorrect credentials and in the scenario of assisted installer
336+
// the credentials are never valid. Since vCenter hostname is
337+
// incorrect as well we shouldn't get this far.
338+
if soap.IsSoapFault(err) {
339+
logrus.Warn("authentication failure to vCenter, Cluster API machine manifests not created, cluster may not install")
340+
return nil
341+
}
308342
return err
309343
}
310344
}

0 commit comments

Comments
 (0)