Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EventPolicy resource for rekt tests #8010

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions test/rekt/resources/eventpolicy/eventpolicy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
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: "eventing.knative.dev", Version: "v1alpha1", 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 WithTo(tos ...eventingv1alpha1.EventPolicySpecTo) manifest.CfgFn {
return func(cfg map[string]interface{}) {
if _, set := cfg["to"]; !set {
cfg["to"] = []map[string]interface{}{}
}

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,
}}
}

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 = append(res, to)
}

cfg["to"] = res
}
}

func WithFrom(froms ...eventingv1alpha1.EventPolicySpecFrom) manifest.CfgFn {
return func(cfg map[string]interface{}) {
if _, set := cfg["from"]; !set {
cfg["from"] = []map[string]interface{}{}
}

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,
}
}

res = append(res, from)
}

cfg["from"] = res
}
}

// 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
}
69 changes: 69 additions & 0 deletions test/rekt/resources/eventpolicy/eventpolicy.yaml
Original file line number Diff line number Diff line change
@@ -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"
creydr marked this conversation as resolved.
Show resolved Hide resolved

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 }}
170 changes: 170 additions & 0 deletions test/rekt/resources/eventpolicy/eventpolicy_test.go
Original file line number Diff line number Diff line change
@@ -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.WithTo([]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.WithFrom([]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
}
Loading