forked from projecteru2/core
/
container.go
211 lines (180 loc) · 5.91 KB
/
container.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
package etcdstore
import (
"encoding/json"
"fmt"
"path/filepath"
"context"
etcdclient "github.com/coreos/etcd/client"
"github.com/projecteru2/core/types"
"github.com/projecteru2/core/utils"
)
// AddContainer add a container
// mainly record its relationship on pod and node
// actually if we already know its node, we will know its pod
// but we still store it
// storage path in etcd is `/container/:containerid`
func (k *Krypton) AddContainer(ctx context.Context, container *types.Container) error {
var err error
appname, entrypoint, _, err := utils.ParseContainerName(container.Name)
if err != nil {
return err
}
defer func() {
if err != nil {
k.CleanContainerData(context.Background(), container.ID, appname, entrypoint, container.Nodename)
}
}()
// now everything is ok
// we use full length id instead
key := fmt.Sprintf(containerInfoKey, container.ID)
bytes, err := json.Marshal(container)
if err != nil {
return err
}
data := string(bytes)
//add deploy status by agent now
_, err = k.etcd.Set(ctx, key, data, nil)
if err != nil {
return err
}
// store deploy status
key = fmt.Sprintf(containerDeployKey, appname, entrypoint, container.Nodename, container.ID)
_, err = k.etcd.Set(ctx, key, "", nil)
if err != nil {
return err
}
// store node-container data
key = fmt.Sprintf(nodeContainersKey, container.Nodename, container.ID)
_, err = k.etcd.Set(ctx, key, data, nil)
return err
}
// RemoveContainer remove a container
// container id must be in full length
func (k *Krypton) RemoveContainer(ctx context.Context, container *types.Container) error {
if l := len(container.ID); l != 64 {
return types.NewDetailedErr(types.ErrBadContainerID,
fmt.Sprintf("containerID: %s, length: %d",
container.ID, len(container.ID)))
}
appname, entrypoint, _, err := utils.ParseContainerName(container.Name)
if err != nil {
return err
}
return k.CleanContainerData(ctx, container.ID, appname, entrypoint, container.Nodename)
}
// CleanContainerData clean container data
func (k *Krypton) CleanContainerData(ctx context.Context, ID, appname, entrypoint, nodename string) error {
key := fmt.Sprintf(containerInfoKey, ID)
if _, err := k.etcd.Delete(ctx, key, nil); err != nil {
return err
}
// remove deploy status by core
key = fmt.Sprintf(containerDeployKey, appname, entrypoint, nodename, ID)
if _, err := k.etcd.Delete(ctx, key, nil); err != nil {
return err
}
// remove node-containers data
key = fmt.Sprintf(nodeContainersKey, nodename, ID)
_, err := k.etcd.Delete(ctx, key, nil)
return err
}
// GetContainer get a container
// container if must be in full length, or we can't find it in etcd
// storage path in etcd is `/container/:containerid`
func (k *Krypton) GetContainer(ctx context.Context, ID string) (*types.Container, error) {
if l := len(ID); l != 64 {
return nil, types.NewDetailedErr(types.ErrBadContainerID,
fmt.Sprintf("containerID: %s, length: %d", ID, l))
}
key := fmt.Sprintf(containerInfoKey, ID)
resp, err := k.etcd.Get(ctx, key, nil)
if err != nil {
return nil, err
}
if resp.Node.Dir {
return nil, types.NewDetailedErr(types.ErrKeyIsDir,
fmt.Sprintf("container storage path %q in etcd is a directory", key))
}
c := &types.Container{}
if err := json.Unmarshal([]byte(resp.Node.Value), c); err != nil {
return nil, err
}
if c, err = k.bindNodeEngine(ctx, c); err != nil {
return nil, err
}
return c, nil
}
// GetContainers get many containers
func (k *Krypton) GetContainers(ctx context.Context, IDs []string) (containers []*types.Container, err error) {
for _, ID := range IDs {
container, err := k.GetContainer(ctx, ID)
if err != nil {
return containers, err
}
containers = append(containers, container)
}
return containers, err
}
// ContainerDeployed store deployed container info
func (k *Krypton) ContainerDeployed(ctx context.Context, ID, appname, entrypoint, nodename, data string) error {
key := fmt.Sprintf(containerDeployKey, appname, entrypoint, nodename, ID)
//Only update when it exist
_, err := k.etcd.Update(ctx, key, data)
return err
}
// ListContainers list containers
func (k *Krypton) ListContainers(ctx context.Context, appname, entrypoint, nodename string) ([]*types.Container, error) {
if appname == "" {
entrypoint = ""
}
if entrypoint == "" {
nodename = ""
}
key := filepath.Join(containerDeployPrefix, appname, entrypoint, nodename)
resp, err := k.etcd.Get(ctx, key, &etcdclient.GetOptions{Recursive: true})
if err != nil {
return []*types.Container{}, err
}
containerIDs := getContainerDeployData(resp.Node.Key, resp.Node.Nodes)
return k.GetContainers(ctx, containerIDs)
}
// ListNodeContainers list containers belong to one node
func (k *Krypton) ListNodeContainers(ctx context.Context, nodename string) ([]*types.Container, error) {
key := fmt.Sprintf(nodeContainers, nodename)
resp, err := k.etcd.Get(ctx, key, &etcdclient.GetOptions{Recursive: true})
if err != nil {
return []*types.Container{}, err
}
containers := []*types.Container{}
for _, n := range resp.Node.Nodes {
c := &types.Container{}
if err := json.Unmarshal([]byte(n.Value), c); err != nil {
return []*types.Container{}, err
}
if c, err = k.bindNodeEngine(ctx, c); err != nil {
return []*types.Container{}, err
}
containers = append(containers, c)
}
return containers, nil
}
// WatchDeployStatus watch deployed status
func (k *Krypton) WatchDeployStatus(appname, entrypoint, nodename string) etcdclient.Watcher {
if appname == "" {
entrypoint = ""
}
if entrypoint == "" {
nodename = ""
}
key := filepath.Join(containerDeployPrefix, appname, entrypoint, nodename)
return k.etcd.Watcher(key, &etcdclient.WatcherOptions{Recursive: true})
}
func (k *Krypton) bindNodeEngine(ctx context.Context, container *types.Container) (*types.Container, error) {
node, err := k.GetNode(ctx, container.Podname, container.Nodename)
if err != nil {
return nil, err
}
container.Engine = node.Engine
container.Node = node
return container, nil
}