From 9648d1d759ebf66dddabdc8f72308cad2d5608ca Mon Sep 17 00:00:00 2001 From: Maxime VISONNEAU Date: Thu, 22 Oct 2020 11:29:58 +0100 Subject: [PATCH] Replaced --annotationTTL with --lockTTL and made it work correctly --- cmd/kured/main.go | 20 +++++++++----------- pkg/daemonsetlock/daemonsetlock.go | 15 ++++++--------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/cmd/kured/main.go b/cmd/kured/main.go index 2bebf93..db3f18b 100644 --- a/cmd/kured/main.go +++ b/cmd/kured/main.go @@ -33,6 +33,7 @@ var ( dsNamespace string dsName string lockAnnotation string + lockTTL time.Duration prometheusURL string alertFilter *regexp.Regexp rebootSentinel string @@ -46,8 +47,6 @@ var ( rebootEnd string timezone string - annotationTTL time.Duration - // Metrics rebootRequiredGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Subsystem: "kured", @@ -74,6 +73,8 @@ func main() { "name of daemonset on which to place lock") rootCmd.PersistentFlags().StringVar(&lockAnnotation, "lock-annotation", "weave.works/kured-node-lock", "annotation in which to record locking node") + rootCmd.PersistentFlags().DurationVar(&lockTTL, "lock-ttl", 0, + "expire lock annotation after this duration (default: 0, disabled)") rootCmd.PersistentFlags().StringVar(&prometheusURL, "prometheus-url", "", "Prometheus instance to probe for active alerts") rootCmd.PersistentFlags().Var(®expValue{&alertFilter}, "alert-filter-regexp", @@ -100,9 +101,6 @@ func main() { rootCmd.PersistentFlags().StringVar(&timezone, "time-zone", "UTC", "use this timezone for schedule inputs") - rootCmd.PersistentFlags().DurationVar(&annotationTTL, "annotation-ttl", 0, - "force clean annotation after this ammount of time (default 0, disabled)") - if err := rootCmd.Execute(); err != nil { log.Fatal(err) } @@ -349,16 +347,16 @@ func root(cmd *cobra.Command, args []string) { log.Infof("Node ID: %s", nodeID) log.Infof("Lock Annotation: %s/%s:%s", dsNamespace, dsName, lockAnnotation) + if lockTTL > 0 { + log.Infof("Lock TTL set, lock will expire after: %v", lockTTL) + } else { + log.Info("Lock TTL not set, lock will remain until being released") + } log.Infof("Reboot Sentinel: %s every %v", rebootSentinel, period) log.Infof("Blocking Pod Selectors: %v", podSelectors) log.Infof("Reboot on: %v", window) - if annotationTTL > 0 { - log.Infof("Force annotation cleanup after: %v", annotationTTL) - } else { - log.Info("Force annotation cleanup disabled.") - } - go rebootAsRequired(nodeID, window, annotationTTL) + go rebootAsRequired(nodeID, window, lockTTL) go maintainRebootRequiredMetric(nodeID) http.Handle("/metrics", promhttp.Handler()) diff --git a/pkg/daemonsetlock/daemonsetlock.go b/pkg/daemonsetlock/daemonsetlock.go index 20fe88d..8f53d80 100644 --- a/pkg/daemonsetlock/daemonsetlock.go +++ b/pkg/daemonsetlock/daemonsetlock.go @@ -44,11 +44,9 @@ func (dsl *DaemonSetLock) Acquire(metadata interface{}, TTL time.Duration) (acqu return false, "", err } - if ttlExpired(value.Created, value.TTL) { - return true, value.NodeID, nil + if !ttlExpired(value.Created, value.TTL) { + return value.NodeID == dsl.nodeID, value.NodeID, nil } - - return value.NodeID == dsl.nodeID, value.NodeID, nil } if ds.ObjectMeta.Annotations == nil { @@ -88,11 +86,9 @@ func (dsl *DaemonSetLock) Test(metadata interface{}) (holding bool, err error) { return false, err } - if ttlExpired(value.Created, value.TTL) { - return true, nil + if !ttlExpired(value.Created, value.TTL) { + return value.NodeID == dsl.nodeID, nil } - - return value.NodeID == dsl.nodeID, nil } return false, nil @@ -111,7 +107,8 @@ func (dsl *DaemonSetLock) Release() error { if err := json.Unmarshal([]byte(valueString), &value); err != nil { return err } - if value.NodeID != dsl.nodeID && !ttlExpired(value.Created, value.TTL) { + + if value.NodeID != dsl.nodeID { return fmt.Errorf("Not lock holder: %v", value.NodeID) } } else {