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

e2e: nodeselector in apiserversauce #7627

Merged
merged 22 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
15 changes: 15 additions & 0 deletions test/rekt/apiserversource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,18 @@ func TestApiserversourceSendEventWithJWTOIDC(t *testing.T) {

env.Test(ctx, t, apiserversourcefeatures.ApiserversourceSendEventWithJWT())
}

func TestApiServerSourceDeployement(t *testing.T) {
sadath-12 marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

ctx, env := global.Environment(
knative.WithKnativeNamespace(system.Namespace()),
knative.WithLoggingConfig,
knative.WithTracingConfig,
k8s.WithEventListener,
environment.Managed(t),
environment.WithPollTimings(5*time.Second, 2*time.Minute),
)

env.Test(ctx, t, apiserversourcefeatures.DeployAPIServerSauceWithNodeSelector())
sadath-12 marked this conversation as resolved.
Show resolved Hide resolved
}
73 changes: 70 additions & 3 deletions test/rekt/features/apiserversource/data_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ import (
"github.com/cloudevents/sdk-go/v2/test"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
client "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/network"
"knative.dev/reconciler-test/pkg/environment"

"knative.dev/eventing/pkg/eventingtls/eventingtlstesting"
"knative.dev/eventing/test/rekt/resources/addressable"
"knative.dev/eventing/test/rekt/resources/configmap"

rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/util/sets"
"knative.dev/reconciler-test/pkg/eventshub"
"knative.dev/reconciler-test/pkg/feature"
Expand Down Expand Up @@ -279,7 +280,7 @@ func SendsEventsWithEventTypes() *feature.Feature {

f := new(feature.Feature)

//Install the broker
// Install the broker
brokerName := feature.MakeRandomK8sName("broker")
f.Setup("install broker", broker.Install(brokerName, broker.WithEnvConfig()...))
f.Setup("broker is ready", broker.IsReady(brokerName))
Expand Down Expand Up @@ -822,6 +823,73 @@ func SendsEventsWithRetries() *feature.Feature {
return f
}

func setupNodeLabels(nodeLabels map[string]string) feature.StepFn {
return func(ctx context.Context, t feature.T) {
nodes, err := client.Get(ctx).CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
t.Fatalf("Could not list nodes: %v", err)
}

if len(nodes.Items) == 0 {
t.Fatal("No nodes found")
}

randomNode := &nodes.Items[0]

randomNode.Labels = nodeLabels

// Update the node with the new labels
_, err = client.Get(ctx).CoreV1().Nodes().Update(ctx, randomNode, metav1.UpdateOptions{})

if err != nil {
t.Fatalf("Could not update node: %v", err)
}
}
}

func DeployAPIServerSauceWithNodeSelector() *feature.Feature {
sadath-12 marked this conversation as resolved.
Show resolved Hide resolved
f := feature.NewFeatureNamed("deploy")
sadath-12 marked this conversation as resolved.
Show resolved Hide resolved

// same labels as in the configmap

f.Requirement("setup config-features and load into context", func(ctx context.Context, t feature.T) {
sadath-12 marked this conversation as resolved.
Show resolved Hide resolved
env := environment.FromContext(ctx)
ns := env.Namespace()
configmap.Install("config-features", ns)
sadath-12 marked this conversation as resolved.
Show resolved Hide resolved
})
f.Requirement("setup node labels", setupNodeLabels(apiserversource.TestLabels()))

source := feature.MakeRandomK8sName("apiserversource")
sink := feature.MakeRandomK8sName("sink")

f.Setup("install sink", eventshub.Install(sink, eventshub.StartReceiver))

sacmName := feature.MakeRandomK8sName("apiserversource")
f.Setup("Create Service Account for ApiServerSource with RBAC for sources.knative.dev/v1 PingSources",
setupAccountAndRoleForPingSources(sacmName))

cfg := []manifest.CfgFn{
apiserversource.WithServiceAccountName(sacmName),
apiserversource.WithEventMode("Reference"),
apiserversource.WithSink(service.AsDestinationRef(sink)),
apiserversource.WithResources(v1.APIVersionKindSelector{
APIVersion: "sources.knative.dev/v1",
Kind: "PingSource",
}),
apiserversource.WithNamespaceSelector(&metav1.LabelSelector{
MatchLabels: map[string]string{},
MatchExpressions: []metav1.LabelSelectorRequirement{},
}),
sadath-12 marked this conversation as resolved.
Show resolved Hide resolved
}

f.Setup("install ApiServerSource", apiserversource.Install(source, cfg...))
f.Setup("ApiServerSource goes ready", apiserversource.IsReady(source))
sadath-12 marked this conversation as resolved.
Show resolved Hide resolved

f.Stable("ApiServerSauce using nodeSelector").Must("must use it from config-features", apiserversource.VerifyNodeSelectorDeployment(source))
sadath-12 marked this conversation as resolved.
Show resolved Hide resolved

return f
}

func SendsEventsWithBrokerAsSinkTLS() *feature.Feature {
src := feature.MakeRandomK8sName("apiserversource")
sacmName := feature.MakeRandomK8sName("apiserversource")
Expand Down Expand Up @@ -902,5 +970,4 @@ func SendsEventsWithBrokerAsSinkTLS() *feature.Feature {
)

return f

}
48 changes: 48 additions & 0 deletions test/rekt/resources/apiserversource/apiserversource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@ package apiserversource
import (
"context"
"embed"
"fmt"
"strings"
"time"

appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/reconciler-test/pkg/environment"
"knative.dev/reconciler-test/pkg/feature"
"knative.dev/reconciler-test/pkg/k8s"

v1 "knative.dev/eventing/pkg/apis/sources/v1"
"knative.dev/pkg/client/injection/kube/client"

duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/kmeta"
"knative.dev/reconciler-test/pkg/manifest"
)

Expand Down Expand Up @@ -166,3 +171,46 @@ func labelSelectorToStringMap(selector *metav1.LabelSelector) map[string]interfa

return r
}

func MatchLabels(labels1 map[string]string, labels2 map[string]string) bool {
for k, v := range labels1 {
if labels2[k] != v {
return false
}
}
return true
}

func TestLabels() map[string]string {
return map[string]string{
"testkey": "testvalue",
"testkey1": "testvalue1",
"testkey2": "testvalue2",
}
}

func VerifyNodeSelectorDeployment(source string) feature.StepFn {
return func(ctx context.Context, t feature.T) {
env := environment.FromContext(ctx)
ns := env.Namespace()

kubeClient := client.Get(ctx)

deps, err := kubeClient.AppsV1().Deployments(ns).List(ctx, metav1.ListOptions{})
if err != nil {
t.Fatalf("error getting deployment: %v", err)
}

var dep appsv1.Deployment

for _, d := range deps.Items {
if kmeta.ChildName(fmt.Sprintf("apiserversource-%s-", source), string(d.GetUID())) == d.Name {
dep = d
}
}
sadath-12 marked this conversation as resolved.
Show resolved Hide resolved

if !MatchLabels(dep.Spec.Template.Spec.NodeSelector, TestLabels()) {
t.Fatalf("NodeSelector labels do not match: %v", dep.Spec.Template.Spec.NodeSelector)
}
}
}
17 changes: 17 additions & 0 deletions test/rekt/resources/configmap/config-features.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .name }}
namespace: {{ .namespace }}
labels:
knative.dev/config-propagation: original
knative.dev/config-category: eventing
data:
_example: |
my-enabled-flag: "enabled"
my-disabled-flag: "disabled"
my-allowed-flag: "allowed"
apiserversources.nodeselector.testkey: testvalue
apiserversources.nodeselector.testkey1: testvalue1
apiserversources.nodeselector.testkey2: testvalue2

61 changes: 61 additions & 0 deletions test/rekt/resources/configmap/configmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2021 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 configmap

import (
"context"
"embed"
"time"

// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
// cfgFeat "knative.dev/eventing/pkg/apis/feature"
// "knative.dev/pkg/client/injection/kube/client"
"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: "sources.knative.dev", Version: "v1", Resource: "containersources"}
}

// IsReady tests to see if a Configmap becomes ready within the time given.
func IsReady(name string, timing ...time.Duration) feature.StepFn {
return k8s.IsReady(Gvr(), name, timing...)
}

sadath-12 marked this conversation as resolved.
Show resolved Hide resolved
// Install will create a configmap resource and will load the configmap to the context.
func Install(name string, ns string, opts ...manifest.CfgFn) feature.StepFn {
cfg := map[string]interface{}{
"name": name,
"namespace": ns,
}

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