@@ -20,24 +20,13 @@ import (
2020 "context"
2121 "encoding/json"
2222 "fmt"
23- "reflect"
2423
2524 cloudevents "github.com/cloudevents/sdk-go/v2"
2625
2726 "knative.dev/eventing/pkg/apis/sources/v1beta2"
2827 "knative.dev/pkg/apis"
2928)
3029
31- const (
32- // V1B1SpecAnnotationKey is used to indicate that a v1beta2 object is converted from v1beta1
33- // also it can be used to downgrade such object to v1beta1
34- V1B1SpecAnnotationKey = "pingsources.sources.knative.dev/v1beta1-spec"
35-
36- // V1B2SpecAnnotationKey is used to indicate that a v1beta1 object is converted from v1beta2
37- // also it can be used to convert the v1beta1 object back to v1beta2, considering that v1beta2 introduces more features.
38- V1B2SpecAnnotationKey = "pingsources.sources.knative.dev/v1beta2-spec"
39- )
40-
4130type message struct {
4231 Body string `json:"body"`
4332}
@@ -61,35 +50,21 @@ func (source *PingSource) ConvertTo(ctx context.Context, obj apis.Convertible) e
6150 sink .Status = v1beta2.PingSourceStatus {
6251 SourceStatus : source .Status .SourceStatus ,
6352 }
64-
65- // deep copy annotations to avoid mutation on source.ObjectMeta.Annotations
66- annotations := make (map [string ]string )
67- for key , value := range source .GetAnnotations () {
68- annotations [key ] = value
53+ sink .Spec = v1beta2.PingSourceSpec {
54+ SourceSpec : source .Spec .SourceSpec ,
55+ Schedule : source .Spec .Schedule ,
56+ Timezone : source .Spec .Timezone ,
6957 }
7058
71- if isCreatedViaV1Beta2API (source ) {
72- // try to unmarshal v1beta2.PingSource.Spec from V1B2SpecAnnotationKey
73- // key existence and json marshal error already checked in isCreatedViaV1Beta2API
74- v1beta2Spec := annotations [V1B2SpecAnnotationKey ]
75- _ = json .Unmarshal ([]byte (v1beta2Spec ), & sink .Spec )
76- } else {
77- var err error
78- if sink .Spec , err = toV1Beta2Spec (& source .Spec ); err != nil {
79- return err
80- }
81- // marshal and store v1beta1.PingSource.Spec into V1B1SpecAnnotationKey
82- // this is to help if we need to convert back to v1beta1.PingSource
83- v1beta1Spec , err := json .Marshal (source .Spec )
59+ if source .Spec .JsonData != "" {
60+ msg , err := makeMessage (source .Spec .JsonData )
8461 if err != nil {
85- return fmt .Errorf ("error marshalling source.Spec: %v, err : %v" , source . Spec , err )
62+ return fmt .Errorf ("error converting jsonData to a higher version : %v" , err )
8663 }
87- annotations [V1B1SpecAnnotationKey ] = string (v1beta1Spec )
64+ sink .Spec .ContentType = cloudevents .ApplicationJSON
65+ sink .Spec .Data = string (msg )
8866 }
8967
90- // we don't need this annotation in a v1beta2.PingSource object
91- delete (annotations , V1B2SpecAnnotationKey )
92- sink .SetAnnotations (annotations )
9368 return nil
9469 default :
9570 return apis .ConvertToViaProxy (ctx , source , & v1beta2.PingSource {}, sink )
@@ -106,94 +81,18 @@ func (sink *PingSource) ConvertFrom(ctx context.Context, obj apis.Convertible) e
10681 SourceStatus : source .Status .SourceStatus ,
10782 }
10883
109- // deep copy annotations to avoid mutation on source.ObjectMeta.Annotations
110- annotations := make (map [string ]string )
111- for key , value := range source .GetAnnotations () {
112- annotations [key ] = value
113- }
114-
115- if isV1Beta1AnnotationConsistentWithV1Beta2Spec (source ) {
116- // errors already handled in isV1Beta1AnnotationConsistentWithV1Beta2Spec
117- v1beta1Spec := annotations [V1B1SpecAnnotationKey ]
118- _ = json .Unmarshal ([]byte (v1beta1Spec ), & sink .Spec )
84+ sink .Spec = PingSourceSpec {
85+ SourceSpec : source .Spec .SourceSpec ,
86+ Schedule : source .Spec .Schedule ,
87+ Timezone : source .Spec .Timezone ,
11988 }
12089
121- // marshal and store v1beta2.PingSource.Spec into V1B2SpecAnnotationKey
122- // this is to help if we need to convert back to v1beta2.PingSource
123- v1beta2Configuration , err := json .Marshal (source .Spec )
124- if err != nil {
125- return fmt .Errorf ("error marshalling source.Spec: %v, err: %v" , source .Spec , err )
90+ if source .Spec .ContentType == cloudevents .ApplicationJSON {
91+ sink .Spec .JsonData = source .Spec .Data
12692 }
127- annotations [V1B2SpecAnnotationKey ] = string (v1beta2Configuration )
128- // we don't need this annotation in a v1beta1.PingSource object
129- delete (annotations , V1B1SpecAnnotationKey )
130- sink .SetAnnotations (annotations )
13193
13294 return nil
13395 default :
13496 return apis .ConvertFromViaProxy (ctx , source , & v1beta2.PingSource {}, sink )
13597 }
13698}
137-
138- func toV1Beta2Spec (srcSpec * PingSourceSpec ) (v1beta2.PingSourceSpec , error ) {
139- targetSpec := v1beta2.PingSourceSpec {
140- SourceSpec : srcSpec .SourceSpec ,
141- Schedule : srcSpec .Schedule ,
142- Timezone : srcSpec .Timezone ,
143- }
144-
145- if srcSpec .JsonData != "" {
146- msg , err := makeMessage (srcSpec .JsonData )
147- if err != nil {
148- return targetSpec , fmt .Errorf ("error converting jsonData to a higher version: %v" , err )
149- }
150- targetSpec .ContentType = cloudevents .ApplicationJSON
151- targetSpec .Data = string (msg )
152- }
153-
154- return targetSpec , nil
155- }
156-
157- // checks if a v1beta1.PingSource is originally created in v1beta2, it must meet both of the following criteria:
158- //
159- // 1. V1B2SpecAnnotationKey annotation must exist and can be unmarshalled to v1beta2.PingSourceSpec, it indicates that it's converted from v1beta2 -> v1beta1.
160- // 2. Spec.Sink must be {Ref: nil, URI: nil}, as we don't set these values during conversion from v1beta2 -> v1beta1, see PingSource.ConvertFrom;
161- func isCreatedViaV1Beta2API (source * PingSource ) bool {
162- v1beta2Annotation , ok := source .GetAnnotations ()[V1B2SpecAnnotationKey ]
163- if ! ok {
164- return false
165- }
166-
167- v1beta2Spec := & v1beta2.PingSourceSpec {}
168- if err := json .Unmarshal ([]byte (v1beta2Annotation ), v1beta2Spec ); err != nil {
169- return false
170- }
171-
172- return source .Spec .Sink .Ref == nil && source .Spec .Sink .URI == nil
173- }
174-
175- // for a v1beta2.PingSource, checks if its V1B1SpecAnnotationKey is consistent with its spec.
176- // returns false if one of the following satisfies:
177- //
178- // 1. V1B1SpecAnnotationKey does not exist.
179- // 2. V1B1SpecAnnotationKey exists, but we cannot unmarshal it to v1beta1.PingSourceSpec.
180- // 3. V1B1SpecAnnotationKey exists, but if we unmarshal it to v1beta1.PingSourceSpec and convert it to v1beta2,
181- // the converted v1beta2.PingSourceSpec is not the same as source.Spec.
182- func isV1Beta1AnnotationConsistentWithV1Beta2Spec (source * v1beta2.PingSource ) bool {
183- v1beta1Annotation , ok := source .GetAnnotations ()[V1B1SpecAnnotationKey ]
184- if ! ok {
185- return false
186- }
187-
188- v1beta1Spec := & PingSourceSpec {}
189- if err := json .Unmarshal ([]byte (v1beta1Annotation ), v1beta1Spec ); err != nil {
190- return false
191- }
192-
193- v1beta2Spec , err := toV1Beta2Spec (v1beta1Spec )
194- if err != nil {
195- return false
196- }
197-
198- return reflect .DeepEqual (v1beta2Spec , source .Spec )
199- }
0 commit comments