forked from kptdev/kpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runfn.go
407 lines (351 loc) · 11.7 KB
/
runfn.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package runfn
import (
"context"
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"github.com/GoogleContainerTools/kpt/internal/printer"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
"github.com/GoogleContainerTools/kpt/internal/fnruntime"
"github.com/GoogleContainerTools/kpt/internal/pkg"
"github.com/GoogleContainerTools/kpt/internal/types"
"github.com/GoogleContainerTools/kpt/internal/util/printerutil"
fnresult "github.com/GoogleContainerTools/kpt/pkg/api/fnresult/v1"
kptfile "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
)
// RunFns runs the set of configuration functions in a local directory against
// the Resources in that directory
type RunFns struct {
Ctx context.Context
StorageMounts []runtimeutil.StorageMount
// Path is the path to the directory containing functions
Path string
// uniquePath is the absolute version of Path
uniquePath types.UniquePath
// FnConfigPath specifies a config file which contains the configs used in
// function input. It can be absolute or relative to kpt working directory.
// The exact format depends on the OS.
FnConfigPath string
// Function is an function to run against the input.
Function *runtimeutil.FunctionSpec
// FnConfig is the configurations passed from command line
FnConfig *yaml.RNode
// Input can be set to read the Resources from Input rather than from a directory
Input io.Reader
// Network enables network access for functions that declare it
Network bool
// Output can be set to write the result to Output rather than back to the directory
Output io.Writer
// ResultsDir is where to write each functions results
ResultsDir string
fnResults *fnresult.ResultList
// functionFilterProvider provides a filter to perform the function.
// this is a variable so it can be mocked in tests
functionFilterProvider func(
filter runtimeutil.FunctionSpec, fnConfig *yaml.RNode, currentUser currentUserFunc) (kio.Filter, error)
// AsCurrentUser is a boolean to indicate whether docker container should use
// the uid and gid that run the command
AsCurrentUser bool
// Env contains environment variables that will be exported to container
Env []string
// ContinueOnEmptyResult configures what happens when the underlying pipeline
// returns an empty result.
// If it is false (default), subsequent functions will be skipped and the
// result will be returned immediately.
// If it is true, the empty result will be provided as input to the next
// function in the list.
ContinueOnEmptyResult bool
// IncludeMetaResources indicates will kpt add pkg meta resources such as
// Kptfile to the input resources to the function.
IncludeMetaResources bool
// ExecArgs are the arguments for exec commands
ExecArgs []string
// OriginalExec is the original exec commands
OriginalExec string
ImagePullPolicy fnruntime.ImagePullPolicy
Selector kptfile.Selector
}
// Execute runs the command
func (r RunFns) Execute() error {
// default the containerFilterProvider if it hasn't been override. Split out for testing.
err := (&r).init()
if err != nil {
return err
}
nodes, fltrs, output, err := r.getNodesAndFilters()
if err != nil {
return err
}
return r.runFunctions(nodes, output, fltrs)
}
func (r RunFns) getNodesAndFilters() (
*kio.PackageBuffer, []kio.Filter, *kio.LocalPackageReadWriter, error) {
// Read Resources from Directory or Input
buff := &kio.PackageBuffer{}
p := kio.Pipeline{Outputs: []kio.Writer{buff}}
// save the output dir because we will need it to write back
// the same one for reading must be used for writing if deleting Resources
var outputPkg *kio.LocalPackageReadWriter
matchFilesGlob := kio.MatchAll
if r.IncludeMetaResources {
matchFilesGlob = append(matchFilesGlob, kptfile.KptFileName)
}
if r.Path != "" {
functionConfigFilter, err := pkg.FunctionConfigFilterFunc(filesys.FileSystemOrOnDisk{}, r.uniquePath, r.IncludeMetaResources)
if err != nil {
return nil, nil, outputPkg, err
}
outputPkg = &kio.LocalPackageReadWriter{
PackagePath: string(r.uniquePath),
MatchFilesGlob: matchFilesGlob,
FileSkipFunc: functionConfigFilter,
PreserveSeqIndent: true,
PackageFileName: kptfile.KptFileName,
IncludeSubpackages: true,
WrapBareSeqNode: true,
}
}
if r.Input == nil {
p.Inputs = []kio.Reader{outputPkg}
} else {
p.Inputs = []kio.Reader{&kio.ByteReader{Reader: r.Input, PreserveSeqIndent: true, WrapBareSeqNode: true}}
}
if err := p.Execute(); err != nil {
return nil, nil, outputPkg, err
}
fltrs, err := r.getFilters()
if err != nil {
return nil, nil, outputPkg, err
}
return buff, fltrs, outputPkg, nil
}
func (r RunFns) getFilters() ([]kio.Filter, error) {
spec := r.Function
if spec == nil {
return nil, nil
}
// merge envs from imperative and declarative
spec.Container.Env = r.mergeContainerEnv(spec.Container.Env)
c, err := r.functionFilterProvider(*spec, r.FnConfig, user.Current)
if err != nil {
return nil, err
}
if c == nil {
return nil, nil
}
return []kio.Filter{c}, nil
}
// runFunctions runs the fltrs against the input and writes to either r.Output or output
func (r RunFns) runFunctions(input kio.Reader, output kio.Writer, fltrs []kio.Filter) error {
// use the previously read Resources as input
var outputs []kio.Writer
if r.Output == nil {
// write back to the package
outputs = append(outputs, output)
} else {
// write to the output instead of the directory if r.Output is specified or
// the output is nil (reading from Input)
outputs = append(outputs, kio.ByteWriter{
Writer: r.Output,
KeepReaderAnnotations: true,
WrappingKind: kio.ResourceListKind,
WrappingAPIVersion: kio.ResourceListAPIVersion,
})
}
inputResources, err := input.Read()
if err != nil {
return err
}
selectedInput := inputResources
if !r.Selector.IsEmpty() {
err = fnruntime.SetResourceIds(inputResources)
if err != nil {
return err
}
// select the resources on which function should be applied
selectedInput, err = fnruntime.SelectInput(
inputResources,
[]kptfile.Selector{r.Selector},
&fnruntime.SelectionContext{RootPackagePath: r.uniquePath})
if err != nil {
return err
}
}
pb := &kio.PackageBuffer{}
pipeline := kio.Pipeline{
Inputs: []kio.Reader{&kio.PackageBuffer{Nodes: selectedInput}},
Filters: fltrs,
Outputs: []kio.Writer{pb},
ContinueOnEmptyResult: r.ContinueOnEmptyResult,
}
err = pipeline.Execute()
outputResources := pb.Nodes
if !r.Selector.IsEmpty() {
outputResources = fnruntime.MergeWithInput(pb.Nodes, selectedInput, inputResources)
deleteAnnoErr := fnruntime.DeleteResourceIds(outputResources)
if deleteAnnoErr != nil {
return deleteAnnoErr
}
}
if err == nil {
writeErr := outputs[0].Write(outputResources)
if writeErr != nil {
return writeErr
}
}
resultsFile, resultErr := fnruntime.SaveResults(filesys.FileSystemOrOnDisk{}, r.ResultsDir, r.fnResults)
if err != nil {
// function fails
if resultErr == nil {
r.printFnResultsStatus(resultsFile)
}
return err
}
if resultErr == nil {
r.printFnResultsStatus(resultsFile)
}
return nil
}
func (r RunFns) printFnResultsStatus(resultsFile string) {
printerutil.PrintFnResultInfo(r.Ctx, resultsFile, true)
}
// mergeContainerEnv will merge the envs specified by command line (imperative) and config
// file (declarative). If they have same key, the imperative value will be respected.
func (r RunFns) mergeContainerEnv(envs []string) []string {
imperative := fnruntime.NewContainerEnvFromStringSlice(r.Env)
declarative := fnruntime.NewContainerEnvFromStringSlice(envs)
for key, value := range imperative.EnvVars {
declarative.AddKeyValue(key, value)
}
for _, key := range imperative.VarsToExport {
declarative.AddKey(key)
}
return declarative.Raw()
}
// init initializes the RunFns with a containerFilterProvider.
func (r *RunFns) init() error {
// if no path is specified, default reading from stdin and writing to stdout
if r.Path == "" {
if r.Output == nil {
r.Output = printer.FromContextOrDie(r.Ctx).OutStream()
}
if r.Input == nil {
r.Input = os.Stdin
}
} else {
// make the path absolute so it works on mac
var err error
absPath, err := filepath.Abs(r.Path)
if err != nil {
return errors.Wrap(err)
}
r.uniquePath = types.UniquePath(absPath)
}
r.fnResults = fnresult.NewResultList()
// functionFilterProvider set the filter provider
if r.functionFilterProvider == nil {
r.functionFilterProvider = r.defaultFnFilterProvider
}
// fn config path should be absolute
if r.FnConfigPath != "" && !filepath.IsAbs(r.FnConfigPath) {
// if the FnConfigPath is relative, we should use the
// current directory to construct full path.
path, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get working directory: %w", err)
}
r.FnConfigPath = filepath.Join(path, r.FnConfigPath)
}
return nil
}
type currentUserFunc func() (*user.User, error)
// getUIDGID will return "nobody" if asCurrentUser is false. Otherwise
// return "uid:gid" according to the return from currentUser function.
func getUIDGID(asCurrentUser bool, currentUser currentUserFunc) (string, error) {
if !asCurrentUser {
return "nobody", nil
}
u, err := currentUser()
if err != nil {
return "", err
}
return fmt.Sprintf("%s:%s", u.Uid, u.Gid), nil
}
// getFunctionConfig returns yaml representation of functionConfig that can
// be provided to a function as input.
func (r *RunFns) getFunctionConfig() (*yaml.RNode, error) {
return kptfile.GetValidatedFnConfigFromPath(filesys.FileSystemOrOnDisk{}, "", r.FnConfigPath)
}
// defaultFnFilterProvider provides function filters
func (r *RunFns) defaultFnFilterProvider(spec runtimeutil.FunctionSpec, fnConfig *yaml.RNode, currentUser currentUserFunc) (kio.Filter, error) {
if spec.Container.Image == "" && spec.Exec.Path == "" {
return nil, fmt.Errorf("either image name or executable path need to be provided")
}
var err error
if r.FnConfigPath != "" {
fnConfig, err = r.getFunctionConfig()
if err != nil {
return nil, err
}
}
var fltr *runtimeutil.FunctionFilter
fnResult := &fnresult.Result{
// TODO(droot): This is required for making structured results subpackage aware.
// Enable this once test harness supports filepath based assertions.
// Pkg: string(r.uniquePath),
}
if spec.Container.Image != "" {
// TODO: Add a test for this behavior
uidgid, err := getUIDGID(r.AsCurrentUser, currentUser)
if err != nil {
return nil, err
}
c := &fnruntime.ContainerFn{
Path: r.uniquePath,
Image: spec.Container.Image,
ImagePullPolicy: r.ImagePullPolicy,
UIDGID: uidgid,
StorageMounts: r.StorageMounts,
Env: spec.Container.Env,
FnResult: fnResult,
Perm: fnruntime.ContainerFnPermission{
AllowNetwork: r.Network,
// mounts are always from CLI flags so we allow
// them by default for eval
AllowMount: true,
},
}
fltr = &runtimeutil.FunctionFilter{
Run: c.Run,
FunctionConfig: fnConfig,
DeferFailure: spec.DeferFailure,
}
fnResult.Image = spec.Container.Image
}
if spec.Exec.Path != "" {
e := &fnruntime.ExecFn{
Path: spec.Exec.Path,
Args: r.ExecArgs,
FnResult: fnResult,
}
fltr = &runtimeutil.FunctionFilter{
Run: e.Run,
FunctionConfig: fnConfig,
DeferFailure: spec.DeferFailure,
}
fnResult.ExecPath = r.OriginalExec
}
displayResourceCount := false
if !r.Selector.IsEmpty() {
displayResourceCount = true
}
return fnruntime.NewFunctionRunner(r.Ctx, fltr, "", fnResult, r.fnResults, false, displayResourceCount)
}