From 3ce9edb7bb1afb0950776dfbf78f5df47190b960 Mon Sep 17 00:00:00 2001 From: Nathaniel Cook Date: Mon, 22 May 2017 13:54:37 -0600 Subject: [PATCH] fix bug where env overrides stopped working for the k8s config section --- CHANGELOG.md | 2 ++ pipeline/k8s_autoscale.go | 1 - server/config.go | 32 +++++++++++++++++++++++++++++--- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e67d25a9f..572674fd2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ # Bugfixes +- [#1396](https://github.com/influxdata/kapacitor/pull/1396): Fix broken ENV var config overrides for the kubernetes section. + ## v1.3.0-rc4 [2017-05-19] - [#1379](https://github.com/influxdata/kapacitor/issues/1379): Copy batch points slice before modification, fixes potential panics and data corruption. diff --git a/pipeline/k8s_autoscale.go b/pipeline/k8s_autoscale.go index 63f2dcde8..b0ddf98dd 100644 --- a/pipeline/k8s_autoscale.go +++ b/pipeline/k8s_autoscale.go @@ -144,7 +144,6 @@ type K8sAutoscaleNode struct { func newK8sAutoscaleNode(e EdgeType) *K8sAutoscaleNode { k := &K8sAutoscaleNode{ chainnode: newBasicChainNode("k8s_autoscale", e, StreamEdge), - Cluster: "default", Min: 1, Kind: client.DeploymentsKind, NamespaceTag: DefaultNamespaceTag, diff --git a/server/config.go b/server/config.go index c1111491d..7a9db47ec 100644 --- a/server/config.go +++ b/server/config.go @@ -102,7 +102,7 @@ type Config struct { Triton []triton.Config `toml:"triton" override:"triton,element-key=id"` // Third-party integrations - Kubernetes k8s.Configs `toml:"kubernetes" override:"kubernetes,element-key=id"` + Kubernetes k8s.Configs `toml:"kubernetes" override:"kubernetes,element-key=id" env-config:"implicit-index"` Reporting reporting.Config `toml:"reporting"` Stats stats.Config `toml:"stats"` @@ -498,6 +498,13 @@ func (c *Config) applyEnvOverrides(prefix string, fieldDesc string, spec reflect return nil } +const ( + // envConfigTag is a struct tag key for specifying information to the apply env overrides configuration process. + envConfigTag = "env-config" + // implicitIndexTag is the name of the value of an env-config tag that instructs the process to allow implicit 0 indexes. + implicitIndexTag = "implicit-index" +) + func (c *Config) applyEnvOverridesToStruct(prefix string, s reflect.Value) error { typeOfSpec := s.Type() for i := 0; i < s.NumField(); i++ { @@ -506,7 +513,8 @@ func (c *Config) applyEnvOverridesToStruct(prefix string, s reflect.Value) error configName := typeOfSpec.Field(i).Tag.Get("toml") // Replace hyphens with underscores to avoid issues with shells configName = strings.Replace(configName, "-", "_", -1) - fieldName := typeOfSpec.Field(i).Name + fieldType := typeOfSpec.Field(i) + fieldName := fieldType.Name // Skip any fields that we cannot set if f.CanSet() || f.Kind() == reflect.Slice { @@ -520,7 +528,25 @@ func (c *Config) applyEnvOverridesToStruct(prefix string, s reflect.Value) error // If the type is s slice, apply to each using the index as a suffix // e.g. GRAPHITE_0 if f.Kind() == reflect.Slice || f.Kind() == reflect.Array { - for i := 0; i < f.Len(); i++ { + // Determine if the field supports implicit indexes. + implicitIndex := false + fieldEnvConfigTags := strings.Split(fieldType.Tag.Get(envConfigTag), ",") + for _, s := range fieldEnvConfigTags { + if s == implicitIndexTag { + implicitIndex = true + break + } + } + + l := f.Len() + for i := 0; i < l; i++ { + // Also support an implicit 0 index, if there is only one entry and the slice supports it. + // e.g. KAPACITOR_KUBERNETES_ENABLED=true + if implicitIndex && l == 1 { + if err := c.applyEnvOverrides(key, fieldName, f.Index(i)); err != nil { + return err + } + } if err := c.applyEnvOverrides(fmt.Sprintf("%s_%d", key, i), fieldName, f.Index(i)); err != nil { return err }