fix: etcd config validation for worker

Fixes an ambigious error when etcd config is supplied to a worker as a
patch.

Signed-off-by: Noel Georgi <git@frezbo.dev>
This commit is contained in:
Noel Georgi 2024-03-11 16:06:50 +05:30
parent 1aa3c91821
commit e89d755c52
No known key found for this signature in database
GPG Key ID: 21A9F444075C9E36
2 changed files with 45 additions and 3 deletions

View File

@ -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)
}
@ -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 {
@ -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(

View File

@ -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{