| 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 leases |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "errors" |
| 21 | "time" |
| 22 | |
| 23 | apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 24 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | "k8s.io/apimachinery/pkg/util/wait" |
| 26 | "k8s.io/client-go/kubernetes" |
| 27 | coordinationv1 "k8s.io/client-go/kubernetes/typed/coordination/v1" |
| 28 | "k8s.io/klog/v2" |
| 29 | "k8s.io/utils/clock" |
| 30 | "sigs.k8s.io/apiserver-network-proxy/pkg/server/metrics" |
| 31 | "sigs.k8s.io/apiserver-network-proxy/pkg/util" |
| 32 | ) |
| 33 | |
| 34 | type GarbageCollectionController struct { |
| 35 | leaseInterface coordinationv1.LeaseInterface |
| 36 | |
| 37 | labelSelector string |
| 38 | gcCheckPeriod time.Duration |
| 39 | |
| 40 | pc clock.PassiveClock |
| 41 | } |
| 42 | |
| 43 | func NewGarbageCollectionController(pc clock.PassiveClock, k8sclient kubernetes.Interface, namespace string, gcCheckPeriod time.Duration, leaseSelector string) *GarbageCollectionController { |
| 44 | return &GarbageCollectionController{ |
| 45 | leaseInterface: k8sclient.CoordinationV1().Leases(namespace), |
| 46 | gcCheckPeriod: gcCheckPeriod, |
| 47 | labelSelector: leaseSelector, |
| 48 | pc: pc, |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func (c *GarbageCollectionController) Run(ctx context.Context) { |
| 53 | wait.UntilWithContext(ctx, c.gc, c.gcCheckPeriod) |
| 54 | } |
| 55 | |
| 56 | func (c *GarbageCollectionController) gc(ctx context.Context) { |
| 57 | start := time.Now() |
| 58 | leases, err := c.leaseInterface.List(ctx, metav1.ListOptions{LabelSelector: c.labelSelector}) |
| 59 | latency := time.Now().Sub(start) |
| 60 | if err != nil { |
| 61 | klog.Errorf("Could not list leases to garbage collect: %v", err) |
| 62 | |
| 63 | var apiStatus apierrors.APIStatus |
| 64 | if errors.As(err, &apiStatus) { |
| 65 | status := apiStatus.Status() |
| 66 | metrics.Metrics.ObserveLeaseList(int(status.Code), string(status.Reason)) |
| 67 | metrics.Metrics.ObserveLeaseListLatency(int(status.Code), latency) |
| 68 | } else { |
| 69 | klog.Errorf("Lease list error could not be logged to metrics as it is not an APIStatus: %v", err) |
| 70 | } |
| 71 | |
| 72 | return |
| 73 | } |
| 74 | metrics.Metrics.ObserveLeaseList(200, "") |
| 75 | metrics.Metrics.ObserveLeaseListLatency(200, latency) |
| 76 | |
| 77 | for _, lease := range leases.Items { |
| 78 | if util.IsLeaseValid(c.pc, lease) { |
| 79 | continue |
| 80 | } |
| 81 | |
| 82 | // Optimistic concurrency: if a lease has a different resourceVersion than |
| 83 | // when we got it, it may have been renewed. |
| 84 | start := time.Now() |
| 85 | err := c.leaseInterface.Delete(ctx, lease.Name, *metav1.NewRVDeletionPrecondition(lease.ResourceVersion)) |
| 86 | if apierrors.IsNotFound(err) { |
| 87 | klog.V(4).Infof("Lease %v was already deleted", lease.Name) |
| 88 | } else if err != nil { |
| 89 | klog.Errorf("Could not delete lease %v: %v", lease.Name, err) |
| 90 | } else { |
| 91 | metrics.Metrics.CulledLeasesInc() |
| 92 | } |
| 93 | |
| 94 | // Log metrics for the deletion call. |
| 95 | latency := time.Now().Sub(start) |
| 96 | if err != nil { |
| 97 | var apiStatus apierrors.APIStatus |
| 98 | if errors.As(err, &apiStatus) { |
| 99 | status := apiStatus.Status() |
| 100 | metrics.Metrics.ObserveLeaseDelete(int(status.Code), string(status.Reason)) |
| 101 | metrics.Metrics.ObserveLeaseDeleteLatency(int(status.Code), latency) |
| 102 | } else { |
| 103 | klog.Errorf("Lease delete error could not be logged to metrics as it is not an APIStatus: %v", err) |
| 104 | } |
| 105 | } else { |
| 106 | metrics.Metrics.ObserveLeaseDelete(200, "") |
| 107 | metrics.Metrics.ObserveLeaseDeleteLatency(200, latency) |
| 108 | } |
| 109 | } |
| 110 | } |