Skip to content

Commit

Permalink
feat: validate bindings (#1030)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Mar 4, 2024
1 parent 839fbc1 commit ffde0e8
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 24 deletions.
24 changes: 0 additions & 24 deletions pkg/apis/v1alpha1/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// For specifies the condition to wait for.
type For struct {
// Deletion specifies parameters for waiting on a resource's deletion.
// +optional
Deletion *Deletion `json:"deletion,omitempty"`

// Condition specifies the condition to wait for.
// +optional
Condition *Condition `json:"condition,omitempty"`
}

// Deletion represents parameters for waiting on a resource's deletion.
type Deletion struct{}

// Condition represents parameters for waiting on a specific condition of a resource.
type Condition struct {
// Name defines the specific condition to wait for, e.g., "Available", "Ready".
Name string `json:"name"`

// Value defines the specific condition status to wait for, e.g., "True", "False".
// +optional
Value *string `json:"value,omitempty"`
}

// Wait specifies how to perform wait operations on resources.
type Wait struct {
// Timeout for the operation. Specifies how long to wait for the condition to be met before timing out.
Expand Down
11 changes: 11 additions & 0 deletions pkg/apis/v1alpha1/wait_condition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package v1alpha1

// Condition represents parameters for waiting on a specific condition of a resource.
type Condition struct {
// Name defines the specific condition to wait for, e.g., "Available", "Ready".
Name string `json:"name"`

// Value defines the specific condition status to wait for, e.g., "True", "False".
// +optional
Value *string `json:"value,omitempty"`
}
4 changes: 4 additions & 0 deletions pkg/apis/v1alpha1/wait_deletion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package v1alpha1

// Deletion represents parameters for waiting on a resource's deletion.
type Deletion struct{}
12 changes: 12 additions & 0 deletions pkg/apis/v1alpha1/wait_for.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package v1alpha1

// For specifies the condition to wait for.
type For struct {
// Deletion specifies parameters for waiting on a resource's deletion.
// +optional
Deletion *Deletion `json:"deletion,omitempty"`

// Condition specifies the condition to wait for.
// +optional
Condition *Condition `json:"condition,omitempty"`
}
1 change: 1 addition & 0 deletions pkg/validation/test/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func ValidateApply(path *field.Path, obj *v1alpha1.Apply) field.ErrorList {
if obj != nil {
errs = append(errs, ValidateFileRefOrResource(path, obj.FileRefOrResource)...)
errs = append(errs, ValidateExpectations(path.Child("expect"), obj.Expect...)...)
errs = append(errs, ValidateBindings(path.Child("bindings"), obj.Bindings...)...)
}
return errs
}
1 change: 1 addition & 0 deletions pkg/validation/test/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func ValidateAssert(path *field.Path, obj *v1alpha1.Assert) field.ErrorList {
var errs field.ErrorList
if obj != nil {
errs = append(errs, ValidateFileRefOrCheck(path, obj.FileRefOrCheck)...)
errs = append(errs, ValidateBindings(path.Child("bindings"), obj.Bindings...)...)
}
return errs
}
22 changes: 22 additions & 0 deletions pkg/validation/test/binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package test

import (
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"k8s.io/apimachinery/pkg/util/validation/field"
)

func ValidateBinding(path *field.Path, obj v1alpha1.Binding) field.ErrorList {
var errs field.ErrorList
if err := obj.CheckName(); err != nil {
errs = append(errs, field.Invalid(path.Child("name"), obj.Name, err.Error()))
}
return errs
}

func ValidateBindings(path *field.Path, objs ...v1alpha1.Binding) field.ErrorList {
var errs field.ErrorList
for i, obj := range objs {
errs = append(errs, ValidateBinding(path.Index(i), obj)...)
}
return errs
}
1 change: 1 addition & 0 deletions pkg/validation/test/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func ValidateCommand(path *field.Path, obj *v1alpha1.Command) field.ErrorList {
errs = append(errs, field.Invalid(path.Child("entrypoint"), obj, "entrypoint must be specified"))
}
errs = append(errs, ValidateCheck(path.Child("check"), obj.Check)...)
errs = append(errs, ValidateBindings(path.Child("bindings"), obj.Bindings...)...)
}
return errs
}
1 change: 1 addition & 0 deletions pkg/validation/test/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func ValidateCreate(path *field.Path, obj *v1alpha1.Create) field.ErrorList {
if obj != nil {
errs = append(errs, ValidateFileRefOrResource(path, obj.FileRefOrResource)...)
errs = append(errs, ValidateExpectations(path.Child("expect"), obj.Expect...)...)
errs = append(errs, ValidateBindings(path.Child("bindings"), obj.Bindings...)...)
}
return errs
}
1 change: 1 addition & 0 deletions pkg/validation/test/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func ValidateDelete(path *field.Path, obj *v1alpha1.Delete) field.ErrorList {
if obj != nil {
errs = append(errs, ValidateObjectReference(path.Child("ref"), obj.ObjectReference)...)
errs = append(errs, ValidateExpectations(path.Child("expect"), obj.Expect...)...)
errs = append(errs, ValidateBindings(path.Child("bindings"), obj.Bindings...)...)
}
return errs
}
1 change: 1 addition & 0 deletions pkg/validation/test/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func ValidateError(path *field.Path, obj *v1alpha1.Error) field.ErrorList {
var errs field.ErrorList
if obj != nil {
errs = append(errs, ValidateFileRefOrCheck(path, obj.FileRefOrCheck)...)
errs = append(errs, ValidateBindings(path.Child("bindings"), obj.Bindings...)...)
}
return errs
}
1 change: 1 addition & 0 deletions pkg/validation/test/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func ValidatePatch(path *field.Path, obj *v1alpha1.Patch) field.ErrorList {
if obj != nil {
errs = append(errs, ValidateFileRefOrResource(path, obj.FileRefOrResource)...)
errs = append(errs, ValidateExpectations(path.Child("expect"), obj.Expect...)...)
errs = append(errs, ValidateBindings(path.Child("bindings"), obj.Bindings...)...)
}
return errs
}
1 change: 1 addition & 0 deletions pkg/validation/test/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func ValidateScript(path *field.Path, obj *v1alpha1.Script) field.ErrorList {
errs = append(errs, field.Invalid(path.Child("content"), obj, "content must be specified"))
}
errs = append(errs, ValidateCheck(path.Child("check"), obj.Check)...)
errs = append(errs, ValidateBindings(path.Child("bindings"), obj.Bindings...)...)
}
return errs
}
1 change: 1 addition & 0 deletions pkg/validation/test/test_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ func ValidateTestSpec(path *field.Path, obj v1alpha1.TestSpec) field.ErrorList {
errs = append(errs, ValidateCatch(path.Child("catch").Index(i), catch)...)
}
errs = append(errs, ValidateCheck(path.Child("namespaceTemplate"), obj.NamespaceTemplate)...)
errs = append(errs, ValidateBindings(path.Child("bindings"), obj.Bindings...)...)
return errs
}
1 change: 1 addition & 0 deletions pkg/validation/test/test_step_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ func ValidateTestStepSpec(path *field.Path, obj v1alpha1.TestStepSpec) field.Err
for i, finally := range obj.Finally {
errs = append(errs, ValidateFinally(path.Child("finally").Index(i), finally)...)
}
errs = append(errs, ValidateBindings(path.Child("bindings"), obj.Bindings...)...)
return errs
}

0 comments on commit ffde0e8

Please sign in to comment.