forked from yunionio/cloudpods
-
Notifications
You must be signed in to change notification settings - Fork 1
/
raw.go
71 lines (61 loc) · 1.63 KB
/
raw.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package k8s
import (
"fmt"
"io/ioutil"
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
o "yunion.io/x/onecloud/pkg/mcclient/options/k8s"
)
type rawOpt struct {
o.NamespaceWithClusterOptions
KIND string `help:"resource kind"`
NAME string `help:"instance name"`
}
type rawGetOpt struct {
rawOpt
Output string `help:"Output format" short-token:"o" default:"yaml" choices:"json|yaml"`
}
type rawDeleteOpt struct {
rawOpt
}
type rawPutOpt struct {
rawOpt
File string `help:"Resource json description file" short-token:"f"`
}
func initRaw() {
R(&rawGetOpt{}, "k8s-get", "Get k8s resource instance raw info", func(s *mcclient.ClientSession, args *rawGetOpt) error {
obj, err := k8s.RawResource.Get(s, args.KIND, args.Namespace, args.NAME, args.Cluster)
if err != nil {
return err
}
if args.Output == "json" {
fmt.Println(obj.PrettyString())
} else {
printObjectYAML(obj)
}
return nil
})
R(&rawDeleteOpt{}, "k8s-delete", "Delete k8s resource instance", func(s *mcclient.ClientSession, args *rawDeleteOpt) error {
err := k8s.RawResource.Delete(s, args.KIND, args.Namespace, args.NAME, args.Cluster)
if err != nil {
return err
}
return nil
})
R(&rawPutOpt{}, "k8s-put", "Update k8s resource instance", func(s *mcclient.ClientSession, args *rawPutOpt) error {
content, err := ioutil.ReadFile(args.File)
if err != nil {
return err
}
body, err := jsonutils.Parse(content)
if err != nil {
return err
}
err = k8s.RawResource.Put(s, args.KIND, args.Namespace, args.NAME, body, args.Cluster)
if err != nil {
return err
}
return nil
})
}