-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathresource.go
84 lines (64 loc) · 1.77 KB
/
resource.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
72
73
74
75
76
77
78
79
80
81
82
83
84
package kubernetes
import (
"context"
"encoding/json"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
)
type Resource struct {
kind string
resourceInterface dynamic.ResourceInterface
}
func (r Resource) List(ctx context.Context) ([]string, error) {
resList, err := r.resourceInterface.List(ctx, v1.ListOptions{})
if err != nil {
return nil, err
}
names := make([]string, 0)
for _, item := range resList.Items {
names = append(names, item.GetName())
}
return names, nil
}
func (r Resource) Get(ctx context.Context, name string) ([]byte, error) {
res, err := r.resourceInterface.Get(ctx, name, v1.GetOptions{})
if err != nil {
return nil, err
}
resBytes, err := json.Marshal(res)
if err != nil {
return nil, err
}
return resBytes, nil
}
func (r Resource) Create(ctx context.Context, resource []byte) error {
object := make(map[string]interface{})
err := json.Unmarshal(resource, &object)
if err != nil {
return err
}
unstructuredObject := &unstructured.Unstructured{Object: object}
_, err = r.resourceInterface.Create(ctx, unstructuredObject, v1.CreateOptions{})
return err
}
func (r Resource) Update(ctx context.Context, resource []byte) error {
object := make(map[string]interface{})
err := json.Unmarshal(resource, &object)
if err != nil {
return err
}
unstructuredObject := &unstructured.Unstructured{Object: object}
_, err = r.resourceInterface.Update(ctx, unstructuredObject, v1.UpdateOptions{})
return err
}
func (r Resource) Watch(ctx context.Context) (watch.Interface, error) {
return r.resourceInterface.Watch(ctx, v1.ListOptions{})
}
func (r Resource) Kind() string {
return r.kind
}
func (r Resource) Close() error {
return nil
}