/
api.go
250 lines (209 loc) · 7.42 KB
/
api.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package api
import (
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/Meetic/blackbeard/pkg/playbook"
"github.com/Meetic/blackbeard/pkg/resource"
"github.com/Meetic/blackbeard/pkg/version"
)
// Api represents the blackbeard entrypoint by defining the list of actions
// blackbeard is able to perform.
type Api interface {
Inventories() playbook.InventoryService
Namespaces() resource.NamespaceService
Playbooks() playbook.PlaybookService
Pods() resource.PodService
Create(namespace string) (playbook.Inventory, error)
Delete(namespace string, wait bool) error
ListExposedServices(namespace string) ([]resource.Service, error)
ListNamespaces() ([]Namespace, error)
Reset(namespace string, configPath string) error
Apply(namespace string, configPath string) error
Update(namespace string, inventory playbook.Inventory, configPath string) error
WaitForNamespaceReady(namespace string, timeout time.Duration, bar progress) error
GetVersion() (*Version, error)
DeleteResource(namespace string, resource string) error
WatchNamespaceDeleted()
}
type api struct {
inventories playbook.InventoryService
configs playbook.ConfigService
playbooks playbook.PlaybookService
namespaces resource.NamespaceService
pods resource.PodService
services resource.ServiceService
cluster resource.ClusterService
job resource.JobService
}
// NewApi creates a blackbeard api. The blackbeard api is responsible for managing playbooks and namespaces.
// Parameters are struct implementing respectively Inventory, Config, Namespace, Pod and Service interfaces.
func NewApi(
inventories playbook.InventoryRepository,
configs playbook.ConfigRepository,
playbooks playbook.PlaybookRepository,
namespaces resource.NamespaceRepository,
pods resource.PodRepository,
deployments resource.DeploymentRepository,
statefulsets resource.StatefulsetRepository,
services resource.ServiceRepository,
cluster resource.ClusterRepository,
job resource.JobRepository,
) Api {
api := &api{
inventories: playbook.NewInventoryService(inventories, playbook.NewPlaybookService(playbooks)),
configs: playbook.NewConfigService(configs, playbook.NewPlaybookService(playbooks)),
playbooks: playbook.NewPlaybookService(playbooks),
namespaces: resource.NewNamespaceService(namespaces, pods, deployments, statefulsets, job),
pods: resource.NewPodService(pods),
services: resource.NewServiceService(services),
cluster: resource.NewClusterService(cluster),
job: resource.NewJobService(job),
}
return api
}
// Inventories returns the Inventory Service from the api
func (api *api) Inventories() playbook.InventoryService {
return api.inventories
}
// Namespaces returns the Namespace Service from the api
func (api *api) Namespaces() resource.NamespaceService {
return api.namespaces
}
// Playbooks returns the Playbook Service from the api
func (api *api) Playbooks() playbook.PlaybookService {
return api.playbooks
}
func (api *api) Pods() resource.PodService {
return api.pods
}
// Create is responsible for creating an inventory, a set of kubernetes configs and a kubernetes namespace
// for a given namespace.
// If an inventory already exist, Create will log the error and continue the process. Configs will be override.
func (api *api) Create(namespace string) (playbook.Inventory, error) {
if err := api.namespaces.Create(namespace); err != nil {
return playbook.Inventory{}, err
}
inv, err := api.inventories.Create(namespace)
if err != nil {
switch e := err.(type) {
default:
return playbook.Inventory{}, e
case *playbook.ErrorInventoryAlreadyExist:
logrus.Warn(e.Error())
logrus.Info("Process continue")
}
}
if err := api.configs.Generate(inv); err != nil {
return playbook.Inventory{}, err
}
return inv, nil
}
// Delete deletes the inventory, configs and kubernetes namespace for the given namespace.
func (api *api) Delete(namespace string, wait bool) error {
// delete namespace
if err := api.namespaces.Delete(namespace); err != nil {
return err
}
if !wait {
api.deletePlaybook(namespace)
}
return nil
}
// ListExposedServices returns a list of services exposed somehow outside of the kubernetes cluster.
// Exposed services could be :
// * NodePort type services
// * LoadBalancer type services
// * Http services exposed throw Ingress
func (api *api) ListExposedServices(namespace string) ([]resource.Service, error) {
return api.services.ListExposed(namespace)
}
// Reset resets an inventory, the associated configs and the kubernetes namespaces to default values.
// Defaults values are defines by the InventoryService GetDefault() method.
func (api *api) Reset(namespace string, configPath string) error {
//Reset inventory file
inv, err := api.inventories.Reset(namespace)
if err != nil {
return err
}
//Apply inventory to configuration
if err := api.configs.Generate(inv); err != nil {
return err
}
//Apply changes to Kubernetes
if err = api.namespaces.ApplyConfig(namespace, configPath); err != nil {
return err
}
return nil
}
// Apply override configs with new generated configs and apply the new configs to the kubernetes namespace.
// Warning : For now, Apply require a configPath as parameter.
// configPath is the location of configs for each namespace. This will change in the future since high level
// api should not be aware that configs are stored in files.
func (api *api) Apply(namespace string, configPath string) error {
inv, err := api.inventories.Get(namespace)
if err != nil {
return err
}
if err := api.configs.Generate(inv); err != nil {
return err
}
if err := api.namespaces.ApplyConfig(inv.Namespace, configPath); err != nil {
return err
}
return nil
}
// Update replace the inventory associated to the given namespace by the one set in parameters
// and apply the changes to configs and kubernetes namespace (using the Apply method)
func (api *api) Update(namespace string, inventory playbook.Inventory, configPath string) error {
if err := api.inventories.Update(namespace, inventory); err != nil {
return err
}
if err := api.Apply(namespace, configPath); err != nil {
return err
}
return nil
}
// DeleteResource delete a resource from a namespace
// Deletion of a Job only for now
func (api *api) DeleteResource(namespace, resource string) error {
if err := api.job.Delete(namespace, resource); err != nil {
return err
}
return nil
}
func (api *api) WatchNamespaceDeleted() {
events := make(chan resource.NamespaceEvent, 0)
go api.namespaces.Watch(events)
// handle delete of inventories and configs files
for event := range events {
if event.Type == "DELETED" {
api.deletePlaybook(event.Namespace)
logrus.
WithFields(logrus.Fields{"component": "watcher", "event": "delete", "namespace": event.Namespace}).
Debug("Playbook deleted")
}
}
}
func (api *api) deletePlaybook(namespace string) {
if inv, _ := api.inventories.Get(namespace); inv.Namespace == namespace {
api.inventories.Delete(namespace)
api.configs.Delete(namespace)
}
}
type Version struct {
Blackbeard string `json:"blackbeard"`
Kubernetes string `json:"kubernetes"`
Kubectl string `json:"kubectl"`
}
func (api *api) GetVersion() (*Version, error) {
v, err := api.cluster.GetVersion()
if err != nil {
return nil, err
}
return &Version{
Blackbeard: version.GetVersion(),
Kubectl: strings.Join([]string{v.ClientVersion.Major, v.ClientVersion.Minor}, "."),
Kubernetes: strings.Join([]string{v.ServerVersion.Major, v.ServerVersion.Minor}, "."),
}, nil
}