Skip to content

Commit

Permalink
fix: etcd config validation for worker
Browse files Browse the repository at this point in the history
Fixes an ambigious error when etcd config is supplied to a worker as a
patch.

Signed-off-by: Noel Georgi <git@frezbo.dev>
(cherry picked from commit e89d755)
  • Loading branch information
frezbo authored and smira committed Mar 20, 2024
1 parent a5920a1 commit 3c942fe
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/machinery/config/types/v1alpha1/v1alpha1_validation.go
Expand Up @@ -86,7 +86,7 @@ func (c *Config) Validate(mode validation.RuntimeMode, options ...validation.Opt
return nil, result.ErrorOrNil()
}

if err := c.ClusterConfig.Validate(); err != nil {
if err := c.ClusterConfig.Validate(c.Machine().Type().IsControlPlane()); err != nil {
result = multierror.Append(result, err)
}

Expand Down Expand Up @@ -334,7 +334,9 @@ func isValidDNSName(name string) bool {
}

// Validate validates the config.
func (c *ClusterConfig) Validate() error {
//
//nolint:gocyclo
func (c *ClusterConfig) Validate(isControlPlane bool) error {
var result *multierror.Error

if c == nil {
Expand All @@ -358,7 +360,11 @@ func (c *ClusterConfig) Validate() error {
}

if c.EtcdConfig != nil {
result = multierror.Append(result, c.EtcdConfig.Validate())
if isControlPlane {
result = multierror.Append(result, c.EtcdConfig.Validate())
} else {
result = multierror.Append(result, errors.New("etcd config is only allowed on control plane machines"))
}
}

result = multierror.Append(
Expand Down
36 changes: 36 additions & 0 deletions pkg/machinery/config/types/v1alpha1/v1alpha1_validation_test.go
Expand Up @@ -1004,6 +1004,42 @@ func TestValidate(t *testing.T) {
},
expectedError: "2 errors occurred:\n\t* cluster discovery service requires .cluster.id\n\t* cluster discovery service requires .cluster.secret\n\n",
},
{
name: "EtcdMissingCa",
config: &v1alpha1.Config{
ConfigVersion: "v1alpha1",
MachineConfig: &v1alpha1.MachineConfig{
MachineType: "controlplane",
},
ClusterConfig: &v1alpha1.ClusterConfig{
ControlPlane: &v1alpha1.ControlPlaneConfig{
Endpoint: &v1alpha1.Endpoint{
endpointURL,
},
},
EtcdConfig: &v1alpha1.EtcdConfig{},
},
},
expectedError: "1 error occurred:\n\t* key/cert combination should not be empty\n\n",
},
{
name: "EtcdConfigProvidedForWorker",
config: &v1alpha1.Config{
ConfigVersion: "v1alpha1",
MachineConfig: &v1alpha1.MachineConfig{
MachineType: "worker",
},
ClusterConfig: &v1alpha1.ClusterConfig{
ControlPlane: &v1alpha1.ControlPlaneConfig{
Endpoint: &v1alpha1.Endpoint{
endpointURL,
},
},
EtcdConfig: &v1alpha1.EtcdConfig{},
},
},
expectedError: "1 error occurred:\n\t* etcd config is only allowed on control plane machines\n\n",
},
{
name: "GoodEtcdSubnet",
config: &v1alpha1.Config{
Expand Down

0 comments on commit 3c942fe

Please sign in to comment.