Skip to content

Commit

Permalink
Add EventPolicy resource for rekt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
creydr committed Jun 17, 2024
1 parent 0eee301 commit 7e58801
Show file tree
Hide file tree
Showing 3 changed files with 398 additions and 0 deletions.
160 changes: 160 additions & 0 deletions test/rekt/resources/eventpolicy/eventpolicy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*

Check failure on line 1 in test/rekt/resources/eventpolicy/eventpolicy.go

View workflow job for this annotation

GitHub Actions / style / Golang / Auto-format and Check

Please run goimports. diff --git a/test/rekt/resources/eventpolicy/eventpolicy.go b/test/rekt/resources/eventpolicy/eventpolicy.go index 00538d9..11a3510 100644 --- a/test/rekt/resources/eventpolicy/eventpolicy.go +++ b/test/rekt/resources/eventpolicy/eventpolicy.go @@ -19,9 +19,10 @@ package eventpolicy import ( "context" "embed" + "time" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - "time" "k8s.io/apimachinery/pkg/runtime/schema" "knative.dev/reconciler-test/pkg/feature"
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

Check failure on line 8 in test/rekt/resources/eventpolicy/eventpolicy.go

View workflow job for this annotation

GitHub Actions / style / Golang / Boilerplate Check (go)

[Go headers] reported by reviewdog 🐶 found mismatched boilerplate lines: Raw Output: test/rekt/resources/eventpolicy/eventpolicy.go:8: found mismatched boilerplate lines: {[]string}[0]: -: "\thttp://www.apache.org/licenses/LICENSE-2.0" +: " 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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1"
"time"

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

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

0 comments on commit 7e58801

Please sign in to comment.