Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/new envsubst #1

Merged
merged 3 commits into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/drone/envsubst v1.0.3
github.com/logandavies181/envsubst v0.1.0
sigs.k8s.io/kustomize/kyaml v0.13.10
)

Expand Down
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/drone/envsubst v1.0.3 h1:PCIBwNDYjs50AsLZPYdfhSATKaRg/FJmDc2D6+C2x8g=
github.com/drone/envsubst v1.0.3/go.mod h1:N2jZmlMufstn1KEqvbHjw40h1KyTmnVzHcSc9bFiJ2g=
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
Expand Down Expand Up @@ -72,6 +70,8 @@ github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
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/logandavies181/envsubst v0.1.0 h1:r/y5QwzcMw26kchMjzKYtq/DkAkGwuv7qSp47Lmb4OM=
github.com/logandavies181/envsubst v0.1.0/go.mod h1:5vxEu24ve2msUFvdG9LsDa1dK8HGFcEElF8QJb1r/Vg=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
Expand Down Expand Up @@ -103,7 +103,7 @@ github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/xlab/treeprint v1.1.0 h1:G/1DjNkPpfZCFt9CSh6b5/nY4VimlbHF3Rh4obvtzDk=
github.com/xlab/treeprint v1.1.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
84 changes: 71 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,73 @@ import (
"fmt"
"os"

"github.com/drone/envsubst"
"github.com/logandavies181/envsubst"

"sigs.k8s.io/kustomize/kyaml/fn/framework"
"sigs.k8s.io/kustomize/kyaml/fn/framework/command"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

func walkSequenceNode(in *yaml.RNode) error {
_, err := filter(in)
type Config struct {
envMapping envsubst.AdvancedMapping

AllowEmpty bool `yaml:"allowEmpty" json:"allowEmpty"`
ExcludedVars []string `yaml:"excludedVars" json:"excludedVars"`
IncludedVars []string `yaml:"includedVars" json:"includedVars"`
}

func isEmpty(str string) bool {
switch str {
case "", `""`, "''", "\n":
return true
default:
return false
}
}

func contains(list []string, str string) bool {
for _, v := range list {
if str == v {
return true
}
}

return false
}

func (c Config) walkSequenceNode(in *yaml.RNode) error {
_, err := c.Filter(in)
if err != nil {
return err
}

return nil
}

func walkMapNode(in *yaml.MapNode) error {
_, err := filter(in.Value)
func (c Config) walkMapNode(in *yaml.MapNode) error {
_, err := c.Filter(in.Value)
if err != nil {
return err
}

return nil
}

func filter(in *yaml.RNode) (*yaml.RNode, error) {
func (c Config) Filter(in *yaml.RNode) (*yaml.RNode, error) {
if in.IsNil() {
return nil, nil
}

switch y := in.YNode().Kind; y {
case yaml.MappingNode:
err := in.VisitFields(walkMapNode)
err := in.VisitFields(c.walkMapNode)
if err != nil {
return nil, fmt.Errorf("Could not visit fields: %v", err)
}
return in, nil
case yaml.SequenceNode:
err := in.VisitElements(walkSequenceNode)
err := in.VisitElements(c.walkSequenceNode)
if err != nil {
return nil, fmt.Errorf("Could not visit elements: %v", err)
}
Expand All @@ -54,17 +82,29 @@ func filter(in *yaml.RNode) (*yaml.RNode, error) {
return nil, fmt.Errorf("Could not parse node into string: %v", err)
}

substed, err := envsubst.EvalEnv(str)
substed, err := envsubst.EvalAdvanced(str, envsubst.AdvancedMapping(c.envMapping))
if err != nil {
return nil, fmt.Errorf("Could not envsubt: %v", err)
return nil, fmt.Errorf("Could not envsubst: %v", err)
}

if substed == str {
return in, nil
}

if isEmpty(substed) {
if !c.AllowEmpty {
return nil, fmt.Errorf(
"Value %s evaluated to empty string. Did you forget to set an environment variable?",
str)
}

substed = `""`
}

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

Expand All @@ -83,16 +123,34 @@ func filter(in *yaml.RNode) (*yaml.RNode, error) {
}

func main() {
config := new(Config)
fn := func(items []*yaml.RNode) ([]*yaml.RNode, error) {
config.envMapping = func(str string, nodeInfo envsubst.NodeInfo) (string, bool) {
if len(config.IncludedVars) == 0 {
if contains(config.ExcludedVars, str) {
return nodeInfo.Orig(), false
}

return str, true
}

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

return str, true
}

for i := range items {
err := items[i].PipeE(yaml.FilterFunc(filter))
err := items[i].PipeE(config)
if err != nil {
return nil, fmt.Errorf("kustomize-krm-envsubt: %v", err)
return nil, fmt.Errorf("kustomize-krm-envsubst: %v ", // leave a space because kustomize doesn't
err)
}
}
return items, nil
}
p := framework.SimpleProcessor{Config: nil, Filter: kio.FilterFunc(fn)}
p := framework.SimpleProcessor{Config: config, Filter: kio.FilterFunc(fn)}
cmd := command.Build(p, command.StandaloneDisabled, false)
command.AddGenerateDockerfile(cmd)
if err := cmd.Execute(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion test/transformer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ metadata:
annotations:
config.kubernetes.io/function: |
exec:
path: ./kustomize-krm-envsubst
path: ../kustomize-krm-envsubst