Skip to content

Commit

Permalink
add support for legacy exec
Browse files Browse the repository at this point in the history
  • Loading branch information
logandavies181 committed Jan 9, 2023
1 parent b4b1bc7 commit 081b3de
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/logandavies181/envsubst v0.1.0
github.com/logandavies181/go-buildversion v0.1.0
sigs.k8s.io/kustomize/kyaml v0.13.10
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ 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/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.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
46 changes: 39 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ package main
import (
"fmt"
"os"
"strings"

"github.com/logandavies181/envsubst"
"github.com/logandavies181/go-buildversion"

"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"
)

var version string // goreleaser will set this

type Config struct {
envMapping envsubst.AdvancedMapping

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

func isEmpty(str string) bool {
Expand Down Expand Up @@ -66,13 +70,13 @@ func (c Config) Filter(in *yaml.RNode) (*yaml.RNode, error) {
case yaml.MappingNode:
err := in.VisitFields(c.walkMapNode)
if err != nil {
return nil, fmt.Errorf("Could not visit fields: %v", err)
return nil, err
}
return in, nil
case yaml.SequenceNode:
err := in.VisitElements(c.walkSequenceNode)
if err != nil {
return nil, fmt.Errorf("Could not visit elements: %v", err)
return nil, err
}

return in, nil
Expand All @@ -94,8 +98,8 @@ func (c Config) Filter(in *yaml.RNode) (*yaml.RNode, error) {
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)
"Value `%s` evaluated to empty string. Did you forget to set an environment variable?",
strings.TrimSuffix(str, "\n"))
}

substed = `""`
Expand Down Expand Up @@ -124,8 +128,34 @@ func (c Config) Filter(in *yaml.RNode) (*yaml.RNode, error) {

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

// This is a hack, but legacy exec plugins get config from the first arg.
// The framework is going to do its own thing here, but let's try read the first arg
// and parse it if it exists and is a file
if len(os.Args) > 1 {
fname := os.Args[1]
fdata, err := os.ReadFile(fname)
if err == nil {
var c Config
err = yaml.Unmarshal(fdata, &c)
if err == nil {
config.AllowEmpty = c.AllowEmpty
config.IncludedVars = c.IncludedVars
config.ExcludedVars = c.ExcludedVars
}
}
}

for _, arg := range os.Args {
fmt.Fprintln(os.Stderr, arg)
}

fn := func(items []*yaml.RNode) ([]*yaml.RNode, error) {
config.envMapping = func(str 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) {
return nodeInfo.Orig(), false
Expand All @@ -152,7 +182,9 @@ func main() {
}
p := framework.SimpleProcessor{Config: config, Filter: kio.FilterFunc(fn)}
cmd := command.Build(p, command.StandaloneDisabled, false)
command.AddGenerateDockerfile(cmd)
version, _ := buildversion.BuildVersionShortE(version)
cmd.Version = version

if err := cmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down
6 changes: 6 additions & 0 deletions test/transformer2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: logantest.com/envsubst
kind: EnvSubst
metadata:
name: notImportantHere
includedVariableNames:
- "PORT"

0 comments on commit 081b3de

Please sign in to comment.