From 7610b7abbbe8fc46841e097e10a5933606743f5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mudrini=C4=87?= Date: Fri, 28 Oct 2022 19:49:46 +0200 Subject: [PATCH 1/2] Refactor validation for csiConfig MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marko Mudrinić --- .golangci.yml | 2 +- pkg/apis/kubeone/validation/validation.go | 12 +++++------- pkg/apis/kubeone/validation/validation_test.go | 13 +++++++++++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 7bf732b37..1be05408b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -92,4 +92,4 @@ issues: text: "cyclomatic complexity 36 of func `openstackValidationFunc` is high" - path: pkg/apis/kubeone - text: "cyclomatic complexity 31 of func `ValidateCloudProviderSpec` is high" + text: "cyclomatic complexity 32 of func `ValidateCloudProviderSpec` is high" diff --git a/pkg/apis/kubeone/validation/validation.go b/pkg/apis/kubeone/validation/validation.go index 4a08aeba1..1bb5adca0 100644 --- a/pkg/apis/kubeone/validation/validation.go +++ b/pkg/apis/kubeone/validation/validation.go @@ -244,6 +244,9 @@ func ValidateCloudProviderSpec(p kubeoneapi.CloudProviderSpec, fldPath *field.Pa if len(p.CloudConfig) == 0 { allErrs = append(allErrs, field.Required(fldPath.Child("cloudConfig"), ".cloudProvider.cloudConfig is required for vSphere provider")) } + if p.External && len(p.CSIConfig) == 0 { + allErrs = append(allErrs, field.Required(fldPath.Child("csiConfig"), ".cloudProvider.csiConfig is required for vSphere provider")) + } providerFound = true } if p.None != nil { @@ -257,13 +260,8 @@ func ValidateCloudProviderSpec(p kubeoneapi.CloudProviderSpec, fldPath *field.Pa allErrs = append(allErrs, field.Invalid(fldPath, "", "provider must be specified")) } - if len(p.CSIConfig) > 0 { - if p.Vsphere == nil { - allErrs = append(allErrs, field.Invalid(fldPath.Child("csiConfig"), "", ".cloudProvider.csiConfig is currently supported only for vsphere clusters")) - } - if !p.External { - allErrs = append(allErrs, field.Invalid(fldPath.Child("csiConfig"), "", ".cloudProvider.csiConfig is supported only for clusters using external cloud provider (.cloudProvider.external)")) - } + if p.Vsphere == nil && len(p.CSIConfig) > 0 { + allErrs = append(allErrs, field.Forbidden(fldPath.Child("csiConfig"), ".cloudProvider.csiConfig is currently supported only for vsphere clusters")) } return allErrs diff --git a/pkg/apis/kubeone/validation/validation_test.go b/pkg/apis/kubeone/validation/validation_test.go index 040d5b752..2676e1d68 100644 --- a/pkg/apis/kubeone/validation/validation_test.go +++ b/pkg/apis/kubeone/validation/validation_test.go @@ -667,13 +667,22 @@ func TestValidateCloudProviderSpec(t *testing.T) { expectedError: true, }, { - name: "vSphere provider config without csiConfig", + name: "vSphere provider config without csiConfig (external disabled)", providerConfig: kubeoneapi.CloudProviderSpec{ Vsphere: &kubeoneapi.VsphereSpec{}, CloudConfig: "test", }, expectedError: false, }, + { + name: "vSphere provider config without csiConfig (external enabled)", + providerConfig: kubeoneapi.CloudProviderSpec{ + Vsphere: &kubeoneapi.VsphereSpec{}, + External: true, + CloudConfig: "test", + }, + expectedError: true, + }, { name: "vSphere provider config with csiConfig", providerConfig: kubeoneapi.CloudProviderSpec{ @@ -692,7 +701,7 @@ func TestValidateCloudProviderSpec(t *testing.T) { CloudConfig: "test", CSIConfig: "test", }, - expectedError: true, + expectedError: false, }, { name: "OpenStack provider config without csiConfig", From ec64cb701d677ebf331d66dea6bb1919d97856f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mudrini=C4=87?= Date: Fri, 28 Oct 2022 19:51:17 +0200 Subject: [PATCH 2/2] Add a warning if CSIConfig is used with in-tree provider MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marko Mudrinić --- pkg/apis/kubeone/config/config.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/apis/kubeone/config/config.go b/pkg/apis/kubeone/config/config.go index ed4392977..82c927d68 100644 --- a/pkg/apis/kubeone/config/config.go +++ b/pkg/apis/kubeone/config/config.go @@ -349,4 +349,8 @@ func checkClusterFeatures(c kubeoneapi.KubeOneCluster, logger logrus.FieldLogger if c.ContainerRuntime.Docker != nil { logger.Warnf("Support for docker will be removed with Kubernetes 1.24 release. It is recommended to switch to containerd as container runtime using `kubeone migrate to-containerd`") } + + if c.CloudProvider.Vsphere != nil && !c.CloudProvider.External && len(c.CloudProvider.CSIConfig) > 0 { + logger.Warnf(".cloudProvider.csiConfig is provided, but is ignored when used with the in-tree cloud provider") + } }