From 231888e58a94bbac4dd1aa122704c9a2db657dcc Mon Sep 17 00:00:00 2001 From: Jean-Philippe Evrard Date: Sat, 19 Oct 2024 11:30:28 +0200 Subject: [PATCH] Use RegexpValue in plags This will remove double pointers, and be explicit about the type we are using. Signed-off-by: Jean-Philippe Evrard --- cmd/kured/main.go | 7 +++---- cmd/kured/pflags.go | 18 +++++++----------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/cmd/kured/main.go b/cmd/kured/main.go index 96b7541..1ff59d7 100644 --- a/cmd/kured/main.go +++ b/cmd/kured/main.go @@ -12,7 +12,6 @@ import ( "net/url" "os" "reflect" - "regexp" "sort" "strconv" "strings" @@ -60,7 +59,7 @@ var ( lockReleaseDelay time.Duration prometheusURL string preferNoScheduleTaintName string - alertFilter *regexp.Regexp + alertFilter regexpValue alertFilterMatchOnly bool alertFiringOnly bool rebootSentinelFile string @@ -150,7 +149,7 @@ func main() { "delay lock release for this duration (default: 0, disabled)") flag.StringVar(&prometheusURL, "prometheus-url", "", "Prometheus instance to probe for active alerts") - flag.Var(®expValue{&alertFilter}, "alert-filter-regexp", + flag.Var(&alertFilter, "alert-filter-regexp", "alert names to ignore when checking for active alerts") flag.BoolVar(&alertFilterMatchOnly, "alert-filter-match-only", false, "Only block if the alert-filter-regexp matches active alerts") @@ -655,7 +654,7 @@ func rebootAsRequired(nodeID string, rebooter reboot.Rebooter, checker checkers. var blockCheckers []blockers.RebootBlocker if prometheusURL != "" { - blockCheckers = append(blockCheckers, blockers.PrometheusBlockingChecker{PromClient: promClient, Filter: alertFilter, FiringOnly: alertFiringOnly, FilterMatchOnly: alertFilterMatchOnly}) + blockCheckers = append(blockCheckers, blockers.PrometheusBlockingChecker{PromClient: promClient, Filter: alertFilter.Regexp, FiringOnly: alertFiringOnly, FilterMatchOnly: alertFilterMatchOnly}) } if podSelectors != nil { blockCheckers = append(blockCheckers, blockers.KubernetesBlockingChecker{Client: client, Nodename: nodeID, Filter: podSelectors}) diff --git a/cmd/kured/pflags.go b/cmd/kured/pflags.go index ffec3a7..a79d694 100644 --- a/cmd/kured/pflags.go +++ b/cmd/kured/pflags.go @@ -1,30 +1,26 @@ package main import ( - "fmt" "regexp" ) -// Custom flag type to handle *regexp.Regexp with pflag type regexpValue struct { - value **regexp.Regexp + *regexp.Regexp } -// String method to return the string representation of the value func (rev *regexpValue) String() string { - if *rev.value != nil { - return (*rev.value).String() + if rev.Regexp == nil { + return "" } - return "" + return rev.Regexp.String() } -// Set method to parse the input string and set the regexp value func (rev *regexpValue) Set(s string) error { - compiledRegexp, err := regexp.Compile(s) + value, err := regexp.Compile(s) if err != nil { - return fmt.Errorf("invalid regular expression: %w", err) + return err } - *rev.value = compiledRegexp + rev.Regexp = value return nil }