Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(kuma-cp): remove disabling metrics from kuma-cp.defaults #4894

Merged
merged 1 commit into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}