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

Commit 6ecb296

Browse files
ndegoryDavid Chung
authored andcommitted
Docker instance plugin (#469)
Signed-off-by: Nicolas Degory <[email protected]>
1 parent afdd155 commit 6ecb296

File tree

97 files changed

+8176
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+8176
-1
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ endif
131131
$(call build_binary,infrakit-instance-terraform,github.com/docker/infrakit/examples/instance/terraform)
132132
$(call build_binary,infrakit-instance-vagrant,github.com/docker/infrakit/examples/instance/vagrant)
133133
$(call build_binary,infrakit-instance-maas,github.com/docker/infrakit/examples/instance/maas)
134+
$(call build_binary,infrakit-instance-docker,github.com/docker/infrakit/examples/instance/docker)
134135
$(call build_binary,infrakit-event-time,github.com/docker/infrakit/examples/event/time)
135136

136137
cli: build-cli

dockerfiles/Dockerfile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM golang:1.8-alpine
22

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

66
WORKDIR /go/src/github.com/docker/infrakit

examples/instance/docker/README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
InfraKit Instance Plugin - Docker
2+
=================================
3+
4+
[InfraKit](https://github.com/docker/infrakit) plugins for creating and managing Docker containers.
5+
6+
## Instance plugin
7+
8+
The InfraKit instance plugin creates and monitors Docker containers.
9+
10+
### Example
11+
12+
Based on the [default](https://github.com/docker/infrakit/tree/master/cmd/group) Group
13+
plugin:
14+
```console
15+
$ build/infrakit-group-default
16+
INFO[0000] Starting discovery
17+
INFO[0000] Starting plugin
18+
INFO[0000] Starting
19+
INFO[0000] Listening on: unix:///run/infrakit/plugins/group.sock
20+
INFO[0000] listener protocol= unix addr= /run/infrakit/plugins/group.sock err= <nil>
21+
```
22+
23+
and the [Vanilla](https://github.com/docker/infrakit/tree/master/pkg/example/flavor/vanilla) Flavor plugin:
24+
```console
25+
$ build/infrakit-flavor-vanilla
26+
INFO[0000] Starting plugin
27+
INFO[0000] Listening on: unix:///run/infrakit/plugins/flavor-vanilla.sock
28+
INFO[0000] listener protocol= unix addr= /run/infrakit/plugins/flavor-vanilla.sock err= <nil>
29+
```
30+
31+
We will use a basic configuration that creates a single instance:
32+
```console
33+
$ cat << EOF > aws-vanilla.json
34+
{
35+
"ID": "docker-example",
36+
"Properties": {
37+
"Allocation": {
38+
"Size": 1
39+
},
40+
"Instance": {
41+
"Plugin": "instance-docker",
42+
"Properties": {
43+
"Config": {
44+
"Image": "alpine:3.5"
45+
},
46+
"HostConfig": {
47+
"AutoRemove": true
48+
},
49+
"Tags": {
50+
"Name": "infrakit-example"
51+
}
52+
}
53+
},
54+
"Flavor": {
55+
"Plugin": "flavor-vanilla",
56+
"Properties": {
57+
"Init": [
58+
"sh -c \"echo 'Hello, World!' > /hello\""
59+
]
60+
}
61+
}
62+
}
63+
}
64+
EOF
65+
```
66+
67+
For the structure of `Config` `HostConfig`, see the plugin properties definition below.
68+
69+
Finally, instruct the Group plugin to start watching the group:
70+
```console
71+
$ build/infrakit group watch docker-vanilla.json
72+
watching docker-example
73+
```
74+
75+
In the console running the Group plugin, we will see input like the following:
76+
```
77+
INFO[1208] Watching group 'docker-example'
78+
INFO[1219] Adding 1 instances to group to reach desired 1
79+
INFO[1219] Created instance i-ba0412a2 with tags map[infrakit.config_sha:dUBtWGmkptbGg29ecBgv1VJYzys= infrakit.group:aws-example]
80+
```
81+
82+
Additionally, the CLI will report the newly-created instance:
83+
```console
84+
$ build/infrakit group inspect docker-example
85+
ID LOGICAL TAGS
86+
90e6f3de4918 elusive_leaky Name=infrakit-example,infrakit.config_sha=dUBtWGmkptbGg29ecBgv1VJYzys=,infrakit.group=docker-example
87+
```
88+
89+
Retrieve the name of the container and connect to it with an exec
90+
91+
```console
92+
$ docker exec -ti elusive_leaky cat /hello
93+
Hello, World!
94+
```
95+
96+
### Plugin properties
97+
98+
The plugin expects properties in the following format:
99+
```json
100+
{
101+
"Tags": {
102+
},
103+
"Config": {
104+
},
105+
"HostConfig": {
106+
},
107+
"NetworkAttachments": [
108+
]
109+
}
110+
```
111+
112+
The `Tags` property is a string-string mapping of labels to apply to all Docker containers that are created.
113+
`Config` follows the structure of the type by the same name in the
114+
[Docker go SDK](https://github.com/docker/docker/blob/master/api/types/container/config.go).
115+
`HostConfig` follows the structure of the type by the same name in the
116+
[Docker go SDK](https://github.com/docker/docker/blob/master/api/types/container/host_config.go).
117+
`NetworkAttachments` is an array of [NetworkResource](https://github.com/docker/docker/blob/master/api/types/types.go).
118+
119+
### LogicalID
120+
121+
To take advantage of the Docker networking DNS, the InfraKit logicalID is mapped to the Docker container hostname (and not its IP).
122+
The plugin is compatible with both allocation methods, logical IDs (cattles) or group size (pets).

examples/instance/docker/builder.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/docker/docker/client"
8+
"github.com/docker/infrakit/pkg/spi/instance"
9+
"github.com/spf13/pflag"
10+
)
11+
12+
const (
13+
defaultDockerURL = "unix:///var/run/docker.sock"
14+
defaultDockerAPIVersion = "1.25"
15+
)
16+
17+
type options struct {
18+
dockerURL string
19+
dockerAPIVersion string
20+
//todo: add TLS options
21+
retries int
22+
}
23+
24+
// Builder is a ProvisionerBuilder that creates a Docker instance provisioner
25+
type Builder struct {
26+
client *client.Client
27+
options options
28+
}
29+
30+
// Flags returns the flags required.
31+
func (b *Builder) Flags() *pflag.FlagSet {
32+
flags := pflag.NewFlagSet("docker", pflag.PanicOnError)
33+
flags.StringVar(&b.options.dockerURL, "url", defaultDockerURL, "Docker API URL")
34+
flags.StringVar(&b.options.dockerAPIVersion, "version", defaultDockerAPIVersion, "Docker API version")
35+
flags.IntVar(&b.options.retries, "retries", 5, "Number of retries for Docker API operations")
36+
return flags
37+
}
38+
39+
// BuildInstancePlugin creates an instance Provisioner configured with the Flags.
40+
func (b *Builder) BuildInstancePlugin(namespaceTags map[string]string) (instance.Plugin, error) {
41+
if b.client == nil {
42+
defaultHeaders := map[string]string{"User-Agent": "InfraKit"}
43+
cli, err := client.NewClient(b.options.dockerURL, b.options.dockerAPIVersion, nil, defaultHeaders)
44+
if err != nil {
45+
return nil, fmt.Errorf("failed to connect to Docker on [%s]\n%v", b.options.dockerURL, err)
46+
}
47+
b.client = cli
48+
}
49+
return NewInstancePlugin(b.client, namespaceTags), nil
50+
}
51+
52+
type logger struct {
53+
logger *log.Logger
54+
}
55+
56+
func (l logger) Log(args ...interface{}) {
57+
l.logger.Println(args...)
58+
}

0 commit comments

Comments
 (0)