|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/google/uuid" |
| 10 | + coordinationv1 "k8s.io/api/coordination/v1" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 13 | + "sigs.k8s.io/e2e-framework/pkg/features" |
| 14 | +) |
| 15 | + |
| 16 | +type leaseController struct { |
| 17 | + labels map[string]string |
| 18 | + validLeases []*coordinationv1.Lease |
| 19 | + expiredLeases []*coordinationv1.Lease |
| 20 | +} |
| 21 | + |
| 22 | +func NewLeaseController(labels map[string]string) *leaseController { |
| 23 | + return &leaseController{ |
| 24 | + labels: labels, |
| 25 | + validLeases: []*coordinationv1.Lease{}, |
| 26 | + expiredLeases: []*coordinationv1.Lease{}, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func (lc *leaseController) PublishValidLease() func(context.Context, *testing.T, *envconf.Config) context.Context { |
| 31 | + return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 32 | + var duration int32 = 999999999 |
| 33 | + acquireTime := metav1.NewMicroTime(time.Now()) |
| 34 | + |
| 35 | + newLease := &coordinationv1.Lease{ |
| 36 | + TypeMeta: metav1.TypeMeta{}, |
| 37 | + ObjectMeta: metav1.ObjectMeta{ |
| 38 | + Name: uuid.New().String(), |
| 39 | + Labels: lc.labels, |
| 40 | + }, |
| 41 | + Spec: coordinationv1.LeaseSpec{ |
| 42 | + LeaseDurationSeconds: &duration, |
| 43 | + AcquireTime: &acquireTime, |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + err := cfg.Client().Resources().Create(ctx, newLease) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("could not publish valid lease: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + lc.validLeases = append(lc.validLeases, newLease) |
| 53 | + return ctx |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func (lc *leaseController) DeleteValidLease() func(context.Context, *testing.T, *envconf.Config) context.Context { |
| 58 | + return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 59 | + leaseToDelete := lc.validLeases[len(lc.validLeases)-1] |
| 60 | + |
| 61 | + err := cfg.Client().Resources().Delete(ctx, leaseToDelete) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("could not delete valid lease: %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + lc.validLeases = lc.validLeases[:len(lc.validLeases)-1] |
| 67 | + return ctx |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func (lc *leaseController) PublishExpiredLease() func(context.Context, *testing.T, *envconf.Config) context.Context { |
| 72 | + return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 73 | + var duration int32 = 1 |
| 74 | + acquireTime := metav1.NewMicroTime(time.Now().Add(-time.Second * 99999999)) |
| 75 | + |
| 76 | + newLease := &coordinationv1.Lease{ |
| 77 | + TypeMeta: metav1.TypeMeta{}, |
| 78 | + ObjectMeta: metav1.ObjectMeta{ |
| 79 | + Name: uuid.New().String(), |
| 80 | + Labels: lc.labels, |
| 81 | + }, |
| 82 | + Spec: coordinationv1.LeaseSpec{ |
| 83 | + LeaseDurationSeconds: &duration, |
| 84 | + AcquireTime: &acquireTime, |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + err := cfg.Client().Resources().Create(ctx, newLease) |
| 89 | + if err != nil { |
| 90 | + t.Fatalf("could not publish expired lease: %v", err) |
| 91 | + } |
| 92 | + |
| 93 | + lc.expiredLeases = append(lc.expiredLeases, newLease) |
| 94 | + return ctx |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func (lc *leaseController) DeleteExpiredLease() func(context.Context, *testing.T, *envconf.Config) context.Context { |
| 99 | + return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 100 | + leaseToDelete := lc.expiredLeases[len(lc.expiredLeases)-1] |
| 101 | + |
| 102 | + err := cfg.Client().Resources().Delete(ctx, leaseToDelete) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("could not delete expired lease: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + lc.expiredLeases = lc.expiredLeases[:len(lc.expiredLeases)-1] |
| 108 | + return ctx |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestLeaseCount(t *testing.T) { |
| 113 | + serverServiceHost := "konnectivity-server.kube-system.svc.cluster.local" |
| 114 | + agentServiceHost := "konnectivity-agent.kube-system.svc.cluster.local" |
| 115 | + adminPort := 8093 |
| 116 | + initialServerReplicas := 6 |
| 117 | + initialAgentReplicas := 2 |
| 118 | + leaseLabels := map[string]string{"aTestLabel": "aTestValue"} |
| 119 | + leaseLabelSelector := "aTestLabel=aTestValue" |
| 120 | + |
| 121 | + serverStatefulSetCfg := StatefulSetConfig{ |
| 122 | + Replicas: initialServerReplicas, |
| 123 | + Image: *serverImage, |
| 124 | + Args: []KeyValue{ |
| 125 | + {"log-file", "/var/log/konnectivity-server.log"}, |
| 126 | + {"logtostderr", "true"}, |
| 127 | + {"log-file-max-size", "0"}, |
| 128 | + {"uds-name", "/etc/kubernetes/konnectivity-server/konnectivity-server.socket"}, |
| 129 | + {Key: "delete-existing-uds-file"}, |
| 130 | + {"cluster-cert", "/etc/kubernetes/pki/apiserver.crt"}, |
| 131 | + {"cluster-key", "/etc/kubernetes/pki/apiserver.key"}, |
| 132 | + {"server-port", "8090"}, |
| 133 | + {"agent-port", "8091"}, |
| 134 | + {"health-port", "8092"}, |
| 135 | + {"admin-port", strconv.Itoa(adminPort)}, |
| 136 | + {"keepalive-time", "1h"}, |
| 137 | + {"mode", *connectionMode}, |
| 138 | + {"agent-namespace", "kube-system"}, |
| 139 | + {"agent-service-account", "konnectivity-agent"}, |
| 140 | + {"kubeconfig", "/etc/kubernetes/admin.conf"}, |
| 141 | + {"authentication-audience", "system:konnectivity-server"}, |
| 142 | + {"server-count", strconv.Itoa(initialServerReplicas)}, |
| 143 | + }, |
| 144 | + } |
| 145 | + serverStatefulSet, _, err := renderTemplate("server/statefulset.yaml", serverStatefulSetCfg) |
| 146 | + if err != nil { |
| 147 | + t.Fatalf("could not render server deployment: %v", err) |
| 148 | + } |
| 149 | + |
| 150 | + agentStatefulSetConfig := StatefulSetConfig{ |
| 151 | + Replicas: initialAgentReplicas, |
| 152 | + Image: *agentImage, |
| 153 | + Args: []KeyValue{ |
| 154 | + {"logtostderr", "true"}, |
| 155 | + {"ca-cert", "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"}, |
| 156 | + {"proxy-server-host", serverServiceHost}, |
| 157 | + {"proxy-server-port", "8091"}, |
| 158 | + {"sync-interval", "1s"}, |
| 159 | + {"sync-interval-cap", "10s"}, |
| 160 | + {Key: "sync-forever"}, |
| 161 | + {"probe-interval", "1s"}, |
| 162 | + {"service-account-token-path", "/var/run/secrets/tokens/konnectivity-agent-token"}, |
| 163 | + {"server-count-lease-selector", leaseLabelSelector}, |
| 164 | + }, |
| 165 | + } |
| 166 | + agentStatefulSet, _, err := renderTemplate("agent/statefulset.yaml", agentStatefulSetConfig) |
| 167 | + if err != nil { |
| 168 | + t.Fatalf("could not render agent deployment: %v", err) |
| 169 | + } |
| 170 | + |
| 171 | + lc := NewLeaseController(leaseLabels) |
| 172 | + |
| 173 | + feature := features.New("konnectivity agent lease counting system") |
| 174 | + feature.Setup(deployAndWaitForStatefulSet(serverStatefulSet)) |
| 175 | + feature.Setup(deployAndWaitForStatefulSet(agentStatefulSet)) |
| 176 | + // We start off by publishing two valid leases and one expired lease. |
| 177 | + feature.Setup(lc.PublishValidLease()) |
| 178 | + feature.Setup(lc.PublishValidLease()) |
| 179 | + feature.Setup(lc.PublishExpiredLease()) |
| 180 | + feature.Assess("agents correctly count 2 leases (2 valid, 1 expired)", assertAgentKnownServerCount(2, agentServiceHost, adminPort)) |
| 181 | + // Publishing additional expired leases should not change the server count. |
| 182 | + feature.Setup(lc.PublishExpiredLease()) |
| 183 | + feature.Setup(lc.PublishExpiredLease()) |
| 184 | + feature.Assess("agents correctly count 2 leases (2 valid, 3 expired)", assertAgentKnownServerCount(2, agentServiceHost, adminPort)) |
| 185 | + // Publishing additional valid leases should increase the server count. |
| 186 | + feature.Setup(lc.PublishValidLease()) |
| 187 | + feature.Setup(lc.PublishValidLease()) |
| 188 | + feature.Assess("agents correctly count 4 leases (4 valid, 3 expired)", assertAgentKnownServerCount(4, agentServiceHost, adminPort)) |
| 189 | + // Deleting a valid lease should reduce the server count. |
| 190 | + feature.Setup(lc.DeleteValidLease()) |
| 191 | + feature.Assess("agents correctly count 3 leases (3 valid, 3 expired)", assertAgentKnownServerCount(3, agentServiceHost, adminPort)) |
| 192 | +} |
0 commit comments