blob: d838d1716453bf94aa551a1e225d7951afdbb54d [file] [log] [blame]
Serge Bazanskibe538db2020-11-12 00:22:42 +01001// Copyright 2017 The kubecfg authors
2//
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16package utils
17
18import (
19 "bytes"
20 "encoding/json"
21 "io"
22 "regexp"
23 "strings"
24
25 goyaml "github.com/ghodss/yaml"
26
27 jsonnet "github.com/google/go-jsonnet"
28 jsonnetAst "github.com/google/go-jsonnet/ast"
29 "k8s.io/apimachinery/pkg/util/yaml"
30)
31
32func resolveImage(resolver Resolver, image string) (string, error) {
33 n, err := ParseImageName(image)
34 if err != nil {
35 return "", err
36 }
37
38 if err := resolver.Resolve(&n); err != nil {
39 return "", err
40 }
41
42 return n.String(), nil
43}
44
45// RegisterNativeFuncs adds kubecfg's native jsonnet functions to provided VM
46func RegisterNativeFuncs(vm *jsonnet.VM, resolver Resolver) {
47 // TODO(mkm): go-jsonnet 0.12.x now contains native std.parseJson; deprecate and remove this one.
48 vm.NativeFunction(&jsonnet.NativeFunction{
49 Name: "parseJson",
50 Params: []jsonnetAst.Identifier{"json"},
51 Func: func(args []interface{}) (res interface{}, err error) {
52 data := []byte(args[0].(string))
53 err = json.Unmarshal(data, &res)
54 return
55 },
56 })
57
58 vm.NativeFunction(&jsonnet.NativeFunction{
59 Name: "parseYaml",
60 Params: []jsonnetAst.Identifier{"yaml"},
61 Func: func(args []interface{}) (res interface{}, err error) {
62 ret := []interface{}{}
63 data := []byte(args[0].(string))
64 d := yaml.NewYAMLToJSONDecoder(bytes.NewReader(data))
65 for {
66 var doc interface{}
67 if err := d.Decode(&doc); err != nil {
68 if err == io.EOF {
69 break
70 }
71 return nil, err
72 }
73 ret = append(ret, doc)
74 }
75 return ret, nil
76 },
77 })
78
79 vm.NativeFunction(&jsonnet.NativeFunction{
80 Name: "manifestJson",
81 Params: []jsonnetAst.Identifier{"json", "indent"},
82 Func: func(args []interface{}) (res interface{}, err error) {
83 value := args[0]
84 indent := int(args[1].(float64))
85 data, err := json.MarshalIndent(value, "", strings.Repeat(" ", indent))
86 if err != nil {
87 return "", err
88 }
89 data = append(data, byte('\n'))
90 return string(data), nil
91 },
92 })
93
94 vm.NativeFunction(&jsonnet.NativeFunction{
95 Name: "manifestYaml",
96 Params: []jsonnetAst.Identifier{"json"},
97 Func: func(args []interface{}) (res interface{}, err error) {
98 value := args[0]
99 output, err := goyaml.Marshal(value)
100 return string(output), err
101 },
102 })
103
104 vm.NativeFunction(&jsonnet.NativeFunction{
105 Name: "resolveImage",
106 Params: []jsonnetAst.Identifier{"image"},
107 Func: func(args []interface{}) (res interface{}, err error) {
108 return resolveImage(resolver, args[0].(string))
109 },
110 })
111
112 vm.NativeFunction(&jsonnet.NativeFunction{
113 Name: "escapeStringRegex",
114 Params: []jsonnetAst.Identifier{"str"},
115 Func: func(args []interface{}) (res interface{}, err error) {
116 return regexp.QuoteMeta(args[0].(string)), nil
117 },
118 })
119
120 vm.NativeFunction(&jsonnet.NativeFunction{
121 Name: "regexMatch",
122 Params: []jsonnetAst.Identifier{"regex", "string"},
123 Func: func(args []interface{}) (res interface{}, err error) {
124 return regexp.MatchString(args[0].(string), args[1].(string))
125 },
126 })
127
128 vm.NativeFunction(&jsonnet.NativeFunction{
129 Name: "regexSubst",
130 Params: []jsonnetAst.Identifier{"regex", "src", "repl"},
131 Func: func(args []interface{}) (res interface{}, err error) {
132 regex := args[0].(string)
133 src := args[1].(string)
134 repl := args[2].(string)
135
136 r, err := regexp.Compile(regex)
137 if err != nil {
138 return "", err
139 }
140 return r.ReplaceAllString(src, repl), nil
141 },
142 })
143}