From ad64fdeda659c051a5b1da3873768e9634463bfa Mon Sep 17 00:00:00 2001 From: bakito Date: Mon, 1 Nov 2021 08:56:12 +0100 Subject: [PATCH] add skip flag for dhcp #38 --- README.md | 10 ++++++++++ cmd/root.go | 3 +++ cmd/run.go | 5 +++++ pkg/sync/sync.go | 33 ++++++++++++++++----------------- pkg/sync/sync_test.go | 7 ++++++- pkg/types/features.go | 32 ++++++++++++++++++++++++++++++++ pkg/types/types.go | 29 +++++++---------------------- 7 files changed, 79 insertions(+), 40 deletions(-) create mode 100644 pkg/types/features.go diff --git a/README.md b/README.md index 2b01c10..16ef7c7 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ Synchronize [AdGuardHome](https://github.com/AdguardTeam/AdGuardHome) config to - DNS Config - DHCP Config +By default, all features are enabled. Single features can be disabled in the config. + ### Setup of initial instances New AdGuardHome replica instances can be automatically installed if enabled via the config autoSetup. During automatic @@ -104,6 +106,9 @@ services: # - REPLICA1_AUTOSETUP=true # if true, AdGuardHome is automatically initialized. - CRON=*/10 * * * * # run every 10 minutes - RUNONSTART=true + # Configure sync features; by default all features are enabled. + # - FEATURES_DHCP_SERVERCONFIG=false + # - FEATURES_DHCP_STATICLEASES=false ports: - 8080:8080 restart: unless-stopped @@ -154,6 +159,11 @@ api: username: username password: password +# Configure sync features; by default all features are enabled. +features: + dhcp: + serverConfig: false + staticLeases: true ``` ## Log Level diff --git a/cmd/root.go b/cmd/root.go index 474bf07..c37b0e7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,6 +22,9 @@ const ( configAPIUsername = "api.username" configAPIPassword = "api.password" + configFeatureDHCPServerConfig = "features.dhcp.serverConfig" + configFeatureDHCPStaticLeases = "features.dhcp.staticLeases" + configOriginURL = "origin.url" configOriginAPIPath = "origin.apiPath" configOriginUsername = "origin.username" diff --git a/cmd/run.go b/cmd/run.go index a489359..7763ecb 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -37,6 +37,11 @@ func init() { doCmd.PersistentFlags().String("api-password", "", "Sync API password") _ = viper.BindPFlag(configAPIPassword, doCmd.PersistentFlags().Lookup("api-password")) + doCmd.PersistentFlags().Bool("feature-dhcp-server-config", true, "Enable DHCP server config feature") + _ = viper.BindPFlag(configFeatureDHCPServerConfig, doCmd.PersistentFlags().Lookup("feature-dhcp-server-config")) + doCmd.PersistentFlags().Bool("feature-dhcp-static-leases", true, "Enable DHCP server static leases feature") + _ = viper.BindPFlag(configFeatureDHCPStaticLeases, doCmd.PersistentFlags().Lookup("feature-dhcp-static-leases")) + doCmd.PersistentFlags().String("beta", "", "Enable beta features (comma separated list)") _ = viper.BindPFlag(configBeta, doCmd.PersistentFlags().Lookup("beta")) diff --git a/pkg/sync/sync.go b/pkg/sync/sync.go index 0d20f4d..eded458 100644 --- a/pkg/sync/sync.go +++ b/pkg/sync/sync.go @@ -28,6 +28,7 @@ func Sync(cfg *types.Config) error { } l.With("version", version.Version, "build", version.Build).Info("AdGuardHome sync") + cfg.Features.LogDisabled(l) cfg.Origin.AutoSetup = false w := &worker{ @@ -53,12 +54,6 @@ func Sync(cfg *types.Config) error { w.cron.Run() } } - if cfg.RunOnStart { - go func() { - l.Info("Running sync on startup") - w.sync() - }() - } if cfg.API.Port != 0 { if cfg.RunOnStart { go func() { @@ -444,22 +439,26 @@ func (w *worker) syncDNS(oal *types.AccessList, odc *types.DNSConfig, rc client. func (w *worker) syncDHCPServer(osc *types.DHCPServerConfig, rc client.Client) error { sc, err := rc.DHCPServerConfig() - if err != nil { - return err - } - if !sc.Equals(osc) { - if err = rc.SetDHCPServerConfig(osc); err != nil { + if w.cfg.Features.DHCP.ServerConfig { + if err != nil { return err } + if !sc.Equals(osc) { + if err = rc.SetDHCPServerConfig(osc); err != nil { + return err + } + } } - a, r := sc.StaticLeases.Merge(osc.StaticLeases) + if w.cfg.Features.DHCP.StaticLeases { + a, r := sc.StaticLeases.Merge(osc.StaticLeases) - if err = rc.AddDHCPStaticLeases(a...); err != nil { - return err - } - if err = rc.DeleteDHCPStaticLeases(r...); err != nil { - return err + if err = rc.AddDHCPStaticLeases(a...); err != nil { + return err + } + if err = rc.DeleteDHCPStaticLeases(r...); err != nil { + return err + } } return nil } diff --git a/pkg/sync/sync_test.go b/pkg/sync/sync_test.go index f43fb98..036a746 100644 --- a/pkg/sync/sync_test.go +++ b/pkg/sync/sync_test.go @@ -412,7 +412,12 @@ var _ = Describe("Sync", func() { w.cfg = &types.Config{ Origin: types.AdGuardInstance{}, Replica: types.AdGuardInstance{URL: "foo"}, - Beta: "", + Features: types.Features{ + DHCP: types.DHCP{ + ServerConfig: true, + StaticLeases: true, + }, + }, } // origin cl.EXPECT().Host() diff --git a/pkg/types/features.go b/pkg/types/features.go new file mode 100644 index 0000000..22425e8 --- /dev/null +++ b/pkg/types/features.go @@ -0,0 +1,32 @@ +package types + +import ( + "fmt" + "go.uber.org/zap" + "strings" +) + +// Features feature flags +type Features struct { + DHCP DHCP `json:"dhcp" yaml:"dhcp"` +} + +// DHCP features +type DHCP struct { + ServerConfig bool `json:"serverConfig" yaml:"serverConfig"` + StaticLeases bool `json:"staticLeases" yaml:"staticLeases"` +} + +func (f *Features) LogDisabled(l *zap.SugaredLogger) { + var features []string + if !f.DHCP.ServerConfig { + features = append(features, "DHCP.ServerConfig") + } + if !f.DHCP.StaticLeases { + features = append(features, "DHCP.StaticLeases") + } + + if len(features) > 0 { + l.With("features", fmt.Sprintf("[%s]", strings.Join(features, ","))).Info("Disabled features") + } +} diff --git a/pkg/types/types.go b/pkg/types/types.go index f80ad0e..7242694 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -19,14 +19,13 @@ var ( // Config application configuration struct type Config struct { - Origin AdGuardInstance `json:"origin" yaml:"origin"` - Replica AdGuardInstance `json:"replica,omitempty" yaml:"replica,omitempty"` - Replicas []AdGuardInstance `json:"replicas,omitempty" yaml:"replicas,omitempty"` - Cron string `json:"cron,omitempty" yaml:"cron,omitempty"` - RunOnStart bool `json:"runOnStart,omitempty" yaml:"runOnStart,omitempty"` - API API `json:"api,omitempty" yaml:"api,omitempty"` - Beta string `json:"beta,omitempty" yaml:"beta,omitempty"` - enabledBeta map[string]bool `json:"-" yaml:"-"` + Origin AdGuardInstance `json:"origin" yaml:"origin"` + Replica AdGuardInstance `json:"replica,omitempty" yaml:"replica,omitempty"` + Replicas []AdGuardInstance `json:"replicas,omitempty" yaml:"replicas,omitempty"` + Cron string `json:"cron,omitempty" yaml:"cron,omitempty"` + RunOnStart bool `json:"runOnStart,omitempty" yaml:"runOnStart,omitempty"` + API API `json:"api,omitempty" yaml:"api,omitempty"` + Features Features `json:"features,omitempty" yaml:"features,omitempty"` } // API configuration @@ -58,20 +57,6 @@ func (cfg *Config) UniqueReplicas() []AdGuardInstance { return r } -// WithBeta return true if the given beta feature is enabled -func (cfg *Config) WithBeta(name string) bool { - doOnce.Do(func() { - cfg.enabledBeta = make(map[string]bool) - - features := strings.Split(cfg.Beta, ",") - for _, f := range features { - cfg.enabledBeta[strings.ToLower(strings.TrimSpace(f))] = true - } - }) - return cfg.enabledBeta[name] - -} - // AdGuardInstance AdguardHome config instance type AdGuardInstance struct { URL string `json:"url" yaml:"url"`