-
Notifications
You must be signed in to change notification settings - Fork 373
/
serviceaccount.go
177 lines (148 loc) · 4.79 KB
/
serviceaccount.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package serviceaccount manipulates the k8s api of serviceaccount object
package serviceaccount
import (
"bytes"
"encoding/json"
"github.com/pkg/errors"
apiv1 "k8s.io/api/core/v1"
"github.com/erda-project/erda/modules/scheduler/executor/plugins/k8s/k8serror"
"github.com/erda-project/erda/pkg/http/httpclient"
"github.com/erda-project/erda/pkg/strutil"
)
// ServiceAccount is the object to encapsulate secrets
type ServiceAccount struct {
addr string
client *httpclient.HTTPClient
}
// Option configures an ServiceAccount
type Option func(*ServiceAccount)
// New news an ServiceAccount
func New(options ...Option) *ServiceAccount {
ns := &ServiceAccount{}
for _, op := range options {
op(ns)
}
return ns
}
// WithCompleteParams provides an Option
func WithCompleteParams(addr string, client *httpclient.HTTPClient) Option {
return func(s *ServiceAccount) {
s.addr = addr
s.client = client
}
}
// Create create a k8s serviceaccount
func (s *ServiceAccount) Create(sa *apiv1.ServiceAccount) error {
var b bytes.Buffer
path := strutil.Concat("/api/v1/namespaces/", sa.Namespace, "/serviceaccounts")
resp, err := s.client.Post(s.addr).
Path(path).
JSONBody(sa).
Do().
Body(&b)
if err != nil {
return errors.Errorf("failed to create serviceaccounts, namespace: %s, name: %s, (%v)", sa.Namespace, sa.Name, err)
}
if !resp.IsOK() {
return errors.Errorf("failed to create serviceaccounts, namespace: %s, name: %s, statuscode: %v, body: %v",
sa.Namespace, sa.Name, resp.StatusCode(), b.String())
}
return nil
}
// Get gets a k8s serviceaccount
func (s *ServiceAccount) Get(namespace, name string) (*apiv1.ServiceAccount, error) {
var b bytes.Buffer
path := strutil.Concat("/api/v1/namespaces/", namespace, "/serviceaccounts/", name)
resp, err := s.client.Get(s.addr).
Path(path).
Do().
Body(&b)
if err != nil {
return nil, errors.Errorf("failed to get serviceaccounts, name: %s, (%v)", name, err)
}
if !resp.IsOK() {
if resp.IsNotfound() {
return nil, k8serror.ErrNotFound
}
return nil, errors.Errorf("failed to get serviceaccounts, name: %s, statuscode: %v, body: %v",
name, resp.StatusCode(), b.String())
}
sa := &apiv1.ServiceAccount{}
if err := json.NewDecoder(&b).Decode(sa); err != nil {
return nil, err
}
return sa, nil
}
// Patch patches a k8s serviceaccount object
func (s *ServiceAccount) Patch(sa *apiv1.ServiceAccount) error {
var b bytes.Buffer
path := strutil.Concat("/api/v1/namespaces/", sa.Namespace, "/serviceaccounts/", sa.Name)
resp, err := s.client.Patch(s.addr).
Path(path).
JSONBody(sa).
Header("Accept", "application/json").
Header("Content-Type", "application/strategic-merge-patch+json").
Do().
Body(&b)
if err != nil {
return errors.Errorf("failed to Patch serviceaccount, name: %s, (%v)", sa.Name, err)
}
if !resp.IsOK() {
if resp.StatusCode() == 409 && strutil.Contains(b.String(), "Conflict") {
return errors.New("Conflict")
}
return errors.Errorf("failed to Patch serviceaccount, statuscode: %v, body: %v", resp.StatusCode(), b.String())
}
return nil
}
// Delete deletes a k8s serviceaccount object
func (s *ServiceAccount) Delete(namespace, name string) error {
var b bytes.Buffer
path := strutil.Concat("/api/v1/namespaces/", namespace, "/serviceaccounts/", name)
resp, err := s.client.Delete(s.addr).
Path(path).
Do().
Body(&b)
if err != nil {
return errors.Errorf("failed to delete serviceaccounts, namespace: %s, name: %s, (%v)", namespace, name, err)
}
if !resp.IsOK() {
if resp.IsNotfound() {
return nil
}
return errors.Errorf("failed to delete serviceaccounts, namespace: %s, name: %s, statuscode: %v, body: %v",
namespace, name, resp.StatusCode(), b.String())
}
return nil
}
// Exists decides whether a serviceaccount exists
func (s *ServiceAccount) Exists(namespace, name string) error {
path := strutil.Concat("/api/v1/namespaces/", namespace, "/serviceaccounts/", name)
resp, err := s.client.Get(s.addr).
Path(path).
Do().
DiscardBody()
if err != nil {
return err
}
if !resp.IsOK() {
if resp.IsNotfound() {
return k8serror.ErrNotFound
}
return errors.Errorf("failed to get serviceaccount, ns: %s, name: %s, statuscode: %v",
namespace, name, resp.StatusCode())
}
return nil
}