@@ -18,6 +18,7 @@ package apiserver
18
18
19
19
import (
20
20
"fmt"
21
+ "net"
21
22
"net/http"
22
23
"net/http/httptest"
23
24
"net/url"
@@ -133,6 +134,7 @@ func setupAPIServices(apiServices []*apiregistration.APIService) (*AvailableCond
133
134
// the maximum disruption time to a minimum, but it does prevent hot loops.
134
135
workqueue .NewItemExponentialFailureRateLimiter (5 * time .Millisecond , 30 * time .Second ),
135
136
"AvailableConditionController" ),
137
+ tlsCache : & tlsTransportCache {transports : make (map [tlsCacheKey ]http.RoundTripper )},
136
138
}
137
139
for _ , svc := range apiServices {
138
140
c .addAPIService (svc )
@@ -202,6 +204,55 @@ func TestBuildCache(t *testing.T) {
202
204
})
203
205
}
204
206
}
207
+
208
+ func TestTLSCache (t * testing.T ) {
209
+ apiServices := []* apiregistration.APIService {newRemoteAPIService ("remote.group" )}
210
+ services := []* v1.Service {newService ("foo" , "bar" , testServicePort , testServicePortName )}
211
+ c , _ := setupAPIServices (apiServices )
212
+ // TLS configs with customized dialers are uncacheable by the client-go
213
+ // TLS transport cache. The local cache will be used.
214
+ c .dialContext = (& net.Dialer {
215
+ Timeout : 30 * time .Second ,
216
+ KeepAlive : 30 * time .Second ,
217
+ }).DialContext
218
+ for _ , svc := range services {
219
+ c .addService (svc )
220
+ }
221
+ tests := []struct {
222
+ name string
223
+ proxyCurrentCertKeyContent certKeyFunc
224
+ expectedCacheSize int
225
+ }{
226
+ {
227
+ name : "nil certKeyFunc" ,
228
+ expectedCacheSize : 1 ,
229
+ },
230
+ {
231
+ name : "empty certKeyFunc" ,
232
+ proxyCurrentCertKeyContent : func () ([]byte , []byte ) { return emptyCert (), emptyCert () },
233
+ // the tlsCacheKey is the same, reuse existing transport
234
+ expectedCacheSize : 1 ,
235
+ },
236
+ {
237
+ name : "different certKeyFunc" ,
238
+ proxyCurrentCertKeyContent : testCertKeyFunc ,
239
+ // the tlsCacheKey is different, create a new transport
240
+ expectedCacheSize : 2 ,
241
+ },
242
+ }
243
+ for _ , tc := range tests {
244
+ t .Run (tc .name , func (t * testing.T ) {
245
+ c .proxyCurrentCertKeyContent = tc .proxyCurrentCertKeyContent
246
+ for _ , apiService := range apiServices {
247
+ c .sync (apiService .Name )
248
+ }
249
+ if len (c .tlsCache .transports ) != tc .expectedCacheSize {
250
+ t .Fatalf ("%v cache size expected %v, got %v" , tc .name , tc .expectedCacheSize , len (c .tlsCache .transports ))
251
+ }
252
+ })
253
+ }
254
+ }
255
+
205
256
func TestSync (t * testing.T ) {
206
257
tests := []struct {
207
258
name string
@@ -356,6 +407,7 @@ func TestSync(t *testing.T) {
356
407
endpointsLister : v1listers .NewEndpointsLister (endpointsIndexer ),
357
408
serviceResolver : & fakeServiceResolver {url : testServer .URL },
358
409
proxyCurrentCertKeyContent : func () ([]byte , []byte ) { return emptyCert (), emptyCert () },
410
+ tlsCache : & tlsTransportCache {transports : make (map [tlsCacheKey ]http.RoundTripper )},
359
411
}
360
412
c .sync (tc .apiServiceName )
361
413
@@ -420,3 +472,33 @@ func TestUpdateAPIServiceStatus(t *testing.T) {
420
472
func emptyCert () []byte {
421
473
return []byte {}
422
474
}
475
+
476
+ func testCertKeyFunc () ([]byte , []byte ) {
477
+ return []byte (`-----BEGIN CERTIFICATE-----
478
+ MIICBDCCAW2gAwIBAgIJAPgVBh+4xbGoMA0GCSqGSIb3DQEBCwUAMBsxGTAXBgNV
479
+ BAMMEHdlYmhvb2tfdGVzdHNfY2EwIBcNMTcwNzI4MjMxNTI4WhgPMjI5MTA1MTMy
480
+ MzE1MjhaMB8xHTAbBgNVBAMMFHdlYmhvb2tfdGVzdHNfY2xpZW50MIGfMA0GCSqG
481
+ SIb3DQEBAQUAA4GNADCBiQKBgQDkGXXSm6Yun5o3Jlmx45rItcQ2pmnoDk4eZfl0
482
+ rmPa674s2pfYo3KywkXQ1Fp3BC8GUgzPLSfJ8xXya9Lg1Wo8sHrDln0iRg5HXxGu
483
+ uFNhRBvj2S0sIff0ZG/IatB9I6WXVOUYuQj6+A0CdULN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BX0hLhXUmjamQGGH5LXL4Owcb4/mM6twJAEVio
500
+ SF/qva9jv+GrKVrKFXT374lOJFY53Qn/rvifEtWUhLCslCA5kzLlctRBafMZPrfH
501
+ Mh5RrJP1BhVysDbenQJASGcc+DiF7rB6K++ZGyC11E2AP29DcZ0pgPESSV7npOGg
502
+ +NqPRZNVCSZOiVmNuejZqmwKhZNGZnBFx1Y+ChAAgw==
503
+ -----END RSA PRIVATE KEY-----` )
504
+ }
0 commit comments