2020-06-18 16:13:24 +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-05-19 17:11:31 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Represents a context as created by the docker cli
|
|
|
|
type defaultContext struct {
|
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 ContextMetadata
|
2020-05-19 17:11:31 +02:00
|
|
|
Endpoints endpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normally (in docker/cli code), the endpoints are mapped as map[string]interface{}
|
|
|
|
// but docker cli contexts always have a "docker" and "kubernetes" key so we
|
|
|
|
// create real types for those to no have to juggle around with interfaces.
|
|
|
|
type endpoints struct {
|
|
|
|
Docker endpoint `json:"docker,omitempty"`
|
|
|
|
Kubernetes endpoint `json:"kubernetes,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both "docker" and "kubernetes" endpoints in the docker cli created contexts
|
|
|
|
// have a "Host", only kubernetes has the "DefaultNamespace", we put both of
|
|
|
|
// those here for easier manipulation and to not have to create two distinct
|
|
|
|
// structs
|
|
|
|
type endpoint struct {
|
|
|
|
Host string
|
|
|
|
DefaultNamespace string
|
|
|
|
}
|
|
|
|
|
2020-06-10 14:07:35 +02:00
|
|
|
func dockerDefaultContext() (*DockerContext, error) {
|
2020-06-13 10:43:23 +02:00
|
|
|
// ensure we run this using default context, in current context has been damaged / removed in store
|
2020-06-17 17:57:44 +02:00
|
|
|
cmd := exec.Command("com.docker.cli", "--context", "default", "context", "inspect", "default")
|
2020-05-19 17:11:31 +02:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctx []defaultContext
|
|
|
|
err = json.Unmarshal(stdout.Bytes(), &ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctx) != 1 {
|
|
|
|
return nil, errors.New("found more than one default context")
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultCtx := ctx[0]
|
|
|
|
|
2020-06-10 14:07:35 +02:00
|
|
|
meta := DockerContext{
|
2020-05-19 17:11:31 +02:00
|
|
|
Name: "default",
|
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
|
|
|
Endpoints: map[string]interface{}{
|
2020-06-11 10:41:51 +02:00
|
|
|
"docker": &Endpoint{
|
2020-05-19 17:11:31 +02:00
|
|
|
Host: defaultCtx.Endpoints.Docker.Host,
|
|
|
|
},
|
2020-06-11 10:41:51 +02:00
|
|
|
"kubernetes": &Endpoint{
|
2020-05-19 17:11:31 +02:00
|
|
|
Host: defaultCtx.Endpoints.Kubernetes.Host,
|
|
|
|
DefaultNamespace: defaultCtx.Endpoints.Kubernetes.DefaultNamespace,
|
|
|
|
},
|
|
|
|
},
|
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: ContextMetadata{
|
2020-06-17 23:12:50 +02:00
|
|
|
Type: DefaultContextType,
|
2020-05-19 17:11:31 +02:00
|
|
|
Description: "Current DOCKER_HOST based configuration",
|
|
|
|
StackOrchestrator: defaultCtx.Metadata.StackOrchestrator,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &meta, nil
|
|
|
|
}
|