This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 254
/
store.go
332 lines (280 loc) · 8.33 KB
/
store.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package store
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"github.com/docker/compose/v2/pkg/api"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
)
const (
// DefaultContextName is an automatically generated local context
DefaultContextName = "default"
// DefaultContextType is the type for all moby contexts (not associated with cli backend)
DefaultContextType = "moby"
// AwsContextType is the type for aws contexts (currently a CLI plugin, not associated with cli backend)
// to be removed with the cli plugin
AwsContextType = "aws"
// EcsContextType is the endpoint key in the context endpoints for an ECS
// backend
EcsContextType = "ecs"
// EcsLocalSimulationContextType is the endpoint key in the context endpoints for an ECS backend
// running local simulation endpoints
EcsLocalSimulationContextType = "ecs-local"
// AciContextType is the endpoint key in the context endpoints for an ACI
// backend
AciContextType = "aci"
// LocalContextType is the endpoint key in the context endpoints for a new
// local backend
LocalContextType = "local"
// KubeContextType is the endpoint key in the context endpoints for a new
// kube backend
KubeContextType = "kube"
)
const (
dockerEndpointKey = "docker"
contextsDir = "contexts"
metadataDir = "meta"
metaFile = "meta.json"
)
var instance Store
// WithContextStore adds the store to the context
func WithContextStore(store Store) {
instance = store
}
// Instance returns the store from the context
func Instance() Store {
return instance
}
// Store is the context store
type Store interface {
// Get returns the context with name, it returns an error if the context
// doesn't exist
Get(name string) (*DockerContext, error)
// GetEndpoint sets the `v` parameter to the value of the endpoint for a
// particular context type
GetEndpoint(name string, v interface{}) error
// Create creates a new context, it returns an error if a context with the
// same name exists already.
Create(name string, contextType string, description string, data interface{}) error
// List returns the list of created contexts
List() ([]*DockerContext, error)
// Remove removes a context by name from the context store
Remove(name string) error
// ContextExists checks if a context already exists
ContextExists(name string) bool
}
// Endpoint holds the Docker or the Kubernetes endpoint, they both have the
// `Host` property, only kubernetes will have the `DefaultNamespace`
type Endpoint struct {
Host string `json:",omitempty"`
DefaultNamespace string `json:",omitempty"`
}
type store struct {
root string
}
// New returns a configured context store with specified root dir (eg. $HOME/.docker) as root
func New(rootDir string) (Store, error) {
s := &store{
root: rootDir,
}
m := filepath.Join(s.root, contextsDir, metadataDir)
if err := createDirIfNotExist(m); err != nil {
return nil, err
}
return s, nil
}
// Get returns the context with the given name
func (s *store) Get(name string) (*DockerContext, error) {
meta := filepath.Join(s.root, contextsDir, metadataDir, contextDirOf(name), metaFile)
m, err := read(meta)
if os.IsNotExist(err) {
return nil, errors.Wrap(api.ErrNotFound, objectName(name))
} else if err != nil {
return nil, err
}
return m, nil
}
func (s *store) GetEndpoint(name string, data interface{}) error {
meta, err := s.Get(name)
if err != nil {
return err
}
contextType := meta.Type()
if _, ok := meta.Endpoints[contextType]; !ok {
return errors.Wrapf(api.ErrNotFound, "endpoint of type %q", contextType)
}
dstPtrValue := reflect.ValueOf(data)
dstValue := reflect.Indirect(dstPtrValue)
val := reflect.ValueOf(meta.Endpoints[contextType])
valIndirect := reflect.Indirect(val)
if dstValue.Type() != valIndirect.Type() {
return api.ErrWrongContextType
}
dstValue.Set(valIndirect)
return nil
}
func read(meta string) (*DockerContext, error) {
bytes, err := ioutil.ReadFile(meta)
if err != nil {
return nil, err
}
var metadata DockerContext
if err := json.Unmarshal(bytes, &metadata); err != nil {
return nil, err
}
metadata.Endpoints, err = toTypedEndpoints(metadata.Endpoints)
if err != nil {
return nil, err
}
return &metadata, nil
}
func toTypedEndpoints(endpoints map[string]interface{}) (map[string]interface{}, error) {
result := map[string]interface{}{}
for k, v := range endpoints {
bytes, err := json.Marshal(v)
if err != nil {
return nil, err
}
typeGetters := getters()
typeGetter, ok := typeGetters[k]
if !ok {
typeGetter = func() interface{} {
return &Endpoint{}
}
}
val := typeGetter()
err = json.Unmarshal(bytes, &val)
if err != nil {
return nil, err
}
result[k] = val
}
return result, nil
}
func (s *store) ContextExists(name string) bool {
if name == DefaultContextName {
return true
}
dir := contextDirOf(name)
metaDir := filepath.Join(s.root, contextsDir, metadataDir, dir)
if _, err := os.Stat(metaDir); !os.IsNotExist(err) {
return true
}
return false
}
func (s *store) Create(name string, contextType string, description string, data interface{}) error {
if s.ContextExists(name) {
return errors.Wrap(api.ErrAlreadyExists, objectName(name))
}
dir := contextDirOf(name)
metaDir := filepath.Join(s.root, contextsDir, metadataDir, dir)
err := os.Mkdir(metaDir, 0755)
if err != nil {
return err
}
meta := DockerContext{
Name: name,
Metadata: ContextMetadata{
Type: contextType,
Description: description,
},
Endpoints: map[string]interface{}{
(dockerEndpointKey): data,
(contextType): data,
},
}
bytes, err := json.Marshal(&meta)
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(metaDir, metaFile), bytes, 0644)
}
func (s *store) List() ([]*DockerContext, error) {
root := filepath.Join(s.root, contextsDir, metadataDir)
c, err := ioutil.ReadDir(root)
if err != nil {
return nil, err
}
var result []*DockerContext
for _, fi := range c {
if fi.IsDir() {
meta := filepath.Join(root, fi.Name(), metaFile)
r, err := read(meta)
if err != nil {
return nil, err
}
result = append(result, r)
}
}
// The default context is not stored in the store, it is in-memory only
// so we need a special case for it.
dockerDefault, err := dockerDefaultContext()
if err != nil {
return nil, err
}
result = append(result, dockerDefault)
return result, nil
}
func (s *store) Remove(name string) error {
if name == DefaultContextName {
return errors.Wrap(api.ErrForbidden, objectName(name))
}
dir := filepath.Join(s.root, contextsDir, metadataDir, contextDirOf(name))
// Check if directory exists because os.RemoveAll returns nil if it doesn't
if _, err := os.Stat(dir); os.IsNotExist(err) {
return errors.Wrap(api.ErrNotFound, objectName(name))
}
if err := os.RemoveAll(dir); err != nil {
return errors.Wrapf(api.ErrUnknown, "unable to remove %s: %s", objectName(name), err)
}
return nil
}
func contextDirOf(name string) string {
return digest.FromString(name).Encoded()
}
func objectName(name string) string {
return fmt.Sprintf("context %q", name)
}
func createDirIfNotExist(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err = os.MkdirAll(dir, 0755); err != nil {
return err
}
}
return nil
}
// Different context types managed by the store.
// TODO(rumpl): we should make this extensible in the future if we want to
// be able to manage other contexts.
func getters() map[string]func() interface{} {
return map[string]func() interface{}{
AciContextType: func() interface{} {
return &AciContext{}
},
EcsContextType: func() interface{} {
return &EcsContext{}
},
LocalContextType: func() interface{} {
return &LocalContext{}
},
KubeContextType: func() interface{} {
return &KubeContext{}
},
}
}