-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (94 loc) · 3.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"os"
"github.com/kaweezle/krmfnbuiltin/pkg/plugins"
"github.com/kaweezle/krmfnbuiltin/pkg/utils"
"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/resid"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
func main() {
var processor framework.ResourceListProcessorFunc = func(rl *framework.ResourceList) error {
config := rl.FunctionConfig
res := resource.Resource{RNode: *config}
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 {
return errors.WrapPrefixf(err, "creating plugin")
}
}
ok := false
var transformer resmap.Transformer
if plugin != nil {
yamlNode := config.YNode()
yamlBytes, err := yaml.Marshal(yamlNode)
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())
}
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")
}
configAnnotations := config.GetAnnotations()
if _, ok := configAnnotations[utils.FunctionAnnotationCleanup]; ok {
for _, r := range rm.Resources() {
utils.RemoveBuildAnnotations(r)
}
}
rl.Items = rm.ToRNodeSlice()
// If the annotation `config.kaweezle.com/prune-local` is present in a
// transformer makes all the local resources disappear.
if _, ok := configAnnotations[utils.FunctionAnnotationPruneLocal]; ok {
err = rl.Filter(utils.UnLocal)
if err != nil {
return errors.WrapPrefixf(err, "while pruning `config.kaweezle.com/local-config` resources")
}
}
} else {
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())
}
rm, err := generator.Generate()
if err != nil {
return errors.WrapPrefixf(err, "generating resource(s)")
}
rrl = rm.ToRNodeSlice()
}
if err := utils.TransferAnnotations(rrl, config); err != nil {
return errors.WrapPrefixf(err, "While transferring annotations")
}
rl.Items = append(rl.Items, rrl...)
}
return nil
}
cmd := command.Build(processor, command.StandaloneDisabled, false)
command.AddGenerateDockerfile(cmd)
cmd.Version = "v0.4.3" // <---VERSION--->
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}