Skip to content

Commit ddc6978

Browse files
committed
Use http/1.1 in apiserver->webhook clients
1 parent aef05c8 commit ddc6978

File tree

3 files changed

+320
-0
lines changed

3 files changed

+320
-0
lines changed

staging/src/k8s.io/apiserver/pkg/util/webhook/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ func (cm *ClientManager) HookClient(cc ClientConfig) (*rest.RESTClient, error) {
136136
}
137137
cfg.TLSClientConfig.CAData = append(cfg.TLSClientConfig.CAData, cc.CABundle...)
138138

139+
// Use http/1.1 instead of http/2.
140+
// This is a workaround for http/2-enabled clients not load-balancing concurrent requests to multiple backends.
141+
// See http://issue.k8s.io/75791 for details.
142+
cfg.NextProtos = []string{"http/1.1"}
143+
139144
cfg.ContentConfig.NegotiatedSerializer = cm.negotiatedSerializer
140145
cfg.ContentConfig.ContentType = runtime.ContentTypeJSON
141146
client, err := rest.UnversionedRESTClientFor(cfg)

test/integration/apiserver/admissionwebhook/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go_test(
55
srcs = [
66
"admission_test.go",
77
"broken_webhook_test.go",
8+
"load_balance_test.go",
89
"main_test.go",
910
"reinvocation_test.go",
1011
"timeout_test.go",
Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
/*
2+
Copyright 2019 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 admissionwebhook
18+
19+
import (
20+
"crypto/tls"
21+
"crypto/x509"
22+
"encoding/json"
23+
"fmt"
24+
"io/ioutil"
25+
"net"
26+
"net/http"
27+
"sync"
28+
"sync/atomic"
29+
"testing"
30+
"time"
31+
32+
"k8s.io/api/admission/v1beta1"
33+
admissionv1beta1 "k8s.io/api/admissionregistration/v1beta1"
34+
corev1 "k8s.io/api/core/v1"
35+
v1 "k8s.io/api/core/v1"
36+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37+
"k8s.io/apimachinery/pkg/types"
38+
"k8s.io/apimachinery/pkg/util/wait"
39+
clientset "k8s.io/client-go/kubernetes"
40+
"k8s.io/client-go/rest"
41+
kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
42+
"k8s.io/kubernetes/test/integration/framework"
43+
)
44+
45+
const (
46+
testLoadBalanceClientUsername = "webhook-balance-integration-client"
47+
)
48+
49+
// TestWebhookLoadBalance ensures that the admission webhook opens multiple connections to backends to satisfy concurrent requests
50+
func TestWebhookLoadBalance(t *testing.T) {
51+
52+
roots := x509.NewCertPool()
53+
if !roots.AppendCertsFromPEM(localhostCert) {
54+
t.Fatal("Failed to append Cert from PEM")
55+
}
56+
cert, err := tls.X509KeyPair(localhostCert, localhostKey)
57+
if err != nil {
58+
t.Fatalf("Failed to build cert with error: %+v", err)
59+
}
60+
61+
localListener, err := net.Listen("tcp", "127.0.0.1:0")
62+
if err != nil {
63+
if localListener, err = net.Listen("tcp6", "[::1]:0"); err != nil {
64+
t.Fatal(err)
65+
}
66+
}
67+
trackingListener := &connectionTrackingListener{delegate: localListener}
68+
69+
recorder := &connectionRecorder{}
70+
handler := newLoadBalanceWebhookHandler(recorder)
71+
httpServer := &http.Server{
72+
Handler: handler,
73+
TLSConfig: &tls.Config{
74+
RootCAs: roots,
75+
Certificates: []tls.Certificate{cert},
76+
},
77+
}
78+
go func() {
79+
httpServer.ServeTLS(trackingListener, "", "")
80+
}()
81+
defer httpServer.Close()
82+
83+
webhookURL := "https://" + localListener.Addr().String()
84+
85+
s := kubeapiservertesting.StartTestServerOrDie(t, kubeapiservertesting.NewDefaultTestServerOptions(), []string{
86+
"--disable-admission-plugins=ServiceAccount",
87+
}, framework.SharedEtcd())
88+
defer s.TearDownFn()
89+
90+
// Configure a client with a distinct user name so that it is easy to distinguish requests
91+
// made by the client from requests made by controllers. We use this to filter out requests
92+
// before recording them to ensure we don't accidentally mistake requests from controllers
93+
// as requests made by the client.
94+
clientConfig := rest.CopyConfig(s.ClientConfig)
95+
clientConfig.QPS = 100
96+
clientConfig.Burst = 200
97+
clientConfig.Impersonate.UserName = testLoadBalanceClientUsername
98+
clientConfig.Impersonate.Groups = []string{"system:masters", "system:authenticated"}
99+
client, err := clientset.NewForConfig(clientConfig)
100+
if err != nil {
101+
t.Fatalf("unexpected error: %v", err)
102+
}
103+
104+
_, err = client.CoreV1().Pods("default").Create(loadBalanceMarkerFixture)
105+
if err != nil {
106+
t.Fatal(err)
107+
}
108+
109+
upCh := recorder.Reset()
110+
ns := "load-balance"
111+
_, err = client.CoreV1().Namespaces().Create(&v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ns}})
112+
if err != nil {
113+
t.Fatal(err)
114+
}
115+
116+
fail := admissionv1beta1.Fail
117+
mutatingCfg, err := client.AdmissionregistrationV1beta1().MutatingWebhookConfigurations().Create(&admissionv1beta1.MutatingWebhookConfiguration{
118+
ObjectMeta: metav1.ObjectMeta{Name: "admission.integration.test"},
119+
Webhooks: []admissionv1beta1.MutatingWebhook{{
120+
Name: "admission.integration.test",
121+
ClientConfig: admissionv1beta1.WebhookClientConfig{
122+
URL: &webhookURL,
123+
CABundle: localhostCert,
124+
},
125+
Rules: []admissionv1beta1.RuleWithOperations{{
126+
Operations: []admissionv1beta1.OperationType{admissionv1beta1.OperationAll},
127+
Rule: admissionv1beta1.Rule{APIGroups: []string{""}, APIVersions: []string{"v1"}, Resources: []string{"pods"}},
128+
}},
129+
FailurePolicy: &fail,
130+
AdmissionReviewVersions: []string{"v1beta1"},
131+
}},
132+
})
133+
if err != nil {
134+
t.Fatal(err)
135+
}
136+
defer func() {
137+
err := client.AdmissionregistrationV1beta1().MutatingWebhookConfigurations().Delete(mutatingCfg.GetName(), &metav1.DeleteOptions{})
138+
if err != nil {
139+
t.Fatal(err)
140+
}
141+
}()
142+
143+
// wait until new webhook is called the first time
144+
if err := wait.PollImmediate(time.Millisecond*5, wait.ForeverTestTimeout, func() (bool, error) {
145+
_, err = client.CoreV1().Pods("default").Patch(loadBalanceMarkerFixture.Name, types.JSONPatchType, []byte("[]"))
146+
select {
147+
case <-upCh:
148+
return true, nil
149+
default:
150+
t.Logf("Waiting for webhook to become effective, getting marker object: %v", err)
151+
return false, nil
152+
}
153+
}); err != nil {
154+
t.Fatal(err)
155+
}
156+
157+
pod := &corev1.Pod{
158+
ObjectMeta: metav1.ObjectMeta{
159+
Namespace: ns,
160+
GenerateName: "loadbalance-",
161+
},
162+
Spec: corev1.PodSpec{
163+
Containers: []v1.Container{{
164+
Name: "fake-name",
165+
Image: "fakeimage",
166+
}},
167+
},
168+
}
169+
170+
// Submit 10 parallel requests
171+
wg := &sync.WaitGroup{}
172+
for i := 0; i < 10; i++ {
173+
wg.Add(1)
174+
go func() {
175+
defer wg.Done()
176+
_, err := client.CoreV1().Pods(ns).Create(pod)
177+
if err != nil {
178+
t.Error(err)
179+
}
180+
}()
181+
}
182+
wg.Wait()
183+
184+
if actual := atomic.LoadInt64(&trackingListener.connections); actual < 10 {
185+
t.Errorf("expected at least 10 connections, got %d", actual)
186+
}
187+
trackingListener.Reset()
188+
189+
// Submit 10 more parallel requests
190+
wg = &sync.WaitGroup{}
191+
for i := 0; i < 10; i++ {
192+
wg.Add(1)
193+
go func() {
194+
defer wg.Done()
195+
_, err := client.CoreV1().Pods(ns).Create(pod)
196+
if err != nil {
197+
t.Error(err)
198+
}
199+
}()
200+
}
201+
wg.Wait()
202+
203+
if actual := atomic.LoadInt64(&trackingListener.connections); actual > 0 {
204+
t.Errorf("expected no additional connections (reusing kept-alive connections), got %d", actual)
205+
}
206+
}
207+
208+
type connectionRecorder struct {
209+
mu sync.Mutex
210+
upCh chan struct{}
211+
upOnce sync.Once
212+
}
213+
214+
// Reset zeros out all counts and returns a channel that is closed when the first admission of the
215+
// marker object is received.
216+
func (i *connectionRecorder) Reset() chan struct{} {
217+
i.mu.Lock()
218+
defer i.mu.Unlock()
219+
i.upCh = make(chan struct{})
220+
i.upOnce = sync.Once{}
221+
return i.upCh
222+
}
223+
224+
func (i *connectionRecorder) MarkerReceived() {
225+
i.mu.Lock()
226+
defer i.mu.Unlock()
227+
i.upOnce.Do(func() {
228+
close(i.upCh)
229+
})
230+
}
231+
232+
func newLoadBalanceWebhookHandler(recorder *connectionRecorder) http.Handler {
233+
allow := func(w http.ResponseWriter) {
234+
w.Header().Set("Content-Type", "application/json")
235+
json.NewEncoder(w).Encode(&v1beta1.AdmissionReview{
236+
Response: &v1beta1.AdmissionResponse{
237+
Allowed: true,
238+
},
239+
})
240+
}
241+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
242+
fmt.Println(r.Proto)
243+
defer r.Body.Close()
244+
data, err := ioutil.ReadAll(r.Body)
245+
if err != nil {
246+
http.Error(w, err.Error(), 400)
247+
}
248+
review := v1beta1.AdmissionReview{}
249+
if err := json.Unmarshal(data, &review); err != nil {
250+
http.Error(w, err.Error(), 400)
251+
}
252+
if review.Request.UserInfo.Username != testLoadBalanceClientUsername {
253+
// skip requests not originating from this integration test's client
254+
allow(w)
255+
return
256+
}
257+
258+
if len(review.Request.Object.Raw) == 0 {
259+
http.Error(w, err.Error(), 400)
260+
}
261+
pod := &corev1.Pod{}
262+
if err := json.Unmarshal(review.Request.Object.Raw, pod); err != nil {
263+
http.Error(w, err.Error(), 400)
264+
}
265+
266+
// When resetting between tests, a marker object is patched until this webhook
267+
// observes it, at which point it is considered ready.
268+
if pod.Namespace == loadBalanceMarkerFixture.Namespace && pod.Name == loadBalanceMarkerFixture.Name {
269+
recorder.MarkerReceived()
270+
allow(w)
271+
return
272+
}
273+
274+
// simulate a loaded backend
275+
time.Sleep(2 * time.Second)
276+
allow(w)
277+
})
278+
}
279+
280+
var loadBalanceMarkerFixture = &corev1.Pod{
281+
ObjectMeta: metav1.ObjectMeta{
282+
Namespace: "default",
283+
Name: "marker",
284+
},
285+
Spec: corev1.PodSpec{
286+
Containers: []v1.Container{{
287+
Name: "fake-name",
288+
Image: "fakeimage",
289+
}},
290+
},
291+
}
292+
293+
type connectionTrackingListener struct {
294+
connections int64
295+
delegate net.Listener
296+
}
297+
298+
func (c *connectionTrackingListener) Reset() {
299+
atomic.StoreInt64(&c.connections, 0)
300+
}
301+
302+
func (c *connectionTrackingListener) Accept() (net.Conn, error) {
303+
conn, err := c.delegate.Accept()
304+
if err == nil {
305+
atomic.AddInt64(&c.connections, 1)
306+
}
307+
return conn, err
308+
}
309+
func (c *connectionTrackingListener) Close() error {
310+
return c.delegate.Close()
311+
}
312+
func (c *connectionTrackingListener) Addr() net.Addr {
313+
return c.delegate.Addr()
314+
}

0 commit comments

Comments
 (0)