k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/controlplane/controller/apiserverleasegc/gc_controller_test.go (about) 1 /* 2 Copyright 2022 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 apiserverleasegc 18 19 import ( 20 "context" 21 "testing" 22 "time" 23 24 coordinationv1 "k8s.io/api/coordination/v1" 25 apierrors "k8s.io/apimachinery/pkg/api/errors" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/client-go/kubernetes/fake" 28 testingclock "k8s.io/utils/clock/testing" 29 "k8s.io/utils/pointer" 30 ) 31 32 // Test_Controller validates the garbage collection logic for the apiserverleasegc controller. 33 func Test_Controller(t *testing.T) { 34 fakeClock := testingclock.NewFakeClock(time.Now()) 35 tests := []struct { 36 name string 37 lease *coordinationv1.Lease 38 expectDeleted bool 39 }{ 40 { 41 name: "lease not expired", 42 lease: &coordinationv1.Lease{ 43 ObjectMeta: metav1.ObjectMeta{ 44 Name: "kube-apiserver-12345", 45 Namespace: metav1.NamespaceSystem, 46 Labels: map[string]string{ 47 "apiserver.kubernetes.io/identity": "kube-apiserver", 48 }, 49 }, 50 Spec: coordinationv1.LeaseSpec{ 51 HolderIdentity: pointer.StringPtr("kube-apiserver-12345"), 52 LeaseDurationSeconds: pointer.Int32Ptr(10), 53 RenewTime: &metav1.MicroTime{Time: fakeClock.Now()}, 54 }, 55 }, 56 expectDeleted: false, 57 }, 58 { 59 name: "expired lease but with a different component label", 60 lease: &coordinationv1.Lease{ 61 ObjectMeta: metav1.ObjectMeta{ 62 Name: "kube-apiserver-12345", 63 Namespace: metav1.NamespaceSystem, 64 Labels: map[string]string{ 65 "apiserver.kubernetes.io/identity": "kube-controller-manager", 66 }, 67 }, 68 Spec: coordinationv1.LeaseSpec{ 69 HolderIdentity: pointer.StringPtr("kube-apiserver-12345"), 70 LeaseDurationSeconds: pointer.Int32Ptr(10), 71 RenewTime: &metav1.MicroTime{Time: fakeClock.Now().Add(-time.Minute)}, 72 }, 73 }, 74 expectDeleted: false, 75 }, 76 { 77 name: "lease expired due to expired renew time", 78 lease: &coordinationv1.Lease{ 79 ObjectMeta: metav1.ObjectMeta{ 80 Name: "kube-apiserver-12345", 81 Namespace: metav1.NamespaceSystem, 82 Labels: map[string]string{ 83 "apiserver.kubernetes.io/identity": "kube-apiserver", 84 }, 85 }, 86 Spec: coordinationv1.LeaseSpec{ 87 HolderIdentity: pointer.StringPtr("kube-apiserver-12345"), 88 LeaseDurationSeconds: pointer.Int32Ptr(10), 89 RenewTime: &metav1.MicroTime{Time: fakeClock.Now().Add(-time.Minute)}, 90 }, 91 }, 92 expectDeleted: true, 93 }, 94 { 95 name: "lease expired due to nil renew time", 96 lease: &coordinationv1.Lease{ 97 ObjectMeta: metav1.ObjectMeta{ 98 Name: "kube-apiserver-12345", 99 Namespace: metav1.NamespaceSystem, 100 Labels: map[string]string{ 101 "apiserver.kubernetes.io/identity": "kube-apiserver", 102 }, 103 }, 104 Spec: coordinationv1.LeaseSpec{ 105 HolderIdentity: pointer.StringPtr("kube-apiserver-12345"), 106 LeaseDurationSeconds: pointer.Int32Ptr(10), 107 RenewTime: nil, 108 }, 109 }, 110 expectDeleted: true, 111 }, 112 { 113 name: "lease expired due to nil lease duration seconds", 114 lease: &coordinationv1.Lease{ 115 ObjectMeta: metav1.ObjectMeta{ 116 Name: "kube-apiserver-12345", 117 Namespace: metav1.NamespaceSystem, 118 Labels: map[string]string{ 119 "apiserver.kubernetes.io/identity": "kube-apiserver", 120 }, 121 }, 122 Spec: coordinationv1.LeaseSpec{ 123 HolderIdentity: pointer.StringPtr("kube-apiserver-12345"), 124 LeaseDurationSeconds: nil, 125 RenewTime: &metav1.MicroTime{Time: fakeClock.Now().Add(-time.Minute)}, 126 }, 127 }, 128 expectDeleted: true, 129 }, 130 } 131 132 for _, test := range tests { 133 t.Run(test.name, func(t *testing.T) { 134 clientset := fake.NewSimpleClientset(test.lease) 135 controller := NewAPIServerLeaseGC(clientset, 100*time.Millisecond, metav1.NamespaceSystem, "apiserver.kubernetes.io/identity=kube-apiserver") 136 go controller.Run(nil) 137 138 time.Sleep(time.Second) 139 140 _, err := clientset.CoordinationV1().Leases(test.lease.Namespace).Get(context.TODO(), test.lease.Name, metav1.GetOptions{}) 141 if err != nil && !apierrors.IsNotFound(err) { 142 t.Errorf("unexpected error %v", err) 143 } 144 145 if apierrors.IsNotFound(err) && !test.expectDeleted { 146 t.Errorf("lease was not deleted") 147 } 148 149 if err == nil && test.expectDeleted { 150 t.Error("lease was not deleted") 151 } 152 }) 153 } 154 }