|
| 1 | +/* |
| 2 | +Copyright 2024 The Knative Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package apiserversource_newfilters |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + rbacv1 "k8s.io/api/rbac/v1" |
| 22 | + "knative.dev/eventing/test/rekt/resources/account_role" |
| 23 | + |
| 24 | + "github.com/cloudevents/sdk-go/v2/test" |
| 25 | + eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1" |
| 26 | + v1 "knative.dev/eventing/pkg/apis/sources/v1" |
| 27 | + "knative.dev/eventing/test/rekt/resources/apiserversource" |
| 28 | + _ "knative.dev/pkg/system/testing" |
| 29 | + "knative.dev/reconciler-test/pkg/eventshub" |
| 30 | + eventassert "knative.dev/reconciler-test/pkg/eventshub/assert" |
| 31 | + "knative.dev/reconciler-test/pkg/feature" |
| 32 | + "knative.dev/reconciler-test/pkg/manifest" |
| 33 | + "knative.dev/reconciler-test/pkg/resources/pod" |
| 34 | + "knative.dev/reconciler-test/pkg/resources/service" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + exampleImage = "ko://knative.dev/eventing/test/test_images/print" |
| 39 | +) |
| 40 | + |
| 41 | +func NewFiltersFeature() *feature.FeatureSet { |
| 42 | + fs := &feature.FeatureSet{ |
| 43 | + Name: "Knative ApiServerSource - Features - New Filter", |
| 44 | + Features: []*feature.Feature{ |
| 45 | + EventsAreFilteredOut(), |
| 46 | + }, |
| 47 | + } |
| 48 | + return fs |
| 49 | +} |
| 50 | + |
| 51 | +func EventsAreFilteredOut() *feature.Feature { |
| 52 | + source := feature.MakeRandomK8sName("apiserversource") |
| 53 | + sink := feature.MakeRandomK8sName("sink") |
| 54 | + f := feature.NewFeatureNamed("Filters properly the messages") |
| 55 | + |
| 56 | + f.Setup("install sink", eventshub.Install(sink, eventshub.StartReceiver)) |
| 57 | + |
| 58 | + sacmName := feature.MakeRandomK8sName("apiserversource") |
| 59 | + f.Setup("Create Service Account for ApiServerSource with RBAC for v1.Pod resources", |
| 60 | + setupAccountAndRoleForPods(sacmName)) |
| 61 | + |
| 62 | + cfg := []manifest.CfgFn{ |
| 63 | + apiserversource.WithServiceAccountName(sacmName), |
| 64 | + apiserversource.WithEventMode(v1.ResourceMode), |
| 65 | + apiserversource.WithSink(service.AsDestinationRef(sink)), |
| 66 | + apiserversource.WithFilters([]eventingv1.SubscriptionsAPIFilter{{ |
| 67 | + Exact: map[string]string{ |
| 68 | + "type": "dev.knative.apiserver.resource.update", |
| 69 | + }, |
| 70 | + }}), |
| 71 | + apiserversource.WithResources(v1.APIVersionKindSelector{ |
| 72 | + APIVersion: "v1", |
| 73 | + Kind: "Pod", |
| 74 | + }), |
| 75 | + } |
| 76 | + |
| 77 | + f.Setup("install ApiServerSource", apiserversource.Install(source, cfg...)) |
| 78 | + f.Setup("ApiServerSource goes ready", apiserversource.IsReady(source)) |
| 79 | + |
| 80 | + examplePodName := feature.MakeRandomK8sName("example") |
| 81 | + |
| 82 | + // create a pod so that ApiServerSource delivers an event to its sink |
| 83 | + // event body is similar to this: |
| 84 | + // {"kind":"Pod","namespace":"test-wmbcixlv","name":"example-axvlzbvc","apiVersion":"v1"} |
| 85 | + f.Requirement("install example pod", pod.Install(examplePodName, exampleImage)) |
| 86 | + |
| 87 | + f.Stable("ApiServerSource as event source"). |
| 88 | + Must("delivers events", |
| 89 | + eventassert.OnStore(sink).MatchEvent( |
| 90 | + test.HasType("dev.knative.apiserver.resource.add"), |
| 91 | + test.HasExtensions(map[string]interface{}{"apiversion": "v1"}), |
| 92 | + test.DataContains(`"kind":"Pod"`), |
| 93 | + test.DataContains(fmt.Sprintf(`"name":"%s"`, examplePodName)), |
| 94 | + ).Exact(0)). |
| 95 | + Must("delivers events", |
| 96 | + eventassert.OnStore(sink).MatchEvent( |
| 97 | + test.HasType("dev.knative.apiserver.resource.update"), |
| 98 | + test.HasExtensions(map[string]interface{}{"apiversion": "v1"}), |
| 99 | + test.DataContains(`"kind":"Pod"`), |
| 100 | + test.DataContains(fmt.Sprintf(`"name":"%s"`, examplePodName)), |
| 101 | + ).AtLeast(1)) |
| 102 | + |
| 103 | + return f |
| 104 | +} |
| 105 | + |
| 106 | +func setupAccountAndRoleForPods(sacmName string) feature.StepFn { |
| 107 | + return account_role.Install(sacmName, |
| 108 | + account_role.WithRole(sacmName+"-clusterrole"), |
| 109 | + account_role.WithRules(rbacv1.PolicyRule{ |
| 110 | + APIGroups: []string{""}, |
| 111 | + Resources: []string{"events", "pods"}, |
| 112 | + Verbs: []string{"get", "list", "watch"}, |
| 113 | + }), |
| 114 | + ) |
| 115 | +} |
0 commit comments