Skip to content

Commit 06666c7

Browse files
committed
terraform vs infra sdk capv folder create
With the terraform vsphere provider its nearly impossible to create multiple nested folders. The introduction using govmomi to create the pre-requisite objects in vCenter elivates that problem. To resolve the validation issue the folder function is split between two files. If terraform is used the confirmation that the folder exist is still present. If capv/altinfra is used the folder does not need to be present and continues if not. These files should only be needed until terraform is removed entirely.
1 parent af88c7c commit 06666c7

File tree

3 files changed

+80
-25
lines changed

3 files changed

+80
-25
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

0 commit comments

Comments
 (0)