Skip to content

Commit 54d316e

Browse files
committed
♻️Make kind and apiVersion available to all generators
1 parent c6a067c commit 54d316e

File tree

3 files changed

+65
-55
lines changed

3 files changed

+65
-55
lines changed

main.go

+36-37
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,36 @@ func main() {
2727
plugin, err := plugins.MakeBuiltinPlugin(resid.GvkFromNode(config))
2828
if err != nil {
2929
// Check if config asks us to inject it
30-
if _, ok := config.GetAnnotations()[utils.FunctionAnnotationInjectLocal]; ok {
31-
injected := config.Copy()
32-
33-
err := utils.TransferAnnotations([]*yaml.RNode{injected}, config)
34-
if err != nil {
35-
return errors.WrapPrefixf(
36-
err, "Error while mangling annotations on %s fails configuration", res.OrgId())
37-
}
38-
rl.Items = append(rl.Items, injected)
39-
return nil
40-
} else {
30+
if _, ok := config.GetAnnotations()[utils.FunctionAnnotationInjectLocal]; !ok {
4131
return errors.WrapPrefixf(err, "creating plugin")
4232
}
4333
}
4434

45-
yamlNode := config.YNode()
46-
yamlBytes, err := yaml.Marshal(yamlNode)
35+
ok := false
36+
var transformer resmap.Transformer
4737

48-
if err != nil {
49-
return errors.WrapPrefixf(err, "marshalling yaml from res %s", res.OrgId())
50-
}
51-
helpers, err := plugins.NewPluginHelpers()
52-
if err != nil {
53-
return errors.WrapPrefixf(err, "Cannot build Plugin helpers")
54-
}
55-
err = plugin.Config(helpers, yamlBytes)
56-
if err != nil {
57-
return errors.WrapPrefixf(
58-
err, "plugin %s fails configuration", res.OrgId())
59-
}
38+
if plugin != nil {
39+
yamlNode := config.YNode()
40+
yamlBytes, err := yaml.Marshal(yamlNode)
6041

61-
transformer, ok := plugin.(resmap.Transformer)
62-
if ok {
63-
rm, err := helpers.ResmapFactory().NewResMapFromRNodeSlice(rl.Items)
6442
if err != nil {
65-
return errors.WrapPrefixf(err, "getting resource maps")
43+
return errors.WrapPrefixf(err, "marshalling yaml from res %s", res.OrgId())
44+
}
45+
helpers, err := plugins.NewPluginHelpers()
46+
if err != nil {
47+
return errors.WrapPrefixf(err, "Cannot build Plugin helpers")
6648
}
49+
err = plugin.Config(helpers, yamlBytes)
50+
if err != nil {
51+
return errors.WrapPrefixf(
52+
err, "plugin %s fails configuration", res.OrgId())
53+
}
54+
55+
transformer, ok = plugin.(resmap.Transformer)
56+
}
57+
58+
if ok {
59+
rm := utils.ResourceMapFromNodes(rl.Items)
6760
err = transformer.Transform(rm)
6861
if err != nil {
6962
return errors.WrapPrefixf(err, "Transforming resources")
@@ -85,18 +78,24 @@ func main() {
8578
}
8679

8780
} else {
88-
generator, ok := plugin.(resmap.Generator)
81+
var rrl []*yaml.RNode
82+
if plugin == nil { // No plugin, it's an heredoc document
83+
rrl = []*yaml.RNode{config.Copy()}
84+
} else {
85+
generator, ok := plugin.(resmap.Generator)
8986

90-
if !ok {
91-
return fmt.Errorf("plugin %s is neither a generator nor a transformer", res.OrgId())
92-
}
87+
if !ok {
88+
return fmt.Errorf("plugin %s is neither a generator nor a transformer", res.OrgId())
89+
}
9390

94-
rm, err := generator.Generate()
95-
if err != nil {
96-
return errors.WrapPrefixf(err, "generating resource(s)")
91+
rm, err := generator.Generate()
92+
if err != nil {
93+
return errors.WrapPrefixf(err, "generating resource(s)")
94+
}
95+
96+
rrl = rm.ToRNodeSlice()
9797
}
9898

99-
rrl := rm.ToRNodeSlice()
10099
if err := utils.TransferAnnotations(rrl, config); err != nil {
101100
return errors.WrapPrefixf(err, "While transferring annotations")
102101
}

pkg/extras/SopsGenerator.go

+5-18
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"go.mozilla.org/sops/v3/keyservice"
1212
"sigs.k8s.io/kustomize/api/resmap"
1313
"sigs.k8s.io/kustomize/kyaml/kio"
14-
kiof "sigs.k8s.io/kustomize/kyaml/kio/filters"
1514
yaml "sigs.k8s.io/kustomize/kyaml/yaml"
1615
oyaml "sigs.k8s.io/yaml"
1716
)
@@ -100,24 +99,12 @@ func (p *SopsGeneratorPlugin) Generate() (resmap.ResMap, error) {
10099
if err != nil {
101100
return nil, errors.Wrapf(err, "error decoding manifest %q, content -->%s<--", name, string(p.buffer))
102101
}
103-
kind := defaultKind
104-
apiVersion := defaultApiVersion
105-
if p.Annotations != nil {
106-
if annoKind, ok := p.Annotations[utils.FunctionAnnotationKind]; ok {
107-
kind = annoKind
108-
}
109-
if annoApiVersion, ok := p.Annotations[utils.FunctionAnnotationApiVersion]; ok {
110-
apiVersion = annoApiVersion
111-
}
112-
}
113102

114103
for _, r := range nodes {
115-
r.SetKind(kind)
116-
r.SetApiVersion(apiVersion)
117-
if err := r.PipeE(yaml.ClearAnnotation(utils.FunctionAnnotationFunction)); err != nil {
118-
return nil, err
119-
}
120-
if err := r.PipeE(yaml.ClearAnnotation(kiof.LocalConfigAnnotation)); err != nil {
104+
r.SetKind(defaultKind)
105+
r.SetApiVersion(defaultApiVersion)
106+
107+
if err := r.PipeE(yaml.SetAnnotation(utils.FunctionAnnotationInjectLocal, "true")); err != nil {
121108
return nil, err
122109
}
123110
}
@@ -140,7 +127,7 @@ func (p *SopsGeneratorPlugin) Generate() (resmap.ResMap, error) {
140127
}
141128

142129
}
143-
return p.h.ResmapFactory().NewResMapFromRNodeSlice(nodes)
130+
return utils.ResourceMapFromNodes(nodes), nil
144131
}
145132

146133
// NewSopsGeneratorPlugin returns a newly Created SopsGenerator

pkg/utils/utils.go

+24
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package utils
33
import (
44
"strconv"
55

6+
"sigs.k8s.io/kustomize/api/resmap"
67
"sigs.k8s.io/kustomize/api/resource"
78
"sigs.k8s.io/kustomize/kyaml/kio"
9+
"sigs.k8s.io/kustomize/kyaml/kio/filters"
810
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
911
"sigs.k8s.io/kustomize/kyaml/yaml"
1012
)
@@ -71,10 +73,24 @@ func TransferAnnotations(list []*yaml.RNode, config *yaml.RNode) (err error) {
7173
annotations[kioutil.LegacyIndexAnnotation] = curIndex
7274
annotations[kioutil.IndexAnnotation] = curIndex
7375
}
76+
77+
if _, ok := annotations[FunctionAnnotationInjectLocal]; ok {
78+
// It's an heredoc document
79+
if kind, ok := configAnnotations[FunctionAnnotationKind]; ok {
80+
r.SetKind(kind)
81+
}
82+
if apiVersion, ok := configAnnotations[FunctionAnnotationApiVersion]; ok {
83+
r.SetApiVersion(apiVersion)
84+
}
85+
}
86+
7487
delete(annotations, FunctionAnnotationInjectLocal)
7588
delete(annotations, FunctionAnnotationFunction)
7689
delete(annotations, FunctionAnnotationPath)
7790
delete(annotations, FunctionAnnotationIndex)
91+
delete(annotations, FunctionAnnotationKind)
92+
delete(annotations, FunctionAnnotationApiVersion)
93+
delete(annotations, filters.LocalConfigAnnotation)
7894
r.SetAnnotations(annotations)
7995
}
8096
return
@@ -108,3 +124,11 @@ func unLocal(list []*yaml.RNode) ([]*yaml.RNode, error) {
108124
}
109125

110126
var UnLocal kio.FilterFunc = unLocal
127+
128+
func ResourceMapFromNodes(nodes []*yaml.RNode) resmap.ResMap {
129+
result := resmap.New()
130+
for _, n := range nodes {
131+
result.Append(&resource.Resource{RNode: *n})
132+
}
133+
return result
134+
}

0 commit comments

Comments
 (0)