forked from viliproject/vili
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replicasets.go
122 lines (112 loc) · 3.35 KB
/
replicasets.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
package kube
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"strings"
"github.com/airware/vili/kube/extensions/v1beta1"
"github.com/airware/vili/kube/unversioned"
)
// ReplicaSets is the default replicasets service instance
var ReplicaSets = &ReplicaSetsService{}
// ReplicaSetsService is the kubernetes service to interace with replicasets
type ReplicaSetsService struct {
}
// List fetches the list of replicasets in `env`
func (s *ReplicaSetsService) List(env string, query *url.Values) (*v1beta1.ReplicaSetList, *unversioned.Status, error) {
client, err := getClient(env)
if err != nil {
return nil, nil, invalidEnvError(env)
}
resp := &v1beta1.ReplicaSetList{}
path := "replicasets"
if query != nil {
path += "?" + query.Encode()
}
status, err := client.makeRequest("GET", path, nil, resp)
if status != nil || err != nil {
return nil, status, err
}
return resp, nil, nil
}
// ListForDeployment fetches the list of replicasets in `env` for the given deployment
func (s *ReplicaSetsService) ListForDeployment(env string, deployment *v1beta1.Deployment) (*v1beta1.ReplicaSetList, *unversioned.Status, error) {
var selector []string
for k, v := range deployment.Spec.Selector.MatchLabels {
selector = append(selector, fmt.Sprintf("%s=%s", k, v))
}
query := &url.Values{}
query.Add("labelSelector", strings.Join(selector, ","))
return s.List(env, query)
}
// Get fetches the replicaset in `env` with `name`
func (s *ReplicaSetsService) Get(env, name string) (*v1beta1.ReplicaSet, *unversioned.Status, error) {
client, err := getClient(env)
if err != nil {
return nil, nil, invalidEnvError(env)
}
resp := &v1beta1.ReplicaSet{}
status, err := client.makeRequest("GET", "replicasets/"+name, nil, resp)
if status != nil || err != nil {
return nil, status, err
}
return resp, nil, nil
}
// Create creates a replicaset in `env`
func (s *ReplicaSetsService) Create(env string, data *v1beta1.ReplicaSet) (*v1beta1.ReplicaSet, *unversioned.Status, error) {
client, err := getClient(env)
if err != nil {
return nil, nil, invalidEnvError(env)
}
dataBytes, err := json.Marshal(data)
if err != nil {
return nil, nil, err
}
resp := &v1beta1.ReplicaSet{}
status, err := client.makeRequest(
"POST",
"replicasets",
bytes.NewReader(dataBytes),
resp,
)
if status != nil || err != nil {
return nil, status, err
}
return resp, nil, nil
}
// Patch patches the replicaset in `env` with `name`
func (s *ReplicaSetsService) Patch(env, name string, data *v1beta1.ReplicaSet) (*v1beta1.ReplicaSet, *unversioned.Status, error) {
client, err := getClient(env)
if err != nil {
return nil, nil, invalidEnvError(env)
}
dataBytes, err := json.Marshal(data)
if err != nil {
return nil, nil, err
}
resp := &v1beta1.ReplicaSet{}
status, err := client.makeRequest(
"PATCH",
"replicasets/"+name,
bytes.NewReader(dataBytes),
resp,
)
if status != nil || err != nil {
return nil, status, err
}
return resp, nil, nil
}
// Delete deletes the replicaset in `env` with `name`
func (s *ReplicaSetsService) Delete(env, name string) (*v1beta1.ReplicaSet, *unversioned.Status, error) {
client, err := getClient(env)
if err != nil {
return nil, nil, invalidEnvError(env)
}
resp := &v1beta1.ReplicaSet{}
status, err := client.makeRequest("DELETE", "replicasets/"+name, nil, resp)
if status != nil || err != nil {
return nil, status, err
}
return resp, nil, nil
}