Skip to content

Commit

Permalink
add logic for treating integers as string when inferred from function…
Browse files Browse the repository at this point in the history
… args
  • Loading branch information
logandavies181 committed Apr 28, 2023
1 parent 31bf46c commit 60257bb
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 34 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/logandavies181/kustomize-krm-envsubst
go 1.19

require (
github.com/logandavies181/envsubst v0.1.0
github.com/logandavies181/envsubst v0.2.0
github.com/logandavies181/go-buildversion v0.1.0
sigs.k8s.io/kustomize/kyaml v0.14.1
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/logandavies181/envsubst v0.1.0 h1:r/y5QwzcMw26kchMjzKYtq/DkAkGwuv7qSp47Lmb4OM=
github.com/logandavies181/envsubst v0.1.0/go.mod h1:5vxEu24ve2msUFvdG9LsDa1dK8HGFcEElF8QJb1r/Vg=
github.com/logandavies181/envsubst v0.2.0 h1:S7rgFU+mbXezSXgMhuoJZ7PCprlPWKA+jEI9gnkoa2E=
github.com/logandavies181/envsubst v0.2.0/go.mod h1:HwAyyeMuxpQGw/KOPAf8jIh6p4mt1Lq6A/AcUyUHUUk=
github.com/logandavies181/go-buildversion v0.1.0 h1:CtmtQ7KBPyN9RQVrB0bvuCgqPCHPv6FPxTCL+a+Xb+4=
github.com/logandavies181/go-buildversion v0.1.0/go.mod h1:c2dv/990RvPOTBOs2GHT8R/WIC79U/FtnCTRNd9dh+Q=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
Expand Down
63 changes: 44 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"os"
"strconv"
"strings"

"github.com/logandavies181/envsubst"
Expand Down Expand Up @@ -54,15 +53,15 @@ func (c Config) walkSequenceNode(in *yaml.RNode) error {
}

func (c Config) walkMapNode(in *yaml.MapNode) error {
if key, err := in.Key.String(); err == nil {
if key == "annotations\n" || key == "labels\n" {
return in.Value.VisitFields(c.walkMetadataNode)
}
} else {
key, err := in.Key.String()
if err != nil {
return err
}
if key == "annotations\n" || key == "labels\n" {
return in.Value.VisitFields(c.walkMetadataNode)
}

_, err := c.Filter(in.Value)
_, err = c.Filter(in.Value)
if err != nil {
return err
}
Expand Down Expand Up @@ -101,21 +100,23 @@ func (c Config) processScalarNode(in *yaml.RNode, alwaysString bool) error {
substed = `""`
}

substed = strings.TrimSuffix(substed, "\n")

if alwaysString {
substed = strings.TrimSuffix(substed, "\n")
if _, strconvErr := strconv.Atoi(string(substed[0])); strconvErr == nil {
if yaml.IsIdxNumber(substed) {
substed = `"` + substed + `"`
}
}

strNode, err := yaml.Parse(substed)
node, err := yaml.Parse(substed)
if err != nil {
fmt.Fprintln(os.Stderr, substed)
fmt.Fprintln(os.Stderr, len(substed))
return fmt.Errorf("Could not parse node after envsubsting: %v", err)
}

_, err = in.Pipe(yaml.Set(strNode))
if node.YNode().Kind != yaml.ScalarNode {
return fmt.Errorf("Invalid output: `%s` did not evaluate to a scalar", str)
}

_, err = in.Pipe(yaml.Set(node))

return err
}
Expand Down Expand Up @@ -148,6 +149,30 @@ func (c Config) Filter(in *yaml.RNode) (*yaml.RNode, error) {
}
}

// environmentSubstitute performs an environment substitution. It
// also inspects the args to infer if the value is intended to be
// a string that represents an integer. If so, it will wrap it in
// quotes
func environmentSubstitute(s string, nodeInfo envsubst.NodeInfo) string {
looksLikeQuotedInt := false
for _, arg := range nodeInfo.Args() {
if len(arg) > 2 && arg[0] == byte('"') && arg[0] == arg[len(arg)-1] {
looksLikeQuotedInt = yaml.IsIdxNumber(arg[1 : len(arg)-1])
}
}

mapped := os.Getenv(s)
ret := nodeInfo.Result(mapped)

// check if args look like quoted integers
// AND ( if the function has triggered OR the function had no effect )
if looksLikeQuotedInt && (!contains(nodeInfo.Args(), ret) || ret == mapped) {
ret = fmt.Sprintf(`"%s"`, ret)
}

return ret
}

func main() {
config := new(Config)

Expand All @@ -169,24 +194,24 @@ func main() {
}

fn := func(items []*yaml.RNode) ([]*yaml.RNode, error) {
config.envMapping = func(str string, nodeInfo envsubst.NodeInfo) (string, bool) {
config.envMapping = func(s string, nodeInfo envsubst.NodeInfo) (string, bool) {
// IncludedVars and ExcludedVars are mutually exclusive
// IncludedVars takes precedent
// TODO: readme

if len(config.IncludedVars) == 0 {
if contains(config.ExcludedVars, str) {
if contains(config.ExcludedVars, s) {
return nodeInfo.Orig(), false
}

return str, true
return environmentSubstitute(s, nodeInfo), false
}

if !contains(config.IncludedVars, str) {
if !contains(config.IncludedVars, s) {
return nodeInfo.Orig(), false
}

return str, true
return environmentSubstitute(s, nodeInfo), false
}

for i := range items {
Expand Down
6 changes: 5 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ set -o errexit
set -o nounset
set -o pipefail

echo "Compiling"
go build
PORT=58008 kustomize build --enable-alpha-plugins --enable-exec test > test/expected.yaml

echo "Kustomizing"
PORT=58008 WORKERS=96 kustomize build --enable-alpha-plugins --enable-exec test > test/expected.yaml

echo "Running kubeconform"
kubeconform test/expected.yaml
13 changes: 9 additions & 4 deletions test/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ spec:
- port: 8667
protocol: TCP
targetPort: 5001
type: LoadBalancer
type: ClusterIP
---
apiVersion: v1
kind: Service
Expand All @@ -21,7 +21,7 @@ spec:
- port: 8666
protocol: TCP
targetPort: 58008
type: LoadBalancer
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
Expand Down Expand Up @@ -56,7 +56,12 @@ spec:
app: nginx
spec:
containers:
- image: nginx:1.14.2
- env:
- name: OTHER_WORKERS
value: "10"
- name: WORKERS
value: "96"
image: nginx:1.14.2
name: nginx
ports:
- containerPort: 80
- containerPort: 5002
21 changes: 14 additions & 7 deletions test/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ apiVersion: v1
metadata:
name: the-service
spec:
type: LoadBalancer
type: ClusterIP
ports:
- protocol: TCP
port: 8666
targetPort: "${PORT}"
targetPort: ${PORT}
---
kind: Service
apiVersion: v1
metadata:
name: the-other-service
spec:
type: LoadBalancer
type: ClusterIP
ports:
- protocol: TCP
port: 8666
targetPort: "${PORT2:-5000}"
targetPort: ${PORT2:-5000}
- protocol: TCP
port: 8667
targetPort: "${PORT2:-5001}"
targetPort: ${PORT2:-5001}
---
apiVersion: apps/v1
kind: Deployment
Expand Down Expand Up @@ -57,7 +57,14 @@ spec:
app: nginx
spec:
containers:
- name: nginx
- env:
- name: OTHER_WORKERS
# Verify quoted default means the default outputs a string
value: ${OTHER_WORKERS:-"10"}
- name: WORKERS
# Verify quoted default means this will output a string
value: ${WORKERS:-"10"}
image: nginx:1.14.2
name: nginx
ports:
- containerPort: 80
- containerPort: ${PORT2:-5002}

0 comments on commit 60257bb

Please sign in to comment.