Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disable via regex in chaosduck #2117

Merged
merged 1 commit into from
May 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions leaderelection/chaosduck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"errors"
"flag"
"log"
"regexp"
"strings"
"time"

Expand All @@ -44,14 +45,16 @@ import (
type components map[string]sets.String

var (
disabledComponents kflag.StringSet
tributePeriod time.Duration = 20 * time.Second
tributeFactor = 2.0
disabledComponents kflag.StringSet
disabledComponentsRegex kflag.StringSet
tributePeriod time.Duration = 20 * time.Second
tributeFactor = 2.0
)

func init() {
// Note that we don't explicitly call flag.Parse() because ParseAndGetConfigOrDie below does this already.
flag.Var(&disabledComponents, "disable", "A repeatable flag to disable chaos for certain components.")
flag.Var(&disabledComponentsRegex, "disableRegex", "A repeatable flag to disable chaos for components matching one of the passed regexes.")
flag.DurationVar(&tributePeriod, "period", tributePeriod, "How frequently to terminate a leader pod per component (this is the base duration used with the jitter factor from -factor).")
flag.Float64Var(&tributeFactor, "factor", tributeFactor, "The jitter factor to apply to the period.")
}
Expand Down Expand Up @@ -116,11 +119,25 @@ func quack(ctx context.Context, kc kubernetes.Interface, component string, leade
return kc.CoreV1().Pods(system.Namespace()).Delete(ctx, tribute, metav1.DeleteOptions{})
}

// matchesAny returns true if any of the given regexes matches the given string.
func matchesAny(regexes []*regexp.Regexp, str string) bool {
for _, re := range regexes {
if re.MatchString(str) {
return true
}
}
return false
}

func main() {
ctx, _ := injection.EnableInjectionOrDie(signals.NewContext(), nil)

kc := kubeclient.Get(ctx)

regexes := make([]*regexp.Regexp, 0, len(disabledComponentsRegex.Value))
for re := range disabledComponentsRegex.Value {
regexes = append(regexes, regexp.MustCompile(re))
}

// Until we are shutdown, build up an index of components and kill
// of a leader at the specified frequency.
wait.JitterUntilWithContext(ctx, func(ctx context.Context) {
Expand All @@ -132,9 +149,10 @@ func main() {

eg, ctx := errgroup.WithContext(ctx)
for name, leaders := range components {
if disabledComponents.Value.Has(name) {
if disabledComponents.Value.Has(name) || matchesAny(regexes, name) {
continue
}

name, leaders := name, leaders
eg.Go(func() error {
return quack(ctx, kc, name, leaders)
Expand Down