Skip to content

Commit

Permalink
cleanup: remove disabling metrics from kuma-cp.defaults
Browse files Browse the repository at this point in the history
These are not optional for a while. Add a test to check to kuma-cp.defaults.yaml
doesn't not contain extra configs

Fix #4854

Signed-off-by: Charly Molter <charly.molter@konghq.com>
  • Loading branch information
lahabana committed Aug 24, 2022
1 parent 98e861e commit f4e643e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
26 changes: 14 additions & 12 deletions pkg/config/app/kuma-cp/default_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@ import (

var _ = Describe("Default config", func() {

var backupEnvVars []string

BeforeEach(func() {
backupEnvVars = os.Environ()
It("should be check against the kuma-cp.defaults.yaml file", func() {
backupEnvVars := os.Environ()
os.Clearenv()
})
AfterEach(func() {
for _, envVar := range backupEnvVars {
parts := strings.SplitN(envVar, "=", 2)
Expect(os.Setenv(parts[0], parts[1])).To(Succeed())
}
})
defer func() {
for _, envVar := range backupEnvVars {
parts := strings.SplitN(envVar, "=", 2)
Expect(os.Setenv(parts[0], parts[1])).To(Succeed())
}

It("should be check against the kuma-cp.defaults.yaml file", func() {
}()
// given
cfg := Config{}

Expand All @@ -36,4 +32,10 @@ var _ = Describe("Default config", func() {
Expect(err).ToNot(HaveOccurred())
Expect(DefaultConfig()).To(Equal(cfg), "The default config generated by Kuma and kuma-cp.defaults.yaml config file are different. Please update the kuma-cp.defaults.yaml file.")
})

It("kuma-cp.defaults.yaml should not have extra keys", func() {
cfg := Config{}
err := config.LoadWithOption("kuma-cp.defaults.yaml", &cfg, true, false, false)
Expect(err).ToNot(HaveOccurred())
})
})
4 changes: 0 additions & 4 deletions pkg/config/app/kuma-cp/kuma-cp.defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,11 @@ defaults:
# Metrics configuration
metrics:
dataplane:
# Enables collecting metrics from Dataplane
enabled: true # ENV: KUMA_METRICS_DATAPLANE_ENABLED
# How many latest subscriptions will be stored in DataplaneInsight object, if equals 0 then unlimited
subscriptionLimit: 2 # ENV: KUMA_METRICS_DATAPLANE_SUBSCRIPTION_LIMIT
# How long data plane proxy can stay Online without active xDS connection
idleTimeout: 5m # ENV: KUMA_METRICS_DATAPLANE_IDLE_TIMEOUT
zone:
# Enables collecting metrics from Zone
enabled: true # ENV: KUMA_METRICS_ZONE_ENABLED
# How many latest subscriptions will be stored in ZoneInsights object, if equals 0 then unlimited
subscriptionLimit: 10 # ENV: KUMA_METRICS_ZONE_SUBSCRIPTION_LIMIT
# How long zone can stay Online without active KDS connection
Expand Down
44 changes: 23 additions & 21 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,41 @@ import (
)

func Load(file string, cfg Config) error {
return LoadWithOption(file, cfg, false, true, true)
}

func LoadWithOption(file string, cfg Config, strict bool, includeEnv bool, validate bool) error {
if file == "" {
core.Log.Info("Skipping reading config from file")
} else if err := loadFromFile(file, cfg); err != nil {
} else if err := loadFromFile(file, cfg, strict); err != nil {
return err
}

if err := loadFromEnv(cfg); err != nil {
return err
if includeEnv {
if err := envconfig.Process("", cfg); err != nil {
return err
}
}

if err := cfg.Validate(); err != nil {
return errors.Wrapf(err, "Invalid configuration")
if validate {
if err := cfg.Validate(); err != nil {
return errors.Wrapf(err, "Invalid configuration")
}
}
return nil
}

func loadFromFile(file string, cfg Config) error {
if !fileExists(file) {
func loadFromFile(file string, cfg Config, strict bool) error {
if _, err := os.Stat(file); err != nil {
return errors.Errorf("Failed to access configuration file %q", file)
}
if contents, err := os.ReadFile(file); err != nil {
contents, err := os.ReadFile(file)
if err != nil {
return errors.Wrapf(err, "Failed to read configuration from file %q", file)
} else if err := yaml.Unmarshal(contents, cfg); err != nil {
return errors.Wrapf(err, "Failed to parse configuration from file %q", file)
}
return nil
}

func loadFromEnv(config Config) error {
return envconfig.Process("", config)
}

func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
if strict {
err = yaml.UnmarshalStrict(contents, cfg)
} else {
err = yaml.Unmarshal(contents, cfg)
}
return errors.Wrapf(err, "Failed to parse configuration from file %q", file)
}

0 comments on commit f4e643e

Please sign in to comment.