Skip to content

Commit

Permalink
fixes #17 to support exposing environment variables to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrachan committed Jan 10, 2017
1 parent 5e3be86 commit dbe473c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type createFunctionCmd struct {
watch bool
debug bool

envVars []string

configMaps map[string]*v1.ConfigMap
}

Expand Down Expand Up @@ -85,6 +87,7 @@ func newCreateFunctionCmd() *cobra.Command {
f.StringVarP(&p.source, "source", "s", "", "the source code of the function to create")
f.StringVarP(&p.file, "file", "f", "", "the file name that contains the source code for the function to create")
f.StringVarP(&p.runtime, "runtime", "r", "nodejs", "the runtime to use. e.g. 'nodejs'")
f.StringArrayVarP(&p.envVars, "env", "e", []string{}, "pass one or more environment variables using the form NAME=VALUE")
f.StringVar(&p.kubeConfigPath, "kubeconfig", "", "the directory to look for the kubernetes configuration")
f.StringVar(&p.namespace, "namespace", "", "the namespace to query")
f.BoolVarP(&p.watch, "watch", "w", false, "whether to keep watching the files for changes to the function source code")
Expand Down Expand Up @@ -293,7 +296,7 @@ func (p *createFunctionCmd) applyFile(fileName string) error {
message := "created"
if old != nil {
oldSource := old.Data[funktion.SourceProperty]
if (source == oldSource) {
if (source == oldSource && cm.Data[funktion.EnvVarsProperty] == old.Data[funktion.EnvVarsProperty]) {
// source not changed so lets not update!
return nil
}
Expand Down Expand Up @@ -409,6 +412,9 @@ func (p *createFunctionCmd) createFunctionFromSource(name, source, runtime strin
if p.debug {
data[funktion.DebugProperty] = "true"
}
if len(p.envVars) > 0 {
data[funktion.EnvVarsProperty] = strings.Join(p.envVars, "\n")
}
cm := &v1.ConfigMap{
ObjectMeta: v1.ObjectMeta{
Name: name,
Expand Down
4 changes: 4 additions & 0 deletions examples/envvar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function(context, callback) {
var name = process.env.NAME || "World";
callback(200, "Hello " + name + "!");
};
51 changes: 51 additions & 0 deletions pkg/funktion/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const (
SourceProperty = "source"
// DebugProperty is the data key for whether to enable debugging in a Function ConfigMap
DebugProperty = "debug"
// EnvVarsProperty represents a newline terminated list of NAME=VALUE expressions for environment variables
EnvVarsProperty = "envVars"

// ExposeLabel is the label key to expose services
ExposeLabel = "expose"
Expand Down Expand Up @@ -247,6 +249,8 @@ func makeFunctionDeployment(function *v1.ConfigMap, runtime *v1.ConfigMap, old *
})
}

envVars := parseEnvVars(function.Data[EnvVarsProperty])

mountPath := runtime.Data[SourceMountPathProperty]
if len(mountPath) == 0 {
mountPath = "/funktion"
Expand All @@ -265,6 +269,9 @@ func makeFunctionDeployment(function *v1.ConfigMap, runtime *v1.ConfigMap, old *
ReadOnly: true,
})
}
if len(envVars) > 0 {
applyEnvVars(&podSpec.Containers[i].Env, &envVars)
}
}
if len(deployment.Spec.Template.Spec.Containers[0].Name) == 0 {
deployment.Spec.Template.Spec.Containers[0].Name = "function"
Expand All @@ -273,6 +280,50 @@ func makeFunctionDeployment(function *v1.ConfigMap, runtime *v1.ConfigMap, old *
return &deployment, nil
}

func parseEnvVars(text string) []v1.EnvVar {
answer := []v1.EnvVar{}
if len(text) > 0 {
lines := strings.Split(text, "\n")
for _, line := range lines {
l := strings.TrimSpace(line)
pair := strings.SplitN(l, "=", 2)
if len(pair) != 2 {
fmt.Printf("Ignoring bad environment variable pair. Expecting `NAME=VALUE` but got: %s\n", l)
continue
}
answer = append(answer, v1.EnvVar{
Name: pair[0],
Value: pair[1],
})
}
return answer
}
return answer
}

func applyEnvVars(envVar *[]v1.EnvVar, overrides *[]v1.EnvVar) {
if overrides == nil {
return

}
if *envVar == nil {
*envVar = []v1.EnvVar{}
}
for _, o := range *overrides {
found := false
for _, v := range *envVar {
if v.Name == o.Name {
v.Value = o.Value
v.ValueFrom = nil
found = true
}
}
if !found {
*envVar = append(*envVar, o)
}
}
}

func setDeploymentLabel(deployment *v1beta1.Deployment, key string, value string) {
deployment.Labels[key] = value
if deployment.Spec.Selector == nil {
Expand Down

0 comments on commit dbe473c

Please sign in to comment.