Debian Codegraph @

tests/framework/proxy_server.go

raw
1 /*
2 Copyright 2023 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 framework
18
19 import (
20 "context"
21 "fmt"
22 "log"
23 "net"
24 "path/filepath"
25 "strconv"
26 "sync"
27 "testing"
28 "time"
29
30 "github.com/google/uuid"
31 "k8s.io/apimachinery/pkg/util/wait"
32 serverapp "sigs.k8s.io/apiserver-network-proxy/cmd/server/app"
33 serveropts "sigs.k8s.io/apiserver-network-proxy/cmd/server/app/options"
34 "sigs.k8s.io/apiserver-network-proxy/pkg/server"
35 metricstest "sigs.k8s.io/apiserver-network-proxy/pkg/testing/metrics"
36 )
37
38 type ProxyServerOpts struct {
39 ServerCount int
40 Mode string
41 AgentPort int // Defaults to random port.
42 }
43
44 type ProxyServerRunner interface {
45 Start(testing.TB, ProxyServerOpts) (ProxyServer, error)
46 }
47
48 type ProxyServer interface {
49 ConnectedBackends() (int, error)
50 AgentAddr() string
51 FrontAddr() string
52 Ready() bool
53 Stop()
54 Metrics() metricstest.ServerTester
55 }
56
57 type InProcessProxyServerRunner struct{}
58
59 func (*InProcessProxyServerRunner) Start(t testing.TB, opts ProxyServerOpts) (ProxyServer, error) {
60 s := &serverapp.Proxy{}
61 o, err := serverOptions(t, opts)
62 if err != nil {
63 return nil, fmt.Errorf("error building server options: %w", err)
64 }
65
66 ctx, cancel := context.WithCancel(context.Background())
67 stopCh := make(chan struct{})
68 go func() {
69 if err := s.Run(o, stopCh); err != nil {
70 log.Printf("ERROR running proxy server: %v", err)
71 cancel()
72 }
73 }()
74
75 healthAddr := net.JoinHostPort(o.HealthBindAddress, strconv.Itoa(o.HealthPort))
76 if err := wait.PollImmediateWithContext(ctx, 100*time.Millisecond, ForeverTestTimeout, func(context.Context) (bool, error) {
77 return checkLiveness(healthAddr), nil
78 }); err != nil {
79 close(stopCh)
80 return nil, fmt.Errorf("server never came up: %v", err)
81 }
82
83 ps := &inProcessProxyServer{
84 proxyServer: s.ProxyServer(),
85 stopCh: stopCh,
86 mode: opts.Mode,
87 healthAddr: healthAddr,
88 agentAddr: net.JoinHostPort(o.AgentBindAddress, strconv.Itoa(o.AgentPort)),
89 frontAddr: o.UdsName,
90 }
91 t.Cleanup(ps.Stop)
92 return ps, nil
93 }
94
95 type inProcessProxyServer struct {
96 proxyServer *server.ProxyServer
97
98 stopOnce sync.Once
99 stopCh chan struct{}
100
101 mode string
102
103 healthAddr string
104 agentAddr string
105 frontAddr string
106 }
107
108 func (ps *inProcessProxyServer) Stop() {
109 ps.stopOnce.Do(func() {
110 close(ps.stopCh)
111 })
112 }
113
114 func (ps *inProcessProxyServer) AgentAddr() string {
115 return ps.agentAddr
116 }
117
118 func (ps *inProcessProxyServer) FrontAddr() string {
119 return ps.frontAddr
120 }
121
122 func (ps *inProcessProxyServer) ConnectedBackends() (int, error) {
123 numBackends := 0
124 for _, bm := range ps.proxyServer.BackendManagers {
125 numBackends += bm.NumBackends()
126 }
127 return numBackends, nil
128 }
129
130 func (ps *inProcessProxyServer) Ready() bool {
131 return checkReadiness(ps.healthAddr)
132 }
133
134 func (ps *inProcessProxyServer) Metrics() metricstest.ServerTester {
135 return metricstest.DefaultTester
136 }
137
138 func serverOptions(t testing.TB, opts ProxyServerOpts) (*serveropts.ProxyRunOptions, error) {
139 t.Helper()
140 o := serveropts.NewProxyRunOptions()
141
142 o.ServerCount = opts.ServerCount
143 o.Mode = opts.Mode
144
145 uid := uuid.New().String()
146 o.UdsName = filepath.Join(CertsDir, fmt.Sprintf("server-%s.sock", uid))
147 o.ServerPort = 0 // Required for UDS
148
149 o.ClusterCert = filepath.Join(CertsDir, TestServerCertFile)
150 o.ClusterKey = filepath.Join(CertsDir, TestServerKeyFile)
151 o.ClusterCaCert = filepath.Join(CertsDir, TestCAFile)
152
153 const localhost = "127.0.0.1"
154 o.AgentBindAddress = localhost
155 o.HealthBindAddress = localhost
156 o.AdminBindAddress = localhost
157
158 ports, err := FreePorts(3)
159 if err != nil {
160 return nil, err
161 }
162 if opts.AgentPort != 0 {
163 o.AgentPort = opts.AgentPort
164 } else {
165 o.AgentPort = ports[0]
166 }
167 o.HealthPort = ports[1]
168 o.AdminPort = ports[2]
169
170 return o, nil
171 }