forked from open-kingfisher/king-k8s
/
role.go
124 lines (115 loc) · 3.28 KB
/
role.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
package resource
import (
"encoding/json"
"errors"
"github.com/gin-gonic/gin"
"github.com/duiniwukenaihe/king-utils/common"
"github.com/duiniwukenaihe/king-utils/common/handle"
"github.com/duiniwukenaihe/king-utils/common/log"
"github.com/duiniwukenaihe/king-utils/kit"
"k8s.io/api/rbac/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
type RoleResource struct {
Params *handle.Resources
PostData *v1beta1.Role
}
func (r *RoleResource) Get() (*v1beta1.Role, error) {
return r.Params.ClientSet.RbacV1beta1().Roles(r.Params.Namespace).Get(r.Params.Name, metav1.GetOptions{})
}
func (r *RoleResource) List() (*v1beta1.RoleList, error) {
return r.Params.ClientSet.RbacV1beta1().Roles(r.Params.Namespace).List(metav1.ListOptions{})
}
func (r *RoleResource) Delete() (err error) {
if err = r.Params.ClientSet.RbacV1beta1().Roles(r.Params.Namespace).Delete(r.Params.Name, &metav1.DeleteOptions{}); err != nil {
return
}
auditLog := handle.AuditLog{
Kind: common.Role,
ActionType: common.Delete,
Resources: r.Params,
Name: r.Params.Name,
}
if err = auditLog.InsertAuditLog(); err != nil {
return
}
return
}
func (r *RoleResource) Patch() (res *v1beta1.Role, err error) {
var data []byte
if data, err = json.Marshal(r.Params.PatchData.Patches); err != nil {
return
}
if res, err = r.Params.ClientSet.RbacV1beta1().Roles(r.Params.Namespace).Patch(r.Params.Name, types.JSONPatchType, data); err != nil {
log.Errorf("Role patch error:%s; Json:%+v; Name:%s", err, string(data), r.Params.Name)
return
}
auditLog := handle.AuditLog{
Kind: common.Role,
ActionType: common.Patch,
Resources: r.Params,
Name: r.Params.Name,
}
if err = auditLog.InsertAuditLog(); err != nil {
return
}
return
}
func (r *RoleResource) Update() (res *v1beta1.Role, err error) {
if res, err = r.Params.ClientSet.RbacV1beta1().Roles(r.Params.Namespace).Update(r.PostData); err != nil {
log.Errorf("Role update error:%s; Json:%+v; Name:%s", err, r.PostData, r.PostData.Name)
return
}
auditLog := handle.AuditLog{
Kind: common.Role,
ActionType: common.Update,
Resources: r.Params,
Name: r.PostData.Name,
PostData: r.PostData,
}
if err = auditLog.InsertAuditLog(); err != nil {
return
}
return
}
func (r *RoleResource) Create() (res *v1beta1.Role, err error) {
if res, err = r.Params.ClientSet.RbacV1beta1().Roles(r.Params.Namespace).Create(r.PostData); err != nil {
log.Errorf("Role create error:%s; Json:%+v; Name:%s", err, r.PostData, r.PostData.Name)
return
}
auditLog := handle.AuditLog{
Kind: common.Role,
ActionType: common.Create,
Resources: r.Params,
Name: r.PostData.Name,
PostData: r.PostData,
}
if err = auditLog.InsertAuditLog(); err != nil {
return
}
return
}
func (r *RoleResource) 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
}