diff --git a/.github/workflows/publish-latest.yaml b/.github/workflows/publish-latest.yaml index 197536d..d94df69 100644 --- a/.github/workflows/publish-latest.yaml +++ b/.github/workflows/publish-latest.yaml @@ -5,7 +5,7 @@ on: branches: [master] types: [completed] env: - VERSION: v0.0.7 + VERSION: v0.0.8 jobs: deploy: runs-on: ubuntu-latest diff --git a/controllers/application_controller.go b/controllers/application_controller.go index f23fc59..4bd0435 100644 --- a/controllers/application_controller.go +++ b/controllers/application_controller.go @@ -402,13 +402,15 @@ func (r *ApplicationReconciler) renderValues(application *v1.Application) error } } } + preRenderData := GetPreRenderData(application.GetLabels()) + for key, value := range values { buf := new(bytes.Buffer) tmpl, err := template.New("properties").Option("missingkey=error").Delims(leftDelimiter, rightDelimiter).Parse(value) if err != nil { return err } - if err = tmpl.Execute(buf, GetPreRenderData(application.GetLabels())); err != nil { + if err = tmpl.Execute(buf, preRenderData); err != nil { return err } values[key] = buf.String() diff --git a/controllers/application_controller_test.go b/controllers/application_controller_test.go index bc4276c..21e2406 100644 --- a/controllers/application_controller_test.go +++ b/controllers/application_controller_test.go @@ -155,13 +155,13 @@ var pod = &v1.Pod{ } func LoadTestPrerenderData() { - preRenderData["cluster"] = map[string]string{ + clusterData["cluster"] = map[string]string{ "cluster": "some-cluster", "region": "us-west-2", "account": "1234", "segment": "some-segment", } - preRenderData["egdata"] = map[string]string{ + clusterData["egdata"] = map[string]string{ "environment": "test", } } diff --git a/controllers/generic.go b/controllers/generic.go index 1b537d0..b08a701 100644 --- a/controllers/generic.go +++ b/controllers/generic.go @@ -1,24 +1,22 @@ package controllers import ( - "strings" - v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/informers" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/cache" ctrl "sigs.k8s.io/controller-runtime" + "strings" ) -var preRenderData = make(map[string]map[string]string) - -const expediaType = "k8s.expediagroup.com" +var clusterData = make(map[string]map[string]string) const ReferenceLabel = "overwhelm.expediagroup.com/render-values-source" const ApplicationKey = "application" +const ExpediaType = "k8s.expediagroup.com" -func LoadPreRenderData() { +func LoadClusterData() { labelOptions := informers.WithTweakListOptions(func(opts *metav1.ListOptions) { opts.LabelSelector = ReferenceLabel }) @@ -28,35 +26,39 @@ func LoadPreRenderData() { defer close(stop) informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ AddFunc: func(cm interface{}) { - addToPrerenderData(cm.(*v1.ConfigMap)) + AddToClusterData(cm.(*v1.ConfigMap)) }, UpdateFunc: func(oldCM interface{}, cm interface{}) { - addToPrerenderData(cm.(*v1.ConfigMap)) + AddToClusterData(cm.(*v1.ConfigMap)) }, DeleteFunc: func(cm interface{}) { - delete(preRenderData, cm.(*v1.ConfigMap).Labels[ReferenceLabel]) + delete(clusterData, cm.(*v1.ConfigMap).Labels[ReferenceLabel]) }, }) informer.Run(stop) } -func addToPrerenderData(cm *v1.ConfigMap) { +func AddToClusterData(cm *v1.ConfigMap) { for k, v := range cm.Data { - if preRenderData[cm.Labels[ReferenceLabel]] == nil { - preRenderData[cm.Labels[ReferenceLabel]] = make(map[string]string) + if clusterData[cm.Labels[ReferenceLabel]] == nil { + clusterData[cm.Labels[ReferenceLabel]] = make(map[string]string) } - preRenderData[cm.Labels[ReferenceLabel]][k] = v + clusterData[cm.Labels[ReferenceLabel]][k] = v } } -func GetPreRenderData(appLabels map[string]string) map[string]map[string]string { - for label, labelValue := range appLabels { - if strings.HasPrefix(label, expediaType) { +func GetPreRenderData(labels map[string]string) map[string]map[string]string { + preRenderData := make(map[string]map[string]string) + for key, value := range clusterData { + preRenderData[key] = value + } + for label, labelValue := range labels { + if strings.HasPrefix(label, ExpediaType) { //Initialise the map, even if there is a single label matching criteria if preRenderData[ApplicationKey] == nil { preRenderData[ApplicationKey] = make(map[string]string) } - trimmedLabel := strings.Trim(strings.TrimPrefix(label, expediaType), "/") + trimmedLabel := strings.Trim(strings.TrimPrefix(label, ExpediaType), "/") preRenderData[ApplicationKey][trimmedLabel] = labelValue } } diff --git a/main.go b/main.go index c77727b..4d3d224 100644 --- a/main.go +++ b/main.go @@ -79,7 +79,7 @@ func main() { } setupLog.Info("Loading cluster data") - go controllers.LoadPreRenderData() + go controllers.LoadClusterData() if err = (&controllers.ApplicationReconciler{ Client: mgr.GetClient(),