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

Commit f9e924e

Browse files
author
David Chung
authored
[WIP] - Enrollment controller (#678)
Signed-off-by: David Chung [email protected]
1 parent a55eca6 commit f9e924e

File tree

19 files changed

+1619
-43
lines changed

19 files changed

+1619
-43
lines changed

cmd/infrakit/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141

4242
// Supported "kinds"
4343
_ "github.com/docker/infrakit/pkg/run/v0/aws"
44+
_ "github.com/docker/infrakit/pkg/run/v0/enrollment"
4445
_ "github.com/docker/infrakit/pkg/run/v0/file"
4546
_ "github.com/docker/infrakit/pkg/run/v0/group"
4647
_ "github.com/docker/infrakit/pkg/run/v0/hyperkit"
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#
2+
#
3+
# Example for enrollment controller
4+
#
5+
# Enrollment controller makes sure the instances in the source group (specified
6+
# by the List field) are in sync with entries in the downstream instance plugin
7+
# specified by Instance field.
8+
#
9+
# This example uses swarm as backend. So be sure your swarm on the same host
10+
# is a leader via `docker swarm init`.
11+
#
12+
#
13+
# 1. Start up all the plugins:
14+
# INFRAKIT_MANAGER_BACKEND=swarm build/infrakit plugin start manager group \
15+
# enrollment:nfs \
16+
# simulator:nfs-auth simulator:simulator vanilla
17+
#
18+
# This starts up the group at 'us-east', one simulator at 'nfs' and another at 'simulator'
19+
#
20+
# 2. Commit the group to create the groups -- see the group.yml included.
21+
#
22+
# infrakit group controller commit -y docs/controller/enrollment/group.yml
23+
#
24+
# Verify the group has workers:
25+
#
26+
# infrakit group/workers describe
27+
#
28+
# 3. Commit this file to create the enrollments
29+
#
30+
# infrakit nfs controller commit -y docs/controller/enrollment/example.yml
31+
#
32+
# 4. Verify that entries are in sync: check the group describe and nfs-auth/disk (that's
33+
# the Plugin specified in the Instance section of this config.
34+
#
35+
# infrakit group/workers describe # returns the list of nodes in the group
36+
# infrakit infrakit nfs-auth/disk describe # returns the list of corresponding enrollments
37+
#
38+
# For each member of the group workers you should see a corresponding entry in the enrollments.
39+
# Note that the instance ID of an enrollment (in nfs-auth/disk) will be different from the instance
40+
# ID of a member in group/workers; however, the ID of a member of group/workers should show up as
41+
# a label in the instance of nfs-auth/disk (as infrakit.enrollment.sourceID).
42+
#
43+
# 5. Try scale up the workers group
44+
#
45+
# infrakit group/workers scale 10
46+
#
47+
# After a while, verify enrollment:
48+
#
49+
# infrakit nfs-auth/disk describe
50+
51+
52+
kind: enrollment
53+
metadata:
54+
name: nfs/workers # socket file = nfs and the name of control loop is 'workers'
55+
properties:
56+
List: group/workers # socket file = group and group id is 'workers'
57+
Instance:
58+
59+
# the name of a plugin that has disk as subtype.
60+
Plugin: nfs-auth/disk
61+
62+
# the entire Properties block here will be rendered and included as the downstream
63+
# instance plugin's instance.Spec when Provision() is called.
64+
Properties:
65+
66+
# You can include template expressions in this block; however, you need to
67+
# escape the braces.
68+
host: \{\{.ID\}\}
69+
iops: 10
70+
options:
71+
# This expression is a template used to select the key from each source entry
72+
# of instance.Description. Note here we escape the template tags
73+
# so that template expressions don't get clobbered by infrakit template when
74+
# using that as a preprocessor prior to committing.
75+
SourceKeySelector: \{\{.ID\}\}
76+
77+
# How often to run the sync. The string value here is in the format of Go's time.Duration.
78+
# For example, 1m means 1 minute.
79+
SyncInterval: 5s # seconds

docs/controller/enrollment/group.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# A group of workers
3+
#
4+
# Start up -- plugin start should include manager, vanilla, simulator, and group
5+
# Then commit
6+
#
7+
# infrakit group controller commit -y docs/controller/enrollment/group.yml
8+
#
9+
kind: group
10+
metadata:
11+
name: workers
12+
properties:
13+
Allocation:
14+
Size: 5
15+
Flavor:
16+
Plugin: vanilla
17+
Properties:
18+
Attachments:
19+
- ID: attachid
20+
Type: attachtype
21+
Init:
22+
- docker pull nginx:alpine
23+
- docker run -d -p 80:80 nginx-alpine
24+
Tags:
25+
project: infrakit
26+
tier: web
27+
Instance:
28+
Plugin: simulator/compute
29+
Properties:
30+
Note: custom field

docs/plugins/instance.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Instance plugin API
22

3-
<!-- SOURCE-CHECKSUM pkg/spi/instance/* 9117db2da2ea1073a6f94b241e6c6a9a0a9048d452c44631323150dfeffdfde2b8c5ba85e274fabe -->
3+
<!-- SOURCE-CHECKSUM pkg/spi/instance/* 6b3c98bed4470312a41376f651cee99a9e35ffb09117db2da2ea1073a6f94b241e6c6a9a0a9048d452c44631323150dfeffdfde2b8c5ba85e274fabe -->
4+
45

56
## API
67

pkg/cli/v0/group/cmd.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ var log = logutil.New("module", "cli/v1/group")
1515
func init() {
1616
cli.Register(group.InterfaceSpec,
1717
[]cli.CmdBuilder{
18-
//Group,
18+
// Group,
1919
Ls,
2020
Inspect,
2121
Describe,
2222
Commit,
2323
Free,
2424
Destroy,
25-
Size,
26-
SetSize,
25+
Scale,
2726
DestroyInstances,
2827
})
2928
}
@@ -42,8 +41,7 @@ func Group(name string, services *cli.Services) *cobra.Command {
4241
Commit(name, services),
4342
Free(name, services),
4443
Destroy(name, services),
45-
Size(name, services),
46-
SetSize(name, services),
44+
Scale(name, services),
4745
DestroyInstances(name, services),
4846
)
4947

pkg/cli/v0/group/scale.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package group
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strconv"
7+
8+
"github.com/docker/infrakit/pkg/cli"
9+
"github.com/docker/infrakit/pkg/plugin"
10+
"github.com/docker/infrakit/pkg/spi/group"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
// Scale returns the scale command
15+
func Scale(name string, services *cli.Services) *cobra.Command {
16+
17+
scale := &cobra.Command{
18+
Use: "scale <groupID> [new-target]",
19+
Short: "Returns size of the group if no args provided. Otherwise set the target size.",
20+
RunE: func(cmd *cobra.Command, args []string) error {
21+
22+
pluginName := plugin.Name(name)
23+
_, gid := pluginName.GetLookupAndType()
24+
25+
size := -1
26+
27+
if gid == "" {
28+
// if gid is not known, then we need it to be provided
29+
switch len(args) {
30+
31+
case 0:
32+
cmd.Usage()
33+
os.Exit(1)
34+
case 1:
35+
gid = args[0]
36+
case 2:
37+
gid = args[0]
38+
sz, err := strconv.Atoi(args[1])
39+
if err != nil {
40+
return err
41+
}
42+
size = sz
43+
default:
44+
cmd.Usage()
45+
os.Exit(1)
46+
}
47+
} else {
48+
// if gid is not known, then we need it to be provided
49+
switch len(args) {
50+
51+
case 0:
52+
size = -1
53+
case 1:
54+
sz, err := strconv.Atoi(args[0])
55+
if err != nil {
56+
return err
57+
}
58+
size = sz
59+
default:
60+
cmd.Usage()
61+
os.Exit(1)
62+
}
63+
}
64+
65+
groupPlugin, err := LoadPlugin(services.Plugins(), name)
66+
if err != nil {
67+
return nil
68+
}
69+
cli.MustNotNil(groupPlugin, "group plugin not found", "name", name)
70+
71+
groupID := group.ID(gid)
72+
target, err := groupPlugin.Size(groupID)
73+
if err != nil {
74+
return err
75+
}
76+
fmt.Printf("Group %v at %d instances", groupID, target)
77+
78+
if size > -1 {
79+
err = groupPlugin.SetSize(groupID, size)
80+
if err != nil {
81+
return err
82+
}
83+
fmt.Printf(", scale to %d", size)
84+
}
85+
86+
fmt.Println()
87+
return nil
88+
},
89+
}
90+
return scale
91+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package enrollment
2+
3+
import (
4+
"github.com/docker/infrakit/pkg/controller"
5+
enrollment "github.com/docker/infrakit/pkg/controller/enrollment/types"
6+
"github.com/docker/infrakit/pkg/controller/internal"
7+
"github.com/docker/infrakit/pkg/discovery"
8+
logutil "github.com/docker/infrakit/pkg/log"
9+
"github.com/docker/infrakit/pkg/manager"
10+
"github.com/docker/infrakit/pkg/types"
11+
)
12+
13+
var (
14+
log = logutil.New("module", "controller/enrollment")
15+
debugV = logutil.V(200)
16+
)
17+
18+
// NewController returns a controller implementation
19+
func NewController(plugins func() discovery.Plugins, leader manager.Leadership,
20+
options enrollment.Options) controller.Controller {
21+
return internal.NewController(
22+
leader,
23+
// the constructor
24+
func(spec types.Spec) (internal.Managed, error) {
25+
return newEnroller(plugins, leader, options), nil
26+
},
27+
// the key function
28+
func(metadata types.Metadata) string {
29+
return metadata.Name
30+
},
31+
)
32+
}
33+
34+
// NewTypedControllers return typed controllers
35+
func NewTypedControllers(plugins func() discovery.Plugins, leader manager.Leadership,
36+
options enrollment.Options) func() (map[string]controller.Controller, error) {
37+
38+
return (internal.NewController(
39+
leader,
40+
// the constructor
41+
func(spec types.Spec) (internal.Managed, error) {
42+
log.Debug("Creating managed object", "spec", spec)
43+
return newEnroller(plugins, leader, options), nil
44+
},
45+
// the key function
46+
func(metadata types.Metadata) string {
47+
return metadata.Name
48+
},
49+
)).ManagedObjects
50+
}
51+
52+
func (l *enroller) started() bool {
53+
l.lock.RLock()
54+
defer l.lock.RUnlock()
55+
56+
return l.running
57+
}

0 commit comments

Comments
 (0)