|
| 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 apiserver |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + v1 "k8s.io/api/core/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/util/wait" |
| 28 | + "k8s.io/apiserver/pkg/features" |
| 29 | + utilfeature "k8s.io/apiserver/pkg/util/feature" |
| 30 | + clientset "k8s.io/client-go/kubernetes" |
| 31 | + restclient "k8s.io/client-go/rest" |
| 32 | + featuregatetesting "k8s.io/component-base/featuregate/testing" |
| 33 | + "k8s.io/kubernetes/pkg/controlplane/reconcilers" |
| 34 | + "k8s.io/kubernetes/test/integration/framework" |
| 35 | +) |
| 36 | + |
| 37 | +// setup create kube-apiserver backed up by two separate etcds, |
| 38 | +// with one of them containing events and the other all other objects. |
| 39 | +func multiEtcdSetup(t testing.TB) (clientset.Interface, framework.CloseFunc) { |
| 40 | + etcdArgs := []string{"--experimental-watch-progress-notify-interval", "1s"} |
| 41 | + etcd0URL, stopEtcd0, err := framework.RunCustomEtcd("etcd_watchcache0", etcdArgs) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("Couldn't start etcd: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + etcd1URL, stopEtcd1, err := framework.RunCustomEtcd("etcd_watchcache1", etcdArgs) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("Couldn't start etcd: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + etcdOptions := framework.DefaultEtcdOptions() |
| 52 | + // Overwrite etcd setup to our custom etcd instances. |
| 53 | + etcdOptions.StorageConfig.Transport.ServerList = []string{etcd0URL} |
| 54 | + etcdOptions.EtcdServersOverrides = []string{fmt.Sprintf("/events#%s", etcd1URL)} |
| 55 | + etcdOptions.EnableWatchCache = true |
| 56 | + |
| 57 | + opts := framework.MasterConfigOptions{EtcdOptions: etcdOptions} |
| 58 | + masterConfig := framework.NewIntegrationTestMasterConfigWithOptions(&opts) |
| 59 | + // Switch off endpoints reconciler to avoid unnecessary operations. |
| 60 | + masterConfig.ExtraConfig.EndpointReconcilerType = reconcilers.NoneEndpointReconcilerType |
| 61 | + _, s, stopMaster := framework.RunAMaster(masterConfig) |
| 62 | + |
| 63 | + closeFn := func() { |
| 64 | + stopMaster() |
| 65 | + stopEtcd1() |
| 66 | + stopEtcd0() |
| 67 | + } |
| 68 | + |
| 69 | + clientSet, err := clientset.NewForConfig(&restclient.Config{Host: s.URL, QPS: -1}) |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("Error in create clientset: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + // Wait for apiserver to be stabilized. |
| 75 | + // Everything but default service creation is checked in RunAMaster above by |
| 76 | + // waiting for post start hooks, so we just wait for default service to exist. |
| 77 | + // TODO(wojtek-t): Figure out less fragile way. |
| 78 | + ctx := context.Background() |
| 79 | + if err := wait.Poll(100*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) { |
| 80 | + _, err := clientSet.CoreV1().Services("default").Get(ctx, "kubernetes", metav1.GetOptions{}) |
| 81 | + return err == nil, nil |
| 82 | + }); err != nil { |
| 83 | + t.Fatalf("Failed to wait for kubernetes service: %v:", err) |
| 84 | + } |
| 85 | + return clientSet, closeFn |
| 86 | +} |
| 87 | + |
| 88 | +func TestWatchCacheUpdatedByEtcd(t *testing.T) { |
| 89 | + defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.EfficientWatchResumption, true)() |
| 90 | + |
| 91 | + c, closeFn := multiEtcdSetup(t) |
| 92 | + defer closeFn() |
| 93 | + |
| 94 | + ctx := context.Background() |
| 95 | + |
| 96 | + makeConfigMap := func(name string) *v1.ConfigMap { |
| 97 | + return &v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: name}} |
| 98 | + } |
| 99 | + makeSecret := func(name string) *v1.Secret { |
| 100 | + return &v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: name}} |
| 101 | + } |
| 102 | + makeEvent := func(name string) *v1.Event { |
| 103 | + return &v1.Event{ObjectMeta: metav1.ObjectMeta{Name: name}} |
| 104 | + } |
| 105 | + |
| 106 | + cm, err := c.CoreV1().ConfigMaps("default").Create(ctx, makeConfigMap("name"), metav1.CreateOptions{}) |
| 107 | + if err != nil { |
| 108 | + t.Errorf("Couldn't create configmap: %v", err) |
| 109 | + } |
| 110 | + ev, err := c.CoreV1().Events("default").Create(ctx, makeEvent("name"), metav1.CreateOptions{}) |
| 111 | + if err != nil { |
| 112 | + t.Errorf("Couldn't create event: %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + listOptions := metav1.ListOptions{ |
| 116 | + ResourceVersion: "0", |
| 117 | + ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, |
| 118 | + } |
| 119 | + |
| 120 | + // Wait until listing from cache returns resource version of corresponding |
| 121 | + // resources (being the last updates). |
| 122 | + t.Logf("Waiting for configmaps watchcache synced to %s", cm.ResourceVersion) |
| 123 | + if err := wait.Poll(100*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) { |
| 124 | + res, err := c.CoreV1().ConfigMaps("default").List(ctx, listOptions) |
| 125 | + if err != nil { |
| 126 | + return false, nil |
| 127 | + } |
| 128 | + return res.ResourceVersion == cm.ResourceVersion, nil |
| 129 | + }); err != nil { |
| 130 | + t.Errorf("Failed to wait for configmaps watchcache synced: %v", err) |
| 131 | + } |
| 132 | + t.Logf("Waiting for events watchcache synced to %s", ev.ResourceVersion) |
| 133 | + if err := wait.Poll(100*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) { |
| 134 | + res, err := c.CoreV1().Events("default").List(ctx, listOptions) |
| 135 | + if err != nil { |
| 136 | + return false, nil |
| 137 | + } |
| 138 | + return res.ResourceVersion == ev.ResourceVersion, nil |
| 139 | + }); err != nil { |
| 140 | + t.Errorf("Failed to wait for events watchcache synced: %v", err) |
| 141 | + } |
| 142 | + |
| 143 | + // Create a secret, that is stored in the same etcd as configmap, but |
| 144 | + // different than events. |
| 145 | + se, err := c.CoreV1().Secrets("default").Create(ctx, makeSecret("name"), metav1.CreateOptions{}) |
| 146 | + if err != nil { |
| 147 | + t.Errorf("Couldn't create secret: %v", err) |
| 148 | + } |
| 149 | + |
| 150 | + t.Logf("Waiting for configmaps watchcache synced to %s", se.ResourceVersion) |
| 151 | + if err := wait.Poll(100*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) { |
| 152 | + res, err := c.CoreV1().ConfigMaps("default").List(ctx, listOptions) |
| 153 | + if err != nil { |
| 154 | + return false, nil |
| 155 | + } |
| 156 | + return res.ResourceVersion == se.ResourceVersion, nil |
| 157 | + }); err != nil { |
| 158 | + t.Errorf("Failed to wait for configmaps watchcache synced: %v", err) |
| 159 | + } |
| 160 | + t.Logf("Waiting for events watchcache NOT synced to %s", se.ResourceVersion) |
| 161 | + if err := wait.Poll(100*time.Millisecond, 5*time.Second, func() (bool, error) { |
| 162 | + res, err := c.CoreV1().Events("default").List(ctx, listOptions) |
| 163 | + if err != nil { |
| 164 | + return false, nil |
| 165 | + } |
| 166 | + return res.ResourceVersion == se.ResourceVersion, nil |
| 167 | + }); err == nil || err != wait.ErrWaitTimeout { |
| 168 | + t.Errorf("Events watchcache unexpected synced: %v", err) |
| 169 | + } |
| 170 | +} |
0 commit comments