From ccafb8526216ff5a72f51e5339bc01ec49c66332 Mon Sep 17 00:00:00 2001 From: Alexei Ledenev Date: Fri, 29 Mar 2024 20:35:10 +0300 Subject: [PATCH] update/lease-namespace (#140) * "Add lease namespace flag to NewConfig function and use it in main for lock creation" * Added support for creating, getting, and deleting leases in coordination.k8s.io API group. Also added flags for lease duration and namespace. --- README.md | 5 +++++ cmd/main.go | 9 ++++++++- internal/config/config.go | 3 +++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index af15701..4271a4c 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,9 @@ rules: - apiGroups: [ "" ] resources: [ "nodes" ] verbs: [ "get" ] + - apiGroups: [ "coordination.k8s.io" ] + resources: [ "leases" ] + verbs: [ "create", "get", "delete" ] --- apiVersion: rbac.authorization.k8s.io/v1 @@ -230,6 +233,8 @@ OPTIONS: --release-on-exit release the static public IP address on exit (default: true) [$RELEASE_ON_EXIT] --retry-attempts value number of attempts to assign the static public IP address (default: 10) [$RETRY_ATTEMPTS] --retry-interval value when the agent fails to assign the static public IP address, it will retry after this interval (default: 5m0s) [$RETRY_INTERVAL] + --lease-duration value duration of the kubernetes lease (default: 5) [$LEASE_DURATION] + --lease-namespace value namespace of the kubernetes lease (default: "default") [$LEASE_NAMESPACE] Development diff --git a/cmd/main.go b/cmd/main.go index 6d736cc..98e4b96 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -91,7 +91,7 @@ func assignAddress(c context.Context, log *logrus.Entry, client kubernetes.Inter defer ticker.Stop() // create new cluster wide lock - lock := lease.NewKubeLeaseLock(client, kubeipLockName, "default", node.Instance, cfg.LeaseDuration) + lock := lease.NewKubeLeaseLock(client, kubeipLockName, cfg.LeaseNamespace, node.Instance, cfg.LeaseDuration) for retryCounter := 0; retryCounter <= cfg.RetryAttempts; retryCounter++ { log.WithFields(logrus.Fields{ @@ -293,6 +293,13 @@ func main() { EnvVars: []string{"LEASE_DURATION"}, Category: "Configuration", }, + &cli.StringFlag{ + Name: "lease-namespace", + Usage: "namespace of the kubernetes lease", + EnvVars: []string{"LEASE_NAMESPACE"}, + Value: "default", // default namespace + Category: "Configuration", + }, &cli.BoolFlag{ Name: "release-on-exit", Usage: "release the static public IP address on exit", diff --git a/internal/config/config.go b/internal/config/config.go index ddd4e1d..7ecf067 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -31,6 +31,8 @@ type Config struct { ReleaseOnExit bool `json:"release-on-exit"` // LeaseDuration is the duration of the kubernetes lease LeaseDuration int `json:"lease-duration"` + // LeaseNamespace is the namespace of the kubernetes lease + LeaseNamespace string `json:"lease-namespace"` } func NewConfig(c *cli.Context) *Config { @@ -47,5 +49,6 @@ func NewConfig(c *cli.Context) *Config { cfg.IPv6 = c.Bool("ipv6") cfg.ReleaseOnExit = c.Bool("release-on-exit") cfg.LeaseDuration = c.Int("lease-duration") + cfg.LeaseNamespace = c.String("lease-namespace") return &cfg }