Skip to content

Commit

Permalink
Updates so that parameters must be given to populate values in inline…
Browse files Browse the repository at this point in the history
… scripts. (#25)

* Updates so that parameters must be given to populate values in inline scripts.

* Updates log statement when running envsubst from env
  • Loading branch information
Eagerod authored Sep 27, 2020
1 parent f079dff commit 4738731
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
13 changes: 8 additions & 5 deletions cmd/hope/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/Eagerod/hope/pkg/hope"
)

const MaximumJobDeploymentPollSeconds int = 60

var deployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy a Kubernetes yaml file",
Expand Down Expand Up @@ -113,9 +111,14 @@ var deployCmd = &cobra.Command{
// are likely being populated.
log.Trace(inline)

inline, err := envsubst.GetEnvsubst(inline)
if err != nil {
return err
if len(resource.Parameters) != 0 {
log.Trace("Populating parameters: ", strings.Join(resource.Parameters, ", "))
inline, err = envsubst.GetEnvsubstArgsFromEnv(resource.Parameters, inline)
if err != nil {
return err
}
} else {
log.Trace(resource.Name, " does not have any parameters. Skipping envsubst.")
}

if err := hope.KubectlApplyStdIn(kubectl, inline); err != nil {
Expand Down
11 changes: 11 additions & 0 deletions cmd/hope/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ func patchInvocations() {
return oldEnvSubstArgs(args, str)
}

oldEnvSubstArgsFromEnv := envsubst.GetEnvsubstArgsFromEnv
envsubst.GetEnvsubstArgsFromEnv = func(args []string, str string) (string, error) {
argsKeys := []string{}
for _, key := range args {
argsKeys = append(argsKeys, fmt.Sprintf("$%s", key))
}

log.Debug("echo **(", len(str), " chars)** | envsubst ", strings.Join(argsKeys, ","))
return oldEnvSubstArgsFromEnv(args, str)
}

oldExecKubectl := kubeutil.ExecKubectl
kubeutil.ExecKubectl = func(kubectl *kubeutil.Kubectl, args ...string) error {
log.Debug("kubectl ", strings.Join(args, " "))
Expand Down
11 changes: 6 additions & 5 deletions cmd/hope/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ type BuildSpec struct {
}

type Resource struct {
Name string
File string
Inline string
Build BuildSpec
Job string
Name string
File string
Inline string
Parameters []string
Build BuildSpec
Job string
}

// TODO: Allow jobs to define max retry parameters, or accept them on the
Expand Down
5 changes: 5 additions & 0 deletions hope.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ resources:
# Values passed in through `inline` will be fed through `envsubst` before
# being passed off to kubectl, so any values that are dynamic/secret can be
# passed in through using environment variables.
# Values that envsubst will be required to populate are provided in the
# parameters list.
# If no parameters are provided, envsubst is skipped.
# As is the case with anything else hitting kubectl apply -f, multiple
# objects can be provided by --- separators.
- name: load-balancer-config
Expand All @@ -47,6 +50,8 @@ resources:
creationTimestamp: null
name: memberlist
namespace: metallb-system
parameters:
- METALLB_SYSTEM_MEMBERLIST_SECRET_KEY
# Build and push a docker image to the registry.
# Doesn't include a kubectl command at all, so that can be done in a step
# after a step like this appears.
Expand Down
25 changes: 25 additions & 0 deletions pkg/envsubst/envsubst.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package envsubst

import (
"errors"
"fmt"
"os"
"os/exec"
Expand All @@ -9,6 +10,7 @@ import (

type GetEnvsubstStringFunc func(str string) (string, error)
type GetEnvsubstStringArgsFunc func(args map[string]string, str string) (string, error)
type GetEnvsubstStringArgsFromEnvFunc func(args []string, str string) (string, error)

var GetEnvsubst GetEnvsubstStringFunc = func(str string) (string, error) {
osCmd := exec.Command("envsubst")
Expand Down Expand Up @@ -38,3 +40,26 @@ var GetEnvsubstArgs GetEnvsubstStringArgsFunc = func(args map[string]string, str
outputBytes, err := osCmd.Output()
return string(outputBytes), err
}

var GetEnvsubstArgsFromEnv GetEnvsubstStringArgsFromEnvFunc = func(args []string, str string) (string, error) {
if len(args) == 0 {
return str, nil
}

// If any argument isn't given, return an error
argsKeys := []string{}
for _, key := range args {
_, exists := os.LookupEnv(key)
if !exists {
return "", errors.New(fmt.Sprintf("Failed to find %s in environment.", key))
}
argsKeys = append(argsKeys, fmt.Sprintf("$%s", key))
}

osCmd := exec.Command("envsubst", strings.Join(argsKeys, ","))
osCmd.Stdin = strings.NewReader(str)
osCmd.Stderr = os.Stderr

outputBytes, err := osCmd.Output()
return string(outputBytes), err
}

0 comments on commit 4738731

Please sign in to comment.