/
funcfile.go
412 lines (360 loc) · 12.9 KB
/
funcfile.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
408
409
410
411
412
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package common
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/fnproject/cli/config"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
var (
validFuncfileNames = [...]string{
"func.yaml",
"func.yml",
"func.json",
}
)
// InputMap to be used within FFTest.
type InputMap struct {
Body interface{}
}
// OutputMap to be used within FFTest.
type OutputMap struct {
Body interface{}
}
// FFTest represents a test for a funcfile.
type FFTest struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Input *InputMap `yaml:"input,omitempty" json:"input,omitempty"`
Output *OutputMap `yaml:"outoutput,omitempty" json:"output,omitempty"`
Err *string `yaml:"err,omitempty" json:"err,omitempty"`
// Env map[string]string `yaml:"env,omitempty" json:"env,omitempty"`
}
type inputVar struct {
Name string `yaml:"name" json:"name"`
Required bool `yaml:"required" json:"required"`
}
// Expects represents expected env vars in funcfile.
type Expects struct {
Config []inputVar `yaml:"config" json:"config"`
}
// FuncFile defines the internal structure of a func.yaml/json/yml
type FuncFile struct {
// just for posterity, this won't be set on old files, but we can check that
Schema_version int `yaml:"schema_version,omitempty" json:"schema_version,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
// Build params
Version string `yaml:"version,omitempty" json:"version,omitempty"`
Runtime string `yaml:"runtime,omitempty" json:"runtime,omitempty"`
Entrypoint string `yaml:"entrypoint,omitempty" json:"entrypoint,omitempty"`
Cmd string `yaml:"cmd,omitempty" json:"cmd,omitempty"`
Build []string `yaml:"build,omitempty" json:"build,omitempty"`
Tests []FFTest `yaml:"tests,omitempty" json:"tests,omitempty"`
BuildImage string `yaml:"build_image,omitempty" json:"build_image,omitempty"` // Image to use as base for building
RunImage string `yaml:"run_image,omitempty" json:"run_image,omitempty"` // Image to use for running
ContentType string `yaml:"content_type,omitempty" json:"content_type,omitempty"`
// Route params
// TODO embed models.Route
Type string `yaml:"type,omitempty" json:"type,omitempty"`
Memory uint64 `yaml:"memory,omitempty" json:"memory,omitempty"`
Cpus string `yaml:"cpus,omitempty" json:"cpus,omitempty"`
Timeout *int32 `yaml:"timeout,omitempty" json:"timeout,omitempty"`
Path string `yaml:"path,omitempty" json:"path,omitempty"`
Config map[string]string `yaml:"config,omitempty" json:"config,omitempty"`
IDLETimeout *int32 `yaml:"idle_timeout,omitempty" json:"idle_timeout,omitempty"`
Annotations map[string]interface{} `yaml:"annotations,omitempty" json:"annotations,omitempty"`
// Run/test
Expects Expects `yaml:"expects,omitempty" json:"expects,omitempty"`
}
// SigningDetails defines configuration for signing a function image
type SigningDetails struct {
ImageCompartmentId string `yaml:"image_compartment_id,omitempty" json:"image_compartment_id,omitempty"`
KmsKeyId string `yaml:"kms_key_id,omitempty" json:"kms_key_id,omitempty"`
KmsKeyVersionId string `yaml:"kms_key_version_id,omitempty" json:"kms_key_version_id,omitempty"`
SigningAlgorithm string `yaml:"signing_algorithm,omitempty" json:"signing_algorithm,omitempty"`
}
// FuncFileV20180708 defines the latest internal structure of a func.yaml/json/yml
type FuncFileV20180708 struct {
Schema_version int `yaml:"schema_version,omitempty" json:"schema_version,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Version string `yaml:"version,omitempty" json:"version,omitempty"`
Runtime string `yaml:"runtime,omitempty" json:"runtime,omitempty"`
Build_image string `yaml:"build_image,omitempty" json:"build_image,omitempty"` // Image to use as base for building
Run_image string `yaml:"run_image,omitempty" json:"run_image,omitempty"` // Image to use for running
Cmd string `yaml:"cmd,omitempty" json:"cmd,omitempty"`
Entrypoint string `yaml:"entrypoint,omitempty" json:"entrypoint,omitempty"`
Content_type string `yaml:"content_type,omitempty" json:"content_type,omitempty"`
Type string `yaml:"type,omitempty" json:"type,omitempty"`
Memory uint64 `yaml:"memory,omitempty" json:"memory,omitempty"`
Timeout *int32 `yaml:"timeout,omitempty" json:"timeout,omitempty"`
IDLE_timeout *int32 `yaml:"idle_timeout,omitempty" json:"idle_timeout,omitempty"`
Config map[string]string `yaml:"config,omitempty" json:"config,omitempty"`
Annotations map[string]interface{} `yaml:"annotations,omitempty" json:"annotations,omitempty"`
SigningDetails SigningDetails `yaml:"signing_details,omitempty" json:"signing_details,omitempty""`
Build []string `yaml:"build,omitempty" json:"build,omitempty"`
Expects Expects `yaml:"expects,omitempty" json:"expects,omitempty"`
Triggers []Trigger `yaml:"triggers,omitempty" json:"triggers,omitempty"`
}
// Trigger represents a trigger for a FuncFileV20180708
type Trigger struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Type string `yaml:"type,omitempty" json:"type,omitempty"`
Source string `yaml:"source,omitempty" json:"source,omitempty"`
}
// ImageName returns the name of a funcfile image
func (ff *FuncFile) ImageName() string {
fname := ff.Name
if !strings.Contains(fname, "/") {
reg := viper.GetString(config.EnvFnRegistry)
if reg != "" {
if reg[len(reg)-1] != '/' {
reg += "/"
}
fname = fmt.Sprintf("%s%s", reg, fname)
}
}
if ff.Version != "" {
fname = fmt.Sprintf("%s:%s", fname, ff.Version)
}
return fname
}
// RuntimeTag returns the runtime and tag.
func (ff *FuncFile) RuntimeTag() (runtime, tag string) {
if ff.Runtime == "" {
return "", ""
}
rt := ff.Runtime
tagpos := strings.Index(rt, ":")
if tagpos == -1 {
return rt, ""
}
return rt[:tagpos], rt[tagpos+1:]
}
// findFuncfile for a func.yaml/json/yml file in path
func FindFuncfile(path string) (string, error) {
for _, fn := range validFuncfileNames {
fullfn := filepath.Join(path, fn)
if Exists(fullfn) {
return fullfn, nil
}
}
return "", NewNotFoundError("could not find function file")
}
// FindAndParseFuncfile for a func.yaml/json/yml file.
func FindAndParseFuncfile(path string) (fpath string, ff *FuncFile, err error) {
fpath, err = FindFuncfile(path)
if err != nil {
return "", nil, err
}
ff, err = ParseFuncfile(fpath)
if err != nil {
return "", nil, err
}
return fpath, ff, err
}
// LoadFuncfile returns a parsed funcfile.
func LoadFuncfile(path string) (string, *FuncFile, error) {
return FindAndParseFuncfile(path)
}
// ParseFuncfile check file type to decode and parse.
func ParseFuncfile(path string) (*FuncFile, error) {
ext := filepath.Ext(path)
switch ext {
case ".json":
return decodeFuncfileJSON(path)
case ".yaml", ".yml":
return decodeFuncfileYAML(path)
}
return nil, errUnexpectedFileFormat
}
func storeFuncfile(path string, ff *FuncFile) error {
ext := filepath.Ext(path)
switch ext {
case ".json":
return encodeFuncfileJSON(path, ff)
case ".yaml", ".yml":
return EncodeFuncfileYAML(path, ff)
}
return errUnexpectedFileFormat
}
func decodeFuncfileJSON(path string) (*FuncFile, error) {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("could not open %s for parsing. Error: %v", path, err)
}
ff := &FuncFile{}
err = json.NewDecoder(f).Decode(ff)
return ff, err
}
func decodeFuncfileYAML(path string) (*FuncFile, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not open %s for parsing. Error: %v", path, err)
}
ff := &FuncFile{}
err = yaml.Unmarshal(b, ff)
return ff, err
}
func encodeFuncfileJSON(path string, ff *FuncFile) error {
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("could not open %s for encoding. Error: %v", path, err)
}
return json.NewEncoder(f).Encode(ff)
}
// EncodeFuncfileYAML encodes function file.
func EncodeFuncfileYAML(path string, ff *FuncFile) error {
b, err := yaml.Marshal(ff)
if err != nil {
return fmt.Errorf("could not encode function file. Error: %v", err)
}
return ioutil.WriteFile(path, b, os.FileMode(0644))
}
// IsFuncFile check vaid funcfile.
func IsFuncFile(path string, info os.FileInfo) bool {
if info.IsDir() {
return false
}
basefn := filepath.Base(path)
for _, fn := range validFuncfileNames {
if basefn == fn {
return true
}
}
return false
}
// --------- FuncFileV20180708 -------------
func FindAndParseFuncFileV20180708(path string) (fpath string, ff *FuncFileV20180708, err error) {
fpath, err = FindFuncfile(path)
if err != nil {
return "", nil, err
}
ff, err = ParseFuncFileV20180708(fpath)
if err != nil {
return "", nil, err
}
return fpath, ff, err
}
func LoadFuncFileV20180708(path string) (string, *FuncFileV20180708, error) {
return FindAndParseFuncFileV20180708(path)
}
func ParseFuncFileV20180708(path string) (ff *FuncFileV20180708, err error) {
ext := filepath.Ext(path)
switch ext {
case ".json":
ff, err = decodeFuncFileV20180708JSON(path)
case ".yaml", ".yml":
ff, err = decodeFuncFileV20180708YAML(path)
default:
return nil, errUnexpectedFileFormat
}
if err == nil && ff.Schema_version != V20180708 {
// todo: we should maybe not assume this, but it's more useful than saying 'version mismatch' for users...
return nil, fmt.Errorf("unsupported func.yaml version, please use the migrate command to update your function metadata")
}
return ff, err
}
func decodeFuncFileV20180708JSON(path string) (*FuncFileV20180708, error) {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("could not open %s for parsing. Error: %v", path, err)
}
ff := &FuncFileV20180708{}
// ff.Route = &fnmodels.Route{}
err = json.NewDecoder(f).Decode(ff)
// ff := fff.MakeFuncFile()
return ff, err
}
func decodeFuncFileV20180708YAML(path string) (*FuncFileV20180708, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not open %s for parsing. Error: %v", path, err)
}
ff := &FuncFileV20180708{}
err = yaml.Unmarshal(b, ff)
// ff := fff.MakeFuncFile()
return ff, err
}
func encodeFuncFileV20180708JSON(path string, ff *FuncFileV20180708) error {
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("could not open %s for encoding. Error: %v", path, err)
}
return json.NewEncoder(f).Encode(ff)
}
// EncodeFuncfileYAML encodes function file.
func EncodeFuncFileV20180708YAML(path string, ff *FuncFileV20180708) error {
b, err := yaml.Marshal(ff)
if err != nil {
return fmt.Errorf("could not encode function file. Error: %v", err)
}
return ioutil.WriteFile(path, b, os.FileMode(0644))
}
func storeFuncFileV20180708(path string, ff *FuncFileV20180708) error {
ext := filepath.Ext(path)
switch ext {
case ".json":
return encodeFuncFileV20180708JSON(path, ff)
case ".yaml", ".yml":
return EncodeFuncFileV20180708YAML(path, ff)
}
return errUnexpectedFileFormat
}
// ImageName returns the name of a funcfile image
func (ff *FuncFileV20180708) ImageNameV20180708() string {
fname := ff.Name
if !strings.Contains(fname, "/") {
reg := viper.GetString(config.EnvFnRegistry)
if reg != "" {
if reg[len(reg)-1] != '/' {
reg += "/"
}
fname = fmt.Sprintf("%s%s", reg, fname)
}
}
if ff.Version != "" {
fname = fmt.Sprintf("%s:%s", fname, ff.Version)
}
return fname
}
// Merge the func.init.yaml from the initImage with a.ff
// write out the new func file
func MergeFuncFileInitYAML(path string, ff *FuncFileV20180708) error {
var initFf, err = ParseFuncfile(path)
if err != nil {
return errors.New("init-image did not produce a valid func.init.yaml")
}
// Build up a combined func.yaml (in a.ff) from the init-image and defaults and cli-args
// The following fields are already in a.ff:
// config, cpus, idle_timeout, memory, name, path, timeout, type, triggers, version
// Add the following from the init-image:
// build, build_image, cmd, content_type, entrypoint, expects, headers, run_image, runtime
ff.Build = initFf.Build
ff.Build_image = initFf.BuildImage
ff.Cmd = initFf.Cmd
ff.Content_type = initFf.ContentType
ff.Entrypoint = initFf.Entrypoint
ff.Expects = initFf.Expects
ff.Run_image = initFf.RunImage
ff.Runtime = initFf.Runtime
return nil
}