/
service.go
157 lines (147 loc) · 4.47 KB
/
service.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
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
package resource
import (
"encoding/json"
"errors"
"github.com/gin-gonic/gin"
"github.com/open-kingfisher/king-k8s/util"
"github.com/open-kingfisher/king-utils/common"
"github.com/open-kingfisher/king-utils/common/handle"
"github.com/open-kingfisher/king-utils/common/log"
"github.com/open-kingfisher/king-utils/kit"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
)
type ServiceResource struct {
Params *handle.Resources
PostData *v1.Service
}
func (r *ServiceResource) Get() (*v1.Service, error) {
s, err := r.Params.ClientSet.CoreV1().Services(r.Params.Namespace).Get(r.Params.Name, metav1.GetOptions{})
if err == nil {
s.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{Kind: "Service", Version: "v1"})
}
return s, err
}
func (r *ServiceResource) List() (*v1.ServiceList, error) {
return r.Params.ClientSet.CoreV1().Services(r.Params.Namespace).List(metav1.ListOptions{})
}
func (r *ServiceResource) Delete() (err error) {
if err = r.Params.ClientSet.CoreV1().Services(r.Params.Namespace).Delete(r.Params.Name, &metav1.DeleteOptions{}); err != nil {
return
}
auditLog := handle.AuditLog{
Kind: common.Service,
ActionType: common.Delete,
Resources: r.Params,
Name: r.Params.Name,
}
if err = auditLog.InsertAuditLog(); err != nil {
return
}
return
}
func (r *ServiceResource) Patch() (res *v1.Service, err error) {
var data []byte
if data, err = json.Marshal(r.Params.PatchData.Patches); err != nil {
return
}
if res, err = r.Params.ClientSet.CoreV1().Services(r.Params.Namespace).Patch(r.Params.Name, types.JSONPatchType, data); err != nil {
log.Errorf("Service patch error:%s; Json:%+v; Name:%s", err, string(data), r.Params.Name)
return
}
auditLog := handle.AuditLog{
Kind: common.Service,
ActionType: common.Patch,
Resources: r.Params,
Name: r.Params.Name,
}
if err = auditLog.InsertAuditLog(); err != nil {
return
}
return
}
func (r *ServiceResource) Update() (res *v1.Service, err error) {
r.Params.Name = r.PostData.Name
old, _ := r.Get()
// 使用提交的结构体替换原来的,避免部分原有字段给删掉
old.Labels = r.PostData.Labels
old.Spec.Type = r.PostData.Spec.Type
if r.PostData.Spec.Type == "ExternalName" {
old.Spec.ExternalName = r.PostData.Spec.ExternalName
}
old.Spec.Ports = r.PostData.Spec.Ports
old.Spec.Selector = r.PostData.Spec.Selector
old.Spec.SessionAffinity = r.PostData.Spec.SessionAffinity
if res, err = r.Params.ClientSet.CoreV1().Services(r.Params.Namespace).Update(old); err != nil {
log.Errorf("Service update error:%s; Json:%+v; Name:%s", err, r.PostData, r.PostData.Name)
return
}
auditLog := handle.AuditLog{
Kind: common.Service,
ActionType: common.Update,
PostType: common.ActionType(r.Params.PostType),
Resources: r.Params,
Name: r.PostData.Name,
PostData: r.PostData,
}
if err = auditLog.InsertAuditLog(); err != nil {
return
}
return
}
func (r *ServiceResource) Create() (res *v1.Service, err error) {
if res, err = r.Params.ClientSet.CoreV1().Services(r.Params.Namespace).Create(r.PostData); err != nil {
log.Errorf("Service create error:%s; Json:%+v; Name:%s", err, r.PostData, r.PostData.Name)
return
}
auditLog := handle.AuditLog{
Kind: common.Service,
ActionType: common.Create,
PostType: common.ActionType(r.Params.PostType),
Resources: r.Params,
Name: r.PostData.Name,
PostData: r.PostData,
}
if err = auditLog.InsertAuditLog(); err != nil {
return
}
return
}
func (r *ServiceResource) ListPodByService() (*v1.PodList, error) {
if service, err := r.Get(); err != nil {
return nil, err
} else {
labelSelector := util.GenerateLabelSelector(service.Spec.Selector)
if pods, err := util.GetPodBySelectorLabel(labelSelector, r.Params.Namespace, r.Params.ClientSet); err != nil {
log.Errorf("get pod by service error:%s", err)
return nil, err
} else {
return pods, nil
}
}
}
func (r *ServiceResource) GenerateCreateData(c *gin.Context) (err error) {
switch r.Params.DataType {
case "yaml":
var j []byte
create := common.PostType{}
if err = c.BindJSON(&create); err != nil {
return
}
if j, _, err = kit.YamlToJson(create.Context); err != nil {
return
}
if err = json.Unmarshal(j, &r.PostData); err != nil {
return
}
case "json":
if err = c.BindJSON(&r.PostData); err != nil {
return
}
default:
return errors.New(common.ContentTypeError)
}
return nil
}