wip: grpc api
Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
parent
1dd748e3f2
commit
2eba8d6511
174 changed files with 22012 additions and 11410 deletions
|
@ -1,9 +1,15 @@
|
|||
package docker
|
||||
|
||||
type Container struct {
|
||||
id string
|
||||
id string
|
||||
endpoint string
|
||||
}
|
||||
|
||||
func (c Container) ID() string {
|
||||
return c.id
|
||||
}
|
||||
|
||||
func (c Container) Endpoint() string {
|
||||
// TODO
|
||||
return c.endpoint
|
||||
}
|
||||
|
|
|
@ -1,13 +1,83 @@
|
|||
package docker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
dockertypes "github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/ehazlett/element/api/types"
|
||||
"github.com/ehazlett/element/runtime"
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func (d *Docker) Create(spec *runtime.Spec) error {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"spec": spec,
|
||||
}).Debug("creating container")
|
||||
return nil
|
||||
func (d *Docker) Create(spec *types.RuntimeSpec) (runtime.Container, error) {
|
||||
if spec.Labels == nil {
|
||||
spec.Labels = map[string]string{}
|
||||
}
|
||||
labels := spec.Labels
|
||||
// insert element labels
|
||||
labels[elementRuntimeLabel] = "docker"
|
||||
|
||||
p := strconv.Itoa(int(spec.Port))
|
||||
port, err := nat.NewPort(spec.Protocol, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
containerConfig := &container.Config{
|
||||
Image: spec.Image,
|
||||
Labels: spec.Labels,
|
||||
ExposedPorts: nat.PortSet{
|
||||
port: struct{}{},
|
||||
},
|
||||
}
|
||||
|
||||
hostConfig := &container.HostConfig{
|
||||
PortBindings: nat.PortMap{
|
||||
port: []nat.PortBinding{
|
||||
{
|
||||
HostIP: "0.0.0.0",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
logrus.Debugf("%+v", containerConfig)
|
||||
|
||||
// create
|
||||
resp, err := d.client.ContainerCreate(context.Background(), containerConfig, hostConfig, nil, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// start
|
||||
if err := d.client.ContainerStart(context.Background(), resp.ID, dockertypes.ContainerStartOptions{}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
optFilters := filters.NewArgs()
|
||||
optFilters.Add("id", resp.ID)
|
||||
optFilters.Add("label", elementRuntimeLabel)
|
||||
|
||||
containers, err := d.client.ContainerList(context.Background(), dockertypes.ContainerListOptions{
|
||||
Filters: optFilters,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(containers) == 0 {
|
||||
return nil, fmt.Errorf("error creating container: no container found after start")
|
||||
}
|
||||
|
||||
container := containers[0]
|
||||
endpoint := getContainerEndpoint(container)
|
||||
|
||||
return Container{
|
||||
id: container.ID,
|
||||
endpoint: endpoint,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package docker
|
||||
|
||||
func (d *Docker) Delete(id string) error {
|
||||
func (d *Docker) Delete(namespace, id string) error {
|
||||
// TODO
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,11 +1,17 @@
|
|||
package docker
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/docker/docker/client"
|
||||
)
|
||||
|
||||
const (
|
||||
elementRuntimeLabel = "runtime.element"
|
||||
elementRuntimeLabel = "element.runtime"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrContainerNotFound = errors.New("container not found")
|
||||
)
|
||||
|
||||
type Docker struct {
|
||||
|
|
18
runtime/docker/get.go
Normal file
18
runtime/docker/get.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package docker
|
||||
|
||||
import "github.com/ehazlett/element/runtime"
|
||||
|
||||
func (d *Docker) Get(namespace, id string) (runtime.Container, error) {
|
||||
containers, err := d.List(namespace)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, c := range containers {
|
||||
if c.ID() == id {
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, ErrContainerNotFound
|
||||
}
|
|
@ -10,7 +10,7 @@ import (
|
|||
|
||||
func (d *Docker) List(namespace string) ([]runtime.Container, error) {
|
||||
optFilters := filters.NewArgs()
|
||||
//optFilters.Add("label", elementRuntimeLabel)
|
||||
optFilters.Add("label", elementRuntimeLabel)
|
||||
|
||||
dockerContainers, err := d.client.ContainerList(context.Background(), types.ContainerListOptions{
|
||||
Filters: optFilters,
|
||||
|
@ -21,8 +21,10 @@ func (d *Docker) List(namespace string) ([]runtime.Container, error) {
|
|||
|
||||
var containers []runtime.Container
|
||||
for _, c := range dockerContainers {
|
||||
endpoint := getContainerEndpoint(c)
|
||||
containers = append(containers, Container{
|
||||
id: c.ID,
|
||||
id: c.ID,
|
||||
endpoint: endpoint,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
27
runtime/docker/utils.go
Normal file
27
runtime/docker/utils.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
package docker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func getContainerEndpoint(c types.Container) string {
|
||||
logrus.Debug("getting container endpoint")
|
||||
endpoint := ""
|
||||
if len(c.Ports) > 0 {
|
||||
for _, p := range c.Ports {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"port": fmt.Sprintf("%+v", p),
|
||||
}).Debug("checking container port")
|
||||
|
||||
if p.IP != "" && p.PublicPort != 0 {
|
||||
endpoint = fmt.Sprintf("%s:%d", p.IP, p.PublicPort)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return endpoint
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue