Skip to content

Commit

Permalink
Nutanix ControlPlaneEndpoint IP Validation (#5701)
Browse files Browse the repository at this point in the history
Add IP Uniqueness validation for ControlPlane Enpoint for Nutanix
platform.
  • Loading branch information
thunderboltsid authored Apr 25, 2023
1 parent 3f698ba commit 5fa0222
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ mocks: ## Generate mocks
${MOCKGEN} -destination=pkg/aws/mocks/imds.go -package=mocks -source "pkg/aws/imds.go"
${MOCKGEN} -destination=pkg/aws/mocks/snowballdevice.go -package=mocks -source "pkg/aws/snowballdevice.go"
${MOCKGEN} -destination=pkg/providers/nutanix/mocks/client.go -package=mocks -source "pkg/providers/nutanix/client.go"
${MOCKGEN} -destination=pkg/providers/nutanix/mocks/validator.go -package=mocks -source "pkg/providers/nutanix/validator.go"
${MOCKGEN} -destination=pkg/providers/nutanix/mocks/roundtripper.go -package=mocks net/http RoundTripper
${MOCKGEN} -destination=pkg/providers/snow/mocks/aws.go -package=mocks -source "pkg/providers/snow/aws.go"
${MOCKGEN} -destination=pkg/providers/snow/mocks/defaults.go -package=mocks -source "pkg/providers/snow/defaults.go"
Expand Down
4 changes: 3 additions & 1 deletion pkg/dependencies/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func (f *Factory) WithProvider(clusterConfigFile string, clusterConfig *v1alpha1
case v1alpha1.SnowDatacenterKind:
f.WithUnAuthKubeClient().WithSnowConfigManager()
case v1alpha1.NutanixDatacenterKind:
f.WithKubectl().WithNutanixClientCache().WithNutanixDefaulter().WithNutanixValidator()
f.WithKubectl().WithNutanixClientCache().WithNutanixDefaulter().WithNutanixValidator().WithIPValidator()
}

f.buildSteps = append(f.buildSteps, func(ctx context.Context) error {
Expand Down Expand Up @@ -514,9 +514,11 @@ func (f *Factory) WithProvider(clusterConfigFile string, clusterConfig *v1alpha1
f.dependencies.Kubectl,
f.dependencies.Writer,
f.dependencies.NutanixClientCache,
f.dependencies.IPValidator,
crypto.NewTlsValidator(),
httpClient,
time.Now,
skipIpCheck,
)
f.dependencies.Provider = provider
default:
Expand Down
49 changes: 49 additions & 0 deletions pkg/providers/nutanix/mocks/validator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions pkg/providers/nutanix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type Provider struct {
kubectlClient ProviderKubectlClient
validator *Validator
writer filewriter.FileWriter
ipValidator IPValidator
skipIPCheck bool
}

var _ providers.Provider = &Provider{}
Expand All @@ -63,9 +65,11 @@ func NewProvider(
providerKubectlClient ProviderKubectlClient,
writer filewriter.FileWriter,
clientCache *ClientCache,
ipValidator IPValidator,
certValidator crypto.TlsValidator,
httpClient *http.Client,
now types.NowFunc,
skipIPCheck bool,
) *Provider {
datacenterConfig.SetDefaults()
for _, machineConfig := range machineConfigs {
Expand Down Expand Up @@ -96,6 +100,8 @@ func NewProvider(
kubectlClient: providerKubectlClient,
validator: nutanixValidator,
writer: writer,
ipValidator: ipValidator,
skipIPCheck: skipIPCheck,
}
}

Expand Down Expand Up @@ -193,6 +199,14 @@ func (p *Provider) SetupAndValidateCreateCluster(ctx context.Context, clusterSpe
return fmt.Errorf("failed to generate ssh key: %v", err)
}

if !p.skipIPCheck {
if err := p.ipValidator.ValidateControlPlaneIPUniqueness(clusterSpec.Cluster); err != nil {
return err
}
} else {
logger.Info("Skipping check for whether control plane ip is in use")
}

return nil
}

Expand Down
10 changes: 8 additions & 2 deletions pkg/providers/nutanix/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ func testNutanixProvider(t *testing.T, nutanixClient Client, kubectl *executable
}
clientCache.clients[dcConf.Name] = nutanixClient

provider := NewProvider(dcConf, workerConfs, clusterConf, kubectl, writer, clientCache, certValidator, httpClient, time.Now)
ctrl := gomock.NewController(t)
mockIPValidator := mocknutanix.NewMockIPValidator(ctrl)
mockIPValidator.EXPECT().ValidateControlPlaneIPUniqueness(gomock.Any()).Return(nil).AnyTimes()
provider := NewProvider(dcConf, workerConfs, clusterConf, kubectl, writer, clientCache, mockIPValidator, certValidator, httpClient, time.Now, false)
require.NotNil(t, provider)
return provider
}
Expand All @@ -111,7 +114,10 @@ func testNutanixProviderWithClusterSpec(t *testing.T, nutanixClient Client, kube
clients: make(map[string]Client),
}
clientCache.clients[clusterSpec.NutanixDatacenter.Name] = nutanixClient
provider := NewProvider(clusterSpec.NutanixDatacenter, clusterSpec.NutanixMachineConfigs, clusterSpec.Cluster, kubectl, writer, clientCache, certValidator, httpClient, time.Now)
ctrl := gomock.NewController(t)
mockIPValidator := mocknutanix.NewMockIPValidator(ctrl)
mockIPValidator.EXPECT().ValidateControlPlaneIPUniqueness(gomock.Any()).Return(nil).AnyTimes()
provider := NewProvider(clusterSpec.NutanixDatacenter, clusterSpec.NutanixMachineConfigs, clusterSpec.Cluster, kubectl, writer, clientCache, mockIPValidator, certValidator, httpClient, time.Now, false)
require.NotNil(t, provider)
return provider
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/providers/nutanix/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ const (
minNutanixDiskGiB = 20
)

// IPValidator is an interface that defines methods to validate the control plane IP.
type IPValidator interface {
ValidateControlPlaneIPUniqueness(cluster *anywherev1.Cluster) error
}

// Validator is a client to validate nutanix resources.
type Validator struct {
httpClient *http.Client
Expand Down

0 comments on commit 5fa0222

Please sign in to comment.