-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EventPolicy resource for rekt tests
- Loading branch information
Showing
3 changed files
with
398 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / style / Golang / Auto-format and Check
|
||
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 GitHub Actions / style / Golang / Boilerplate Check (go)
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
Oops, something went wrong.