Skip to content

Commit

Permalink
WIP: pingsource v1beta1 types
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelvillard committed Jul 16, 2020
1 parent 23afb23 commit d6c5ea2
Show file tree
Hide file tree
Showing 21 changed files with 1,495 additions and 467 deletions.
427 changes: 0 additions & 427 deletions pkg/apis/sources/v1beta1/ping_lifecycle_test.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pkg/apis/sources/v1beta1/ping_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ type PingSourceSpec struct {

// Timezone modifies the actual time relative to the specified timezone.
// Defaults to the system time zone.
// More information about time zones: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
// More general information about time zones: https://www.iana.org/time-zones
// List of valid timezone values: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
Timezone string `json:"timezone,omitempty`

// JsonData is json encoded data used as the body of the event posted to
Expand Down
23 changes: 6 additions & 17 deletions pkg/apis/sources/v1beta1/ping_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ import (

"github.com/robfig/cron/v3"
"knative.dev/pkg/apis"

"knative.dev/eventing/pkg/apis/eventing"
)

func (c *PingSource) Validate(ctx context.Context) *apis.FieldError {
errs := c.Spec.Validate(ctx).ViaField("spec")
return ValidateAnnotations(errs, c.Annotations)
return c.Spec.Validate(ctx).ViaField("spec")
}

func (cs *PingSourceSpec) Validate(ctx context.Context) *apis.FieldError {
var errs *apis.FieldError

schedule := cs.Schedule
if cs.Timezone != "" {
schedule = "CRON_TZ=" + cs.Timezone + " " + schedule
}

if _, err := cron.ParseStandard(cs.Schedule); err != nil {
fe := apis.ErrInvalidValue(cs.Schedule, "schedule")
errs = errs.Also(fe)
Expand All @@ -43,16 +45,3 @@ func (cs *PingSourceSpec) Validate(ctx context.Context) *apis.FieldError {
}
return errs
}

func ValidateAnnotations(errs *apis.FieldError, annotations map[string]string) *apis.FieldError {
if annotations != nil {
if scope, ok := annotations[eventing.ScopeAnnotationKey]; ok {
if scope != eventing.ScopeResource && scope != eventing.ScopeCluster {
iv := apis.ErrInvalidValue(scope, "")
iv.Details = "expected either 'cluster' or 'resource'"
errs = errs.Also(iv.ViaFieldKey("annotations", eventing.ScopeAnnotationKey).ViaField("metadata"))
}
}
}
return errs
}
49 changes: 27 additions & 22 deletions pkg/apis/sources/v1beta1/ping_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import (
"context"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/eventing/pkg/apis/eventing"

duckv1 "knative.dev/pkg/apis/duck/v1"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -52,20 +49,29 @@ func TestPingSourceValidation(t *testing.T) {
},
want: nil,
}, {
name: "empty sink",
name: "valid spec with timezone",
source: PingSource{
Spec: PingSourceSpec{
Schedule: "*/2 * * * *",
Timezone: "Europe/Paris",
SourceSpec: duckv1.SourceSpec{
Sink: duckv1.Destination{
Ref: &duckv1.KReference{
APIVersion: "v1alpha1",
Kind: "broker",
Name: "default",
},
},
},
},
},
want: func() *apis.FieldError {
return apis.ErrGeneric("expected at least one, got none", "ref", "uri").ViaField("spec.sink")
}(),
want: nil,
}, {
name: "invalid schedule",
name: "valid spec with invalid timezone",
source: PingSource{
Spec: PingSourceSpec{
Schedule: "2",
Schedule: "*/2 * * * *",
Timezone: "Knative/Land",
SourceSpec: duckv1.SourceSpec{
Sink: duckv1.Destination{
Ref: &duckv1.KReference{
Expand All @@ -77,37 +83,36 @@ func TestPingSourceValidation(t *testing.T) {
},
},
},
want: nil,
}, {
name: "empty sink",
source: PingSource{
Spec: PingSourceSpec{
Schedule: "*/2 * * * *",
},
},
want: func() *apis.FieldError {
var errs *apis.FieldError
fe := apis.ErrInvalidValue("2", "spec.schedule")
errs = errs.Also(fe)
return errs
return apis.ErrGeneric("expected at least one, got none", "ref", "uri").ViaField("spec.sink")
}(),
}, {
name: "invalid annotation",
name: "invalid schedule",
source: PingSource{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventing.ScopeAnnotationKey: "notvalid",
},
},
Spec: PingSourceSpec{
Schedule: "*/2 * * * *",
Schedule: "2",
SourceSpec: duckv1.SourceSpec{
Sink: duckv1.Destination{
Ref: &duckv1.KReference{
APIVersion: "v1alpha1",
Kind: "broker",
Name: "default",
Namespace: "namespace",
},
},
},
},
},
want: func() *apis.FieldError {
var errs *apis.FieldError
fe := apis.ErrInvalidValue("notvalid", "metadata.annotations.[eventing.knative.dev/scope]\nexpected either 'cluster' or 'resource'")
fe := apis.ErrInvalidValue("2", "spec.schedule")
errs = errs.Also(fe)
return errs
}(),
Expand Down
95 changes: 95 additions & 0 deletions pkg/apis/sources/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d6c5ea2

Please sign in to comment.