2020-04-24 18:04:32 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2020 Docker Inc.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person
|
|
|
|
obtaining a copy of this software and associated documentation
|
|
|
|
files (the "Software"), to deal in the Software without
|
|
|
|
restriction, including without limitation the rights to use, copy,
|
|
|
|
modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED,
|
|
|
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
DAMAGES OR OTHER LIABILITY,
|
|
|
|
WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
TORT OR OTHERWISE,
|
|
|
|
ARISING FROM, OUT OF OR IN CONNECTION WITH
|
|
|
|
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2020-05-11 17:57:17 +02:00
|
|
|
"fmt"
|
2020-04-24 18:04:32 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/opencontainers/go-digest"
|
2020-05-11 17:56:58 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-05-14 09:55:40 +02:00
|
|
|
|
|
|
|
"github.com/docker/api/errdefs"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultContextName is an automatically generated local context
|
|
|
|
DefaultContextName = "default"
|
2020-04-24 18:04:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-05-18 15:32:35 +02:00
|
|
|
dockerEndpointKey = "docker"
|
|
|
|
configDir = ".docker"
|
|
|
|
contextsDir = "contexts"
|
|
|
|
metadataDir = "meta"
|
|
|
|
metaFile = "meta.json"
|
2020-04-24 18:04:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type contextStoreKey struct{}
|
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
// WithContextStore adds the store to the context
|
2020-04-24 18:04:32 +02:00
|
|
|
func WithContextStore(ctx context.Context, store Store) context.Context {
|
|
|
|
return context.WithValue(ctx, contextStoreKey{}, store)
|
|
|
|
}
|
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
// ContextStore returns the store from the context
|
2020-04-24 18:04:32 +02:00
|
|
|
func ContextStore(ctx context.Context) Store {
|
|
|
|
s, _ := ctx.Value(contextStoreKey{}).(Store)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
// Store is the context store
|
2020-04-24 18:04:32 +02:00
|
|
|
type Store interface {
|
2020-04-27 11:32:16 +02:00
|
|
|
// Get returns the context with name, it returns an error if the context
|
|
|
|
// doesn't exist
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
Get(name string) (*Metadata, error)
|
|
|
|
// GetEndpoint sets the `v` parameter to the value of the endpoint for a
|
|
|
|
// particular context type
|
|
|
|
GetEndpoint(name string, v interface{}) error
|
2020-04-26 22:07:50 +02:00
|
|
|
// Create creates a new context, it returns an error if a context with the
|
|
|
|
// same name exists already.
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
Create(name string, contextType string, description string, data interface{}) error
|
2020-04-26 22:07:50 +02:00
|
|
|
// List returns the list of created contexts
|
|
|
|
List() ([]*Metadata, error)
|
2020-05-11 17:57:17 +02:00
|
|
|
// Remove removes a context by name from the context store
|
|
|
|
Remove(name string) error
|
2020-04-24 18:04:32 +02:00
|
|
|
}
|
|
|
|
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// AciContextType is the endpoint key in the context endpoints for an ACI
|
|
|
|
// backend
|
|
|
|
AciContextType = "aci"
|
|
|
|
// MobyContextType is the endpoint key in the context endpoints for a moby
|
|
|
|
// backend
|
|
|
|
MobyContextType = "moby"
|
|
|
|
// ExampleContextType is the endpoint key in the context endpoints for an
|
|
|
|
// example backend
|
|
|
|
ExampleContextType = "example"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Metadata represents the docker context metadata
|
|
|
|
type Metadata struct {
|
|
|
|
Name string `json:",omitempty"`
|
|
|
|
Type string `json:",omitempty"`
|
|
|
|
Metadata ContextMetadata `json:",omitempty"`
|
|
|
|
Endpoints map[string]interface{} `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContextMetadata is represtentation of the data we put in a context
|
|
|
|
// metadata
|
|
|
|
type ContextMetadata struct {
|
|
|
|
Description string `json:",omitempty"`
|
|
|
|
StackOrchestrator string `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// AciContext is the context for the ACI backend
|
|
|
|
type AciContext struct {
|
|
|
|
SubscriptionID string `json:",omitempty"`
|
|
|
|
Location string `json:",omitempty"`
|
|
|
|
ResourceGroup string `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// MobyContext is the context for the moby backend
|
|
|
|
type MobyContext struct{}
|
|
|
|
|
|
|
|
// ExampleContext is the context for the example backend
|
|
|
|
type ExampleContext struct{}
|
|
|
|
|
2020-04-24 18:04:32 +02:00
|
|
|
type store struct {
|
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
// Opt is a functional option for the store
|
|
|
|
type Opt func(*store)
|
2020-04-27 18:16:46 +02:00
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
// WithRoot sets a new root to the store
|
|
|
|
func WithRoot(root string) Opt {
|
2020-04-27 18:16:46 +02:00
|
|
|
return func(s *store) {
|
|
|
|
s.root = root
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
// New returns a configured context store with $HOME/.docker as root
|
|
|
|
func New(opts ...Opt) (Store, error) {
|
2020-04-27 18:16:46 +02:00
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-19 17:11:31 +02:00
|
|
|
|
|
|
|
root := filepath.Join(home, configDir)
|
|
|
|
if err := createDirIfNotExist(root); err != nil {
|
|
|
|
return nil, err
|
2020-04-27 18:16:46 +02:00
|
|
|
}
|
2020-05-19 17:11:31 +02:00
|
|
|
|
|
|
|
s := &store{
|
|
|
|
root: root,
|
2020-05-14 13:35:55 +02:00
|
|
|
}
|
2020-05-19 17:11:31 +02:00
|
|
|
|
2020-04-27 18:16:46 +02:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(s)
|
|
|
|
}
|
2020-05-19 17:11:31 +02:00
|
|
|
|
|
|
|
m := filepath.Join(s.root, contextsDir, metadataDir)
|
|
|
|
if err := createDirIfNotExist(m); err != nil {
|
|
|
|
return nil, err
|
2020-04-24 18:04:32 +02:00
|
|
|
}
|
2020-05-19 17:11:31 +02:00
|
|
|
|
2020-04-27 18:16:46 +02:00
|
|
|
return s, nil
|
2020-04-24 18:04:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the context with the given name
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
func (s *store) Get(name string) (*Metadata, error) {
|
2020-05-19 17:11:31 +02:00
|
|
|
meta := filepath.Join(s.root, contextsDir, metadataDir, contextDirOf(name), metaFile)
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
m, err := read(meta)
|
2020-04-29 23:39:54 +02:00
|
|
|
if os.IsNotExist(err) {
|
2020-05-11 17:57:17 +02:00
|
|
|
return nil, errors.Wrap(errdefs.ErrNotFound, objectName(name))
|
2020-04-29 23:44:01 +02:00
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
2020-04-29 23:39:54 +02:00
|
|
|
}
|
2020-04-29 23:44:01 +02:00
|
|
|
|
2020-04-29 23:39:54 +02:00
|
|
|
return m, nil
|
2020-04-26 22:07:50 +02:00
|
|
|
}
|
|
|
|
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
func (s *store) GetEndpoint(name string, data interface{}) error {
|
|
|
|
meta, err := s.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, ok := meta.Endpoints[meta.Type]; !ok {
|
|
|
|
return errors.Wrapf(errdefs.ErrNotFound, "endpoint of type %q", meta.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
dstPtrValue := reflect.ValueOf(data)
|
|
|
|
dstValue := reflect.Indirect(dstPtrValue)
|
|
|
|
|
|
|
|
val := reflect.ValueOf(meta.Endpoints[meta.Type])
|
|
|
|
valIndirect := reflect.Indirect(val)
|
|
|
|
|
|
|
|
if dstValue.Type() != valIndirect.Type() {
|
|
|
|
return errdefs.ErrWrongContextType
|
|
|
|
}
|
|
|
|
|
|
|
|
dstValue.Set(valIndirect)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func read(meta string) (*Metadata, error) {
|
2020-04-24 18:04:32 +02:00
|
|
|
bytes, err := ioutil.ReadFile(meta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
var metadata Metadata
|
|
|
|
if err := json.Unmarshal(bytes, &metadata); err != nil {
|
2020-04-26 22:07:50 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
metadata.Endpoints, err = toTypedEndpoints(metadata.Endpoints)
|
|
|
|
if err != nil {
|
2020-04-29 16:21:46 +02:00
|
|
|
return nil, err
|
2020-04-24 18:04:32 +02:00
|
|
|
}
|
|
|
|
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
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)
|
2020-05-19 17:11:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
typeGetters := getters()
|
|
|
|
if _, ok := typeGetters[k]; !ok {
|
|
|
|
result[k] = v
|
|
|
|
continue
|
|
|
|
}
|
2020-04-24 18:04:32 +02:00
|
|
|
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
val := typeGetters[k]()
|
|
|
|
err = json.Unmarshal(bytes, &val)
|
|
|
|
if err != nil {
|
2020-04-29 16:21:46 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-24 18:04:32 +02:00
|
|
|
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
result[k] = val
|
2020-04-29 16:21:46 +02:00
|
|
|
}
|
2020-05-18 15:32:35 +02:00
|
|
|
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
return result, nil
|
2020-04-29 16:21:46 +02:00
|
|
|
}
|
|
|
|
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
func (s *store) Create(name string, contextType string, description string, data interface{}) error {
|
2020-05-14 09:55:40 +02:00
|
|
|
if name == DefaultContextName {
|
|
|
|
return errors.Wrap(errdefs.ErrAlreadyExists, objectName(name))
|
|
|
|
}
|
2020-05-19 17:11:31 +02:00
|
|
|
dir := contextDirOf(name)
|
2020-04-24 18:04:32 +02:00
|
|
|
metaDir := filepath.Join(s.root, contextsDir, metadataDir, dir)
|
|
|
|
if _, err := os.Stat(metaDir); !os.IsNotExist(err) {
|
2020-05-11 17:57:17 +02:00
|
|
|
return errors.Wrap(errdefs.ErrAlreadyExists, objectName(name))
|
2020-04-24 18:04:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := os.Mkdir(metaDir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := Metadata{
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
Name: name,
|
|
|
|
Type: contextType,
|
|
|
|
Metadata: ContextMetadata{
|
|
|
|
Description: description,
|
|
|
|
},
|
|
|
|
Endpoints: map[string]interface{}{
|
|
|
|
(dockerEndpointKey): data,
|
|
|
|
(contextType): data,
|
2020-04-29 16:21:46 +02:00
|
|
|
},
|
2020-04-24 18:04:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := json.Marshal(&meta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(filepath.Join(metaDir, metaFile), bytes, 0644)
|
|
|
|
}
|
|
|
|
|
2020-04-26 22:07:50 +02:00
|
|
|
func (s *store) List() ([]*Metadata, error) {
|
|
|
|
root := filepath.Join(s.root, contextsDir, metadataDir)
|
|
|
|
c, err := ioutil.ReadDir(root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []*Metadata
|
|
|
|
for _, fi := range c {
|
|
|
|
if fi.IsDir() {
|
|
|
|
meta := filepath.Join(root, fi.Name(), metaFile)
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
r, err := read(meta)
|
2020-04-26 22:07:50 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 14:32:56 +02:00
|
|
|
// The default context is not stored in the store, it is in-memory only
|
|
|
|
// so we need a special case for it.
|
2020-05-20 15:06:08 +02:00
|
|
|
dockerDefault, err := dockerDefaultContext()
|
2020-05-19 17:11:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, dockerDefault)
|
2020-04-26 22:07:50 +02:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:57:17 +02:00
|
|
|
func (s *store) Remove(name string) error {
|
2020-05-14 09:55:40 +02:00
|
|
|
if name == DefaultContextName {
|
|
|
|
return errors.Wrap(errdefs.ErrForbidden, objectName(name))
|
|
|
|
}
|
2020-05-19 17:11:31 +02:00
|
|
|
dir := filepath.Join(s.root, contextsDir, metadataDir, contextDirOf(name))
|
2020-05-11 17:57:17 +02:00
|
|
|
// Check if directory exists because os.RemoveAll returns nil if it doesn't
|
|
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
|
|
|
return errors.Wrap(errdefs.ErrNotFound, objectName(name))
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
return errors.Wrapf(errdefs.ErrUnknown, "unable to remove %s: %s", objectName(name), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-19 17:11:31 +02:00
|
|
|
func contextDirOf(name string) string {
|
2020-04-24 18:04:32 +02:00
|
|
|
return digest.FromString(name).Encoded()
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:57:17 +02:00
|
|
|
func objectName(name string) string {
|
|
|
|
return fmt.Sprintf("context %q", name)
|
|
|
|
}
|
|
|
|
|
2020-05-19 17:11:31 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
// 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{}{
|
|
|
|
"aci": func() interface{} {
|
|
|
|
return &AciContext{}
|
|
|
|
},
|
|
|
|
"moby": func() interface{} {
|
|
|
|
return &MobyContext{}
|
|
|
|
},
|
|
|
|
"example": func() interface{} {
|
|
|
|
return &ExampleContext{}
|
|
|
|
},
|
|
|
|
}
|
2020-04-24 18:04:32 +02:00
|
|
|
}
|