Skip to content

Commit bed7ddf

Browse files
authored
fix: add tag validation to MatchTags (#410)
this fixes a small oversight in #343 and actually validates the individual tags against what is accepted by the API https://github.com/proxmox/pve-common/blob/master/src/PVE/JSONSchema.pm#L706 ``` our $PVE_TAG_RE = qr/[a-z0-9_][a-z0-9_\-\+\.]*/i; ```
1 parent 58c106a commit bed7ddf

6 files changed

+62
-0
lines changed

api/v1alpha1/proxmoxmachine_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ type TemplateSelector struct {
229229
// Passed tags must be an exact 1:1 match with the tags on the template you want to use.
230230
// If multiple VM templates with the same set of tags are found, provisioning will fail.
231231
//
232+
// +listType=set
233+
// +kubebuilder:validation:items:Pattern=`^(?i)[a-z0-9_][a-z0-9_\-\+\.]*$`
232234
// +kubebuilder:validation:MinItems=1
233235
MatchTags []string `json:"matchTags"`
234236
}

api/v1alpha1/proxmoxmachine_types_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1alpha1
1818

1919
import (
2020
"context"
21+
"strconv"
2122

2223
. "github.com/onsi/ginkgo/v2"
2324
. "github.com/onsi/gomega"
@@ -86,6 +87,57 @@ var _ = Describe("ProxmoxMachine Test", func() {
8687

8788
Expect(k8sClient.Create(context.Background(), dm)).Should(MatchError(ContainSubstring("should have at least 1 items")))
8889
})
90+
91+
It("Should only allow valid MatchTags", func() {
92+
testCases := []struct {
93+
tag string
94+
expectErrror bool
95+
errorMessage string
96+
}{
97+
// Valid Tags
98+
{"valid_tag", false, ""},
99+
{"Valid-Tag", false, ""},
100+
{"valid.tag", false, ""},
101+
{"VALID+TAG", false, ""},
102+
{"123tag", false, ""},
103+
{"tag123", false, ""},
104+
{"tag_with-hyphen", false, ""},
105+
{"tag.with.plus+_and-hyphen", false, ""},
106+
{"_tag_with_underscore", false, ""},
107+
108+
// Invalid Tags
109+
{"", true, "in body should match"}, // Empty string
110+
{"-invalid", true, "in body should match"}, // Starts with a hyphen
111+
{"+invalid", true, "in body should match"}, // Starts with a plus
112+
{".invalid", true, "in body should match"}, // Starts with a dot
113+
{" invalid", true, "in body should match"}, // Starts with a space
114+
{"invalid!", true, "in body should match"}, // Contains an exclamation mark
115+
{"invalid@", true, "in body should match"}, // Contains an at symbol
116+
{"invalid#", true, "in body should match"}, // Contains a hash symbol
117+
{"inval id", true, "in body should match"}, // Contains a whitespace
118+
}
119+
120+
// Iterate through each test case
121+
for i, testCase := range testCases {
122+
// Create a new ProxmoxMachine object for each test case
123+
dm := defaultMachine()
124+
125+
// Set the name of the machine to a unique value based on the test case index
126+
dm.ObjectMeta.Name = "test-machine-" + strconv.Itoa(i)
127+
128+
// Set the template selector to match the tag from the test case
129+
dm.Spec.TemplateSource.SourceNode = ""
130+
dm.Spec.TemplateSource.TemplateID = nil
131+
dm.Spec.TemplateSelector = &TemplateSelector{MatchTags: []string{testCase.tag}}
132+
133+
// Run test
134+
if !testCase.expectErrror {
135+
Expect(k8sClient.Create(context.Background(), dm)).To(Succeed())
136+
} else {
137+
Expect(k8sClient.Create(context.Background(), dm)).Should(MatchError(ContainSubstring(testCase.errorMessage)))
138+
}
139+
}
140+
})
89141
})
90142

91143
Context("Disks", func() {

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxclusters.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,11 @@ spec:
569569
Passed tags must be an exact 1:1 match with the tags on the template you want to use.
570570
If multiple VM templates with the same set of tags are found, provisioning will fail.
571571
items:
572+
pattern: ^(?i)[a-z0-9_][a-z0-9_\-\+\.]*$
572573
type: string
573574
minItems: 1
574575
type: array
576+
x-kubernetes-list-type: set
575577
required:
576578
- matchTags
577579
type: object

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxclustertemplates.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,11 @@ spec:
610610
Passed tags must be an exact 1:1 match with the tags on the template you want to use.
611611
If multiple VM templates with the same set of tags are found, provisioning will fail.
612612
items:
613+
pattern: ^(?i)[a-z0-9_][a-z0-9_\-\+\.]*$
613614
type: string
614615
minItems: 1
615616
type: array
617+
x-kubernetes-list-type: set
616618
required:
617619
- matchTags
618620
type: object

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxmachines.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,9 +537,11 @@ spec:
537537
Passed tags must be an exact 1:1 match with the tags on the template you want to use.
538538
If multiple VM templates with the same set of tags are found, provisioning will fail.
539539
items:
540+
pattern: ^(?i)[a-z0-9_][a-z0-9_\-\+\.]*$
540541
type: string
541542
minItems: 1
542543
type: array
544+
x-kubernetes-list-type: set
543545
required:
544546
- matchTags
545547
type: object

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxmachinetemplates.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,11 @@ spec:
569569
Passed tags must be an exact 1:1 match with the tags on the template you want to use.
570570
If multiple VM templates with the same set of tags are found, provisioning will fail.
571571
items:
572+
pattern: ^(?i)[a-z0-9_][a-z0-9_\-\+\.]*$
572573
type: string
573574
minItems: 1
574575
type: array
576+
x-kubernetes-list-type: set
575577
required:
576578
- matchTags
577579
type: object

0 commit comments

Comments
 (0)