-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.go
50 lines (41 loc) · 1.43 KB
/
api.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
package api
import (
"bytes"
"text/template"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes/scheme"
)
// ProcessTemplate substitutes the supplied values into a yaml template.
func ProcessTemplate(yamlTemplate string, values interface{}) (yaml []byte, err error) {
t, err := template.New("").Parse(yamlTemplate)
if err != nil {
return nil, errors.Wrapf(err, "unable to parse the yaml template\n%s", yamlTemplate)
}
var buf bytes.Buffer
err = t.Execute(&buf, values)
if err != nil {
return nil, errors.Wrapf(err, "unable to execute the yaml template with the values:\n%#v", values)
}
return buf.Bytes(), nil
}
// DeserializeObject reads a k8s object from yaml.
func DeserializeObject(yaml []byte) (runtime.Object, error) {
serializer := scheme.Codecs.UniversalDeserializer()
obj, _, err := serializer.Decode(yaml, nil, nil)
if err != nil {
return nil, errors.Wrapf(err, "cannot decode the yaml into a k8s runtime object\n%s", string(yaml))
}
return obj, err
}
// SerializeObject returns the yaml representation of a k8s object.
func SerializeObject(obj runtime.Object) (string, error) {
serializer := json.NewYAMLSerializer(json.DefaultMetaFactory, nil, nil)
var buf bytes.Buffer
err := serializer.Encode(obj, &buf)
if err != nil {
return "", errors.Wrapf(err, "unable to serialize object:\n%#v", obj)
}
return buf.String(), nil
}