Skip to content

Commit

Permalink
update/lease-namespace (#140)
Browse files Browse the repository at this point in the history
* "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.
  • Loading branch information
alexei-led authored Mar 29, 2024
1 parent 1728875 commit ccafb85
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit ccafb85

Please sign in to comment.