Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
tinkerbell: fix labels && taints with spaced value
Browse files Browse the repository at this point in the history
Now user cannot have spaced key or value in labels, taints map for
worker pools.
Add tests.

closes: #1280
  • Loading branch information
knrt10 committed Dec 28, 2020
1 parent a07ebec commit e9fc218
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/platform/tinkerbell/tinkerbell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
28 changes: 28 additions & 0 deletions pkg/platform/tinkerbell/tinkerbell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit e9fc218

Please sign in to comment.