add skip flag for dhcp #38

This commit is contained in:
bakito 2021-11-01 08:56:12 +01:00
parent 7aea49c315
commit ad64fdeda6
No known key found for this signature in database
GPG key ID: FAF93C1C384DD6B4
7 changed files with 79 additions and 40 deletions

View file

@ -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

View file

@ -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"

View file

@ -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"))

View file

@ -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
}

View file

@ -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()

32
pkg/types/features.go Normal file
View file

@ -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")
}
}

View file

@ -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"`