/
service.go
194 lines (163 loc) · 4.99 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// 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 k8sservice manipulates the k8s api of service object
package k8sservice
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
apiv1 "k8s.io/api/core/v1"
"github.com/erda-project/erda/modules/scheduler/executor/plugins/k8s/k8sapi"
"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"
)
// Service is the object to manipulate k8s api of service
type Service struct {
addr string
client *httpclient.HTTPClient
}
// Option configures a Service
type Option func(*Service)
// New news a Service
func New(options ...Option) *Service {
ns := &Service{}
for _, op := range options {
op(ns)
}
return ns
}
// WithCompleteParams provides an Option
func WithCompleteParams(addr string, client *httpclient.HTTPClient) Option {
return func(s *Service) {
s.addr = addr
s.client = client
}
}
// Create creates a k8s service
func (s *Service) Create(service *apiv1.Service) error {
var b bytes.Buffer
path := strutil.Concat("/api/v1/namespaces/", service.Namespace, "/services")
resp, err := s.client.Post(s.addr).
Path(path).
JSONBody(service).
Do().
Body(&b)
if err != nil {
return errors.Wrapf(err, "failed to create service, name: %s, (%v)", service.Name, err)
}
if !resp.IsOK() {
return errors.Errorf("failed to create service, name: %s, statuscode: %v, body: %v",
service.Name, resp.StatusCode(), b.String())
}
return nil
}
func (s *Service) List(namespace string, labelSelector map[string]string) (apiv1.ServiceList, error) {
var b bytes.Buffer
var params url.Values
if len(labelSelector) > 0 {
var kvs []string
params = make(url.Values, 0)
for key, value := range labelSelector {
kvs = append(kvs, fmt.Sprintf("%s=%s", key, value))
}
params.Add("labelSelector", strings.Join(kvs, ","))
}
svclist := apiv1.ServiceList{}
path := strutil.Concat("/api/v1/namespaces/", namespace, "/services")
resp, err := s.client.Get(s.addr).
Path(path).
Do().
Body(&b)
if err != nil {
return svclist, errors.Errorf("failed to get service list, namespace: %s, err: %v", namespace, err)
}
if !resp.IsOK() {
return svclist, errors.Errorf("failed to get service list, namespace: %s, statuscode: %v, body: %v",
namespace, resp.StatusCode(), b.String())
}
if err := json.NewDecoder(&b).Decode(&svclist); err != nil {
return svclist, err
}
return svclist, nil
}
// Get gets a k8s service
func (s *Service) Get(namespace, name string) (*apiv1.Service, error) {
var b bytes.Buffer
path := strutil.Concat("/api/v1/namespaces/", namespace, "/services/", name)
resp, err := s.client.Get(s.addr).
Path(path).
Do().
Body(&b)
if err != nil {
return nil, errors.Errorf("failed to get service, name: %s, (%v)", name, err)
}
if !resp.IsOK() {
if resp.IsNotfound() {
return nil, k8serror.ErrNotFound
}
return nil, errors.Errorf("failed to get service, name: %s, statuscode: %v, body: %v",
name, resp.StatusCode(), b.String())
}
svc := &apiv1.Service{}
if err := json.NewDecoder(&b).Decode(svc); err != nil {
return nil, err
}
return svc, nil
}
// Put updates a k8s service
func (s *Service) Put(service *apiv1.Service) error {
var b bytes.Buffer
namespace := service.Namespace
name := service.Name
path := strutil.Concat("/api/v1/namespaces/", namespace, "/services/", name)
resp, err := s.client.Put(s.addr).
Path(path).
JSONBody(service).
Do().
Body(&b)
if err != nil {
return errors.Wrapf(err, "failed to put service, name: %s, (%v)", name, err)
}
if !resp.IsOK() {
return errors.Errorf("failed to put service, name: %s, statuscode: %v, body: %v",
name, resp.StatusCode(), b.String())
}
return nil
}
// Delete deletes a k8s service
func (s *Service) Delete(namespace, name string) error {
var b bytes.Buffer
path := strutil.Concat("/api/v1/namespaces/", namespace, "/services/", name)
resp, err := s.client.Delete(s.addr).
Path(path).
JSONBody(k8sapi.DeleteOptions).
Do().
Body(&b)
if err != nil {
return errors.Wrapf(err, "failed to delete service, name: %s, (%v)", name, err)
}
if !resp.IsOK() {
if resp.IsNotfound() {
logrus.Debugf("service not found, ns: %s, name: %s", namespace, name)
return nil
}
return errors.Errorf("failed to delete service, name: %s, status code: %v, resp body: %v",
name, resp.StatusCode(), b.String())
}
return nil
}