Skip to content

Commit ccafb85

Browse files
authored
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.
1 parent 1728875 commit ccafb85

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ rules:
6666
- apiGroups: [ "" ]
6767
resources: [ "nodes" ]
6868
verbs: [ "get" ]
69+
- apiGroups: [ "coordination.k8s.io" ]
70+
resources: [ "leases" ]
71+
verbs: [ "create", "get", "delete" ]
6972

7073
---
7174
apiVersion: rbac.authorization.k8s.io/v1
@@ -230,6 +233,8 @@ OPTIONS:
230233
--release-on-exit release the static public IP address on exit (default: true) [$RELEASE_ON_EXIT]
231234
--retry-attempts value number of attempts to assign the static public IP address (default: 10) [$RETRY_ATTEMPTS]
232235
--retry-interval value when the agent fails to assign the static public IP address, it will retry after this interval (default: 5m0s) [$RETRY_INTERVAL]
236+
--lease-duration value duration of the kubernetes lease (default: 5) [$LEASE_DURATION]
237+
--lease-namespace value namespace of the kubernetes lease (default: "default") [$LEASE_NAMESPACE]
233238
234239
Development
235240

cmd/main.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func assignAddress(c context.Context, log *logrus.Entry, client kubernetes.Inter
9191
defer ticker.Stop()
9292

9393
// create new cluster wide lock
94-
lock := lease.NewKubeLeaseLock(client, kubeipLockName, "default", node.Instance, cfg.LeaseDuration)
94+
lock := lease.NewKubeLeaseLock(client, kubeipLockName, cfg.LeaseNamespace, node.Instance, cfg.LeaseDuration)
9595

9696
for retryCounter := 0; retryCounter <= cfg.RetryAttempts; retryCounter++ {
9797
log.WithFields(logrus.Fields{
@@ -293,6 +293,13 @@ func main() {
293293
EnvVars: []string{"LEASE_DURATION"},
294294
Category: "Configuration",
295295
},
296+
&cli.StringFlag{
297+
Name: "lease-namespace",
298+
Usage: "namespace of the kubernetes lease",
299+
EnvVars: []string{"LEASE_NAMESPACE"},
300+
Value: "default", // default namespace
301+
Category: "Configuration",
302+
},
296303
&cli.BoolFlag{
297304
Name: "release-on-exit",
298305
Usage: "release the static public IP address on exit",

internal/config/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ type Config struct {
3131
ReleaseOnExit bool `json:"release-on-exit"`
3232
// LeaseDuration is the duration of the kubernetes lease
3333
LeaseDuration int `json:"lease-duration"`
34+
// LeaseNamespace is the namespace of the kubernetes lease
35+
LeaseNamespace string `json:"lease-namespace"`
3436
}
3537

3638
func NewConfig(c *cli.Context) *Config {
@@ -47,5 +49,6 @@ func NewConfig(c *cli.Context) *Config {
4749
cfg.IPv6 = c.Bool("ipv6")
4850
cfg.ReleaseOnExit = c.Bool("release-on-exit")
4951
cfg.LeaseDuration = c.Int("lease-duration")
52+
cfg.LeaseNamespace = c.String("lease-namespace")
5053
return &cfg
5154
}

0 commit comments

Comments
 (0)