forked from Qihoo360/wayne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
167 lines (147 loc) · 4.94 KB
/
handler.go
File metadata and controls
167 lines (147 loc) · 4.94 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Proxy is a package responsible for doing common operations on kubernetes resources
// like UPDATE DELETE CREATE GET deployment and so on.
package client
import (
"fmt"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"github.com/Qihoo360/wayne/src/backend/client/api"
"github.com/Qihoo360/wayne/src/backend/util/logs"
)
type ResourceHandler interface {
Create(kind string, namespace string, object *runtime.Unknown) (*runtime.Unknown, error)
Update(kind string, namespace string, name string, object *runtime.Unknown) (*runtime.Unknown, error)
Get(kind string, namespace string, name string) (runtime.Object, error)
List(kind string, namespace string, labelSelector string) ([]runtime.Object, error)
Delete(kind string, namespace string, name string, options *meta_v1.DeleteOptions) error
}
type resourceHandler struct {
client *kubernetes.Clientset
cacheFactory *CacheFactory
}
func NewResourceHandler(kubeClient *kubernetes.Clientset, cacheFactory *CacheFactory) ResourceHandler {
return &resourceHandler{
client: kubeClient,
cacheFactory: cacheFactory,
}
}
func (h *resourceHandler) Create(kind string, namespace string, object *runtime.Unknown) (*runtime.Unknown, error) {
resource, ok := api.KindToResourceMap[kind]
if !ok {
return nil, fmt.Errorf("Resource kind (%s) not support yet . ", kind)
}
kubeClient := h.getClientByGroupVersion(resource.GroupVersionResourceKind.GroupVersionResource)
req := kubeClient.Post().
Resource(kind).
SetHeader("Content-Type", "application/json").
Body([]byte(object.Raw))
if resource.Namespaced {
req.Namespace(namespace)
}
var result runtime.Unknown
err := req.Do().Into(&result)
return &result, err
}
func (h *resourceHandler) Update(kind string, namespace string, name string, object *runtime.Unknown) (*runtime.Unknown, error) {
resource, ok := api.KindToResourceMap[kind]
if !ok {
return nil, fmt.Errorf("Resource kind (%s) not support yet . ", kind)
}
kubeClient := h.getClientByGroupVersion(resource.GroupVersionResourceKind.GroupVersionResource)
req := kubeClient.Put().
Resource(kind).
Name(name).
SetHeader("Content-Type", "application/json").
Body([]byte(object.Raw))
if resource.Namespaced {
req.Namespace(namespace)
}
var result runtime.Unknown
err := req.Do().Into(&result)
return &result, err
}
func (h *resourceHandler) Delete(kind string, namespace string, name string, options *meta_v1.DeleteOptions) error {
resource, ok := api.KindToResourceMap[kind]
if !ok {
return fmt.Errorf("Resource kind (%s) not support yet . ", kind)
}
kubeClient := h.getClientByGroupVersion(resource.GroupVersionResourceKind.GroupVersionResource)
req := kubeClient.Delete().
Resource(kind).
Name(name).
Body(options)
if resource.Namespaced {
req.Namespace(namespace)
}
return req.Do().Error()
}
// Get object from cache
func (h *resourceHandler) Get(kind string, namespace string, name string) (runtime.Object, error) {
resource, ok := api.KindToResourceMap[kind]
if !ok {
return nil, fmt.Errorf("Resource kind (%s) not support yet . ", kind)
}
genericInformer, err := h.cacheFactory.sharedInformerFactory.ForResource(resource.GroupVersionResourceKind.GroupVersionResource)
if err != nil {
return nil, err
}
lister := genericInformer.Lister()
var result runtime.Object
if resource.Namespaced {
result, err = lister.ByNamespace(namespace).Get(name)
if err != nil {
return nil, err
}
} else {
result, err = lister.Get(name)
if err != nil {
return nil, err
}
}
result.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: resource.GroupVersionResourceKind.Group,
Version: resource.GroupVersionResourceKind.Version,
Kind: resource.GroupVersionResourceKind.Kind,
})
return result, nil
}
// Get object from cache
func (h *resourceHandler) List(kind string, namespace string, labelSelector string) ([]runtime.Object, error) {
resource, ok := api.KindToResourceMap[kind]
if !ok {
return nil, fmt.Errorf("Resource kind (%s) not support yet . ", kind)
}
genericInformer, err := h.cacheFactory.sharedInformerFactory.ForResource(resource.GroupVersionResourceKind.GroupVersionResource)
if err != nil {
return nil, err
}
selectors, err := labels.Parse(labelSelector)
if err != nil {
logs.Error("Build label selector error.", err)
return nil, err
}
lister := genericInformer.Lister()
var objs []runtime.Object
if resource.Namespaced {
objs, err = lister.ByNamespace(namespace).List(selectors)
if err != nil {
return nil, err
}
} else {
objs, err = lister.List(selectors)
if err != nil {
return nil, err
}
}
for i := range objs {
objs[i].GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: resource.GroupVersionResourceKind.Group,
Version: resource.GroupVersionResourceKind.Version,
Kind: resource.GroupVersionResourceKind.Kind,
})
}
return objs, nil
}