cluster/tools/kartongips: init
This forks bitnami/kubecfg into kartongips. The rationale is that we
want to implement hscloud-specific functionality that wouldn't really be
upstreamable into kubecfg (like secret support, mulit-cluster support).
We forked off from github.com/q3k/kubecfg at commit b6817a94492c561ed61a44eeea2d92dcf2e6b8c0.
Change-Id: If5ba513905e0a86f971576fe7061a471c1d8b398
diff --git a/cluster/tools/kartongips/utils/nativefuncs.go b/cluster/tools/kartongips/utils/nativefuncs.go
new file mode 100644
index 0000000..d838d17
--- /dev/null
+++ b/cluster/tools/kartongips/utils/nativefuncs.go
@@ -0,0 +1,143 @@
+// Copyright 2017 The kubecfg authors
+//
+//
+// 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 utils
+
+import (
+ "bytes"
+ "encoding/json"
+ "io"
+ "regexp"
+ "strings"
+
+ goyaml "github.com/ghodss/yaml"
+
+ jsonnet "github.com/google/go-jsonnet"
+ jsonnetAst "github.com/google/go-jsonnet/ast"
+ "k8s.io/apimachinery/pkg/util/yaml"
+)
+
+func resolveImage(resolver Resolver, image string) (string, error) {
+ n, err := ParseImageName(image)
+ if err != nil {
+ return "", err
+ }
+
+ if err := resolver.Resolve(&n); err != nil {
+ return "", err
+ }
+
+ return n.String(), nil
+}
+
+// RegisterNativeFuncs adds kubecfg's native jsonnet functions to provided VM
+func RegisterNativeFuncs(vm *jsonnet.VM, resolver Resolver) {
+ // TODO(mkm): go-jsonnet 0.12.x now contains native std.parseJson; deprecate and remove this one.
+ vm.NativeFunction(&jsonnet.NativeFunction{
+ Name: "parseJson",
+ Params: []jsonnetAst.Identifier{"json"},
+ Func: func(args []interface{}) (res interface{}, err error) {
+ data := []byte(args[0].(string))
+ err = json.Unmarshal(data, &res)
+ return
+ },
+ })
+
+ vm.NativeFunction(&jsonnet.NativeFunction{
+ Name: "parseYaml",
+ Params: []jsonnetAst.Identifier{"yaml"},
+ Func: func(args []interface{}) (res interface{}, err error) {
+ ret := []interface{}{}
+ data := []byte(args[0].(string))
+ d := yaml.NewYAMLToJSONDecoder(bytes.NewReader(data))
+ for {
+ var doc interface{}
+ if err := d.Decode(&doc); err != nil {
+ if err == io.EOF {
+ break
+ }
+ return nil, err
+ }
+ ret = append(ret, doc)
+ }
+ return ret, nil
+ },
+ })
+
+ vm.NativeFunction(&jsonnet.NativeFunction{
+ Name: "manifestJson",
+ Params: []jsonnetAst.Identifier{"json", "indent"},
+ Func: func(args []interface{}) (res interface{}, err error) {
+ value := args[0]
+ indent := int(args[1].(float64))
+ data, err := json.MarshalIndent(value, "", strings.Repeat(" ", indent))
+ if err != nil {
+ return "", err
+ }
+ data = append(data, byte('\n'))
+ return string(data), nil
+ },
+ })
+
+ vm.NativeFunction(&jsonnet.NativeFunction{
+ Name: "manifestYaml",
+ Params: []jsonnetAst.Identifier{"json"},
+ Func: func(args []interface{}) (res interface{}, err error) {
+ value := args[0]
+ output, err := goyaml.Marshal(value)
+ return string(output), err
+ },
+ })
+
+ vm.NativeFunction(&jsonnet.NativeFunction{
+ Name: "resolveImage",
+ Params: []jsonnetAst.Identifier{"image"},
+ Func: func(args []interface{}) (res interface{}, err error) {
+ return resolveImage(resolver, args[0].(string))
+ },
+ })
+
+ vm.NativeFunction(&jsonnet.NativeFunction{
+ Name: "escapeStringRegex",
+ Params: []jsonnetAst.Identifier{"str"},
+ Func: func(args []interface{}) (res interface{}, err error) {
+ return regexp.QuoteMeta(args[0].(string)), nil
+ },
+ })
+
+ vm.NativeFunction(&jsonnet.NativeFunction{
+ Name: "regexMatch",
+ Params: []jsonnetAst.Identifier{"regex", "string"},
+ Func: func(args []interface{}) (res interface{}, err error) {
+ return regexp.MatchString(args[0].(string), args[1].(string))
+ },
+ })
+
+ vm.NativeFunction(&jsonnet.NativeFunction{
+ Name: "regexSubst",
+ Params: []jsonnetAst.Identifier{"regex", "src", "repl"},
+ Func: func(args []interface{}) (res interface{}, err error) {
+ regex := args[0].(string)
+ src := args[1].(string)
+ repl := args[2].(string)
+
+ r, err := regexp.Compile(regex)
+ if err != nil {
+ return "", err
+ }
+ return r.ReplaceAllString(src, repl), nil
+ },
+ })
+}