mirror of
https://github.com/stakater/Reloader.git
synced 2026-08-23 22:16:45 +00:00
refactoring
This commit is contained in:
@@ -182,6 +182,26 @@ func BindFlags(fs *pflag.FlagSet, cfg *Config) {
|
||||
"secret-annotation", cfg.Annotations.SecretReload,
|
||||
"Annotation to detect changes in secrets, specified by name",
|
||||
)
|
||||
fs.String(
|
||||
"configmap-exclude-annotation", cfg.Annotations.ConfigmapExclude,
|
||||
"Annotation to exclude named configmaps from triggering reloads",
|
||||
)
|
||||
fs.String(
|
||||
"secret-exclude-annotation", cfg.Annotations.SecretExclude,
|
||||
"Annotation to exclude named secrets from triggering reloads",
|
||||
)
|
||||
fs.String(
|
||||
"secretproviderclass-auto-annotation", cfg.Annotations.SecretProviderClassAuto,
|
||||
"Annotation to detect changes in secret provider classes (CSI)",
|
||||
)
|
||||
fs.String(
|
||||
"secretproviderclass-annotation", cfg.Annotations.SecretProviderClassReload,
|
||||
"Annotation to detect changes in secret provider classes (CSI), specified by name",
|
||||
)
|
||||
fs.String(
|
||||
"secretproviderclass-exclude-annotation", cfg.Annotations.SecretProviderClassExclude,
|
||||
"Annotation to exclude named secret provider classes (CSI) from triggering reloads",
|
||||
)
|
||||
fs.String(
|
||||
"auto-search-annotation", cfg.Annotations.Search,
|
||||
"Annotation to detect changes in configmaps or secrets tagged with special match annotation",
|
||||
@@ -190,6 +210,10 @@ func BindFlags(fs *pflag.FlagSet, cfg *Config) {
|
||||
"search-match-annotation", cfg.Annotations.Match,
|
||||
"Annotation to mark secrets or configmaps to match the search",
|
||||
)
|
||||
fs.String(
|
||||
"ignore-annotation", cfg.Annotations.Ignore,
|
||||
"Annotation to ignore changes on watched resources",
|
||||
)
|
||||
fs.String(
|
||||
"pause-deployment-annotation", cfg.Annotations.PausePeriod,
|
||||
"Annotation to define the time period to pause a deployment after a configmap/secret change",
|
||||
@@ -289,23 +313,17 @@ func ApplyFlags(cfg *Config) error {
|
||||
cfg.Annotations.SecretAuto = v.GetString("secret-auto-annotation")
|
||||
cfg.Annotations.ConfigmapReload = v.GetString("configmap-annotation")
|
||||
cfg.Annotations.SecretReload = v.GetString("secret-annotation")
|
||||
cfg.Annotations.ConfigmapExclude = v.GetString("configmap-exclude-annotation")
|
||||
cfg.Annotations.SecretExclude = v.GetString("secret-exclude-annotation")
|
||||
cfg.Annotations.SecretProviderClassAuto = v.GetString("secretproviderclass-auto-annotation")
|
||||
cfg.Annotations.SecretProviderClassReload = v.GetString("secretproviderclass-annotation")
|
||||
cfg.Annotations.SecretProviderClassExclude = v.GetString("secretproviderclass-exclude-annotation")
|
||||
cfg.Annotations.Search = v.GetString("auto-search-annotation")
|
||||
cfg.Annotations.Match = v.GetString("search-match-annotation")
|
||||
cfg.Annotations.Ignore = v.GetString("ignore-annotation")
|
||||
cfg.Annotations.PausePeriod = v.GetString("pause-deployment-annotation")
|
||||
cfg.Annotations.PausedAt = v.GetString("pause-deployment-time-annotation")
|
||||
|
||||
// SecretProviderClass annotations have no dedicated CLI flag (parity with
|
||||
// master); keep the configured defaults.
|
||||
if cfg.Annotations.SecretProviderClassAuto == "" {
|
||||
cfg.Annotations.SecretProviderClassAuto = DefaultAnnotations().SecretProviderClassAuto
|
||||
}
|
||||
if cfg.Annotations.SecretProviderClassReload == "" {
|
||||
cfg.Annotations.SecretProviderClassReload = DefaultAnnotations().SecretProviderClassReload
|
||||
}
|
||||
if cfg.Annotations.SecretProviderClassExclude == "" {
|
||||
cfg.Annotations.SecretProviderClassExclude = DefaultAnnotations().SecretProviderClassExclude
|
||||
}
|
||||
|
||||
// Alerting
|
||||
cfg.Alerting.Enabled = v.GetBool("alert-on-reload")
|
||||
cfg.Alerting.WebhookURL = v.GetString("alert-webhook-url")
|
||||
|
||||
@@ -55,8 +55,14 @@ func TestBindFlags(t *testing.T) {
|
||||
"secret-auto-annotation",
|
||||
"configmap-annotation",
|
||||
"secret-annotation",
|
||||
"configmap-exclude-annotation",
|
||||
"secret-exclude-annotation",
|
||||
"secretproviderclass-auto-annotation",
|
||||
"secretproviderclass-annotation",
|
||||
"secretproviderclass-exclude-annotation",
|
||||
"auto-search-annotation",
|
||||
"search-match-annotation",
|
||||
"ignore-annotation",
|
||||
"pause-deployment-annotation",
|
||||
"pause-deployment-time-annotation",
|
||||
"watch-namespace",
|
||||
@@ -153,6 +159,131 @@ func TestBindFlags_CustomValues(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyFlags_SecretProviderClassAnnotations(t *testing.T) {
|
||||
// Defaults are preserved when the flags are not provided.
|
||||
resetViper()
|
||||
cfg := NewDefault()
|
||||
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||
BindFlags(fs, cfg)
|
||||
if err := fs.Parse(nil); err != nil {
|
||||
t.Fatalf("Parse() error = %v", err)
|
||||
}
|
||||
if err := ApplyFlags(cfg); err != nil {
|
||||
t.Fatalf("ApplyFlags() error = %v", err)
|
||||
}
|
||||
defaults := DefaultAnnotations()
|
||||
if cfg.Annotations.SecretProviderClassAuto != defaults.SecretProviderClassAuto {
|
||||
t.Errorf("SecretProviderClassAuto = %q, want default %q", cfg.Annotations.SecretProviderClassAuto, defaults.SecretProviderClassAuto)
|
||||
}
|
||||
if cfg.Annotations.SecretProviderClassReload != defaults.SecretProviderClassReload {
|
||||
t.Errorf("SecretProviderClassReload = %q, want default %q", cfg.Annotations.SecretProviderClassReload, defaults.SecretProviderClassReload)
|
||||
}
|
||||
if cfg.Annotations.SecretProviderClassExclude != defaults.SecretProviderClassExclude {
|
||||
t.Errorf("SecretProviderClassExclude = %q, want default %q", cfg.Annotations.SecretProviderClassExclude, defaults.SecretProviderClassExclude)
|
||||
}
|
||||
|
||||
// Custom values are applied from the flags.
|
||||
resetViper()
|
||||
cfg = NewDefault()
|
||||
fs = pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||
BindFlags(fs, cfg)
|
||||
args := []string{
|
||||
"--secretproviderclass-auto-annotation=spc.example.com/auto",
|
||||
"--secretproviderclass-annotation=spc.example.com/reload",
|
||||
"--secretproviderclass-exclude-annotation=spc.example.com/exclude",
|
||||
}
|
||||
if err := fs.Parse(args); err != nil {
|
||||
t.Fatalf("Parse() error = %v", err)
|
||||
}
|
||||
if err := ApplyFlags(cfg); err != nil {
|
||||
t.Fatalf("ApplyFlags() error = %v", err)
|
||||
}
|
||||
if cfg.Annotations.SecretProviderClassAuto != "spc.example.com/auto" {
|
||||
t.Errorf("SecretProviderClassAuto = %q, want %q", cfg.Annotations.SecretProviderClassAuto, "spc.example.com/auto")
|
||||
}
|
||||
if cfg.Annotations.SecretProviderClassReload != "spc.example.com/reload" {
|
||||
t.Errorf("SecretProviderClassReload = %q, want %q", cfg.Annotations.SecretProviderClassReload, "spc.example.com/reload")
|
||||
}
|
||||
if cfg.Annotations.SecretProviderClassExclude != "spc.example.com/exclude" {
|
||||
t.Errorf("SecretProviderClassExclude = %q, want %q", cfg.Annotations.SecretProviderClassExclude, "spc.example.com/exclude")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyFlags_ExcludeAnnotations(t *testing.T) {
|
||||
// Defaults are preserved when the flags are not provided.
|
||||
resetViper()
|
||||
cfg := NewDefault()
|
||||
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||
BindFlags(fs, cfg)
|
||||
if err := fs.Parse(nil); err != nil {
|
||||
t.Fatalf("Parse() error = %v", err)
|
||||
}
|
||||
if err := ApplyFlags(cfg); err != nil {
|
||||
t.Fatalf("ApplyFlags() error = %v", err)
|
||||
}
|
||||
defaults := DefaultAnnotations()
|
||||
if cfg.Annotations.ConfigmapExclude != defaults.ConfigmapExclude {
|
||||
t.Errorf("ConfigmapExclude = %q, want default %q", cfg.Annotations.ConfigmapExclude, defaults.ConfigmapExclude)
|
||||
}
|
||||
if cfg.Annotations.SecretExclude != defaults.SecretExclude {
|
||||
t.Errorf("SecretExclude = %q, want default %q", cfg.Annotations.SecretExclude, defaults.SecretExclude)
|
||||
}
|
||||
|
||||
// Custom values are applied from the flags.
|
||||
resetViper()
|
||||
cfg = NewDefault()
|
||||
fs = pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||
BindFlags(fs, cfg)
|
||||
args := []string{
|
||||
"--configmap-exclude-annotation=cm.example.com/exclude",
|
||||
"--secret-exclude-annotation=sec.example.com/exclude",
|
||||
}
|
||||
if err := fs.Parse(args); err != nil {
|
||||
t.Fatalf("Parse() error = %v", err)
|
||||
}
|
||||
if err := ApplyFlags(cfg); err != nil {
|
||||
t.Fatalf("ApplyFlags() error = %v", err)
|
||||
}
|
||||
if cfg.Annotations.ConfigmapExclude != "cm.example.com/exclude" {
|
||||
t.Errorf("ConfigmapExclude = %q, want %q", cfg.Annotations.ConfigmapExclude, "cm.example.com/exclude")
|
||||
}
|
||||
if cfg.Annotations.SecretExclude != "sec.example.com/exclude" {
|
||||
t.Errorf("SecretExclude = %q, want %q", cfg.Annotations.SecretExclude, "sec.example.com/exclude")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyFlags_IgnoreAnnotation(t *testing.T) {
|
||||
// Default is preserved when the flag is not provided.
|
||||
resetViper()
|
||||
cfg := NewDefault()
|
||||
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||
BindFlags(fs, cfg)
|
||||
if err := fs.Parse(nil); err != nil {
|
||||
t.Fatalf("Parse() error = %v", err)
|
||||
}
|
||||
if err := ApplyFlags(cfg); err != nil {
|
||||
t.Fatalf("ApplyFlags() error = %v", err)
|
||||
}
|
||||
if cfg.Annotations.Ignore != DefaultAnnotations().Ignore {
|
||||
t.Errorf("Ignore = %q, want default %q", cfg.Annotations.Ignore, DefaultAnnotations().Ignore)
|
||||
}
|
||||
|
||||
// Custom value is applied from the flag.
|
||||
resetViper()
|
||||
cfg = NewDefault()
|
||||
fs = pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||
BindFlags(fs, cfg)
|
||||
if err := fs.Parse([]string{"--ignore-annotation=my.company.com/reloader-ignore"}); err != nil {
|
||||
t.Fatalf("Parse() error = %v", err)
|
||||
}
|
||||
if err := ApplyFlags(cfg); err != nil {
|
||||
t.Fatalf("ApplyFlags() error = %v", err)
|
||||
}
|
||||
if cfg.Annotations.Ignore != "my.company.com/reloader-ignore" {
|
||||
t.Errorf("Ignore = %q, want %q", cfg.Annotations.Ignore, "my.company.com/reloader-ignore")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyFlags_BooleanStrings(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in New Issue
Block a user