Skip to content

Commit af88c7c

Browse files
committed
remove double loop of fd in vsphere zones
This commit removes the double loop of failure domains that caused redundant OVA imports and errors because of duplicate templates. It also moves the location when the OVA is downloaded locally so that this only performed once.
1 parent d8a8d2b commit af88c7c

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

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)