-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
starlark.go
203 lines (174 loc) · 4.76 KB
/
starlark.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package starlark
import (
"bytes"
goerrors "errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"go.starlark.net/starlark"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
"sigs.k8s.io/kustomize/kyaml/internal/forked/github.com/qri-io/starlib/util"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/filters"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
// Filter transforms a set of resources through the provided program
type Filter struct {
Name string
// Program is a starlark script which will be run against the resources
Program string
// URL is the url of a starlark program to fetch and run
URL string
// Path is the path to a starlark program to read and run
Path string
runtimeutil.FunctionFilter
}
func (sf *Filter) String() string {
return fmt.Sprintf(
"name: %v path: %v url: %v program: %v", sf.Name, sf.Path, sf.URL, sf.Program)
}
func (sf *Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
err := sf.setup()
if err != nil {
return nil, err
}
sf.FunctionFilter.Run = sf.Run
return sf.FunctionFilter.Filter(nodes)
}
func (sf *Filter) setup() error {
if (sf.URL != "" && sf.Path != "") ||
(sf.URL != "" && sf.Program != "") ||
(sf.Path != "" && sf.Program != "") {
return errors.Errorf("Filter Path, Program and URL are mutually exclusive")
}
// read the program from a file
if sf.Path != "" {
b, err := ioutil.ReadFile(sf.Path)
if err != nil {
return err
}
sf.Program = string(b)
}
// read the program from a URL
if sf.URL != "" {
err := func() error {
resp, err := http.Get(sf.URL)
if err != nil {
return err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
sf.Program = string(b)
return nil
}()
if err != nil {
return err
}
}
return nil
}
func (sf *Filter) Run(reader io.Reader, writer io.Writer) error {
// retain map of inputs to outputs by id so if the name is changed by the
// starlark program, we are able to match the same resources
value, err := sf.readResourceList(reader)
if err != nil {
return errors.Wrap(err)
}
// run the starlark as program as transformation function
thread := &starlark.Thread{Name: sf.Name}
ctx := &Context{resourceList: value}
pd, err := ctx.predeclared()
if err != nil {
return errors.Wrap(err)
}
_, err = starlark.ExecFile(thread, sf.Name, sf.Program, pd)
if err != nil {
return errors.Wrap(err)
}
return sf.writeResourceList(value, writer)
}
// inputToResourceList transforms input into a starlark.Value
func (sf *Filter) readResourceList(reader io.Reader) (starlark.Value, error) {
// read and parse the inputs
rl := bytes.Buffer{}
_, err := rl.ReadFrom(reader)
if err != nil {
return nil, errors.Wrap(err)
}
rn, err := yaml.Parse(rl.String())
if err != nil {
return nil, errors.Wrap(err)
}
// convert to a starlark value
b, err := yaml.Marshal(rn.Document()) // convert to bytes
if err != nil {
return nil, errors.Wrap(err)
}
var in map[string]interface{}
err = yaml.Unmarshal(b, &in) // convert to map[string]interface{}
if err != nil {
return nil, errors.Wrap(err)
}
return util.Marshal(in) // convert to starlark value
}
// resourceListToOutput converts the output of the starlark program to the filter output
func (sf *Filter) writeResourceList(value starlark.Value, writer io.Writer) error {
// convert the modified resourceList back into a slice of RNodes
// by first converting to a map[string]interface{}
out, err := util.Unmarshal(value)
if err != nil {
return errors.Wrap(err)
}
b, err := yaml.Marshal(out)
if err != nil {
return errors.Wrap(err)
}
rl, err := yaml.Parse(string(b))
if err != nil {
return errors.Wrap(err)
}
// preserve the comments from the input
items, err := rl.Pipe(yaml.Lookup("items"))
if err != nil {
return errors.Wrap(err)
}
var allResults kio.Results
err = items.VisitElements(func(node *yaml.RNode) error {
// starlark will serialize the resources sorting the fields alphabetically,
// format them to have a better ordering
_, err := filters.FormatFilter{}.Filter([]*yaml.RNode{node})
// Results are collected for reporting in the final ResourceList
if err != nil {
var results kio.Results
if !goerrors.As(err, &results) {
return err
}
allResults = append(allResults, results...)
}
return nil
})
if err != nil {
return errors.Wrap(err)
}
bResults, err := yaml.Marshal(allResults)
if err != nil {
return errors.Wrap(err)
}
rl, err = yaml.SetField("results", yaml.MustParse(string(bResults))).Filter(rl)
if err != nil {
return errors.Wrap(err)
}
s, err := rl.String()
if err != nil {
return errors.Wrap(err)
}
_, err = writer.Write([]byte(s))
return err
}