github.com/splunk/dan1-qbec@v0.7.3/internal/vm/nativefuncs.go (about) 1 // 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 16 package vm 17 18 // copied from original code at https://github.com/ksonnet/kubecfg/blob/master/utils/nativefuncs.go 19 // and modified for use. 20 21 import ( 22 "bytes" 23 "encoding/json" 24 "regexp" 25 26 "github.com/google/go-jsonnet" 27 "github.com/google/go-jsonnet/ast" 28 "github.com/pkg/errors" 29 ) 30 31 // registerNativeFuncs adds kubecfg's native jsonnet functions to provided VM 32 func registerNativeFuncs(vm *jsonnet.VM) { 33 // NB: libjsonnet native functions can only pass primitive 34 // types, so some functions json-encode the arg. These 35 // "*FromJson" functions will be replaced by regular native 36 // version when libjsonnet is able to support this. 37 38 vm.NativeFunction(&jsonnet.NativeFunction{ 39 Name: "parseJson", 40 Params: []ast.Identifier{"json"}, 41 Func: func(args []interface{}) (res interface{}, err error) { 42 str := args[0].(string) 43 data := []byte(str) 44 err = json.Unmarshal(data, &res) 45 return 46 }, 47 }) 48 49 vm.NativeFunction(&jsonnet.NativeFunction{ 50 Name: "parseYaml", 51 Params: []ast.Identifier{"yaml"}, 52 Func: func(args []interface{}) (res interface{}, err error) { 53 data := []byte(args[0].(string)) 54 return parseYAMLDocuments(bytes.NewReader(data)) 55 }, 56 }) 57 58 vm.NativeFunction(&jsonnet.NativeFunction{ 59 Name: "escapeStringRegex", 60 Params: []ast.Identifier{"str"}, 61 Func: func(args []interface{}) (res interface{}, err error) { 62 return regexp.QuoteMeta(args[0].(string)), nil 63 }, 64 }) 65 66 vm.NativeFunction(&jsonnet.NativeFunction{ 67 Name: "regexMatch", 68 Params: []ast.Identifier{"regex", "string"}, 69 Func: func(args []interface{}) (res interface{}, err error) { 70 return regexp.MatchString(args[0].(string), args[1].(string)) 71 }, 72 }) 73 74 vm.NativeFunction(&jsonnet.NativeFunction{ 75 Name: "regexSubst", 76 Params: []ast.Identifier{"regex", "src", "repl"}, 77 Func: func(args []interface{}) (res interface{}, err error) { 78 regex := args[0].(string) 79 src := args[1].(string) 80 repl := args[2].(string) 81 82 r, err := regexp.Compile(regex) 83 if err != nil { 84 return "", err 85 } 86 return r.ReplaceAllString(src, repl), nil 87 }, 88 }) 89 90 vm.NativeFunction(&jsonnet.NativeFunction{ 91 Name: "expandHelmTemplate", 92 Params: []ast.Identifier{"chart", "values", "options"}, 93 Func: func(args []interface{}) (res interface{}, err error) { 94 chart := args[0].(string) 95 values := args[1].(map[string]interface{}) 96 options := args[2].(map[string]interface{}) 97 var h helmOptions 98 b, err := json.Marshal(options) 99 if err != nil { 100 return nil, errors.Wrap(err, "marshal options to JSON") 101 } 102 if err := json.Unmarshal(b, &h); err != nil { 103 return nil, errors.Wrap(err, "unmarshal options from JSON") 104 } 105 return expandHelmTemplate(chart, values, h) 106 }, 107 }) 108 109 }