Debian Codegraph @

e2e/lease_count_test.go

raw
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 "testing"
21
22 "sigs.k8s.io/controller-runtime/pkg/client"
23 "sigs.k8s.io/e2e-framework/pkg/features"
24 )
25
26 func renderLeaseCountDeployments(serverReplicas, agentReplicas int) (serverDeployment client.Object, agentDeployment client.Object, err error) {
27 serverDeploymentConfig := DeploymentConfig{
28 Replicas: serverReplicas,
29 Image: *serverImage,
30 Args: []CLIFlag{
31 {Flag: "log-file", Value: "/var/log/konnectivity-server.log"},
32 {Flag: "logtostderr", Value: "true"},
33 {Flag: "log-file-max-size", Value: "0"},
34 {Flag: "uds-name", Value: "/etc/kubernetes/konnectivity-server/konnectivity-server.socket"},
35 {Flag: "delete-existing-uds-file"},
36 {Flag: "cluster-cert", Value: "/etc/kubernetes/pki/apiserver.crt"},
37 {Flag: "cluster-key", Value: "/etc/kubernetes/pki/apiserver.key"},
38 {Flag: "server-port", Value: "0"},
39 {Flag: "kubeconfig", Value: "/etc/kubernetes/admin.conf"},
40 {Flag: "keepalive-time", Value: "1h"},
41 {Flag: "mode", Value: "grpc"},
42 {Flag: "agent-namespace", Value: "kube-system"},
43 {Flag: "agent-service-account", Value: "konnectivity-agent"},
44 {Flag: "authentication-audience", Value: "system:konnectivity-server"},
45 {Flag: "enable-lease-controller"},
46 {Flag: "admin-bind-address", EmptyValue: true},
47 {Flag: "mode", Value: *connectionMode},
48 },
49 }
50 serverDeployment, _, err = renderTemplate("server/deployment.yaml", serverDeploymentConfig)
51 if err != nil {
52 return nil, nil, fmt.Errorf("could not render server deployment: %w", err)
53 }
54
55 agentDeploymentConfig := DeploymentConfig{
56 Replicas: agentReplicas,
57 Image: *agentImage,
58 Args: []CLIFlag{
59 {Flag: "logtostderr", Value: "true"},
60 {Flag: "ca-cert", Value: "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"},
61 {Flag: "proxy-server-host", Value: "konnectivity-server.kube-system.svc.cluster.local"},
62 {Flag: "proxy-server-port", Value: "8091"},
63 {Flag: "sync-interval", Value: "1s"},
64 {Flag: "sync-interval-cap", Value: "10s"},
65 {Flag: "sync-forever"},
66 {Flag: "probe-interval", Value: "1s"},
67 {Flag: "service-account-token-path", Value: "/var/run/secrets/tokens/konnectivity-agent-token"},
68 {Flag: "count-server-leases"},
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_LeaseCount(t *testing.T) {
82 serverDeployment, agentDeployment, err := renderLeaseCountDeployments(1, 1)
83 if err != nil {
84 t.Fatalf("could not render lease 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(serverDeployment))
91 feature = feature.Setup(waitForDeployment(agentDeployment))
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("agent correctly counts 1 lease", 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_LeaseCount(t *testing.T) {
102 serverDeployment, agentDeployment, err := renderLeaseCountDeployments(2, 2)
103 if err != nil {
104 t.Fatalf("could not render lease count deployments: %v", err)
105 }
106
107 feature := features.New("konnectivity server and agent deployment with multiple replicas")
108 feature = feature.Setup(createDeployment(serverDeployment))
109 feature = feature.Setup(createDeployment(agentDeployment))
110 feature = feature.Setup(waitForDeployment(serverDeployment))
111 feature = feature.Setup(waitForDeployment(agentDeployment))
112 feature = feature.Setup(scaleDeployment(serverDeployment, 4))
113 feature = feature.Setup(scaleDeployment(agentDeployment, 4))
114 feature = feature.Setup(waitForDeployment(agentDeployment))
115 feature = feature.Setup(waitForDeployment(serverDeployment))
116 feature = feature.Assess("all servers connected to all clients after scale up", assertServersAreConnected(4))
117 feature = feature.Assess("all agents connected to all servers after scale up", assertAgentsAreConnected(4))
118 feature = feature.Assess("agents correctly count 4 leases after scale up", assertAgentKnownServerCount(4))
119 feature = feature.Teardown(deleteDeployment(agentDeployment))
120 feature = feature.Teardown(deleteDeployment(serverDeployment))
121
122 testenv.Test(t, feature.Feature())
123 }