-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
45 lines (39 loc) · 879 Bytes
/
main.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
package main
import (
"encoding/json"
"fmt"
"os"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
)
const dsManifest = `
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: example
namespace: default
spec:
selector:
matchLabels:
name: nginx-ds
template:
metadata:
labels:
name: nginx-ds
spec:
containers:
- name: nginx
image: nginx:latest
`
func main() {
obj := &unstructured.Unstructured{}
// decode YAML into unstructured.Unstructured
dec := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
_, gvk, _ := dec.Decode([]byte(dsManifest), nil, obj)
// Get the common metadata, and show GVK
fmt.Println(obj.GetName(), gvk.String())
// encode back to JSON
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(obj)
}