Skip to content

Commit

Permalink
Merge pull request #3 from kaweezle/feature/extended-replacement-tran…
Browse files Browse the repository at this point in the history
…sformer

✨ Add extended ReplacementTransformer
  • Loading branch information
antoinemartin committed Jan 26, 2023
2 parents aff71b3 + d7f3b82 commit 7fedc73
Show file tree
Hide file tree
Showing 29 changed files with 2,768 additions and 94 deletions.
551 changes: 522 additions & 29 deletions README.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ go 1.18

require (
github.com/go-git/go-git/v5 v5.5.2
github.com/lithammer/dedent v1.1.0
github.com/stretchr/testify v1.8.1
golang.org/x/tools v0.5.0
sigs.k8s.io/kustomize/api v0.12.1
sigs.k8s.io/kustomize/kyaml v0.13.10
sigs.k8s.io/yaml v1.2.0

)

require (
Expand All @@ -24,6 +27,7 @@ require (
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.4.0 // indirect
github.com/go-ini/ini v1.67.0
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
Expand All @@ -37,8 +41,10 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pelletier/go-toml/v2 v2.0.6
github.com/pjbgf/sha1cd v0.2.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.1.0 // indirect
github.com/spf13/cobra v1.4.0 // indirect
Expand Down
15 changes: 13 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ github.com/go-git/go-git-fixtures/v4 v4.3.1 h1:y5z6dd3qi8Hl+stezc8p3JxDkoTRqMAlK
github.com/go-git/go-git-fixtures/v4 v4.3.1/go.mod h1:8LHG1a3SRW71ettAD/jW13h8c6AqjVSeL11RAdgaqpo=
github.com/go-git/go-git/v5 v5.5.2 h1:v8lgZa5k9ylUw+OR/roJHTxR4QItsNFI5nKtAXFuynw=
github.com/go-git/go-git/v5 v5.5.2/go.mod h1:BE5hUJ5yaV2YMxhmaP4l6RBQ08kMxKSPD4BlxtH7OjI=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
Expand Down Expand Up @@ -110,6 +112,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/lithammer/dedent v1.1.0 h1:VNzHMVCBNG1j0fh3OrsFRkVUwStdDArbgBWoPAffktY=
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
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 All @@ -127,6 +131,8 @@ github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8m
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
github.com/pjbgf/sha1cd v0.2.3 h1:uKQP/7QOzNtKYH7UTohZLcjF5/55EnTw0jO/Ru4jZwI=
github.com/pjbgf/sha1cd v0.2.3/go.mod h1:HOK9QrgzdHpbc2Kzip0Q1yi3M2MFGPADtR6HjG65m5M=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -147,13 +153,18 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
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.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/xlab/treeprint v1.1.0 h1:G/1DjNkPpfZCFt9CSh6b5/nY4VimlbHF3Rh4obvtzDk=
Expand Down
74 changes: 26 additions & 48 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,18 @@ import (
"os"

"github.com/kaweezle/krmfnbuiltin/pkg/plugins"
"github.com/kaweezle/krmfnbuiltin/pkg/utils"

"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
"sigs.k8s.io/kustomize/kyaml/fn/framework/command"
"sigs.k8s.io/kustomize/kyaml/kio/filters"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/resid"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

const (
// build annotations
BuildAnnotationPreviousKinds = konfig.ConfigAnnoDomain + "/previousKinds"
BuildAnnotationPreviousNames = konfig.ConfigAnnoDomain + "/previousNames"
BuildAnnotationPrefixes = konfig.ConfigAnnoDomain + "/prefixes"
BuildAnnotationSuffixes = konfig.ConfigAnnoDomain + "/suffixes"
BuildAnnotationPreviousNamespaces = konfig.ConfigAnnoDomain + "/previousNamespaces"
BuildAnnotationsRefBy = konfig.ConfigAnnoDomain + "/refBy"
BuildAnnotationsGenBehavior = konfig.ConfigAnnoDomain + "/generatorBehavior"
BuildAnnotationsGenAddHashSuffix = konfig.ConfigAnnoDomain + "/needsHashSuffix"
)

var BuildAnnotations = []string{
BuildAnnotationPreviousKinds,
BuildAnnotationPreviousNames,
BuildAnnotationPrefixes,
BuildAnnotationSuffixes,
BuildAnnotationPreviousNamespaces,
BuildAnnotationsRefBy,
BuildAnnotationsGenBehavior,
BuildAnnotationsGenAddHashSuffix,
}

func RemoveBuildAnnotations(r *resource.Resource) {
annotations := r.GetAnnotations()
if len(annotations) == 0 {
return
}
for _, a := range BuildAnnotations {
delete(annotations, a)
}
if err := r.SetAnnotations(annotations); err != nil {
panic(err)
}
}

func main() {

var processor framework.ResourceListProcessorFunc = func(rl *framework.ResourceList) error {
Expand All @@ -64,7 +27,20 @@ func main() {

plugin, err := plugins.MakeBuiltinPlugin(resid.GvkFromNode(config))
if err != nil {
return errors.WrapPrefixf(err, "creating plugin")
// Check if config asks us to inject it
if _, ok := config.GetAnnotations()[utils.FunctionAnnotationInjectLocal]; ok {
injected := config.Copy()

err := utils.MakeResourceLocal(injected)
if err != nil {
return errors.WrapPrefixf(
err, "Error while mangling annotations on %s fails configuration", res.OrgId())
}
rl.Items = append(rl.Items, injected)
return nil
} else {
return errors.WrapPrefixf(err, "creating plugin")
}
}

yamlNode := config.YNode()
Expand Down Expand Up @@ -95,16 +71,20 @@ func main() {
}

for _, r := range rm.Resources() {
RemoveBuildAnnotations(r)
utils.RemoveBuildAnnotations(r)
}

rl.Items = rm.ToRNodeSlice()

// kustomize fn don't remove config.kubernetes.io/local-config resources upon completion.
// As it always add a filename by default, the local resources keep saved.
// To avoid this, an annotation `config.kubernetes.io/prune-local` present in a
// To avoid this, an annotation `config.kaweezle.com/prune-local` present in a
// transformer makes all the local resources disappear.
if _, ok := config.GetAnnotations()["config.kubernetes.io/prune-local"]; ok {
if _, ok := config.GetAnnotations()[utils.FunctionAnnotationPruneLocal]; ok {
err = rl.Filter(utils.UnLocal)
if err != nil {
return errors.WrapPrefixf(err, "Removing local from keep-local resources")
}
filter := &filters.IsLocalConfig{IncludeLocalConfig: false, ExcludeNonLocalConfig: false}
err = rl.Filter(filter)
if err != nil {
Expand All @@ -125,15 +105,13 @@ func main() {
}

for _, r := range rm.Resources() {
r.RemoveBuildAnnotations()
utils.RemoveBuildAnnotations(r)
// We add the annotation config.kubernetes.io/local-config to be able to delete
// The generated resource at the end of the process. Unfortunately, kustomize doesn't
// do that on functions. So we have added a special annotation
// `config.kubernetes.io/prune-local` to add on the last transformer.
// `config.kaweezle.com/prune-local` to add on the last transformer.
// We set the filename of the generated resource in case it is forgotten.
r.Pipe(yaml.SetAnnotation(filters.LocalConfigAnnotation, "true"))
r.Pipe(yaml.SetAnnotation(kioutil.PathAnnotation, ".generated.yaml"))
r.Pipe(yaml.SetAnnotation(kioutil.LegacyPathAnnotation, ".generated.yaml"))
utils.MakeResourceLocal(&r.RNode)
}

rl.Items = append(rl.Items, rm.ToRNodeSlice()...)
Expand All @@ -146,7 +124,7 @@ func main() {

cmd := command.Build(processor, command.StandaloneDisabled, false)
command.AddGenerateDockerfile(cmd)
cmd.Version = "v0.1.0" // <---VERSION--->
cmd.Version = "v0.2.0" // <---VERSION--->

if err := cmd.Execute(); err != nil {
os.Exit(1)
Expand Down
17 changes: 17 additions & 0 deletions pkg/extras/GitConfigMapGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,28 @@ import (
"sigs.k8s.io/yaml"
)

// GitConfigMapGeneratorPlugin generates a config map that includes two
// properties of the current git repository:
//
// - repoURL contains the URL or the remote specified by remoteName. by
// default, it takes the URL of the remote named "origin".
// - targetRevision contains the name of the current branch.
//
// This generator is useful in transformations that use those values, like for
// instance Argo CD application customization.
//
// Information about the configuration can be found in the [kustomize doc].
//
// [kustomize doc]: https://kubectl.docs.kubernetes.io/references/kustomize/builtins/#_configmapgenerator_
type GitConfigMapGeneratorPlugin struct {
h *resmap.PluginHelpers
types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
types.ConfigMapArgs
// The name of the remote which URL to include. defaults to "origin".
RemoteName string `json:"remoteName,omitempty" yaml:"remoteName,omitempty"`
}

// Config configures the generator with the functionConfig passed in config.
func (p *GitConfigMapGeneratorPlugin) Config(h *resmap.PluginHelpers, config []byte) (err error) {
p.ConfigMapArgs = types.ConfigMapArgs{}
err = yaml.Unmarshal(config, p)
Expand All @@ -31,6 +46,7 @@ func (p *GitConfigMapGeneratorPlugin) Config(h *resmap.PluginHelpers, config []b
return
}

// Generate generates the config map
func (p *GitConfigMapGeneratorPlugin) Generate() (resmap.ResMap, error) {
// Add git repository properties

Expand Down Expand Up @@ -63,6 +79,7 @@ func (p *GitConfigMapGeneratorPlugin) Generate() (resmap.ResMap, error) {
kv.NewLoader(p.h.Loader(), p.h.Validator()), p.ConfigMapArgs)
}

// NewGitConfigMapGeneratorPlugin returns a newly created GitConfigMapGenerator.
func NewGitConfigMapGeneratorPlugin() resmap.GeneratorPlugin {
return &GitConfigMapGeneratorPlugin{}
}
49 changes: 49 additions & 0 deletions pkg/extras/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Package extras contains additional utility transformers and generators.
[GitConfigMapGeneratorPlugin] is identical to ConfigMapGeneratorPlugin
but automatically creates two properties when run inside a git repository:
- repoURL gives the URL of the origin remote.
- targetRevision gives the current branch.
[ExtendedReplacementTransformerPlugin] is a copy of ReplacementTransformerPlugin
that provides extended target paths into embedded data structures. For instance,
consider the following resource snippet:
helm:
parameters:
- name: common.targetRevision
# This resource is accessible by traditional transformer
value: deploy/citest
- name: common.repoURL
value: https://github.com/antoinemartin/autocloud.git
values: |
uninode: true
apps:
enabled: true
common:
# This embedded resource is not accessible
targetRevision: deploy/citest
repoURL: https://github.com/antoinemartin/autocloud.git
In the above, the common.targetRevision property of the yaml embedded in the
spec.source.helm.values property is not accessible with the traditional
ReplacementTransformerPlugin. With the extended transformer, you can target
it with:
fieldPaths:
- spec.source.helm.parameters.[name=common.targetRevision].value
- spec.source.helm.values.!!yaml.common.targetRevision
Note the use of !!yaml to designate the encoding of the embedded structure. The
extended transformer supports the following encodings:
- YAML
- JSON
- TOML
- INI
- base64
- Plain text (with Regexp)
*/
package extras
Loading

0 comments on commit 7fedc73

Please sign in to comment.