From e9fc218814dbd46c511a7fa03aaeacc91da15400 Mon Sep 17 00:00:00 2001 From: knrt10 Date: Mon, 28 Dec 2020 13:20:01 +0530 Subject: [PATCH] tinkerbell: fix labels && taints with spaced value Now user cannot have spaced key or value in labels, taints map for worker pools. Add tests. closes: #1280 --- pkg/platform/tinkerbell/tinkerbell.go | 32 ++++++++++++++++++++++ pkg/platform/tinkerbell/tinkerbell_test.go | 28 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/pkg/platform/tinkerbell/tinkerbell.go b/pkg/platform/tinkerbell/tinkerbell.go index 621d82932..d246a80f7 100644 --- a/pkg/platform/tinkerbell/tinkerbell.go +++ b/pkg/platform/tinkerbell/tinkerbell.go @@ -230,6 +230,7 @@ func (c *Config) Validate() hcl.Diagnostics { d = append(d, platform.WorkerPoolNamesUnique(x)...) d = append(d, c.validateRequiredFields()...) + d = append(d, c.CheckWorkerPoolLabelsAndTaints()...) return d } @@ -288,3 +289,34 @@ func (c *Config) validateRequiredFields() hcl.Diagnostics { return d } + +// CheckWorkerPoolLabelsAndTaints verifies that all worker pool labels +// and taints are in correct format. Neither key nor value of labels +// and taints map should have empty space in them. +func (c *Config) CheckWorkerPoolLabelsAndTaints() hcl.Diagnostics { + var diagnostics hcl.Diagnostics + + for _, w := range c.WorkerPools { + for k, v := range w.Labels { + if strings.Contains(k, " ") || strings.Contains(v, " ") { + diagnostics = append(diagnostics, &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Worker pools labels map should not contain empty spaces", + Detail: fmt.Sprintf("Worker pool %q label with key %q is incorrect", w.PoolName, k), + }) + } + } + + for k, v := range w.Taints { + if strings.Contains(k, " ") || strings.Contains(v, " ") { + diagnostics = append(diagnostics, &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Worker pools taints map should not contain empty spaces", + Detail: fmt.Sprintf("Worker pool %q taints with key %q is incorrect", w.PoolName, k), + }) + } + } + } + + return diagnostics +} diff --git a/pkg/platform/tinkerbell/tinkerbell_test.go b/pkg/platform/tinkerbell/tinkerbell_test.go index c23f920b9..06603c38e 100644 --- a/pkg/platform/tinkerbell/tinkerbell_test.go +++ b/pkg/platform/tinkerbell/tinkerbell_test.go @@ -244,3 +244,31 @@ func TestMeta(t *testing.T) { t.Errorf("Expected %d nodes, got %d", expectedNodes, m.ExpectedNodes) } } + +func TestCheckWorkerPoolLabelsWithSpacedValue(t *testing.T) { + c := &tinkerbell.Config{ + WorkerPools: []tinkerbell.WorkerPool{ + { + Labels: map[string]string{"foo-1": "bar "}, + }, + }, + } + + if d := c.CheckWorkerPoolLabelsAndTaints(); !d.HasErrors() { + t.Error("Should fail with space in worker pool labels") + } +} + +func TestCheckWorkerPoolTaintsWithSpacedValue(t *testing.T) { + c := &tinkerbell.Config{ + WorkerPools: []tinkerbell.WorkerPool{ + { + Taints: map[string]string{"foo-1": "bar "}, + }, + }, + } + + if d := c.CheckWorkerPoolLabelsAndTaints(); !d.HasErrors() { + t.Error("Should fail with space in worker pool taints") + } +}