Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: regex field #186

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/testtriggers/v1/testtrigger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ const (
type TestTriggerSelector struct {
// Name selector is used to identify a Kubernetes Object based on the metadata name
Name string `json:"name,omitempty"`
// kubernetes resource name regex
NameRegex string `json:"nameRegex,omitempty"`
// Namespace of the Kubernetes object
Namespace string `json:"namespace,omitempty"`
// LabelSelector is used to identify a group of Kubernetes Objects based on their metadata labels
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/tests.testkube.io_testtriggers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ spec:
description: Name selector is used to identify a Kubernetes Object
based on the metadata name
type: string
nameRegex:
description: kubernetes resource name regex
type: string
namespace:
description: Namespace of the Kubernetes object
type: string
Expand Down Expand Up @@ -295,6 +298,9 @@ spec:
description: Name selector is used to identify a Kubernetes Object
based on the metadata name
type: string
nameRegex:
description: kubernetes resource name regex
type: string
namespace:
description: Namespace of the Kubernetes object
type: string
Expand Down
9 changes: 5 additions & 4 deletions internal/controller/testtriggers/testtrigger_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,14 @@ func (v *Validator) validateSelector(fld *field.Path, selector testtriggerv1.Tes
isLabelSelectorEmpty = isEmpty
}

if selector.Name != "" && selector.LabelSelector != nil {
verr := field.Duplicate(fld, "either name or label selector can be used")
if (selector.Name != "" && selector.LabelSelector != nil) || (selector.NameRegex != "" && selector.LabelSelector != nil) ||
(selector.Name != "" && selector.NameRegex != "") {
verr := field.Duplicate(fld, "either name, name regex or label selector can be used")
allErrs = append(allErrs, verr)
}

if selector.Name == "" && isLabelSelectorEmpty {
verr := field.Invalid(fld, selector, "neither name nor label selector is specified")
if selector.Name == "" && selector.NameRegex == "" && isLabelSelectorEmpty {
verr := field.Invalid(fld, selector, "neither name, name regex nor label selector is specified")
allErrs = append(allErrs, verr)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ func TestValidator_validateSelector(t *testing.T) {
assert.Empty(t, verrs)
})

t.Run("error when neither name nor label selector are specified", func(t *testing.T) {
t.Run("error when neither name, name regex nor label selector are specified", func(t *testing.T) {
t.Parallel()

fld := field.NewPath("spec").Child("testSelector")
selector := testtriggerv1.TestTriggerSelector{LabelSelector: &metav1.LabelSelector{}}
verrs := v.validateSelector(fld, selector)
assert.Len(t, verrs, 1)
assert.ErrorContains(t, verrs[0], "neither name nor label selector is specified")
assert.ErrorContains(t, verrs[0], "neither name, name regex nor label selector is specified")
})

t.Run("error when invalid labels are specified", func(t *testing.T) {
Expand Down
Loading