Skip to content

Commit

Permalink
[Fix] Validate when multiple providers are defined
Browse files Browse the repository at this point in the history
resolves #448

 - `controller_test.go` was missing NS1EnabledKey in the cleanup() and configureEnvVar()
 - rewrite depresolver.getEdgeDNSType(), the function now return one more DNSType: `DNSTypeError`
 indicating, more than one providers were resolved. Depresolver generates error if finds that state.
 `DNSTypeNoEdgeDNS` is returned when no provider is recognised and is valid state.
 - cover functionality by depresolver tests.

Signed-off-by: kuritka <kuritka@gmail.com>
  • Loading branch information
kuritka committed Apr 19, 2021
1 parent 10dce1c commit 3586b31
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 41 deletions.
20 changes: 11 additions & 9 deletions controllers/depresolver/depresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,19 @@ func (f LogFormat) String() string {
}

// EdgeDNSType specifies to which edge DNS is k8gb connecting
type EdgeDNSType int
type EdgeDNSType string

const (
// DNSTypeNoEdgeDNS is default DNSType. Is used during integration testing when no edgeDNS provider exists
DNSTypeNoEdgeDNS EdgeDNSType = 1 << iota
DNSTypeNoEdgeDNS EdgeDNSType = "EmptyDNS"
// DNSTypeInfoblox type
DNSTypeInfoblox
DNSTypeInfoblox EdgeDNSType = "Infoblox"
// DNSTypeRoute53 type
DNSTypeRoute53
DNSTypeRoute53 EdgeDNSType = "Route53"
// DNSTypeNS1 type
DNSTypeNS1
DNSTypeNS1 EdgeDNSType = "NS1"
// DNSTypeMultipleProviders type
DNSTypeMultipleProviders EdgeDNSType = "DNSError"
)

// Log configuration
Expand Down Expand Up @@ -131,14 +133,14 @@ type Config struct {
Infoblox Infoblox
// Override the behavior of GSLB in the test environments
Override Override
// route53Enabled hidden. EdgeDNSType defines all enabled Enabled types
route53Enabled bool
// ns1Enabled flag
ns1Enabled bool
// CoreDNSExposed flag
CoreDNSExposed bool
// Log configuration
Log Log
// route53Enabled hidden. EdgeDNSType defines all enabled Enabled types
route53Enabled bool
// ns1Enabled flag
ns1Enabled bool
}

// DependencyResolver resolves configuration for GSLB
Expand Down
31 changes: 19 additions & 12 deletions controllers/depresolver/depresolver_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const (
// ResolveOperatorConfig executes once. It reads operator's configuration
// from environment variables into &Config and validates
func (dr *DependencyResolver) ResolveOperatorConfig() (*Config, error) {
var recognizedDNSTypes []EdgeDNSType
dr.onceConfig.Do(func() {
dr.config = &Config{}
dr.config.ReconcileRequeueSeconds, _ = env.GetEnvAsIntOrFallback(ReconcileRequeueSecondsKey, 30)
Expand All @@ -79,13 +80,13 @@ func (dr *DependencyResolver) ResolveOperatorConfig() (*Config, error) {
dr.config.Log.Level, _ = zerolog.ParseLevel(strings.ToLower(env.GetEnvAsStringOrFallback(LogLevelKey, zerolog.InfoLevel.String())))
dr.config.Log.Format = parseLogOutputFormat(strings.ToLower(env.GetEnvAsStringOrFallback(LogFormatKey, SimpleFormat.String())))
dr.config.Log.NoColor = env.GetEnvAsBoolOrFallback(LogNoColorKey, false)
dr.errorConfig = dr.validateConfig(dr.config)
dr.config.EdgeDNSType = getEdgeDNSType(dr.config)
dr.config.EdgeDNSType, recognizedDNSTypes = getEdgeDNSType(dr.config)
dr.errorConfig = dr.validateConfig(dr.config, recognizedDNSTypes)
})
return dr.config, dr.errorConfig
}

func (dr *DependencyResolver) validateConfig(config *Config) (err error) {
func (dr *DependencyResolver) validateConfig(config *Config, recognisedDNSTypes []EdgeDNSType) (err error) {
if config.Log.Level == zerolog.NoLevel {
return fmt.Errorf("invalid '%s', allowed values ['','%s','%s','%s','%s','%s','%s','%s']", LogLevelKey,
zerolog.TraceLevel, zerolog.DebugLevel, zerolog.InfoLevel, zerolog.WarnLevel, zerolog.FatalLevel,
Expand All @@ -94,6 +95,9 @@ func (dr *DependencyResolver) validateConfig(config *Config) (err error) {
if config.Log.Format == NoFormat {
return fmt.Errorf("invalid '%s', allowed values ['','%s','%s']", LogFormatKey, JSONFormat, SimpleFormat)
}
if config.EdgeDNSType == DNSTypeMultipleProviders {
return fmt.Errorf("several EdgeDNS recognized %s", recognisedDNSTypes)
}
err = field(K8gbNamespaceKey, config.K8gbNamespace).isNotEmpty().matchRegexp(k8sNamespaceRegex).err
if err != nil {
return err
Expand Down Expand Up @@ -163,22 +167,25 @@ func (dr *DependencyResolver) validateConfig(config *Config) (err error) {
return nil
}

// getEdgeDNSType contains logic retrieving EdgeDNSType
func getEdgeDNSType(config *Config) EdgeDNSType {
var t = DNSTypeNoEdgeDNS
// getEdgeDNSType contains logic retrieving EdgeDNSType.
func getEdgeDNSType(config *Config) (EdgeDNSType, []EdgeDNSType) {
recognised := make([]EdgeDNSType, 0)
if config.ns1Enabled {
t |= DNSTypeNS1
recognised = append(recognised, DNSTypeNS1)
}
if config.route53Enabled {
t |= DNSTypeRoute53
recognised = append(recognised, DNSTypeRoute53)
}
if isNotEmpty(config.Infoblox.Host) {
t |= DNSTypeInfoblox
recognised = append(recognised, DNSTypeInfoblox)
}
if t > DNSTypeNoEdgeDNS {
t -= DNSTypeNoEdgeDNS
switch len(recognised) {
case 0:
return DNSTypeNoEdgeDNS, recognised
case 1:
return recognised[0], recognised
}
return t
return DNSTypeMultipleProviders, recognised
}

func parseLogOutputFormat(value string) LogFormat {
Expand Down
61 changes: 50 additions & 11 deletions controllers/depresolver/depresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,19 +629,58 @@ func TestResolveGeoTagWithRepeatingExtGeoTags(t *testing.T) {
arrangeVariablesAndAssert(t, expected, assert.Error)
}

func TestRoute53IsEnabledAndInfobloxIsConfigured(t *testing.T) {
func TestBothRoute53AndInfobloxAreEnabled(t *testing.T) {
// arrange
defer cleanup()
expected := predefinedConfig
expected.route53Enabled = true
expected.EdgeDNSType = DNSTypeRoute53 | DNSTypeInfoblox
expected.Infoblox.Host = "Infoblox.domain"
expected.Infoblox.Version = "0.0.1"
expected.Infoblox.Port = 443
expected.Infoblox.Username = "foo"
expected.Infoblox.Password = "blah"
// act,assert
arrangeVariablesAndAssert(t, expected, assert.NoError)
customConfig := predefinedConfig
customConfig.Infoblox.Host = "Infoblox.domain"
customConfig.Infoblox.Version = "0.0.1"
customConfig.Infoblox.Port = 443
customConfig.Infoblox.Username = "foo"
customConfig.Infoblox.Password = "blah"
configureEnvVar(customConfig)
_ = os.Setenv(Route53EnabledKey, "true")
resolver := NewDependencyResolver()
// act
config, err := resolver.ResolveOperatorConfig()
// assert
assert.Error(t, err)
assert.Equal(t, DNSTypeMultipleProviders, config.EdgeDNSType)
}

func TestRoute53NS1AndInfobloxAreConfigured(t *testing.T) {
// arrange
defer cleanup()
// predefinedConfig has Infoblox preconfigured
configureEnvVar(predefinedConfig)
_ = os.Setenv(Route53EnabledKey, "true")
_ = os.Setenv(NS1EnabledKey, "true")
resolver := NewDependencyResolver()
// act
config, err := resolver.ResolveOperatorConfig()
recognizedEdgeDNSType, recognizedEdgeDNSTypes := getEdgeDNSType(config)
// assert
assert.Error(t, err)
assert.Equal(t, DNSTypeMultipleProviders, config.EdgeDNSType)
assert.Equal(t, recognizedEdgeDNSType, config.EdgeDNSType)
assert.Equal(t, recognizedEdgeDNSTypes, []EdgeDNSType{DNSTypeNS1, DNSTypeRoute53, DNSTypeInfoblox})
}

func TestNoDNSIsConfigured(t *testing.T) {
// arrange
defer cleanup()
customConfig := predefinedConfig
customConfig.Infoblox.Host = ""
configureEnvVar(customConfig)
resolver := NewDependencyResolver()
// act
config, err := resolver.ResolveOperatorConfig()
recognizedEdgeDNSType, recognizedEdgeDNSTypes := getEdgeDNSType(config)
// assert
assert.NoError(t, err)
assert.Equal(t, DNSTypeNoEdgeDNS, config.EdgeDNSType)
assert.Equal(t, recognizedEdgeDNSType, config.EdgeDNSType)
assert.Equal(t, recognizedEdgeDNSTypes, []EdgeDNSType{})
}

func TestRoute53IsDisabledAndInfobloxIsNotConfigured(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions controllers/gslb_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1222,8 +1222,8 @@ func provideSettings(t *testing.T, expected depresolver.Config) (settings testSe
func cleanup() {
for _, s := range []string{depresolver.ReconcileRequeueSecondsKey, depresolver.ClusterGeoTagKey, depresolver.ExtClustersGeoTagsKey,
depresolver.EdgeDNSZoneKey, depresolver.DNSZoneKey, depresolver.EdgeDNSServerKey, depresolver.K8gbNamespaceKey,
depresolver.Route53EnabledKey, depresolver.InfobloxGridHostKey, depresolver.InfobloxVersionKey, depresolver.InfobloxPortKey,
depresolver.InfobloxUsernameKey, depresolver.InfobloxPasswordKey, depresolver.InfobloxHTTPRequestTimeoutKey,
depresolver.Route53EnabledKey, depresolver.NS1EnabledKey, depresolver.InfobloxGridHostKey, depresolver.InfobloxVersionKey,
depresolver.InfobloxPortKey, depresolver.InfobloxUsernameKey, depresolver.InfobloxPasswordKey, depresolver.InfobloxHTTPRequestTimeoutKey,
depresolver.InfobloxHTTPPoolConnectionsKey, depresolver.OverrideWithFakeDNSKey, depresolver.OverrideFakeInfobloxKey,
depresolver.LogLevelKey, depresolver.LogFormatKey, depresolver.LogNoColorKey} {
if os.Unsetenv(s) != nil {
Expand All @@ -1241,6 +1241,7 @@ func configureEnvVar(config depresolver.Config) {
_ = os.Setenv(depresolver.DNSZoneKey, config.DNSZone)
_ = os.Setenv(depresolver.K8gbNamespaceKey, config.K8gbNamespace)
_ = os.Setenv(depresolver.Route53EnabledKey, strconv.FormatBool(config.EdgeDNSType == depresolver.DNSTypeRoute53))
_ = os.Setenv(depresolver.NS1EnabledKey, strconv.FormatBool(config.EdgeDNSType == depresolver.DNSTypeNS1))
_ = os.Setenv(depresolver.InfobloxGridHostKey, config.Infoblox.Host)
_ = os.Setenv(depresolver.InfobloxVersionKey, config.Infoblox.Version)
_ = os.Setenv(depresolver.InfobloxPortKey, strconv.Itoa(config.Infoblox.Port))
Expand Down
12 changes: 5 additions & 7 deletions controllers/providers/dns/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ func NewDNSProviderFactory(client client.Client, config depresolver.Config) (f *
return
}

func (f *ProviderFactory) Provider() (provider IDnsProvider) {
func (f *ProviderFactory) Provider() IDnsProvider {
a := assistant.NewGslbAssistant(f.client, f.config.K8gbNamespace, f.config.EdgeDNSServer)
switch f.config.EdgeDNSType {
case depresolver.DNSTypeNS1:
provider = NewExternalDNS(externalDNSTypeNS1, f.config, a)
return NewExternalDNS(externalDNSTypeNS1, f.config, a)
case depresolver.DNSTypeRoute53:
provider = NewExternalDNS(externalDNSTypeRoute53, f.config, a)
return NewExternalDNS(externalDNSTypeRoute53, f.config, a)
case depresolver.DNSTypeInfoblox:
provider = NewInfobloxDNS(f.config, a)
case depresolver.DNSTypeNoEdgeDNS:
provider = NewEmptyDNS(f.config, a)
return NewInfobloxDNS(f.config, a)
}
return
return NewEmptyDNS(f.config, a)
}

0 comments on commit 3586b31

Please sign in to comment.