-
Notifications
You must be signed in to change notification settings - Fork 66
/
main.go
176 lines (157 loc) · 4.81 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// This file will be processed and embedded to pluginator.
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-annotations/generated"
"sigs.k8s.io/kustomize/api/hasher"
"sigs.k8s.io/kustomize/api/konfig/builtinpluginconsts"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
"sigs.k8s.io/kustomize/kyaml/fn/framework/command"
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
"sigs.k8s.io/yaml"
)
const (
fnConfigGroup = "fn.kpt.dev"
fnConfigVersion = "v1alpha1"
fnConfigAPIVersion = fnConfigGroup + "/" + fnConfigVersion
legacyFnConfigKind = "SetAnnotationConfig"
fnConfigKind = "SetAnnotations"
)
type SetAnnotationsProcessor struct{}
func (sap *SetAnnotationsProcessor) Process(resourceList *framework.ResourceList) error {
results, err := run(resourceList)
if err != nil {
resourceList.Results = framework.Results{
&framework.Result{
Message: err.Error(),
Severity: framework.Error,
},
}
return resourceList.Results
}
resourceList.Results = results
return nil
}
type transformerConfig struct {
FieldSpecs types.FsSlice `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"`
}
type setAnnotationFunction struct {
kyaml.ResourceMeta `json:",inline" yaml:",inline"`
plugin `json:",inline" yaml:",inline"`
}
func (f *setAnnotationFunction) Config(rn *kyaml.RNode) error {
switch {
case f.validGVK(rn, "v1", "ConfigMap"):
f.plugin.Annotations = rn.GetDataMap()
case f.validGVK(rn, fnConfigAPIVersion, legacyFnConfigKind):
fallthrough
case f.validGVK(rn, fnConfigAPIVersion, fnConfigKind):
// input config is a CRD
y, err := rn.String()
if err != nil {
return fmt.Errorf("cannot get YAML from RNode: %w", err)
}
err = f.plugin.Config(nil, []byte(y))
if err != nil {
return err
}
default:
return fmt.Errorf("`functionConfig` must be a `ConfigMap` or `%s`", fnConfigKind)
}
if len(f.plugin.Annotations) == 0 {
return fmt.Errorf("input annotation list cannot be empty")
}
tc, err := getDefaultConfig()
if err != nil {
return err
}
// append default field specs
f.plugin.AdditionalAnnotationFields = append(f.plugin.AdditionalAnnotationFields, tc.FieldSpecs...)
return nil
}
func (f *setAnnotationFunction) Run(items []*kyaml.RNode) ([]*kyaml.RNode, error) {
resmapFactory := newResMapFactory()
resMap, err := resmapFactory.NewResMapFromRNodeSlice(items)
if err != nil {
return nil, err
}
err = f.plugin.Transform(resMap)
if err != nil {
return nil, fmt.Errorf("failed to run transformer: %w", err)
}
return resMap.ToRNodeSlice(), nil
}
func (f *setAnnotationFunction) validGVK(rn *kyaml.RNode, apiVersion, kind string) bool {
meta, err := rn.GetMeta()
if err != nil {
return false
}
if meta.APIVersion != apiVersion || meta.Kind != kind {
return false
}
return true
}
// resultsToItems converts the set annotation results to
// equivalent framework.Results
func (f *setAnnotationFunction) resultsToItems() (framework.Results, error) {
var results framework.Results
if len(f.plugin.Results) == 0 {
results = append(results, &framework.Result{
Message: "no annotations applied",
})
return results, nil
}
for resKey, annoVals := range f.plugin.Results {
fileIndex, _ := strconv.Atoi(resKey.FileIndex)
annotationJson, err := json.Marshal(annoVals)
if err != nil {
return nil, err
}
results = append(results, &framework.Result{
Message: fmt.Sprintf("set annotations: %s", annotationJson),
Field: &framework.Field{Path: resKey.FieldPath},
File: &framework.File{Path: resKey.FilePath, Index: fileIndex},
})
}
results.Sort()
return results, nil
}
func getDefaultConfig() (transformerConfig, error) {
defaultConfigString := builtinpluginconsts.GetDefaultFieldSpecsAsMap()["commonannotations"]
var tc transformerConfig
err := yaml.Unmarshal([]byte(defaultConfigString), &tc)
return tc, err
}
func newResMapFactory() *resmap.Factory {
resourceFactory := resource.NewFactory(&hasher.Hasher{})
resourceFactory.IncludeLocalConfigs = true
return resmap.NewFactory(resourceFactory)
}
//nolint
func main() {
asp := SetAnnotationsProcessor{}
cmd := command.Build(&asp, command.StandaloneEnabled, false)
cmd.Short = generated.SetAnnotationsShort
cmd.Long = generated.SetAnnotationsLong
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
func run(resourceList *framework.ResourceList) (framework.Results, error) {
var fn setAnnotationFunction
err := fn.Config(resourceList.FunctionConfig)
if err != nil {
return nil, fmt.Errorf("failed to configure function: %w", err)
}
resourceList.Items, err = fn.Run(resourceList.Items)
if err != nil {
return nil, fmt.Errorf("failed to run function: %w", err)
}
return fn.resultsToItems()
}