Skip to content

Commit

Permalink
Refactor to use ValidatorBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmeier5 committed Aug 16, 2022
1 parent dbd2a8f commit 353bca5
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 47 deletions.
8 changes: 6 additions & 2 deletions pkg/dependencies/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,8 +1046,12 @@ func (f *Factory) WithVSphereValidator() *Factory {
if f.dependencies.VSphereValidator != nil {
return nil
}

f.dependencies.VSphereValidator = vsphere.NewValidator(f.dependencies.Govc, &networkutils.DefaultNetClient{}, nil)
vb := vsphere.NewValidatorBuilder(
f.dependencies.Govc,
&networkutils.DefaultNetClient{},
&vsphere.VMOMIClientBuilder{},
)
f.dependencies.VSphereValidator = vb.Build()

return nil
})
Expand Down
8 changes: 6 additions & 2 deletions pkg/providers/vsphere/govmomiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ type VMOMIClient struct {
username string
}

type NewVSphereClientCallable func(ctx context.Context, host string, username string, password string, insecure bool, datacenter string) (VSphereClient, error)
type VSphereClientBuilder interface {
Build(ctx context.Context, host string, username string, password string, insecure bool, datacenter string) (VSphereClient, error)
}

type VMOMIClientBuilder struct{}

func NewVMOMIClient(ctx context.Context, host string, username string, password string, insecure bool, datacenter string) (*VMOMIClient, error) {
func (*VMOMIClientBuilder) Build(ctx context.Context, host string, username string, password string, insecure bool, datacenter string) (VSphereClient, error) {
u, err := soap.ParseURL(host)
u.User = url.UserPassword(username, password)
if err != nil {
Expand Down
39 changes: 24 additions & 15 deletions pkg/providers/vsphere/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,30 @@ type RequiredAccess struct {
}

type Validator struct {
govc ProviderGovcClient
netClient networkutils.NetClient
newVSphereClient NewVSphereClientCallable
govc ProviderGovcClient
netClient networkutils.NetClient
vSphereClientBuilder VSphereClientBuilder
}

func NewValidator(govc ProviderGovcClient, netClient networkutils.NetClient, f NewVSphereClientCallable) *Validator {
if f == nil {
f = func(ctx context.Context, host string, username string, password string, insecure bool, datacenter string) (VSphereClient, error) {
return NewVMOMIClient(ctx, host, username, password, insecure, datacenter)
}
}
type validatorBuilder struct {
govc ProviderGovcClient
netClient networkutils.NetClient
vscb VSphereClientBuilder
}

func NewValidatorBuilder(govc ProviderGovcClient, netClient networkutils.NetClient, vscb VSphereClientBuilder) *validatorBuilder {
return &validatorBuilder{govc, netClient, vscb}
}

func (b *validatorBuilder) Build() *Validator {
return NewValidator(b.govc, b.netClient, b.vscb)
}

func NewValidator(govc ProviderGovcClient, netClient networkutils.NetClient, vscb VSphereClientBuilder) *Validator {
return &Validator{
govc: govc,
netClient: netClient,
newVSphereClient: f,
govc: govc,
netClient: netClient,
vSphereClientBuilder: vscb,
}
}

Expand Down Expand Up @@ -456,7 +465,7 @@ func (v *Validator) validateUserPrivs(ctx context.Context, spec *Spec, vuc *conf
host := spec.datacenterConfig.Spec.Server
datacenter := spec.datacenterConfig.Spec.Datacenter

vsc, err := v.newVSphereClient(
vsc, err := v.vSphereClientBuilder.Build(
ctx,
host,
vuc.EksaVsphereCPUsername,
Expand Down Expand Up @@ -501,7 +510,7 @@ func (v *Validator) validateCSIUserPrivs(ctx context.Context, spec *Spec, vuc *c
host := spec.datacenterConfig.Spec.Server
datacenter := spec.datacenterConfig.Spec.Datacenter

vsc, err := v.newVSphereClient(
vsc, err := v.vSphereClientBuilder.Build(
ctx,
host,
vuc.EksaVsphereCSIUsername,
Expand Down Expand Up @@ -529,7 +538,7 @@ func (v *Validator) validateCPUserPrivs(ctx context.Context, spec *Spec, vuc *co
host := spec.datacenterConfig.Spec.Server
datacenter := spec.datacenterConfig.Spec.Datacenter

vsc, err := v.newVSphereClient(
vsc, err := v.vSphereClientBuilder.Build(
ctx,
host,
vuc.EksaVsphereCPUsername,
Expand Down
22 changes: 15 additions & 7 deletions pkg/providers/vsphere/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,34 @@ type ClusterResourceSetManager interface {
ForceUpdate(ctx context.Context, name, namespace string, managementCluster, workloadCluster *types.Cluster) error
}

type ValidatorBuilder interface {
Build() *Validator
}

func NewProvider(datacenterConfig *v1alpha1.VSphereDatacenterConfig, machineConfigs map[string]*v1alpha1.VSphereMachineConfig, clusterConfig *v1alpha1.Cluster, providerGovcClient ProviderGovcClient, providerKubectlClient ProviderKubectlClient, writer filewriter.FileWriter, now types.NowFunc, skipIpCheck bool, resourceSetManager ClusterResourceSetManager) *vsphereProvider {
var validator *Validator = nil
netClient := &networkutils.DefaultNetClient{}
vb := NewValidatorBuilder(
providerGovcClient,
netClient,
&VMOMIClientBuilder{},
)

return NewProviderCustomNet(
datacenterConfig,
machineConfigs,
clusterConfig,
providerGovcClient,
providerKubectlClient,
writer,
&networkutils.DefaultNetClient{},
netClient,
now,
skipIpCheck,
resourceSetManager,
validator,
vb,
)
}

func NewProviderCustomNet(datacenterConfig *v1alpha1.VSphereDatacenterConfig, machineConfigs map[string]*v1alpha1.VSphereMachineConfig, clusterConfig *v1alpha1.Cluster, providerGovcClient ProviderGovcClient, providerKubectlClient ProviderKubectlClient, writer filewriter.FileWriter, netClient networkutils.NetClient, now types.NowFunc, skipIpCheck bool, resourceSetManager ClusterResourceSetManager, validator *Validator) *vsphereProvider {
func NewProviderCustomNet(datacenterConfig *v1alpha1.VSphereDatacenterConfig, machineConfigs map[string]*v1alpha1.VSphereMachineConfig, clusterConfig *v1alpha1.Cluster, providerGovcClient ProviderGovcClient, providerKubectlClient ProviderKubectlClient, writer filewriter.FileWriter, netClient networkutils.NetClient, now types.NowFunc, skipIpCheck bool, resourceSetManager ClusterResourceSetManager, vb ValidatorBuilder) *vsphereProvider {
var controlPlaneMachineSpec, etcdMachineSpec *v1alpha1.VSphereMachineConfigSpec
if clusterConfig.Spec.ControlPlaneConfiguration.MachineGroupRef != nil && machineConfigs[clusterConfig.Spec.ControlPlaneConfiguration.MachineGroupRef.Name] != nil {
controlPlaneMachineSpec = &machineConfigs[clusterConfig.Spec.ControlPlaneConfiguration.MachineGroupRef.Name].Spec
Expand All @@ -173,9 +183,7 @@ func NewProviderCustomNet(datacenterConfig *v1alpha1.VSphereDatacenterConfig, ma
}
}

if validator == nil {
validator = NewValidator(providerGovcClient, netClient, nil)
}
validator := vb.Build()
retrier := retrier.NewWithMaxRetries(maxRetries, backOffPeriod)
return &vsphereProvider{
datacenterConfig: datacenterConfig,
Expand Down
99 changes: 78 additions & 21 deletions pkg/providers/vsphere/vsphere_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ func newProviderTest(t *testing.T) *providerTest {
ctrl := gomock.NewController(t)
kubectl := mocks.NewMockProviderKubectlClient(ctrl)
govc := mocks.NewMockProviderGovcClient(ctrl)
vscb, _ := newMockVSphereClientBuilder(ctrl)
vb := NewValidatorBuilder(govc, &DummyNetClient{}, vscb)
resourceSetManager := mocks.NewMockClusterResourceSetManager(ctrl)
clusterConfig := givenClusterConfig(t, testClusterConfigMainFilename)
datacenterConfig := givenDatacenterConfig(t, testClusterConfigMainFilename)
Expand All @@ -333,6 +335,7 @@ func newProviderTest(t *testing.T) *providerTest {
govc,
kubectl,
resourceSetManager,
vb,
)
return &providerTest{
WithT: NewWithT(t),
Expand Down Expand Up @@ -402,20 +405,26 @@ func TestNewProvider(t *testing.T) {

func newProviderWithKubectl(t *testing.T, datacenterConfig *v1alpha1.VSphereDatacenterConfig, machineConfigs map[string]*v1alpha1.VSphereMachineConfig, clusterConfig *v1alpha1.Cluster, kubectl ProviderKubectlClient) *vsphereProvider {
ctrl := gomock.NewController(t)
govc := NewDummyProviderGovcClient()
vscb, _ := newMockVSphereClientBuilder(ctrl)
vb := NewValidatorBuilder(govc, &DummyNetClient{}, vscb)
resourceSetManager := mocks.NewMockClusterResourceSetManager(ctrl)
return newProvider(
t,
datacenterConfig,
machineConfigs,
clusterConfig,
NewDummyProviderGovcClient(),
govc,
kubectl,
resourceSetManager,
vb,
)
}

func newProviderWithGovc(t *testing.T, datacenterConfig *v1alpha1.VSphereDatacenterConfig, machineConfigs map[string]*v1alpha1.VSphereMachineConfig, clusterConfig *v1alpha1.Cluster, govc ProviderGovcClient) *vsphereProvider {
ctrl := gomock.NewController(t)
vscb, _ := newMockVSphereClientBuilder(ctrl)
vb := NewValidatorBuilder(govc, &DummyNetClient{}, vscb)
resourceSetManager := mocks.NewMockClusterResourceSetManager(ctrl)
kubectl := mocks.NewMockProviderKubectlClient(ctrl)
return newProvider(
Expand All @@ -426,27 +435,42 @@ func newProviderWithGovc(t *testing.T, datacenterConfig *v1alpha1.VSphereDatacen
govc,
kubectl,
resourceSetManager,
vb,
)
}

func newProvider(t *testing.T, datacenterConfig *v1alpha1.VSphereDatacenterConfig, machineConfigs map[string]*v1alpha1.VSphereMachineConfig, clusterConfig *v1alpha1.Cluster, govc ProviderGovcClient, kubectl ProviderKubectlClient, resourceSetManager ClusterResourceSetManager) *vsphereProvider {
_, writer := test.NewWriter(t)
netClient := &DummyNetClient{}
ctrl := gomock.NewController(t)
vSphereClientCallable := func(ctx context.Context, host string, username string, password string, insecure bool, datacenter string) (VSphereClient, error) {
vsc := mocks.NewMockVSphereClient(ctrl)
vsc.EXPECT().Username().Return("foobar").AnyTimes()

var privs []string
err := json.Unmarshal([]byte(config.VSphereAdminPrivsFile), &privs)
if err != nil {
return nil, err
}
type mockVSphereClientBuilder struct {
vsc *mocks.MockVSphereClient
}

func (mvscb *mockVSphereClientBuilder) Build(ctx context.Context, host string, username string, password string, insecure bool, datacenter string) (VSphereClient, error) {
return mvscb.vsc, nil
}

vsc.EXPECT().GetPrivsOnEntity(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(privs, nil).AnyTimes()
return vsc, nil
func setDefaultVSphereClientMock(vsc *mocks.MockVSphereClient) error {
vsc.EXPECT().Username().Return("foobar").AnyTimes()

var privs []string
err := json.Unmarshal([]byte(config.VSphereAdminPrivsFile), &privs)
if err != nil {
return err
}
validator := NewValidator(govc, netClient, vSphereClientCallable)

vsc.EXPECT().GetPrivsOnEntity(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(privs, nil).AnyTimes()

return nil
}

func newMockVSphereClientBuilder(ctrl *gomock.Controller) (VSphereClientBuilder, error) {
vsc := mocks.NewMockVSphereClient(ctrl)
err := setDefaultVSphereClientMock(vsc)
mvscb := mockVSphereClientBuilder{vsc}
return &mvscb, err
}

func newProvider(t *testing.T, datacenterConfig *v1alpha1.VSphereDatacenterConfig, machineConfigs map[string]*v1alpha1.VSphereMachineConfig, clusterConfig *v1alpha1.Cluster, govc ProviderGovcClient, kubectl ProviderKubectlClient, resourceSetManager ClusterResourceSetManager, vb ValidatorBuilder) *vsphereProvider {
_, writer := test.NewWriter(t)
netClient := &DummyNetClient{}

return NewProviderCustomNet(
datacenterConfig,
Expand All @@ -459,7 +483,7 @@ func newProvider(t *testing.T, datacenterConfig *v1alpha1.VSphereDatacenterConfi
test.FakeNow,
false,
resourceSetManager,
validator,
vb,
)
}

Expand Down Expand Up @@ -939,8 +963,19 @@ func TestProviderGenerateCAPISpecForCreateWithBottlerocketAndExternalEtcd(t *tes
machineConfigs := givenMachineConfigs(t, clusterSpecManifest)
ctx := context.Background()
govc := NewDummyProviderGovcClient()
vscb, _ := newMockVSphereClientBuilder(mockCtrl)
vb := NewValidatorBuilder(govc, &DummyNetClient{}, vscb)
govc.osTag = bottlerocketOSTag
provider := newProvider(t, datacenterConfig, machineConfigs, clusterSpec.Cluster, govc, kubectl, resourceSetManager)
provider := newProvider(
t,
datacenterConfig,
machineConfigs,
clusterSpec.Cluster,
govc,
kubectl,
resourceSetManager,
vb,
)

if err := provider.SetupAndValidateCreateCluster(ctx, clusterSpec); err != nil {
t.Fatalf("failed to setup and validate: %v", err)
Expand All @@ -967,8 +1002,19 @@ func TestProviderGenerateDeploymentFileForBottleRocketWithMirrorConfig(t *testin
machineConfigs := givenMachineConfigs(t, clusterSpecManifest)
ctx := context.Background()
govc := NewDummyProviderGovcClient()
vscb, _ := newMockVSphereClientBuilder(mockCtrl)
vb := NewValidatorBuilder(govc, &DummyNetClient{}, vscb)
govc.osTag = bottlerocketOSTag
provider := newProvider(t, datacenterConfig, machineConfigs, clusterSpec.Cluster, govc, kubectl, resourceSetManager)
provider := newProvider(
t,
datacenterConfig,
machineConfigs,
clusterSpec.Cluster,
govc,
kubectl,
resourceSetManager,
vb,
)
if err := provider.SetupAndValidateCreateCluster(ctx, clusterSpec); err != nil {
t.Fatalf("failed to setup and validate: %v", err)
}
Expand All @@ -995,7 +1041,18 @@ func TestProviderGenerateDeploymentFileForBottleRocketWithMirrorAndCertConfig(t
ctx := context.Background()
govc := NewDummyProviderGovcClient()
govc.osTag = bottlerocketOSTag
provider := newProvider(t, datacenterConfig, machineConfigs, clusterSpec.Cluster, govc, kubectl, resourceSetManager)
vscb, _ := newMockVSphereClientBuilder(mockCtrl)
vb := NewValidatorBuilder(govc, &DummyNetClient{}, vscb)
provider := newProvider(
t,
datacenterConfig,
machineConfigs,
clusterSpec.Cluster,
govc,
kubectl,
resourceSetManager,
vb,
)
if err := provider.SetupAndValidateCreateCluster(ctx, clusterSpec); err != nil {
t.Fatalf("failed to setup and validate: %v", err)
}
Expand Down

0 comments on commit 353bca5

Please sign in to comment.