diff --git a/docs/eventing-api.md b/docs/eventing-api.md index 2244e88604f..cefd2e02164 100644 --- a/docs/eventing-api.md +++ b/docs/eventing-api.md @@ -504,6 +504,23 @@ For more details: http - https://en.wikipedia.org/wiki/ISO_8601

+ + +format
+ +string + + + +(Optional) +

format specifies the desired event format for the cloud event. +It can be one of the following values: +- nil: default value, no specific format required. +- “JSON”: indicates the event should be in structured mode. +- “binary”: indicates the event should be in binary mode. +- “ingress”: indicates the event should be in ingress mode.

+ +

DeliveryStatus diff --git a/pkg/apis/duck/v1/delivery_types.go b/pkg/apis/duck/v1/delivery_types.go index 48048a09d6e..b70b33d65da 100644 --- a/pkg/apis/duck/v1/delivery_types.go +++ b/pkg/apis/duck/v1/delivery_types.go @@ -18,6 +18,7 @@ package v1 import ( "context" + "strings" "github.com/rickb777/date/period" "knative.dev/pkg/apis" @@ -81,6 +82,15 @@ type DeliverySpec struct { // // +optional RetryAfterMax *string `json:"retryAfterMax,omitempty"` + + // format specifies the desired event format for the cloud event. + // It can be one of the following values: + // - nil: default value, no specific format required. + // - "JSON": indicates the event should be in structured mode. + // - "binary": indicates the event should be in binary mode. + // - "ingress": indicates the event should be in ingress mode. + //+optional + Format *string `json:"format,omitempty"` } func (ds *DeliverySpec) Validate(ctx context.Context) *apis.FieldError { @@ -123,6 +133,17 @@ func (ds *DeliverySpec) Validate(ctx context.Context) *apis.FieldError { } } + if ds.Format != nil { + validFormats := map[string]bool{ + "json": true, + "binary": true, + "ingress": true, + } + if _, ok := validFormats[strings.ToLower(*ds.Format)]; !ok { + errs = errs.Also(apis.ErrInvalidValue(*ds.Format, "format")) + } + } + if ds.RetryAfterMax != nil { if feature.FromContext(ctx).IsEnabled(feature.DeliveryRetryAfter) { p, me := period.Parse(*ds.RetryAfterMax) diff --git a/pkg/apis/duck/v1/delivery_types_test.go b/pkg/apis/duck/v1/delivery_types_test.go index 5ac8f469d56..9605e21f588 100644 --- a/pkg/apis/duck/v1/delivery_types_test.go +++ b/pkg/apis/duck/v1/delivery_types_test.go @@ -141,7 +141,27 @@ func TestDeliverySpecValidation(t *testing.T) { want: func() *apis.FieldError { return apis.ErrDisallowedFields("retryAfterMax") }(), - }} + }, + { + name: "valid format JSON", + spec: &DeliverySpec{Format: pointer.String("json")}, + want: nil, + }, + { + name: "vaalid format binary", + spec: &DeliverySpec{Format: pointer.String("binary")}, + want: nil, + }, { + name: "valid format ingress", + spec: &DeliverySpec{Format: pointer.String("ingress")}, + want: nil, + }, { + name: "invalid format", + spec: &DeliverySpec{Format: pointer.String("invalid")}, + want: func() *apis.FieldError { + return apis.ErrInvalidValue("invalid", "format") + }(), + }} for _, test := range tests { t.Run(test.name, func(t *testing.T) { diff --git a/pkg/apis/duck/v1/zz_generated.deepcopy.go b/pkg/apis/duck/v1/zz_generated.deepcopy.go index d7965aaf4c6..313f09afa74 100644 --- a/pkg/apis/duck/v1/zz_generated.deepcopy.go +++ b/pkg/apis/duck/v1/zz_generated.deepcopy.go @@ -201,6 +201,11 @@ func (in *DeliverySpec) DeepCopyInto(out *DeliverySpec) { *out = new(string) **out = **in } + if in.Format != nil { + in, out := &in.Format, &out.Format + *out = new(string) + **out = **in + } return }