Skip to content

Commit

Permalink
Merge pull request #8 from jstrachan/changes
Browse files Browse the repository at this point in the history
lets use an environment variable to define which variables to watch to     reduce the security requirements on openshift     avoid issues with - in a configmap name
  • Loading branch information
jstrachan authored May 16, 2017
2 parents 0c21626 + 807e560 commit 8b3ea61
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
10 changes: 9 additions & 1 deletion configmapcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ func main() {
}
}

c, err := controller.NewController(kubeClient, oc, restClientConfig, factory.JSONEncoder(), *resyncPeriod, api.NamespaceAll)
watchNamespaces := api.NamespaceAll
currentNamespace := os.Getenv("KUBERNETES_NAMESPACE")
if len(currentNamespace) > 0 {
watchNamespaces = currentNamespace
}
glog.Infof("Watching services in namespaces: `%s`", watchNamespaces)


c, err := controller.NewController(kubeClient, oc, restClientConfig, factory.JSONEncoder(), *resyncPeriod, watchNamespaces)
if err != nil {
glog.Fatalf("%s", err)
}
Expand Down
25 changes: 23 additions & 2 deletions controller/controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"bytes"
"strings"
"time"

Expand Down Expand Up @@ -233,8 +234,7 @@ func updateContainers(containers []api.Container, annotationValue, configMapVers
answer := false
configmaps := strings.Split(annotationValue, ",")
for _, cmNameToUpdate := range configmaps {

configmapEnvar := "FABRIC8_" + strings.ToUpper(strings.Replace(cmNameToUpdate, "-", "_", -1)) + "_CONFIGMAP"
configmapEnvar := "FABRIC8_" + convertToEnvVarName(cmNameToUpdate) + "_CONFIGMAP"

for i := range containers {
envs := containers[i].Env
Expand Down Expand Up @@ -262,3 +262,24 @@ func updateContainers(containers []api.Container, annotationValue, configMapVers
}
return answer
}

// convertToEnvVarName converts the given text into a usable env var
// removing any special chars with '_'
func convertToEnvVarName(text string) string {
var buffer bytes.Buffer
lower := strings.ToUpper(text)
lastCharValid := false
for i := 0; i < len(lower); i++ {
ch := lower[i]
if (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') {
buffer.WriteString(string(ch))
lastCharValid = true
} else {
if lastCharValid {
buffer.WriteString("_")
}
lastCharValid = false
}
}
return buffer.String()
}

0 comments on commit 8b3ea61

Please sign in to comment.