| 1 | /* |
| 2 | Copyright 2022 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 | |
| 17 | package tests |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "io" |
| 22 | "log" |
| 23 | "math/rand" |
| 24 | "net" |
| 25 | "net/http" |
| 26 | "net/http/httptest" |
| 27 | "sync" |
| 28 | "testing" |
| 29 | "time" |
| 30 | |
| 31 | "sigs.k8s.io/apiserver-network-proxy/tests/framework" |
| 32 | ) |
| 33 | |
| 34 | type tcpLB struct { |
| 35 | t *testing.T |
| 36 | mu sync.RWMutex |
| 37 | backends []string |
| 38 | } |
| 39 | |
| 40 | func ioCopy(wc io.WriteCloser, r io.Reader) { |
| 41 | defer wc.Close() |
| 42 | io.Copy(wc, r) |
| 43 | } |
| 44 | |
| 45 | func (lb *tcpLB) handleConnection(in net.Conn, backend string) { |
| 46 | out, err := net.Dial("tcp", backend) |
| 47 | if err != nil { |
| 48 | lb.t.Log(err) |
| 49 | return |
| 50 | } |
| 51 | go ioCopy(out, in) |
| 52 | go ioCopy(in, out) |
| 53 | } |
| 54 | |
| 55 | func (lb *tcpLB) serve(stopCh chan struct{}) string { |
| 56 | ln, err := net.Listen("tcp", "127.0.0.1:") |
| 57 | if err != nil { |
| 58 | log.Fatalf("failed to bind: %s", err) |
| 59 | } |
| 60 | |
| 61 | go func() { |
| 62 | <-stopCh |
| 63 | ln.Close() |
| 64 | }() |
| 65 | |
| 66 | go func() { |
| 67 | for { |
| 68 | select { |
| 69 | case <-stopCh: |
| 70 | return |
| 71 | default: |
| 72 | } |
| 73 | conn, err := ln.Accept() |
| 74 | if err != nil { |
| 75 | log.Printf("failed to accept: %s", err) |
| 76 | continue |
| 77 | } |
| 78 | // go lb.handleConnection(conn, lb.randomBackend()) |
| 79 | back := lb.randomBackend() |
| 80 | go lb.handleConnection(conn, back) |
| 81 | } |
| 82 | }() |
| 83 | |
| 84 | return ln.Addr().String() |
| 85 | } |
| 86 | |
| 87 | func (lb *tcpLB) addBackend(backend string) { |
| 88 | lb.mu.Lock() |
| 89 | defer lb.mu.Unlock() |
| 90 | lb.backends = append(lb.backends, backend) |
| 91 | |
| 92 | } |
| 93 | |
| 94 | func (lb *tcpLB) removeBackend(backend string) { |
| 95 | lb.mu.Lock() |
| 96 | defer lb.mu.Unlock() |
| 97 | for i := range lb.backends { |
| 98 | if lb.backends[i] == backend { |
| 99 | lb.backends = append(lb.backends[:i], lb.backends[i+1:]...) |
| 100 | return |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func (lb *tcpLB) randomBackend() string { |
| 106 | lb.mu.RLock() |
| 107 | defer lb.mu.RUnlock() |
| 108 | i := rand.Intn(len(lb.backends)) /* #nosec G404 */ |
| 109 | return lb.backends[i] |
| 110 | } |
| 111 | |
| 112 | const haServerCount = 3 |
| 113 | |
| 114 | func setupHAProxyServer(t *testing.T) []framework.ProxyServer { |
| 115 | ps := make([]framework.ProxyServer, haServerCount) |
| 116 | for i := 0; i < haServerCount; i++ { |
| 117 | ps[i] = runGRPCProxyServerWithServerCount(t, haServerCount) |
| 118 | } |
| 119 | return ps |
| 120 | } |
| 121 | |
| 122 | func TestBasicHAProxyServer_GRPC(t *testing.T) { |
| 123 | server := httptest.NewServer(newEchoServer("hello")) |
| 124 | defer server.Close() |
| 125 | |
| 126 | stopCh := make(chan struct{}) |
| 127 | defer close(stopCh) |
| 128 | |
| 129 | proxy := setupHAProxyServer(t) |
| 130 | |
| 131 | lb := tcpLB{ |
| 132 | backends: []string{ |
| 133 | proxy[0].AgentAddr(), |
| 134 | proxy[1].AgentAddr(), |
| 135 | proxy[2].AgentAddr(), |
| 136 | }, |
| 137 | t: t, |
| 138 | } |
| 139 | lbAddr := lb.serve(stopCh) |
| 140 | |
| 141 | a := runAgent(t, lbAddr) |
| 142 | defer a.Stop() |
| 143 | waitForConnectedServerCount(t, 3, a) |
| 144 | |
| 145 | // run test client |
| 146 | testProxyServer(t, proxy[0].FrontAddr(), server.URL) |
| 147 | testProxyServer(t, proxy[1].FrontAddr(), server.URL) |
| 148 | testProxyServer(t, proxy[2].FrontAddr(), server.URL) |
| 149 | |
| 150 | t.Logf("basic HA proxy server test passed") |
| 151 | |
| 152 | // interrupt the HA server |
| 153 | lb.removeBackend(proxy[0].AgentAddr()) |
| 154 | proxy[0].Stop() |
| 155 | |
| 156 | // give the agent some time to detect the disconnection |
| 157 | waitForConnectedServerCount(t, 2, a) |
| 158 | |
| 159 | proxy4 := runGRPCProxyServerWithServerCount(t, haServerCount) |
| 160 | lb.addBackend(proxy4.AgentAddr()) |
| 161 | defer func() { |
| 162 | proxy[1].Stop() |
| 163 | proxy[2].Stop() |
| 164 | proxy4.Stop() |
| 165 | }() |
| 166 | |
| 167 | // wait for the new server to be connected. |
| 168 | waitForConnectedServerCount(t, 3, a) |
| 169 | |
| 170 | // run test client |
| 171 | testProxyServer(t, proxy[1].FrontAddr(), server.URL) |
| 172 | testProxyServer(t, proxy[2].FrontAddr(), server.URL) |
| 173 | testProxyServer(t, proxy4.FrontAddr(), server.URL) |
| 174 | } |
| 175 | |
| 176 | func testProxyServer(t *testing.T, front string, target string) { |
| 177 | ctx, cancel := context.WithCancel(context.Background()) |
| 178 | defer cancel() // Close the tunnel |
| 179 | tunnel, err := createSingleUseGrpcTunnel(ctx, front) |
| 180 | if err != nil { |
| 181 | t.Fatal(err) |
| 182 | } |
| 183 | |
| 184 | c := &http.Client{ |
| 185 | Transport: &http.Transport{ |
| 186 | DialContext: tunnel.DialContext, |
| 187 | }, |
| 188 | Timeout: 1 * time.Second, |
| 189 | } |
| 190 | |
| 191 | r, err := c.Get(target) |
| 192 | if err != nil { |
| 193 | t.Fatal(err) |
| 194 | } |
| 195 | |
| 196 | data, err := io.ReadAll(r.Body) |
| 197 | r.Body.Close() |
| 198 | if err != nil { |
| 199 | t.Error(err) |
| 200 | } |
| 201 | |
| 202 | if string(data) != "hello" { |
| 203 | t.Errorf("expect %v; got %v", "hello", string(data)) |
| 204 | } |
| 205 | } |