Skip to content

Commit 2729e65

Browse files
Merge pull request openshift#8059 from jcpowermac/capv-zonal
OCPBUGS-29860: double looping of failuredomains breaks zonal
2 parents b8d5671 + 06666c7 commit 2729e65

File tree

5 files changed

+124
-63
lines changed

5 files changed

+124
-63
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//go:build !altinfra
2+
// +build !altinfra
3+
4+
package vsphere
5+
6+
import (
7+
"context"
8+
"time"
9+
10+
"k8s.io/apimachinery/pkg/util/validation/field"
11+
)
12+
13+
// folderExists returns an error if a folder is specified in the vSphere platform but a folder with that name is not found in the datacenter.
14+
func folderExists(validationCtx *validationContext, folderPath string, fldPath *field.Path) field.ErrorList {
15+
allErrs := field.ErrorList{}
16+
finder := validationCtx.Finder
17+
// If no folder is specified, skip this check as the folder will be created.
18+
if folderPath == "" {
19+
return allErrs
20+
}
21+
22+
ctx, cancel := context.WithTimeout(context.TODO(), 60*time.Second)
23+
defer cancel()
24+
25+
folder, err := finder.Folder(ctx, folderPath)
26+
if err != nil {
27+
return append(allErrs, field.Invalid(fldPath, folderPath, err.Error()))
28+
}
29+
permissionGroup := permissions[permissionFolder]
30+
31+
err = comparePrivileges(ctx, validationCtx, folder.Reference(), permissionGroup)
32+
if err != nil {
33+
return append(allErrs, field.InternalError(fldPath, err))
34+
}
35+
return allErrs
36+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//go:build altinfra
2+
// +build altinfra
3+
4+
package vsphere
5+
6+
import (
7+
"context"
8+
"errors"
9+
"time"
10+
11+
"github.com/vmware/govmomi/find"
12+
"k8s.io/apimachinery/pkg/util/validation/field"
13+
)
14+
15+
// folderExists returns an error if a folder is specified in the vSphere platform but a folder with that name is not found in the datacenter.
16+
func folderExists(validationCtx *validationContext, folderPath string, fldPath *field.Path) field.ErrorList {
17+
var notFoundError *find.NotFoundError
18+
allErrs := field.ErrorList{}
19+
finder := validationCtx.Finder
20+
// If no folder is specified, skip this check as the folder will be created.
21+
if folderPath == "" {
22+
return allErrs
23+
}
24+
25+
ctx, cancel := context.WithTimeout(context.TODO(), 60*time.Second)
26+
defer cancel()
27+
28+
folder, err := finder.Folder(ctx, folderPath)
29+
if err != nil && !errors.As(err, &notFoundError) {
30+
return append(allErrs, field.Invalid(fldPath, folderPath, err.Error()))
31+
}
32+
33+
// folder was not found so no privilege check can be performed
34+
if folder == nil {
35+
return allErrs
36+
}
37+
permissionGroup := permissions[permissionFolder]
38+
39+
err = comparePrivileges(ctx, validationCtx, folder.Reference(), permissionGroup)
40+
if err != nil {
41+
return append(allErrs, field.InternalError(fldPath, err))
42+
}
43+
return allErrs
44+
}

pkg/asset/installconfig/vsphere/validation.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -190,31 +190,6 @@ func validateFailureDomain(validationCtx *validationContext, failureDomain *vsph
190190
return allErrs
191191
}
192192

193-
// folderExists returns an error if a folder is specified in the vSphere platform but a folder with that name is not found in the datacenter.
194-
func folderExists(validationCtx *validationContext, folderPath string, fldPath *field.Path) field.ErrorList {
195-
allErrs := field.ErrorList{}
196-
finder := validationCtx.Finder
197-
// If no folder is specified, skip this check as the folder will be created.
198-
if folderPath == "" {
199-
return allErrs
200-
}
201-
202-
ctx, cancel := context.WithTimeout(context.TODO(), 60*time.Second)
203-
defer cancel()
204-
205-
folder, err := finder.Folder(ctx, folderPath)
206-
if err != nil {
207-
return append(allErrs, field.Invalid(fldPath, folderPath, err.Error()))
208-
}
209-
permissionGroup := permissions[permissionFolder]
210-
211-
err = comparePrivileges(ctx, validationCtx, folder.Reference(), permissionGroup)
212-
if err != nil {
213-
return append(allErrs, field.InternalError(fldPath, err))
214-
}
215-
return allErrs
216-
}
217-
218193
func validateVCenterVersion(validationCtx *validationContext, fldPath *field.Path) field.ErrorList {
219194
allErrs := field.ErrorList{}
220195

pkg/infrastructure/vsphere/clusterapi/clusterapi.go

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/session"
1111

1212
"github.com/openshift/installer/pkg/asset/installconfig"
13-
"github.com/openshift/installer/pkg/asset/rhcos"
1413
"github.com/openshift/installer/pkg/infrastructure/clusterapi"
1514
"github.com/openshift/installer/pkg/rhcos/cache"
1615
"github.com/openshift/installer/pkg/types/vsphere"
@@ -28,58 +27,59 @@ func (p Provider) Name() string {
2827
return vsphere.Name
2928
}
3029

31-
func initializeFoldersAndTemplates(ctx context.Context, rhcosImage *rhcos.Image, installConfig *installconfig.InstallConfig, session *session.Session, clusterID, server, tagID string) error {
30+
func initializeFoldersAndTemplates(ctx context.Context, cachedImage string, failureDomain vsphere.FailureDomain, session *session.Session, diskType vsphere.DiskType, clusterID, tagID string) error {
3231
finder := session.Finder
3332

34-
platform := installConfig.Config.VSphere
35-
failureDomains := platform.FailureDomains
36-
37-
for _, failureDomain := range failureDomains {
38-
dc, err := finder.Datacenter(ctx, failureDomain.Topology.Datacenter)
39-
if err != nil {
40-
return err
41-
}
42-
dcFolders, err := dc.Folders(ctx)
43-
if err != nil {
44-
return fmt.Errorf("unable to get datacenter folder: %w", err)
45-
}
46-
47-
folderPath := path.Join(dcFolders.VmFolder.InventoryPath, clusterID)
33+
dc, err := finder.Datacenter(ctx, failureDomain.Topology.Datacenter)
34+
if err != nil {
35+
return err
36+
}
37+
dcFolders, err := dc.Folders(ctx)
38+
if err != nil {
39+
return fmt.Errorf("unable to get datacenter folder: %w", err)
40+
}
4841

49-
// we must set the Folder to the infraId somewhere, we will need to remove that.
50-
// if we are overwriting folderPath it needs to have a slash (path)
51-
folder := failureDomain.Topology.Folder
52-
if strings.Contains(folder, "/") {
53-
folderPath = folder
54-
}
42+
folderPath := path.Join(dcFolders.VmFolder.InventoryPath, clusterID)
5543

56-
folderMo, err := createFolder(ctx, folderPath, session)
57-
if err != nil {
58-
return fmt.Errorf("unable to create folder: %w", err)
59-
}
44+
// we must set the Folder to the infraId somewhere, we will need to remove that.
45+
// if we are overwriting folderPath it needs to have a slash (path)
46+
folder := failureDomain.Topology.Folder
47+
if strings.Contains(folder, "/") {
48+
folderPath = folder
49+
}
6050

61-
cachedImage, err := cache.DownloadImageFile(string(*rhcosImage), cache.InstallerApplicationName)
62-
if err != nil {
63-
return fmt.Errorf("failed to use cached vsphere image: %w", err)
64-
}
51+
folderMo, err := createFolder(ctx, folderPath, session)
52+
if err != nil {
53+
return fmt.Errorf("unable to create folder: %w", err)
54+
}
6555

66-
// if the template is empty, the ova must be imported
67-
if len(failureDomain.Topology.Template) == 0 {
68-
if err = importRhcosOva(ctx, session, folderMo,
69-
cachedImage, clusterID, tagID, string(platform.DiskType), failureDomain); err != nil {
70-
return fmt.Errorf("failed to import ova: %w", err)
71-
}
56+
// if the template is empty, the ova must be imported
57+
if len(failureDomain.Topology.Template) == 0 {
58+
if err = importRhcosOva(ctx, session, folderMo,
59+
cachedImage, clusterID, tagID, string(diskType), failureDomain); err != nil {
60+
return fmt.Errorf("failed to import ova: %w", err)
7261
}
7362
}
7463
return nil
7564
}
7665

7766
// PreProvision creates the vCenter objects required prior to running capv.
7867
func (p Provider) PreProvision(ctx context.Context, in clusterapi.PreProvisionInput) error {
68+
/*
69+
* one locally cached image
70+
* one tag and tag category per vcenter
71+
* one folder per datacenter
72+
* one template per region/zone aka failuredomain
73+
*/
7974
installConfig := in.InstallConfig
8075
clusterID := &installconfig.ClusterID{InfraID: in.InfraID}
8176
var tagID string
8277

78+
cachedImage, err := cache.DownloadImageFile(string(*in.RhcosImage), cache.InstallerApplicationName)
79+
if err != nil {
80+
return fmt.Errorf("failed to use cached vsphere image: %w", err)
81+
}
82+
8383
for _, vcenter := range installConfig.Config.VSphere.VCenters {
8484
server := vcenter.Server
8585
vctrSession, err := installConfig.VSphere.Session(context.TODO(), server)
@@ -105,7 +105,8 @@ func (p Provider) PreProvision(ctx context.Context, in clusterapi.PreProvisionIn
105105
if failureDomain.Server != server {
106106
continue
107107
}
108-
if err = initializeFoldersAndTemplates(ctx, in.RhcosImage, installConfig, vctrSession, clusterID.InfraID, server, tagID); err != nil {
108+
109+
if err = initializeFoldersAndTemplates(ctx, cachedImage, failureDomain, vctrSession, installConfig.Config.VSphere.DiskType, clusterID.InfraID, tagID); err != nil {
109110
return fmt.Errorf("unable to initialize folders and templates: %w", err)
110111
}
111112
}

pkg/infrastructure/vsphere/clusterapi/folder.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@ import (
1111
)
1212

1313
func createFolder(ctx context.Context, fullpath string, session *session.Session) (*object.Folder, error) {
14+
var notFoundError *find.NotFoundError
15+
1416
dir := path.Dir(fullpath)
1517
base := path.Base(fullpath)
1618
finder := session.Finder
1719

1820
folder, err := finder.Folder(ctx, fullpath)
21+
if err != nil && !errors.As(err, &notFoundError) {
22+
return nil, err
23+
}
1924

25+
// if folder is nil the fullpath does not exist
2026
if folder == nil {
2127
folder, err = finder.Folder(ctx, dir)
2228

23-
var notFoundError *find.NotFoundError
2429
if errors.As(err, &notFoundError) {
2530
folder, err = createFolder(ctx, dir, session)
2631
if err != nil {

0 commit comments

Comments
 (0)