Skip to content

Commit

Permalink
Validation (#255)
Browse files Browse the repository at this point in the history
* make use go-get

* use apimachinery validation

* use apimachinery validation

* revert makefile changes

* linter finding fix
  • Loading branch information
m-terra authored Nov 22, 2022
1 parent cb3865d commit 0600a8f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 40 deletions.
1 change: 0 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ linters:
- unconvert
- unparam
- unused
- wastedassign
linters-settings:
gosec:
# Exclude generated files
Expand Down
6 changes: 3 additions & 3 deletions api/v1/eventlogger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ type EventLoggerSpec struct {
EventTypes []string `json:"eventTypes,omitempty"`

// Labels additional labels for the logger pod
Labels map[string]string `json:"labels,omitempty" validate:"k8s-label-keys,k8s-label-values"`
Labels map[string]string `json:"labels,omitempty" validate:"k8s-label-annotation-keys,k8s-label-values"`

// Labels additional annotations for the logger pod
Annotations map[string]string `json:"annotations,omitempty" validate:"k8s-annotation-keys"`
Annotations map[string]string `json:"annotations,omitempty" validate:"k8s-label-annotation-keys"`

// ScrapeMetrics if true, prometheus scrape annotations are added to the pod
ScrapeMetrics *bool `json:"scrapeMetrics,omitempty"`
Expand All @@ -52,7 +52,7 @@ type EventLoggerSpec struct {
// Selector which must match a node's labels for the pod to be scheduled on that node.
// More info: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/
// +optional
NodeSelector map[string]string `json:"nodeSelector,omitempty" validate:"k8s-label-keys,k8s-label-values"`
NodeSelector map[string]string `json:"nodeSelector,omitempty" validate:"k8s-label-annotation-keys,k8s-label-values"`

// LogFields fields ot the event to be logged.
LogFields []LogField `json:"logFields,omitempty"`
Expand Down
41 changes: 11 additions & 30 deletions api/v1/eventlogger_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@ import (
"errors"
"fmt"
"reflect"
"regexp"
"strings"

"github.com/bakito/k8s-event-logger-operator/version"
english "github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
"github.com/go-playground/validator/v10/translations/en"
)

var (
annotationKeyPattern = regexp.MustCompile(`^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9](\/([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$`)
labelKeyPattern = regexp.MustCompile(`^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$`)
labelValuePattern = regexp.MustCompile(`^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$`)
"k8s.io/apimachinery/pkg/util/validation"
)

// Custom type for context key, so we don't have to use 'string' directly
Expand Down Expand Up @@ -53,29 +47,18 @@ func (in *EventLoggerSpec) Validate() error {
func k8sLabelValues(_ context.Context, fl validator.FieldLevel) bool {
if labels, ok := fl.Field().Interface().(map[string]string); ok {
for _, v := range labels {
if !labelValuePattern.MatchString(v) {
if errs := validation.IsValidLabelValue(v); len(errs) > 0 {
return false
}
}
}
return true
}

func k8sLabelKeys(_ context.Context, fl validator.FieldLevel) bool {
func k8sLabelAnnotationKeys(_ context.Context, fl validator.FieldLevel) bool {
if annotations, ok := fl.Field().Interface().(map[string]string); ok {
for k := range annotations {
if !labelKeyPattern.MatchString(k) {
return false
}
}
}
return true
}

func k8sAnnotationKeys(_ context.Context, fl validator.FieldLevel) bool {
if annotations, ok := fl.Field().Interface().(map[string]string); ok {
for k := range annotations {
if !annotationKeyPattern.MatchString(k) {
if errs := validation.IsQualifiedName(k); len(errs) > 0 {
return false
}
}
Expand Down Expand Up @@ -108,9 +91,11 @@ func (err *eventLoggerValidatorError) addError(errStr string) {
func newEventLoggerValidator(spec *EventLoggerSpec) *eventLoggerValidator {
result := validator.New()

_ = result.RegisterValidationCtx("k8s-label-keys", k8sLabelKeys)
_ = result.RegisterValidationCtx("k8s-label-annotation-keys", k8sLabelAnnotationKeys)
_ = result.RegisterValidationCtx("k8s-label-values", k8sLabelValues)
_ = result.RegisterValidationCtx("k8s-annotation-keys", k8sAnnotationKeys)

errKey := strings.Join(validation.IsQualifiedName("a@a"), " ")
errLabelVal := strings.Join(validation.IsValidLabelValue("a:/a"), " ")

// context
ctx := context.WithValue(context.Background(), specKey, spec)
Expand All @@ -128,16 +113,12 @@ func newEventLoggerValidator(spec *EventLoggerSpec) *eventLoggerValidator {
translation string
}{
{
tag: "k8s-label-keys",
translation: fmt.Sprintf("'key in {0}' must match the pattern %s", labelKeyPattern.String()),
tag: "k8s-label-annotation-keys",
translation: fmt.Sprintf("'key in {0}' must match the pattern %s", errKey),
},
{
tag: "k8s-label-values",
translation: fmt.Sprintf("'values in {0}' must match the pattern %s", labelValuePattern.String()),
},
{
tag: "k8s-annotation-keys",
translation: fmt.Sprintf("'key in {0}' must match the pattern %s", annotationKeyPattern.String()),
translation: fmt.Sprintf("'values in {0}' must match the pattern %s", errLabelVal),
},
}
for _, t := range translations {
Expand Down
15 changes: 9 additions & 6 deletions api/v1/eventlogger_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,30 @@ var _ = Describe("V1", func() {
s := &v1.EventLoggerSpec{
Labels: map[string]string{"in valid": "valid"},
}

Ω(s.Validate()).Should(HaveOccurred())
s = &v1.EventLoggerSpec{
Labels: map[string]string{"in:valid": "valid"},
}
Ω(s.Validate()).Should(HaveOccurred())
})
It("should have invalid label value", func() {
s := &v1.EventLoggerSpec{
Labels: map[string]string{"valid": "in valid"},
}

Ω(s.Validate()).Should(HaveOccurred())
s = &v1.EventLoggerSpec{
Labels: map[string]string{"valid": "in:valid"},
}
Ω(s.Validate()).Should(HaveOccurred())
})
It("should have invalid annotation key", func() {
s := &v1.EventLoggerSpec{
Annotations: map[string]string{"in valid": "valid"},
}

Ω(s.Validate()).Should(HaveOccurred())

s = &v1.EventLoggerSpec{
Annotations: map[string]string{"in/valid/": "valid"},
Annotations: map[string]string{"in:valid:": "valid"},
}

Ω(s.Validate()).Should(HaveOccurred())
})
})
Expand Down

0 comments on commit 0600a8f

Please sign in to comment.