2020-04-24 18:04:32 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-06-18 16:13:24 +02:00
|
|
|
|
|
|
|
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.
|
2020-04-24 18:04:32 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
2021-01-15 16:51:31 +01:00
|
|
|
"github.com/docker/compose-cli/api/errdefs"
|
2020-05-14 09:55:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultContextName is an automatically generated local context
|
|
|
|
DefaultContextName = "default"
|
2020-06-17 23:12:50 +02:00
|
|
|
// DefaultContextType is the type for all moby contexts (not associated with cli backend)
|
|
|
|
DefaultContextType = "moby"
|
2020-07-29 15:12:45 +02:00
|
|
|
|
|
|
|
// 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
|
2020-07-20 10:48:05 +02:00
|
|
|
EcsContextType = "ecs"
|
2020-08-31 14:26:00 +02:00
|
|
|
|
|
|
|
// EcsLocalSimulationContextType is the endpoint key in the context endpoints for an ECS backend
|
|
|
|
// running local simulation endpoints
|
|
|
|
EcsLocalSimulationContextType = "ecs-local"
|
|
|
|
|
2020-06-17 23:12:50 +02:00
|
|
|
// 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"
|
2021-01-19 11:49:50 +01:00
|
|
|
// KubeContextType is the endpoint key in the context endpoints for a new
|
|
|
|
// kube backend
|
2021-01-26 16:40:18 +01:00
|
|
|
KubeContextType = "kube"
|
2020-04-24 18:04:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-05-18 15:32:35 +02:00
|
|
|
dockerEndpointKey = "docker"
|
|
|
|
contextsDir = "contexts"
|
|
|
|
metadataDir = "meta"
|
|
|
|
metaFile = "meta.json"
|
2020-04-24 18:04:32 +02:00
|
|
|
)
|
|
|
|
|
2021-03-11 11:43:40 +01:00
|
|
|
var instance Store
|
2020-04-24 18:04:32 +02:00
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
// WithContextStore adds the store to the context
|
2021-03-11 11:43:40 +01:00
|
|
|
func WithContextStore(store Store) {
|
|
|
|
instance = store
|
2020-04-24 18:04:32 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 11:43:40 +01:00
|
|
|
// Instance returns the store from the context
|
|
|
|
func Instance() Store {
|
|
|
|
return instance
|
2020-04-24 18:04:32 +02:00
|
|
|
}
|
|
|
|
|
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
|
2020-06-10 14:07:35 +02:00
|
|
|
Get(name string) (*DockerContext, error)
|
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
|
|
|
// 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
|
2020-06-10 14:07:35 +02:00
|
|
|
List() ([]*DockerContext, error)
|
2020-05-11 17:57:17 +02:00
|
|
|
// Remove removes a context by name from the context store
|
|
|
|
Remove(name string) error
|
2020-06-22 16:35:06 +02:00
|
|
|
// ContextExists checks if a context already exists
|
|
|
|
ContextExists(name string) bool
|
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"`
|
|
|
|
}
|
|
|
|
|
2020-04-24 18:04:32 +02:00
|
|
|
type store struct {
|
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
2020-09-16 15:53:25 +02:00
|
|
|
// New returns a configured context store with specified root dir (eg. $HOME/.docker) as root
|
|
|
|
func New(rootDir string) (Store, error) {
|
2020-05-19 17:11:31 +02:00
|
|
|
s := &store{
|
2020-09-16 15:53:25 +02:00
|
|
|
root: rootDir,
|
2020-04-27 18:16:46 +02:00
|
|
|
}
|
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
|
2020-06-10 14:07:35 +02:00
|
|
|
func (s *store) Get(name string) (*DockerContext, error) {
|
2020-07-29 11:58:09 +02:00
|
|
|
if name == "default" {
|
|
|
|
return dockerDefaultContext()
|
|
|
|
}
|
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
|
|
|
|
}
|
2020-06-10 14:07:35 +02:00
|
|
|
contextType := meta.Type()
|
|
|
|
if _, ok := meta.Endpoints[contextType]; !ok {
|
|
|
|
return errors.Wrapf(errdefs.ErrNotFound, "endpoint of type %q", contextType)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
dstPtrValue := reflect.ValueOf(data)
|
|
|
|
dstValue := reflect.Indirect(dstPtrValue)
|
|
|
|
|
2020-06-10 14:07:35 +02:00
|
|
|
val := reflect.ValueOf(meta.Endpoints[contextType])
|
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
|
|
|
valIndirect := reflect.Indirect(val)
|
|
|
|
|
|
|
|
if dstValue.Type() != valIndirect.Type() {
|
|
|
|
return errdefs.ErrWrongContextType
|
|
|
|
}
|
|
|
|
|
|
|
|
dstValue.Set(valIndirect)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-10 14:07:35 +02:00
|
|
|
func read(meta string) (*DockerContext, error) {
|
2020-04-24 18:04:32 +02:00
|
|
|
bytes, err := ioutil.ReadFile(meta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-10 14:07:35 +02:00
|
|
|
var metadata DockerContext
|
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
|
|
|
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()
|
2020-06-11 10:12:41 +02:00
|
|
|
typeGetter, ok := typeGetters[k]
|
|
|
|
if !ok {
|
2020-06-11 10:41:51 +02:00
|
|
|
typeGetter = func() interface{} {
|
|
|
|
return &Endpoint{}
|
|
|
|
}
|
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
|
|
|
}
|
2020-04-24 18:04:32 +02:00
|
|
|
|
2020-06-11 10:41:51 +02:00
|
|
|
val := typeGetter()
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-22 16:35:06 +02:00
|
|
|
func (s *store) ContextExists(name string) bool {
|
2020-05-14 09:55:40 +02:00
|
|
|
if name == DefaultContextName {
|
2020-06-22 16:35:06 +02:00
|
|
|
return true
|
2020-05-14 09:55:40 +02:00
|
|
|
}
|
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-06-22 16:35:06 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) Create(name string, contextType string, description string, data interface{}) error {
|
|
|
|
if s.ContextExists(name) {
|
2020-05-11 17:57:17 +02:00
|
|
|
return errors.Wrap(errdefs.ErrAlreadyExists, objectName(name))
|
2020-04-24 18:04:32 +02:00
|
|
|
}
|
2020-06-22 16:35:06 +02:00
|
|
|
dir := contextDirOf(name)
|
|
|
|
metaDir := filepath.Join(s.root, contextsDir, metadataDir, dir)
|
2020-04-24 18:04:32 +02:00
|
|
|
|
|
|
|
err := os.Mkdir(metaDir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-10 14:07:35 +02:00
|
|
|
meta := DockerContext{
|
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,
|
|
|
|
Metadata: ContextMetadata{
|
2020-06-10 14:07:35 +02:00
|
|
|
Type: contextType,
|
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
|
|
|
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-06-10 14:07:35 +02:00
|
|
|
func (s *store) List() ([]*DockerContext, error) {
|
2020-04-26 22:07:50 +02:00
|
|
|
root := filepath.Join(s.root, contextsDir, metadataDir)
|
|
|
|
c, err := ioutil.ReadDir(root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-10 14:07:35 +02:00
|
|
|
var result []*DockerContext
|
2020-04-26 22:07:50 +02:00
|
|
|
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{}{
|
2020-07-15 15:09:05 +02:00
|
|
|
AciContextType: func() interface{} {
|
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 &AciContext{}
|
|
|
|
},
|
2020-07-20 10:48:05 +02:00
|
|
|
EcsContextType: func() interface{} {
|
|
|
|
return &EcsContext{}
|
2020-07-15 15:09:05 +02:00
|
|
|
},
|
|
|
|
LocalContextType: func() interface{} {
|
2020-06-12 15:00:30 +02:00
|
|
|
return &LocalContext{}
|
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
|
|
|
},
|
2021-01-19 11:49:50 +01:00
|
|
|
KubeContextType: func() interface{} {
|
|
|
|
return &KubeContext{}
|
|
|
|
},
|
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
|
|
|
}
|
2020-04-24 18:04:32 +02:00
|
|
|
}
|