|
| 1 | +/* |
| 2 | +Copyright 2020 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 apimachinery |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "net/http/httptest" |
| 22 | + "net/http/httputil" |
| 23 | + "net/url" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "k8s.io/apimachinery/pkg/runtime" |
| 30 | + "k8s.io/apimachinery/pkg/watch" |
| 31 | + "k8s.io/client-go/kubernetes" |
| 32 | + restclient "k8s.io/client-go/rest" |
| 33 | + "k8s.io/client-go/tools/cache" |
| 34 | + kubectlproxy "k8s.io/kubectl/pkg/proxy" |
| 35 | + "k8s.io/kubernetes/test/integration/framework" |
| 36 | +) |
| 37 | + |
| 38 | +func TestWatchClientTimeout(t *testing.T) { |
| 39 | + masterConfig := framework.NewIntegrationTestMasterConfig() |
| 40 | + _, s, closeFn := framework.RunAMaster(masterConfig) |
| 41 | + defer closeFn() |
| 42 | + |
| 43 | + t.Run("direct", func(t *testing.T) { |
| 44 | + t.Logf("client at %s", s.URL) |
| 45 | + testWatchClientTimeouts(t, s.URL) |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("reverse proxy", func(t *testing.T) { |
| 49 | + u, _ := url.Parse(s.URL) |
| 50 | + proxy := httputil.NewSingleHostReverseProxy(u) |
| 51 | + proxy.FlushInterval = -1 |
| 52 | + proxyServer := httptest.NewServer(httputil.NewSingleHostReverseProxy(u)) |
| 53 | + defer proxyServer.Close() |
| 54 | + |
| 55 | + t.Logf("client to %s, backend at %s", proxyServer.URL, s.URL) |
| 56 | + testWatchClientTimeouts(t, proxyServer.URL) |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("kubectl proxy", func(t *testing.T) { |
| 60 | + kubectlProxyServer, err := kubectlproxy.NewServer("", "/", "/static/", nil, &restclient.Config{Host: s.URL, Timeout: 2 * time.Second}, 0) |
| 61 | + if err != nil { |
| 62 | + t.Fatal(err) |
| 63 | + } |
| 64 | + kubectlProxyListener, err := kubectlProxyServer.Listen("", 0) |
| 65 | + if err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + defer kubectlProxyListener.Close() |
| 69 | + go kubectlProxyServer.ServeOnListener(kubectlProxyListener) |
| 70 | + |
| 71 | + t.Logf("client to %s, backend at %s", kubectlProxyListener.Addr().String(), s.URL) |
| 72 | + testWatchClientTimeouts(t, "http://"+kubectlProxyListener.Addr().String()) |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +func testWatchClientTimeouts(t *testing.T, url string) { |
| 77 | + t.Run("timeout", func(t *testing.T) { |
| 78 | + testWatchClientTimeout(t, url, time.Second, 0) |
| 79 | + }) |
| 80 | + t.Run("timeoutSeconds", func(t *testing.T) { |
| 81 | + testWatchClientTimeout(t, url, 0, time.Second) |
| 82 | + }) |
| 83 | + t.Run("timeout+timeoutSeconds", func(t *testing.T) { |
| 84 | + testWatchClientTimeout(t, url, time.Second, time.Second) |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +func testWatchClientTimeout(t *testing.T, serverURL string, timeout, timeoutSeconds time.Duration) { |
| 89 | + // client |
| 90 | + client, err := kubernetes.NewForConfig(&restclient.Config{Host: serverURL, Timeout: timeout}) |
| 91 | + if err != nil { |
| 92 | + t.Fatal(err) |
| 93 | + } |
| 94 | + |
| 95 | + listCount := 0 |
| 96 | + watchCount := 0 |
| 97 | + stopCh := make(chan struct{}) |
| 98 | + listWatch := &cache.ListWatch{ |
| 99 | + ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { |
| 100 | + t.Logf("listing (version=%s continue=%s)", options.ResourceVersion, options.Continue) |
| 101 | + listCount++ |
| 102 | + if listCount > 1 { |
| 103 | + t.Errorf("listed more than once") |
| 104 | + close(stopCh) |
| 105 | + } |
| 106 | + return client.CoreV1().ConfigMaps(metav1.NamespaceAll).List(context.TODO(), options) |
| 107 | + }, |
| 108 | + WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { |
| 109 | + t.Logf("watching (version=%s)", options.ResourceVersion) |
| 110 | + if timeoutSeconds != 0 { |
| 111 | + timeout := int64(timeoutSeconds / time.Second) |
| 112 | + options.TimeoutSeconds = &timeout |
| 113 | + } |
| 114 | + watchCount++ |
| 115 | + if watchCount > 1 { |
| 116 | + // success, restarted watch |
| 117 | + close(stopCh) |
| 118 | + } |
| 119 | + return client.CoreV1().ConfigMaps(metav1.NamespaceAll).Watch(context.TODO(), options) |
| 120 | + }, |
| 121 | + } |
| 122 | + _, informer := cache.NewIndexerInformer(listWatch, &corev1.ConfigMap{}, 30*time.Minute, cache.ResourceEventHandlerFuncs{}, cache.Indexers{}) |
| 123 | + informer.Run(stopCh) |
| 124 | + select { |
| 125 | + case <-stopCh: |
| 126 | + case <-time.After(time.Minute): |
| 127 | + t.Fatal("timeout") |
| 128 | + } |
| 129 | +} |
0 commit comments