Skip to content

Commit

Permalink
♻️Make kind and apiVersion available to all generators
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinemartin committed Feb 6, 2023
1 parent c6a067c commit 54d316e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 55 deletions.
73 changes: 36 additions & 37 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,36 @@ func main() {
plugin, err := plugins.MakeBuiltinPlugin(resid.GvkFromNode(config))
if err != nil {
// Check if config asks us to inject it
if _, ok := config.GetAnnotations()[utils.FunctionAnnotationInjectLocal]; ok {
injected := config.Copy()

err := utils.TransferAnnotations([]*yaml.RNode{injected}, config)
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 {
if _, ok := config.GetAnnotations()[utils.FunctionAnnotationInjectLocal]; !ok {
return errors.WrapPrefixf(err, "creating plugin")
}
}

yamlNode := config.YNode()
yamlBytes, err := yaml.Marshal(yamlNode)
ok := false
var transformer resmap.Transformer

if err != nil {
return errors.WrapPrefixf(err, "marshalling yaml from res %s", res.OrgId())
}
helpers, err := plugins.NewPluginHelpers()
if err != nil {
return errors.WrapPrefixf(err, "Cannot build Plugin helpers")
}
err = plugin.Config(helpers, yamlBytes)
if err != nil {
return errors.WrapPrefixf(
err, "plugin %s fails configuration", res.OrgId())
}
if plugin != nil {
yamlNode := config.YNode()
yamlBytes, err := yaml.Marshal(yamlNode)

transformer, ok := plugin.(resmap.Transformer)
if ok {
rm, err := helpers.ResmapFactory().NewResMapFromRNodeSlice(rl.Items)
if err != nil {
return errors.WrapPrefixf(err, "getting resource maps")
return errors.WrapPrefixf(err, "marshalling yaml from res %s", res.OrgId())
}
helpers, err := plugins.NewPluginHelpers()
if err != nil {
return errors.WrapPrefixf(err, "Cannot build Plugin helpers")
}
err = plugin.Config(helpers, yamlBytes)
if err != nil {
return errors.WrapPrefixf(
err, "plugin %s fails configuration", res.OrgId())
}

transformer, ok = plugin.(resmap.Transformer)
}

if ok {
rm := utils.ResourceMapFromNodes(rl.Items)
err = transformer.Transform(rm)
if err != nil {
return errors.WrapPrefixf(err, "Transforming resources")
Expand All @@ -85,18 +78,24 @@ func main() {
}

} else {
generator, ok := plugin.(resmap.Generator)
var rrl []*yaml.RNode
if plugin == nil { // No plugin, it's an heredoc document
rrl = []*yaml.RNode{config.Copy()}
} else {
generator, ok := plugin.(resmap.Generator)

if !ok {
return fmt.Errorf("plugin %s is neither a generator nor a transformer", res.OrgId())
}
if !ok {
return fmt.Errorf("plugin %s is neither a generator nor a transformer", res.OrgId())
}

rm, err := generator.Generate()
if err != nil {
return errors.WrapPrefixf(err, "generating resource(s)")
rm, err := generator.Generate()
if err != nil {
return errors.WrapPrefixf(err, "generating resource(s)")
}

rrl = rm.ToRNodeSlice()
}

rrl := rm.ToRNodeSlice()
if err := utils.TransferAnnotations(rrl, config); err != nil {
return errors.WrapPrefixf(err, "While transferring annotations")
}
Expand Down
23 changes: 5 additions & 18 deletions pkg/extras/SopsGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"go.mozilla.org/sops/v3/keyservice"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/kyaml/kio"
kiof "sigs.k8s.io/kustomize/kyaml/kio/filters"
yaml "sigs.k8s.io/kustomize/kyaml/yaml"
oyaml "sigs.k8s.io/yaml"
)
Expand Down Expand Up @@ -100,24 +99,12 @@ func (p *SopsGeneratorPlugin) Generate() (resmap.ResMap, error) {
if err != nil {
return nil, errors.Wrapf(err, "error decoding manifest %q, content -->%s<--", name, string(p.buffer))
}
kind := defaultKind
apiVersion := defaultApiVersion
if p.Annotations != nil {
if annoKind, ok := p.Annotations[utils.FunctionAnnotationKind]; ok {
kind = annoKind
}
if annoApiVersion, ok := p.Annotations[utils.FunctionAnnotationApiVersion]; ok {
apiVersion = annoApiVersion
}
}

for _, r := range nodes {
r.SetKind(kind)
r.SetApiVersion(apiVersion)
if err := r.PipeE(yaml.ClearAnnotation(utils.FunctionAnnotationFunction)); err != nil {
return nil, err
}
if err := r.PipeE(yaml.ClearAnnotation(kiof.LocalConfigAnnotation)); err != nil {
r.SetKind(defaultKind)
r.SetApiVersion(defaultApiVersion)

if err := r.PipeE(yaml.SetAnnotation(utils.FunctionAnnotationInjectLocal, "true")); err != nil {
return nil, err
}
}
Expand All @@ -140,7 +127,7 @@ func (p *SopsGeneratorPlugin) Generate() (resmap.ResMap, error) {
}

}
return p.h.ResmapFactory().NewResMapFromRNodeSlice(nodes)
return utils.ResourceMapFromNodes(nodes), nil
}

// NewSopsGeneratorPlugin returns a newly Created SopsGenerator
Expand Down
24 changes: 24 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package utils
import (
"strconv"

"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/filters"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
Expand Down Expand Up @@ -71,10 +73,24 @@ func TransferAnnotations(list []*yaml.RNode, config *yaml.RNode) (err error) {
annotations[kioutil.LegacyIndexAnnotation] = curIndex
annotations[kioutil.IndexAnnotation] = curIndex
}

if _, ok := annotations[FunctionAnnotationInjectLocal]; ok {
// It's an heredoc document
if kind, ok := configAnnotations[FunctionAnnotationKind]; ok {
r.SetKind(kind)
}
if apiVersion, ok := configAnnotations[FunctionAnnotationApiVersion]; ok {
r.SetApiVersion(apiVersion)
}
}

delete(annotations, FunctionAnnotationInjectLocal)
delete(annotations, FunctionAnnotationFunction)
delete(annotations, FunctionAnnotationPath)
delete(annotations, FunctionAnnotationIndex)
delete(annotations, FunctionAnnotationKind)
delete(annotations, FunctionAnnotationApiVersion)
delete(annotations, filters.LocalConfigAnnotation)
r.SetAnnotations(annotations)
}
return
Expand Down Expand Up @@ -108,3 +124,11 @@ func unLocal(list []*yaml.RNode) ([]*yaml.RNode, error) {
}

var UnLocal kio.FilterFunc = unLocal

func ResourceMapFromNodes(nodes []*yaml.RNode) resmap.ResMap {
result := resmap.New()
for _, n := range nodes {
result.Append(&resource.Resource{RNode: *n})
}
return result
}

0 comments on commit 54d316e

Please sign in to comment.