Skip to content

Commit

Permalink
fix: Updated the code to copy the prerenderdata into another variable (
Browse files Browse the repository at this point in the history
…#43)

Co-authored-by: Sangeetha Madamanchi <[email protected]>
  • Loading branch information
sangdammad and Sangeetha Madamanchi authored Dec 14, 2023
1 parent 12c64e8 commit 694be3c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches: [master]
types: [completed]
env:
VERSION: v0.0.7
VERSION: v0.0.8
jobs:
deploy:
runs-on: ubuntu-latest
Expand Down
4 changes: 3 additions & 1 deletion controllers/application_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions controllers/application_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}
Expand Down
36 changes: 19 additions & 17 deletions controllers/generic.go
Original file line number Diff line number Diff line change
@@ -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
})
Expand All @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func main() {
}
setupLog.Info("Loading cluster data")

go controllers.LoadPreRenderData()
go controllers.LoadClusterData()

if err = (&controllers.ApplicationReconciler{
Client: mgr.GetClient(),
Expand Down

0 comments on commit 694be3c

Please sign in to comment.