From 536d5bb6ba2b73456f1295b1743208c2ebea3302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20St=C3=A4bler?= Date: Mon, 17 Jun 2024 14:19:35 +0200 Subject: [PATCH 1/3] Add EventPolicy resource for rekt tests --- .../rekt/resources/eventpolicy/eventpolicy.go | 161 +++++++++++++++++ .../resources/eventpolicy/eventpolicy.yaml | 69 +++++++ .../resources/eventpolicy/eventpolicy_test.go | 170 ++++++++++++++++++ 3 files changed, 400 insertions(+) create mode 100644 test/rekt/resources/eventpolicy/eventpolicy.go create mode 100644 test/rekt/resources/eventpolicy/eventpolicy.yaml create mode 100644 test/rekt/resources/eventpolicy/eventpolicy_test.go diff --git a/test/rekt/resources/eventpolicy/eventpolicy.go b/test/rekt/resources/eventpolicy/eventpolicy.go new file mode 100644 index 00000000000..ca0b716e399 --- /dev/null +++ b/test/rekt/resources/eventpolicy/eventpolicy.go @@ -0,0 +1,161 @@ +/* +Copyright 2024 The Knative Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package eventpolicy + +import ( + "context" + "embed" + "time" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1" + + "k8s.io/apimachinery/pkg/runtime/schema" + "knative.dev/reconciler-test/pkg/feature" + "knative.dev/reconciler-test/pkg/k8s" + "knative.dev/reconciler-test/pkg/manifest" +) + +//go:embed *.yaml +var yaml embed.FS + +func GVR() schema.GroupVersionResource { + return schema.GroupVersionResource{Group: "messaging.knative.dev", Version: "v1", Resource: "eventpolicies"} +} + +// Install will create an EventPolicy resource, augmented with the config fn options. +func Install(name string, opts ...manifest.CfgFn) feature.StepFn { + cfg := map[string]interface{}{ + "name": name, + } + for _, fn := range opts { + fn(cfg) + } + return func(ctx context.Context, t feature.T) { + if _, err := manifest.InstallYamlFS(ctx, yaml, cfg); err != nil { + t.Fatal(err) + } + } +} + +func WithTos(tos []eventingv1alpha1.EventPolicySpecTo) manifest.CfgFn { + return func(cfg map[string]interface{}) { + for _, to := range tos { + WithTo(to)(cfg) + } + } +} + +func WithTo(ref eventingv1alpha1.EventPolicySpecTo) manifest.CfgFn { + return func(cfg map[string]interface{}) { + if _, set := cfg["to"]; !set { + cfg["to"] = []map[string]interface{}{} + } + + to := map[string]interface{}{} + if ref.Ref != nil { + to = map[string]interface{}{ + "ref": map[string]interface{}{ + "apiVersion": ref.Ref.APIVersion, + "kind": ref.Ref.Kind, + "name": ref.Ref.Name, + }} + } + + if ref.Selector != nil { + selector := labelSelectorToStringMap(ref.Selector.LabelSelector) + selector["apiVersion"] = ref.Selector.APIVersion + selector["kind"] = ref.Selector.Kind + + to = map[string]interface{}{ + "selector": selector, + } + } + + tos := cfg["to"].([]map[string]interface{}) + tos = append(tos, to) + + cfg["to"] = tos + } +} + +func WithFroms(froms []eventingv1alpha1.EventPolicySpecFrom) manifest.CfgFn { + return func(cfg map[string]interface{}) { + for _, from := range froms { + WithFrom(from)(cfg) + } + } +} + +func WithFrom(ref eventingv1alpha1.EventPolicySpecFrom) manifest.CfgFn { + return func(cfg map[string]interface{}) { + if _, set := cfg["from"]; !set { + cfg["from"] = []map[string]interface{}{} + } + + from := map[string]interface{}{} + if ref.Ref != nil { + from = map[string]interface{}{ + "ref": map[string]interface{}{ + "apiVersion": ref.Ref.APIVersion, + "kind": ref.Ref.Kind, + "name": ref.Ref.Name, + "namespace": ref.Ref.Namespace, + }} + } + + if ref.Sub != nil && *ref.Sub != "" { + from = map[string]interface{}{ + "sub": *ref.Sub, + } + } + + froms := cfg["from"].([]map[string]interface{}) + froms = append(froms, from) + + cfg["from"] = froms + } +} + +// IsReady tests to see if an EventPolicy becomes ready within the time given. +func IsReady(name string, timing ...time.Duration) feature.StepFn { + return k8s.IsReady(GVR(), name, timing...) +} + +func labelSelectorToStringMap(selector *metav1.LabelSelector) map[string]interface{} { + if selector == nil { + return nil + } + + r := map[string]interface{}{} + + r["matchLabels"] = selector.MatchLabels + + if selector.MatchExpressions != nil { + me := []map[string]interface{}{} + for _, ml := range selector.MatchExpressions { + me = append(me, map[string]interface{}{ + "key": ml.Key, + "operator": ml.Operator, + "values": ml.Values, + }) + } + r["matchExpressions"] = me + } + + return r +} diff --git a/test/rekt/resources/eventpolicy/eventpolicy.yaml b/test/rekt/resources/eventpolicy/eventpolicy.yaml new file mode 100644 index 00000000000..02a98422e67 --- /dev/null +++ b/test/rekt/resources/eventpolicy/eventpolicy.yaml @@ -0,0 +1,69 @@ +# Copyright 2020 The Knative Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: eventing.knative.dev/v1alpha1 +kind: EventPolicy +metadata: + name: {{ .name }} + namespace: {{ .namespace }} +spec: + {{ if .to }} + to: + {{ range $to := .to }} + {{ if $to.ref }} + - ref: + apiVersion: {{ $to.ref.apiVersion }} + kind: {{ $to.ref.kind }} + name: {{ $to.ref.name }} + {{ end }} #end if $to.ref + + {{ if $to.selector }} + - selector: + apiVersion: {{ $to.selector.apiVersion }} + kind: {{ $to.selector.kind }} + {{ if $to.selector.matchLabels }} + matchLabels: + {{ range $key, $value := $to.selector.matchLabels }} + {{ $key }}: {{ $value }} + {{ end }} + {{ end }} #end if to.matchLabels + + {{ if $to.selector.matchExpressions }} + matchExpressions: + {{ range $expr := $to.selector.matchExpressions }} + - key: {{ $expr.key }} + operator: {{ $expr.operator }} + values: + {{ range $exprValue := $expr.values }} + - {{ $exprValue }} + {{ end }} + {{ end }} #end matchExpressions range + {{ end }} # end if matchExpressions + {{ end }} #end if $to.selector + {{ end }} #end "range $to" + {{ end }} #end "if .to" + + from: + {{ range $from := .from }} + {{ if $from.ref }} + - ref: + apiVersion: {{ $from.ref.apiVersion }} + kind: {{ $from.ref.kind }} + name: {{ $from.ref.name }} + namespace: {{ $from.ref.namespace }} + {{ end }} + {{ if $from.sub }} + - sub: {{ $from.sub }} + {{ end }} + {{ end }} diff --git a/test/rekt/resources/eventpolicy/eventpolicy_test.go b/test/rekt/resources/eventpolicy/eventpolicy_test.go new file mode 100644 index 00000000000..3be65fd125a --- /dev/null +++ b/test/rekt/resources/eventpolicy/eventpolicy_test.go @@ -0,0 +1,170 @@ +/* +Copyright 2024 The Knative Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package eventpolicy_test + +import ( + "embed" + "os" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "knative.dev/eventing/pkg/apis/eventing/v1alpha1" + "knative.dev/eventing/test/rekt/resources/eventpolicy" + "knative.dev/pkg/ptr" + + testlog "knative.dev/reconciler-test/pkg/logging" + "knative.dev/reconciler-test/pkg/manifest" +) + +//go:embed *.yaml +var yaml embed.FS + +// The following examples validate the processing of the With* helper methods +// applied to config and go template parser. +func Example_min() { + ctx := testlog.NewContext() + images := map[string]string{} + cfg := map[string]interface{}{ + "name": "foo", + "namespace": "bar", + "from": []map[string]interface{}{ + { + "ref": map[string]string{ + "kind": "Broker", + "name": "my-broker", + "namespace": "my-ns", + "apiVersion": "eventing.knative.dev/v1", + }, + }, + }, + } + + files, err := manifest.ExecuteYAML(ctx, yaml, images, cfg) + if err != nil { + panic(err) + } + + manifest.OutputYAML(os.Stdout, files) + // Output: + // apiVersion: eventing.knative.dev/v1alpha1 + // kind: EventPolicy + // metadata: + // name: foo + // namespace: bar + // spec: + // from: + // - ref: + // apiVersion: eventing.knative.dev/v1 + // kind: Broker + // name: my-broker + // namespace: my-ns +} + +func Example_full() { + ctx := testlog.NewContext() + images := map[string]string{} + cfg := map[string]interface{}{ + "name": "foo", + "namespace": "bar", + } + + cfgFn := []manifest.CfgFn{ + eventpolicy.WithTos([]v1alpha1.EventPolicySpecTo{ + { + Ref: &v1alpha1.EventPolicyToReference{ + Name: "my-broker", + Kind: "Broker", + APIVersion: "eventing.knative.dev/v1", + }, + }, + { + Selector: &v1alpha1.EventPolicySelector{ + LabelSelector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "matchlabel1": "matchlabelvalue1", + "matchlabel2": "matchlabelvalue2", + }, + MatchExpressions: []metav1.LabelSelectorRequirement{ + { + Key: "matchlabelselector1", + Values: []string{"matchlabelselectorvalue1"}, + Operator: metav1.LabelSelectorOpIn, + }, + }, + }, + TypeMeta: &metav1.TypeMeta{ + APIVersion: "eventing.knative.dev/v1", + Kind: "Broker", + }, + }, + }, + }), + eventpolicy.WithFroms([]v1alpha1.EventPolicySpecFrom{ + { + Ref: &v1alpha1.EventPolicyFromReference{ + APIVersion: "eventing.knative.dev/v1", + Name: "my-broker", + Kind: "Broker", + Namespace: "my-ns-2", + }, + }, + { + Sub: ptr.String("my-sub"), + }, + }), + } + + for _, fn := range cfgFn { + fn(cfg) + } + + files, err := manifest.ExecuteYAML(ctx, yaml, images, cfg) + if err != nil { + panic(err) + } + + manifest.OutputYAML(os.Stdout, files) + // Output: + // apiVersion: eventing.knative.dev/v1alpha1 + // kind: EventPolicy + // metadata: + // name: foo + // namespace: bar + // spec: + // to: + // - ref: + // apiVersion: eventing.knative.dev/v1 + // kind: Broker + // name: my-broker + // - selector: + // apiVersion: eventing.knative.dev/v1 + // kind: Broker + // matchLabels: + // matchlabel1: matchlabelvalue1 + // matchlabel2: matchlabelvalue2 + // matchExpressions: + // - key: matchlabelselector1 + // operator: In + // values: + // - matchlabelselectorvalue1 + // from: + // - ref: + // apiVersion: eventing.knative.dev/v1 + // kind: Broker + // name: my-broker + // namespace: my-ns-2 + // - sub: my-sub +} From 168313840f746f65ae232000d6693fd83976a72c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20St=C3=A4bler?= Date: Mon, 17 Jun 2024 15:15:36 +0200 Subject: [PATCH 2/3] Change to variadic functions --- .../rekt/resources/eventpolicy/eventpolicy.go | 96 ++++++++----------- .../resources/eventpolicy/eventpolicy_test.go | 8 +- 2 files changed, 46 insertions(+), 58 deletions(-) diff --git a/test/rekt/resources/eventpolicy/eventpolicy.go b/test/rekt/resources/eventpolicy/eventpolicy.go index ca0b716e399..d75318aa6a7 100644 --- a/test/rekt/resources/eventpolicy/eventpolicy.go +++ b/test/rekt/resources/eventpolicy/eventpolicy.go @@ -52,82 +52,70 @@ func Install(name string, opts ...manifest.CfgFn) feature.StepFn { } } -func WithTos(tos []eventingv1alpha1.EventPolicySpecTo) manifest.CfgFn { - return func(cfg map[string]interface{}) { - for _, to := range tos { - WithTo(to)(cfg) - } - } -} - -func WithTo(ref eventingv1alpha1.EventPolicySpecTo) manifest.CfgFn { +func WithTo(tos ...eventingv1alpha1.EventPolicySpecTo) manifest.CfgFn { return func(cfg map[string]interface{}) { if _, set := cfg["to"]; !set { cfg["to"] = []map[string]interface{}{} } - to := map[string]interface{}{} - if ref.Ref != nil { - to = map[string]interface{}{ - "ref": map[string]interface{}{ - "apiVersion": ref.Ref.APIVersion, - "kind": ref.Ref.Kind, - "name": ref.Ref.Name, - }} - } - - if ref.Selector != nil { - selector := labelSelectorToStringMap(ref.Selector.LabelSelector) - selector["apiVersion"] = ref.Selector.APIVersion - selector["kind"] = ref.Selector.Kind - - to = map[string]interface{}{ - "selector": selector, + res := cfg["to"].([]map[string]interface{}) + for _, ref := range tos { + to := map[string]interface{}{} + if ref.Ref != nil { + to = map[string]interface{}{ + "ref": map[string]interface{}{ + "apiVersion": ref.Ref.APIVersion, + "kind": ref.Ref.Kind, + "name": ref.Ref.Name, + }} } - } - tos := cfg["to"].([]map[string]interface{}) - tos = append(tos, to) + if ref.Selector != nil { + selector := labelSelectorToStringMap(ref.Selector.LabelSelector) + selector["apiVersion"] = ref.Selector.APIVersion + selector["kind"] = ref.Selector.Kind - cfg["to"] = tos - } -} + to = map[string]interface{}{ + "selector": selector, + } + } -func WithFroms(froms []eventingv1alpha1.EventPolicySpecFrom) manifest.CfgFn { - return func(cfg map[string]interface{}) { - for _, from := range froms { - WithFrom(from)(cfg) + res = append(res, to) } + + cfg["to"] = res } } -func WithFrom(ref eventingv1alpha1.EventPolicySpecFrom) manifest.CfgFn { +func WithFrom(froms ...eventingv1alpha1.EventPolicySpecFrom) manifest.CfgFn { return func(cfg map[string]interface{}) { if _, set := cfg["from"]; !set { cfg["from"] = []map[string]interface{}{} } - from := map[string]interface{}{} - if ref.Ref != nil { - from = map[string]interface{}{ - "ref": map[string]interface{}{ - "apiVersion": ref.Ref.APIVersion, - "kind": ref.Ref.Kind, - "name": ref.Ref.Name, - "namespace": ref.Ref.Namespace, - }} - } + res := cfg["from"].([]map[string]interface{}) + for _, ref := range froms { + from := map[string]interface{}{} + if ref.Ref != nil { + from = map[string]interface{}{ + "ref": map[string]interface{}{ + "apiVersion": ref.Ref.APIVersion, + "kind": ref.Ref.Kind, + "name": ref.Ref.Name, + "namespace": ref.Ref.Namespace, + }} + } - if ref.Sub != nil && *ref.Sub != "" { - from = map[string]interface{}{ - "sub": *ref.Sub, + if ref.Sub != nil && *ref.Sub != "" { + from = map[string]interface{}{ + "sub": *ref.Sub, + } } - } - froms := cfg["from"].([]map[string]interface{}) - froms = append(froms, from) + res = append(res, from) + } - cfg["from"] = froms + cfg["from"] = res } } diff --git a/test/rekt/resources/eventpolicy/eventpolicy_test.go b/test/rekt/resources/eventpolicy/eventpolicy_test.go index 3be65fd125a..944aff4c199 100644 --- a/test/rekt/resources/eventpolicy/eventpolicy_test.go +++ b/test/rekt/resources/eventpolicy/eventpolicy_test.go @@ -82,7 +82,7 @@ func Example_full() { } cfgFn := []manifest.CfgFn{ - eventpolicy.WithTos([]v1alpha1.EventPolicySpecTo{ + eventpolicy.WithTo([]v1alpha1.EventPolicySpecTo{ { Ref: &v1alpha1.EventPolicyToReference{ Name: "my-broker", @@ -111,8 +111,8 @@ func Example_full() { }, }, }, - }), - eventpolicy.WithFroms([]v1alpha1.EventPolicySpecFrom{ + }...), + eventpolicy.WithFrom([]v1alpha1.EventPolicySpecFrom{ { Ref: &v1alpha1.EventPolicyFromReference{ APIVersion: "eventing.knative.dev/v1", @@ -124,7 +124,7 @@ func Example_full() { { Sub: ptr.String("my-sub"), }, - }), + }...), } for _, fn := range cfgFn { From 05c817b38801935f4012a392202a72e2c0c9e850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20St=C3=A4bler?= Date: Thu, 20 Jun 2024 09:14:27 +0200 Subject: [PATCH 3/3] Update GVR with correct group and version --- test/rekt/resources/eventpolicy/eventpolicy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/rekt/resources/eventpolicy/eventpolicy.go b/test/rekt/resources/eventpolicy/eventpolicy.go index d75318aa6a7..070b3f48d2c 100644 --- a/test/rekt/resources/eventpolicy/eventpolicy.go +++ b/test/rekt/resources/eventpolicy/eventpolicy.go @@ -34,7 +34,7 @@ import ( var yaml embed.FS func GVR() schema.GroupVersionResource { - return schema.GroupVersionResource{Group: "messaging.knative.dev", Version: "v1", Resource: "eventpolicies"} + return schema.GroupVersionResource{Group: "eventing.knative.dev", Version: "v1alpha1", Resource: "eventpolicies"} } // Install will create an EventPolicy resource, augmented with the config fn options.