| 1 | /* |
| 2 | Copyright 2024 The Kubernetes 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 | package e2e |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "strconv" |
| 21 | "testing" |
| 22 | |
| 23 | "sigs.k8s.io/controller-runtime/pkg/client" |
| 24 | "sigs.k8s.io/e2e-framework/pkg/features" |
| 25 | ) |
| 26 | |
| 27 | func renderStaticCountDeployments(serverReplicas, agentReplicas int) (serverDeployment client.Object, agentDeployment client.Object, err error) { |
| 28 | serverDeploymentConfig := DeploymentConfig{ |
| 29 | Replicas: serverReplicas, |
| 30 | Image: *serverImage, |
| 31 | Args: []CLIFlag{ |
| 32 | {Flag: "log-file", Value: "/var/log/konnectivity-server.log"}, |
| 33 | {Flag: "logtostderr", Value: "true"}, |
| 34 | {Flag: "log-file-max-size", Value: "0"}, |
| 35 | {Flag: "uds-name", Value: "/etc/kubernetes/konnectivity-server/konnectivity-server.socket"}, |
| 36 | {Flag: "delete-existing-uds-file"}, |
| 37 | {Flag: "cluster-cert", Value: "/etc/kubernetes/pki/apiserver.crt"}, |
| 38 | {Flag: "cluster-key", Value: "/etc/kubernetes/pki/apiserver.key"}, |
| 39 | {Flag: "server-port", Value: "0"}, |
| 40 | {Flag: "kubeconfig", Value: "/etc/kubernetes/admin.conf"}, |
| 41 | {Flag: "keepalive-time", Value: "1h"}, |
| 42 | {Flag: "mode", Value: "grpc"}, |
| 43 | {Flag: "agent-namespace", Value: "kube-system"}, |
| 44 | {Flag: "agent-service-account", Value: "konnectivity-agent"}, |
| 45 | {Flag: "authentication-audience", Value: "system:konnectivity-server"}, |
| 46 | {Flag: "admin-bind-address", EmptyValue: true}, |
| 47 | {Flag: "server-count", Value: strconv.Itoa(serverReplicas)}, |
| 48 | {Flag: "mode", Value: *connectionMode}, |
| 49 | }, |
| 50 | } |
| 51 | serverDeployment, _, err = renderTemplate("server/deployment.yaml", serverDeploymentConfig) |
| 52 | if err != nil { |
| 53 | return nil, nil, fmt.Errorf("could not render server deployment: %w", err) |
| 54 | } |
| 55 | |
| 56 | agentDeploymentConfig := DeploymentConfig{ |
| 57 | Replicas: agentReplicas, |
| 58 | Image: *agentImage, |
| 59 | Args: []CLIFlag{ |
| 60 | {Flag: "logtostderr", Value: "true"}, |
| 61 | {Flag: "ca-cert", Value: "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"}, |
| 62 | {Flag: "proxy-server-host", Value: "konnectivity-server.kube-system.svc.cluster.local"}, |
| 63 | {Flag: "proxy-server-port", Value: "8091"}, |
| 64 | {Flag: "sync-interval", Value: "1s"}, |
| 65 | {Flag: "sync-interval-cap", Value: "10s"}, |
| 66 | {Flag: "sync-forever"}, |
| 67 | {Flag: "probe-interval", Value: "1s"}, |
| 68 | {Flag: "service-account-token-path", Value: "/var/run/secrets/tokens/konnectivity-agent-token"}, |
| 69 | {Flag: "agent-identifiers", Value: "ipv4=${HOST_IP}"}, |
| 70 | {Flag: "admin-bind-address", EmptyValue: true}, |
| 71 | }, |
| 72 | } |
| 73 | agentDeployment, _, err = renderTemplate("agent/deployment.yaml", agentDeploymentConfig) |
| 74 | if err != nil { |
| 75 | return nil, nil, fmt.Errorf("could not render agent deployment: %w", err) |
| 76 | } |
| 77 | |
| 78 | return serverDeployment, agentDeployment, nil |
| 79 | } |
| 80 | |
| 81 | func TestSingleServer_SingleAgent_StaticCount(t *testing.T) { |
| 82 | serverDeployment, agentDeployment, err := renderStaticCountDeployments(1, 1) |
| 83 | if err != nil { |
| 84 | t.Fatalf("could not render static count deployments: %v", err) |
| 85 | } |
| 86 | |
| 87 | feature := features.New("konnectivity server and agent deployment with single replica for each") |
| 88 | feature = feature.Setup(createDeployment(agentDeployment)) |
| 89 | feature = feature.Setup(createDeployment(serverDeployment)) |
| 90 | feature = feature.Setup(waitForDeployment(agentDeployment)) |
| 91 | feature = feature.Setup(waitForDeployment(serverDeployment)) |
| 92 | feature = feature.Assess("konnectivity server has a connected client", assertServersAreConnected(1)) |
| 93 | feature = feature.Assess("konnectivity agent is connected to a server", assertAgentsAreConnected(1)) |
| 94 | feature = feature.Assess("agents correctly count 1 server", assertAgentKnownServerCount(1)) |
| 95 | feature = feature.Teardown(deleteDeployment(agentDeployment)) |
| 96 | feature = feature.Teardown(deleteDeployment(serverDeployment)) |
| 97 | |
| 98 | testenv.Test(t, feature.Feature()) |
| 99 | } |
| 100 | |
| 101 | func TestMultiServer_MultiAgent_StaticCount(t *testing.T) { |
| 102 | replicas := 3 |
| 103 | serverDeployment, agentDeployment, err := renderStaticCountDeployments(replicas, replicas) |
| 104 | if err != nil { |
| 105 | t.Fatalf("could not render static count deployments: %v", err) |
| 106 | } |
| 107 | |
| 108 | feature := features.New("konnectivity server and agent deployment with multiple replicas") |
| 109 | feature = feature.Setup(createDeployment(agentDeployment)) |
| 110 | feature = feature.Setup(createDeployment(serverDeployment)) |
| 111 | feature = feature.Setup(waitForDeployment(agentDeployment)) |
| 112 | feature = feature.Setup(waitForDeployment(serverDeployment)) |
| 113 | feature = feature.Assess("all servers connected to all clients", assertServersAreConnected(replicas)) |
| 114 | feature = feature.Assess("all agents connected to all servers", assertAgentsAreConnected(replicas)) |
| 115 | feature = feature.Assess("agents correctly count all servers", assertAgentKnownServerCount(replicas)) |
| 116 | feature = feature.Teardown(deleteDeployment(agentDeployment)) |
| 117 | feature = feature.Teardown(deleteDeployment(serverDeployment)) |
| 118 | |
| 119 | testenv.Test(t, feature.Feature()) |
| 120 | } |