Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Docker instance plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Degory <ndegory@axway.com>
  • Loading branch information
ndegory committed Apr 10, 2017
1 parent 84dfccc commit 619bd06
Show file tree
Hide file tree
Showing 7 changed files with 832 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ endif
$(call build_binary,infrakit-instance-terraform,github.com/docker/infrakit/examples/instance/terraform)
$(call build_binary,infrakit-instance-vagrant,github.com/docker/infrakit/examples/instance/vagrant)
$(call build_binary,infrakit-instance-maas,github.com/docker/infrakit/examples/instance/maas)
$(call build_binary,infrakit-instance-docker,github.com/docker/infrakit/examples/instance/docker)
$(call build_binary,infrakit-event-time,github.com/docker/infrakit/examples/event/time)

cli: build-cli
Expand Down
2 changes: 1 addition & 1 deletion dockerfiles/Dockerfile.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM golang:1.8-alpine

RUN apk add --update git make
RUN apk add --update git make gcc musl-dev
RUN go get github.com/rancher/trash

WORKDIR /go/src/github.com/docker/infrakit
Expand Down
122 changes: 122 additions & 0 deletions examples/instance/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
InfraKit Instance Plugin - Docker
=================================

[InfraKit](https://github.com/docker/infrakit) plugins for creating and managing Docker containers.

## Instance plugin

The InfraKit instance plugin creates and monitors Docker containers.

### Example

Based on the [default](https://github.com/docker/infrakit/tree/master/cmd/group) Group
plugin:
```console
$ build/infrakit-group-default
INFO[0000] Starting discovery
INFO[0000] Starting plugin
INFO[0000] Starting
INFO[0000] Listening on: unix:///run/infrakit/plugins/group.sock
INFO[0000] listener protocol= unix addr= /run/infrakit/plugins/group.sock err= <nil>
```

and the [Vanilla](https://github.com/docker/infrakit/tree/master/pkg/example/flavor/vanilla) Flavor plugin:
```console
$ build/infrakit-flavor-vanilla
INFO[0000] Starting plugin
INFO[0000] Listening on: unix:///run/infrakit/plugins/flavor-vanilla.sock
INFO[0000] listener protocol= unix addr= /run/infrakit/plugins/flavor-vanilla.sock err= <nil>
```

We will use a basic configuration that creates a single instance:
```console
$ cat << EOF > aws-vanilla.json
{
"ID": "docker-example",
"Properties": {
"Allocation": {
"Size": 1
},
"Instance": {
"Plugin": "instance-docker",
"Properties": {
"Config": {
"Image": "alpine:3.5"
},
"HostConfig": {
"AutoRemove": true
},
"Tags": {
"Name": "infrakit-example"
}
}
},
"Flavor": {
"Plugin": "flavor-vanilla",
"Properties": {
"Init": [
"sh -c \"echo 'Hello, World!' > /hello\""
]
}
}
}
}
EOF
```

For the structure of `Config` `HostConfig`, see the plugin properties definition below.

Finally, instruct the Group plugin to start watching the group:
```console
$ build/infrakit group watch docker-vanilla.json
watching docker-example
```

In the console running the Group plugin, we will see input like the following:
```
INFO[1208] Watching group 'docker-example'
INFO[1219] Adding 1 instances to group to reach desired 1
INFO[1219] Created instance i-ba0412a2 with tags map[infrakit.config_sha:dUBtWGmkptbGg29ecBgv1VJYzys= infrakit.group:aws-example]
```

Additionally, the CLI will report the newly-created instance:
```console
$ build/infrakit group inspect docker-example
ID LOGICAL TAGS
90e6f3de4918 elusive_leaky Name=infrakit-example,infrakit.config_sha=dUBtWGmkptbGg29ecBgv1VJYzys=,infrakit.group=docker-example
```

Retrieve the name of the container and connect to it with an exec

```console
$ docker exec -ti elusive_leaky cat /hello
Hello, World!
```

### Plugin properties

The plugin expects properties in the following format:
```json
{
"Tags": {
},
"Config": {
},
"HostConfig": {
},
"NetworkAttachments": [
]
}
```

The `Tags` property is a string-string mapping of labels to apply to all Docker containers that are created.
`Config` follows the structure of the type by the same name in the
[Docker go SDK](https://github.com/docker/docker/blob/master/api/types/container/config.go).
`HostConfig` follows the structure of the type by the same name in the
[Docker go SDK](https://github.com/docker/docker/blob/master/api/types/container/host_config.go).
`NetworkAttachments` is an array of [NetworkResource](https://github.com/docker/docker/blob/master/api/types/types.go).

### LogicalID

To take advantage of the Docker networking DNS, the InfraKit logicalID is mapped to the Docker container hostname (and not its IP).
The plugin is compatible with both allocation methods, logical IDs (cattles) or group size (pets).
58 changes: 58 additions & 0 deletions examples/instance/docker/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"
"log"

"github.com/docker/docker/client"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/spf13/pflag"
)

const (
defaultDockerURL = "unix:///var/run/docker.sock"
defaultDockerAPIVersion = "1.25"
)

type options struct {
dockerURL string
dockerAPIVersion string
//todo: add TLS options
retries int
}

// Builder is a ProvisionerBuilder that creates a Docker instance provisioner
type Builder struct {
client *client.Client
options options
}

// Flags returns the flags required.
func (b *Builder) Flags() *pflag.FlagSet {
flags := pflag.NewFlagSet("docker", pflag.PanicOnError)
flags.StringVar(&b.options.dockerURL, "url", defaultDockerURL, "Docker API URL")
flags.StringVar(&b.options.dockerAPIVersion, "version", defaultDockerAPIVersion, "Docker API version")
flags.IntVar(&b.options.retries, "retries", 5, "Number of retries for Docker API operations")
return flags
}

// BuildInstancePlugin creates an instance Provisioner configured with the Flags.
func (b *Builder) BuildInstancePlugin(namespaceTags map[string]string) (instance.Plugin, error) {
if b.client == nil {
defaultHeaders := map[string]string{"User-Agent": "InfraKit"}
cli, err := client.NewClient(b.options.dockerURL, b.options.dockerAPIVersion, nil, defaultHeaders)
if err != nil {
return nil, fmt.Errorf("failed to connect to Docker on [%s]\n%v", b.options.dockerURL, err)
}
b.client = cli
}
return NewInstancePlugin(b.client, namespaceTags), nil
}

type logger struct {
logger *log.Logger
}

func (l logger) Log(args ...interface{}) {
l.logger.Println(args...)
}
Loading

0 comments on commit 619bd06

Please sign in to comment.