Skip to content

Commit

Permalink
Ignore unknown fields in data plane contract (knative-extensions#3335) (
Browse files Browse the repository at this point in the history
#828)

Signed-off-by: Calum Murray <[email protected]>
  • Loading branch information
Cali0707 authored Sep 15, 2023
1 parent d72d1a2 commit b272432
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
6 changes: 5 additions & 1 deletion control-plane/pkg/reconciler/base/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ const (
Json = "json"
)

var (
jsonUnmarshalOptions = protojson.UnmarshalOptions{DiscardUnknown: true}
)

// Base reconciler for broker and trigger reconciler.
// It contains common logic for both trigger and broker reconciler.
type Reconciler struct {
Expand Down Expand Up @@ -213,7 +217,7 @@ func GetDataPlaneConfigMapData(logger *zap.Logger, dataPlaneConfigMap *corev1.Co
case Protobuf:
err = proto.Unmarshal(dataPlaneDataRaw, ct)
case Json:
err = protojson.Unmarshal(dataPlaneDataRaw, ct)
err = jsonUnmarshalOptions.Unmarshal(dataPlaneDataRaw, ct)
}
if err != nil {

Expand Down
66 changes: 66 additions & 0 deletions control-plane/pkg/reconciler/base/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,25 @@ func TestGetDataPlaneConfigMapDataCorrupted(t *testing.T) {
require.Equal(t, uint64(0), got.Generation)
}

func TestGetDataPlaneConfigMapDataUnknownField(t *testing.T) {
ctx, _ := reconcilertesting.SetupFakeContext(t)

r := &base.Reconciler{
KubeClient: kubeclient.Get(ctx),
ContractConfigMapFormat: base.Json,
}

cm := &corev1.ConfigMap{
BinaryData: map[string][]byte{
base.ConfigMapDataKey: []byte(dataPlaneContractExtraData),
},
}

got, err := r.GetDataPlaneConfigMapData(logging.FromContext(ctx).Desugar(), cm)
require.Nil(t, err)
require.Equal(t, uint64(11), got.Generation)
}

func TestUpdateReceiverPodAnnotation(t *testing.T) {
ctx, _ := reconcilertesting.SetupFakeContext(t)

Expand Down Expand Up @@ -297,3 +316,50 @@ func addRunningPod(store cache.Store, kc kubernetes.Interface, label string) {
panic(err)
}
}

const dataPlaneContractExtraData = `{
"generation": "11",
"resources": [
{
"uid": "50a30fb7-9710-45f5-9724-9a7ebb677a29",
"topics": [
"knative-messaging-kafka.eventing-e2e17.sut"
],
"bootstrapServers": "my-cluster-kafka-bootstrap.kafka:9092",
"ingress": {
"host": "sut-kn-channel.eventing-e2e17.svc.cluster.local"
},
"egressConfig": {
"retry": 12,
"backoffDelay": "1000"
},
"egresses": [
{
"consumerGroup": "kafka.eventing-e2e17.sut.e21ad4f4-bf2d-4fe2-879a-728bb9d5626d",
"destination": "http://wathola-receiver.eventing-e2e17.svc.cluster.local",
"discardReply": {},
"uid": "e21ad4f4-bf2d-4fe2-879a-728bb9d5626d",
"egressConfig": {
"retry": 12,
"backoffDelay": "1000"
},
"deliveryOrder": "ORDERED",
"reference": {
"uuid": "e21ad4f4-bf2d-4fe2-879a-728bb9d5626d",
"namespace": "eventing-e2e17",
"name": "sut"
}
}
],
"reference": {
"uuid": "50a30fb7-9710-45f5-9724-9a7ebb677a29",
"namespace": "eventing-e2e17",
"name": "sut",
"kind": "KafkaChannel",
"groupVersion": "messaging.knative.dev/v1beta1"
},
"extraKey": "extraValue"
}
]
}
`

0 comments on commit b272432

Please sign in to comment.