文章目录
  1. 1. Parse the kubernetes manifest in yaml or json, don’t care a manifest type.

Parse the kubernetes manifest in yaml or json, don’t care a manifest type.

Examples:

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main

import (
"bytes"
"context"
"flag"
"io"
"io/ioutil"
"log"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
yamlutil "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
)

var (
kubeconfig string
filename string
)

func main() {
flag.StringVar(&kubeconfig, "kubeconfig", "", "")
flag.StringVar(&filename, "f", "", "")
flag.Parse()

b, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
log.Printf("%q \n", string(b))

config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
log.Fatal(err)
}

c, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err)
}

dd, err := dynamic.NewForConfig(config)
if err != nil {
log.Fatal(err)
}

decoder := yamlutil.NewYAMLOrJSONDecoder(bytes.NewReader(b), 100)
for {
var rawObj runtime.RawExtension
if err = decoder.Decode(&rawObj); err != nil {
break
}

obj, gvk, err := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, nil)
unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
log.Fatal(err)
}

unstructuredObj := &unstructured.Unstructured{Object: unstructuredMap}

gr, err := restmapper.GetAPIGroupResources(c.Discovery())
if err != nil {
log.Fatal(err)
}

mapper := restmapper.NewDiscoveryRESTMapper(gr)
mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
log.Fatal(err)
}

var dri dynamic.ResourceInterface
if mapping.Scope.Name() == meta.RESTScopeNameNamespace {
if unstructuredObj.GetNamespace() == "" {
unstructuredObj.SetNamespace("default")
}
dri = dd.Resource(mapping.Resource).Namespace(unstructuredObj.GetNamespace())
} else {
dri = dd.Resource(mapping.Resource)
}

if _, err := dri.Create(context.Background(), unstructuredObj, metav1.CreateOptions{}); err != nil {
log.Fatal(err)
}
}
if err != io.EOF {
log.Fatal("eof ", err)
}
}

Usage:

app.yaml:

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
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
---
apiVersion: v1
kind: Service
metadata:
labels:
app: demo
name: demo
namespace: default
spec:
ports:
- name: web
port: 80
selector:
app: demo
type: ClusterIP
1
go run demo.go -kubeconfig ~/.kube/config -f app.yaml
文章目录
  1. 1. Parse the kubernetes manifest in yaml or json, don’t care a manifest type.