/
namespace.go
210 lines (167 loc) · 4.89 KB
/
namespace.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
package resource
import (
"fmt"
"sync"
"time"
"github.com/sirupsen/logrus"
)
type Namespace struct {
Name string
Phase string
Status int
}
// NamespaceService defined the way namespace are managed.
type NamespaceService interface {
Create(namespace string) error
ApplyConfig(namespace string, configPath string) error
Delete(namespace string) error
GetStatus(namespace string) (*NamespaceStatus, error)
List() ([]Namespace, error)
Watch(events chan NamespaceEvent)
}
// NamespaceRepository defined the way namespace area actually managed.
type NamespaceRepository interface {
Create(namespace string) error
Get(namespace string) (*Namespace, error)
ApplyConfig(namespace string, configPath string) error
Delete(namespace string) error
List() ([]Namespace, error)
Watch(events chan<- NamespaceEvent) error
}
type namespaceService struct {
namespaces NamespaceRepository
pods PodRepository
deployments DeploymentRepository
statefulsets StatefulsetRepository
jobs JobRepository
}
// NamespaceStatus represent namespace with percentage of pods running and status phase (Active or Terminating)
type NamespaceStatus struct {
Status int `json:"status"`
Phase string `json:"phase"`
}
type NamespaceEvent struct {
Namespace string
Type string
}
// NewNamespaceService creates a new NamespaceService
func NewNamespaceService(
namespaces NamespaceRepository,
pods PodRepository,
deployments DeploymentRepository,
statefulsets StatefulsetRepository,
jobs JobRepository,
) NamespaceService {
ns := &namespaceService{
namespaces: namespaces,
pods: pods,
deployments: deployments,
statefulsets: statefulsets,
jobs: jobs,
}
return ns
}
// Create creates a kubernetes namespace
func (ns *namespaceService) Create(n string) error {
err := ns.namespaces.Create(n)
if err != nil {
return ErrorCreateNamespace{err.Error()}
}
return nil
}
// ApplyConfig apply kubernetes configurations to the given namespace.
// Warning : For now, this method takes a configPath as parameter. This parameter is the directory containing configs in a playbook
// This may change since the NamespaceService should not be aware that configs are stored in files.
func (ns *namespaceService) ApplyConfig(namespace, configPath string) error {
return ns.namespaces.ApplyConfig(namespace, configPath)
}
// Delete deletes a kubernetes namespace
func (ns *namespaceService) Delete(namespace string) error {
return ns.namespaces.Delete(namespace)
}
// List returns a slice of namespace from the kubernetes package and enrich each of the
// returned namespace with their status.
func (ns *namespaceService) List() ([]Namespace, error) {
namespaces, err := ns.namespaces.List()
if err != nil {
return nil, err
}
var wg sync.WaitGroup
for i := range namespaces {
wg.Add(1)
go func(index int) {
status, err := ns.GetStatus(namespaces[index].Name)
if err != nil {
namespaces[index].Status = 0
}
namespaces[index].Status = status.Status
wg.Done()
}(i)
}
wg.Wait()
return namespaces, nil
}
// GetStatus returns the status of an inventory
// The status is an int that represents the percentage of pods in a "running" state inside the given namespace
func (ns *namespaceService) GetStatus(namespace string) (*NamespaceStatus, error) {
// get namespace state
n, err := ns.namespaces.Get(namespace)
if err != nil {
return nil, fmt.Errorf("namespace get status: %v", err)
}
if n.Phase == "Terminating" {
return &NamespaceStatus{0, n.Phase}, nil
}
dps, errDps := ns.deployments.List(namespace)
sfs, errSfs := ns.statefulsets.List(namespace)
jbs, errJbs := ns.jobs.List(namespace)
if errDps != nil || errSfs != nil || errJbs != nil {
return &NamespaceStatus{0, ""}, fmt.Errorf("namespace get status: list deployments, statefulsets or jobs: %v", err)
}
totalApps := len(dps) + len(sfs) + len(jbs)
if totalApps == 0 {
return &NamespaceStatus{0, n.Phase}, nil
}
var i int
for _, dp := range dps {
if dp.Status == DeploymentReady {
i++
}
}
for _, sf := range sfs {
if sf.Status == StatefulsetReady {
i++
}
}
for _, job := range jbs {
if job.Status == JobReady {
i++
}
}
status := i * 100 / totalApps
return &NamespaceStatus{status, n.Phase}, nil
}
func (ns *namespaceService) Watch(events chan NamespaceEvent) {
ticker := time.NewTicker(5 * time.Second)
defer close(events)
for range ticker.C {
err := ns.namespaces.Watch(events)
if err != nil {
ticker.Stop()
}
logrus.
WithFields(logrus.Fields{"component": "watcher"}).
Debug("watch namespace restarted")
}
logrus.
WithFields(logrus.Fields{"component": "watcher"}).
Error("watch namespace stopped due to error")
}
// ErrorCreateNamespace represents an error due to a namespace creation failure on kubernetes cluster
type ErrorCreateNamespace struct {
Msg string
}
// Error returns the error message
func (err ErrorCreateNamespace) Error() string {
return err.Msg
}