| 1 | /* |
| 2 | Copyright 2024 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 | package agent |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "fmt" |
| 21 | "testing" |
| 22 | "time" |
| 23 | |
| 24 | coordinationv1 "k8s.io/api/coordination/v1" |
| 25 | "k8s.io/apimachinery/pkg/labels" |
| 26 | "k8s.io/apimachinery/pkg/runtime" |
| 27 | "k8s.io/client-go/kubernetes/fake" |
| 28 | coordinationv1lister "k8s.io/client-go/listers/coordination/v1" |
| 29 | "k8s.io/client-go/tools/cache" |
| 30 | clocktesting "k8s.io/utils/clock/testing" |
| 31 | proxytesting "sigs.k8s.io/apiserver-network-proxy/pkg/testing" |
| 32 | ) |
| 33 | |
| 34 | type labelMap map[string]string |
| 35 | |
| 36 | func TestServerLeaseCounter(t *testing.T) { |
| 37 | testCases := []struct { |
| 38 | name string |
| 39 | |
| 40 | templates []proxytesting.LeaseTemplate |
| 41 | leaseListerError error |
| 42 | |
| 43 | labelSelector string |
| 44 | |
| 45 | want int |
| 46 | }{ |
| 47 | { |
| 48 | name: "returns fallback count (1) when no leases exist", |
| 49 | templates: []proxytesting.LeaseTemplate{}, |
| 50 | labelSelector: "label=value", |
| 51 | want: 1, |
| 52 | }, { |
| 53 | name: "returns fallback count (1) when no leases matching selector exist", |
| 54 | templates: []proxytesting.LeaseTemplate{ |
| 55 | { |
| 56 | DurationSecs: 1000, |
| 57 | TimeSinceAcquire: time.Second, |
| 58 | Labels: labelMap{"label": "wrong_value"}, |
| 59 | }, |
| 60 | { |
| 61 | DurationSecs: 1000, |
| 62 | TimeSinceAcquire: time.Second, |
| 63 | Labels: labelMap{"wrong_label": "value"}, |
| 64 | }, |
| 65 | }, |
| 66 | labelSelector: "label=value", |
| 67 | want: 1, |
| 68 | }, { |
| 69 | name: "returns fallback count (1) when no leases matching selector are still valid", |
| 70 | templates: []proxytesting.LeaseTemplate{ |
| 71 | { |
| 72 | DurationSecs: 1000, |
| 73 | TimeSinceAcquire: 10000 * time.Second, |
| 74 | Labels: labelMap{"label": "value"}, |
| 75 | }, |
| 76 | { |
| 77 | DurationSecs: 1000, |
| 78 | TimeSinceAcquire: time.Second, |
| 79 | Labels: labelMap{"wrong_label": "value"}, |
| 80 | }, |
| 81 | }, |
| 82 | labelSelector: "label=value", |
| 83 | want: 1, |
| 84 | }, { |
| 85 | name: "returns fallback count (1) when LeaseLister returns an error", |
| 86 | templates: []proxytesting.LeaseTemplate{}, |
| 87 | labelSelector: "label=value", |
| 88 | leaseListerError: fmt.Errorf("test error"), |
| 89 | want: 1, |
| 90 | }, { |
| 91 | name: "counts only valid leases matching label selector", |
| 92 | templates: []proxytesting.LeaseTemplate{ |
| 93 | { |
| 94 | DurationSecs: 1000, |
| 95 | TimeSinceAcquire: time.Second, |
| 96 | Labels: labelMap{"label": "value"}, |
| 97 | }, |
| 98 | { |
| 99 | DurationSecs: 1000, |
| 100 | TimeSinceAcquire: time.Second, |
| 101 | Labels: labelMap{"label": "value"}, |
| 102 | }, |
| 103 | { |
| 104 | DurationSecs: 1000, |
| 105 | TimeSinceAcquire: time.Second, |
| 106 | Labels: labelMap{"label": "wrong_value"}, |
| 107 | }, |
| 108 | }, |
| 109 | labelSelector: "label=value", |
| 110 | want: 2, |
| 111 | }, |
| 112 | } |
| 113 | |
| 114 | for _, tc := range testCases { |
| 115 | t.Run(tc.name, func(t *testing.T) { |
| 116 | pc := clocktesting.NewFakePassiveClock(time.Now()) |
| 117 | leases := make([]runtime.Object, len(tc.templates)) |
| 118 | for i, template := range tc.templates { |
| 119 | leases[i] = proxytesting.NewLeaseFromTemplate(pc, template) |
| 120 | } |
| 121 | |
| 122 | k8sClient := fake.NewSimpleClientset(leases...) |
| 123 | selector, _ := labels.Parse(tc.labelSelector) |
| 124 | leaseInformer := NewLeaseInformerWithMetrics(k8sClient, "", time.Millisecond) |
| 125 | go leaseInformer.Run(context.Background().Done()) |
| 126 | cache.WaitForCacheSync(context.Background().Done(), leaseInformer.HasSynced) |
| 127 | leaseLister := coordinationv1lister.NewLeaseLister(leaseInformer.GetIndexer()) |
| 128 | |
| 129 | counter := NewServerLeaseCounter(pc, leaseLister, selector) |
| 130 | |
| 131 | got := counter.Count() |
| 132 | if tc.want != got { |
| 133 | t.Errorf("incorrect server count (got: %v, want: %v)", got, tc.want) |
| 134 | } |
| 135 | }) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | type fakeLeaseLister struct { |
| 140 | LeaseList []*coordinationv1.Lease |
| 141 | Err error |
| 142 | } |
| 143 | |
| 144 | func (lister *fakeLeaseLister) List(selector labels.Selector) (ret []*coordinationv1.Lease, err error) { |
| 145 | if lister.Err != nil { |
| 146 | return nil, lister.Err |
| 147 | } |
| 148 | |
| 149 | for _, lease := range lister.LeaseList { |
| 150 | if selector.Matches(labels.Set(lease.Labels)) { |
| 151 | ret = append(ret, lease) |
| 152 | } |
| 153 | } |
| 154 | return ret, nil |
| 155 | } |
| 156 | |
| 157 | func (lister *fakeLeaseLister) Leases(_ string) coordinationv1lister.LeaseNamespaceLister { |
| 158 | panic("should not be used") |
| 159 | } |
| 160 | |
| 161 | func TestServerLeaseCounter_FallbackCount(t *testing.T) { |
| 162 | validLease := proxytesting.LeaseTemplate{ |
| 163 | DurationSecs: 1000, |
| 164 | TimeSinceAcquire: time.Second, |
| 165 | Labels: map[string]string{"label": "value"}, |
| 166 | } |
| 167 | invalidLease := proxytesting.LeaseTemplate{ |
| 168 | DurationSecs: 1000, |
| 169 | TimeSinceAcquire: time.Second * 10000, |
| 170 | Labels: map[string]string{"label": "value"}, |
| 171 | } |
| 172 | |
| 173 | pc := clocktesting.NewFakeClock(time.Now()) |
| 174 | leases := []*coordinationv1.Lease{proxytesting.NewLeaseFromTemplate(pc, validLease), proxytesting.NewLeaseFromTemplate(pc, validLease), proxytesting.NewLeaseFromTemplate(pc, validLease), proxytesting.NewLeaseFromTemplate(pc, invalidLease)} |
| 175 | selector, _ := labels.Parse("label=value") |
| 176 | leaseLister := &fakeLeaseLister{LeaseList: leases} |
| 177 | |
| 178 | counter := NewServerLeaseCounter(pc, leaseLister, selector) |
| 179 | |
| 180 | leaseLister.Err = fmt.Errorf("fake lease listing error") |
| 181 | got := counter.Count() |
| 182 | if got != 1 { |
| 183 | t.Errorf("lease counter did not return fallback count on leaseLister error (got: %v, want: 1)", got) |
| 184 | } |
| 185 | |
| 186 | // Second call should return the actual count (3) upon leaseClient success. |
| 187 | leaseLister.Err = nil |
| 188 | actualCount := 3 |
| 189 | got = counter.Count() |
| 190 | if got != actualCount { |
| 191 | t.Errorf("lease counter did not return actual count on leaseClient success (got: %v, want: %v)", got, actualCount) |
| 192 | } |
| 193 | |
| 194 | // Third call should return updated fallback count (3) upon leaseClient failure. |
| 195 | leaseLister.Err = fmt.Errorf("fake lease listing error") |
| 196 | got = counter.Count() |
| 197 | if got != actualCount { |
| 198 | t.Errorf("lease counter did not update fallback count after leaseClient success, returned incorrect count on subsequent leaseClient error (got: %v, want: %v)", got, actualCount) |
| 199 | } |
| 200 | } |