| 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 |
"time" |
| 21 |
|
| 22 |
coordinationv1 "k8s.io/api/coordination/v1" |
| 23 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 |
"k8s.io/apimachinery/pkg/labels" |
| 25 |
"k8s.io/client-go/kubernetes" |
| 26 |
"k8s.io/component-helpers/apimachinery/lease" |
| 27 |
"k8s.io/klog/v2" |
| 28 |
"k8s.io/utils/clock" |
| 29 |
) |
| 30 |
|
| 31 |
type Controller struct { |
| 32 |
leaseName string |
| 33 |
leaseNamespace string |
| 34 |
k8sClient kubernetes.Interface |
| 35 |
|
| 36 |
gcController *GarbageCollectionController |
| 37 |
acquireController lease.Controller |
| 38 |
} |
| 39 |
|
| 40 |
func NewController(k8sClient kubernetes.Interface, holderIdentity string, leaseDurationSeconds int32, renewInterval time.Duration, gcCheckPeriod time.Duration, leaseName, leaseNamespace string, leaseLabels map[string]string) *Controller { |
| 41 |
acquireController := lease.NewController( |
| 42 |
clock.RealClock{}, |
| 43 |
k8sClient, |
| 44 |
holderIdentity, |
| 45 |
leaseDurationSeconds, |
| 46 |
nil, |
| 47 |
renewInterval, |
| 48 |
leaseName, |
| 49 |
leaseNamespace, |
| 50 |
func(lease *coordinationv1.Lease) error { |
| 51 |
lease.SetLabels(leaseLabels) |
| 52 |
return nil |
| 53 |
}, |
| 54 |
) |
| 55 |
gcController := NewGarbageCollectionController(clock.RealClock{}, k8sClient, leaseNamespace, gcCheckPeriod, labels.FormatLabels(leaseLabels)) |
| 56 |
|
| 57 |
return &Controller{ |
| 58 |
leaseNamespace: leaseNamespace, |
| 59 |
leaseName: leaseName, |
| 60 |
k8sClient: k8sClient, |
| 61 |
gcController: gcController, |
| 62 |
acquireController: acquireController, |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
// Run starts the garbage collection and lease acquisition controllers. Non-blocking. |
| 67 |
func (c *Controller) Run(ctx context.Context) { |
| 68 |
go c.gcController.Run(ctx) |
| 69 |
go c.acquireController.Run(ctx) |
| 70 |
} |
| 71 |
|
| 72 |
func (c *Controller) Stop() { |
| 73 |
klog.Infof("Cleaning up server lease %q", c.leaseName) |
| 74 |
err := c.k8sClient.CoordinationV1().Leases(c.leaseNamespace).Delete(context.Background(), c.leaseName, metav1.DeleteOptions{}) |
| 75 |
if err != nil { |
| 76 |
klog.Errorf("Could not clean up lease %q in namespace %q", c.leaseName, c.leaseNamespace) |
| 77 |
} |
| 78 |
} |