Skip to content

Commit

Permalink
added container label support (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
subfuzion authored and chrisccoy committed Sep 26, 2016
1 parent 34425b0 commit 7bc3583
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 30 deletions.
40 changes: 29 additions & 11 deletions api/rpc/stack/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
)

type serviceSpec struct {
Image string `yaml:"image"`
Replicas uint64 `yaml:"replicas"`
Environment interface{} `yaml:"environment"`
Labels interface{} `yaml:"labels"`
Public []publishSpec `yaml:"public"`
Image string `yaml:"image"`
Public []publishSpec `yaml:"public"`
Replicas uint64 `yaml:"replicas"`
Environment interface{} `yaml:"environment"`
Labels interface{} `yaml:"labels"`
ContainerLabels interface{} `yaml:"container_labels"`
}

type publishSpec struct {
Expand Down Expand Up @@ -62,6 +63,22 @@ func ParseStackfile(ctx context.Context, in string) (stack *Stack, err error) {
}
}

// try to parse container labels as a map
// else try to parse container labels as string entries
containerLabels := map[string]string{}
if labelMap, ok := spec.ContainerLabels.(map[interface{}]interface{}); ok {
for k, v := range labelMap {
containerLabels[k.(string)] = v.(string)
}
} else if labelList, ok := spec.ContainerLabels.([]interface{}); ok {
for _, s := range labelList {
a := strings.Split(s.(string), "=")
k := a[0]
v := a[1]
containerLabels[k] = v
}
}

replicas := spec.Replicas
if replicas == 0 {
replicas = 1
Expand All @@ -78,12 +95,13 @@ func ParseStackfile(ctx context.Context, in string) (stack *Stack, err error) {
}

stack.Services = append(stack.Services, &service.ServiceSpec{
Name: name,
Image: spec.Image,
Replicas: replicas,
Env: env,
Labels: labels,
PublishSpecs: publishSpecs,
Name: name,
Image: spec.Image,
PublishSpecs: publishSpecs,
Replicas: replicas,
Env: env,
Labels: labels,
ContainerLabels: containerLabels,
})
}

Expand Down
43 changes: 33 additions & 10 deletions api/rpc/stack/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,28 @@ var (
},
}

sample5 = map[string]serviceSpec{
sample6 = map[string]serviceSpec{
"pinger": {
Image: "appcelerator/pinger",
Labels: map[string]string{
"foo": "bar",
"foo": "bar",
"hello": "world",
},
Public: []publishSpec{
{
PublishPort: 3000,
InternalPort: 3000,
},
},
},
}

sample7 = map[string]serviceSpec{
"pinger": {
Image: "appcelerator/pinger",
ContainerLabels: map[string]string{
"foo": "bar",
"hello": "world",
},
Public: []publishSpec{
{
Expand All @@ -117,13 +134,15 @@ var (

// map of filenames to a map of serviceSpec elements (each file has one or more)
compareSpecs = map[string]map[string]serviceSpec{
"sample-01.yml": sample1,
"sample-02.yml": sample2,
"sample-03.yml": sample3,
"sample-03.json": sample3,
"sample-04.yml": sample4,
"sample-05-labels.yml": sample5,
"sample-06-labels.yml": sample5,
"sample-01.yml": sample1,
"sample-02.yml": sample2,
"sample-03.yml": sample3,
"sample-03.json": sample3,
"sample-04.yml": sample4,
"sample-06-1-service-labels.yml": sample6,
"sample-06-2-service-labels.yml": sample6,
"sample-07-1-container-labels.yml": sample7,
"sample-07-2-container-labels.yml": sample7,
}
)

Expand Down Expand Up @@ -202,7 +221,11 @@ func (a serviceSpec) compare(t *testing.T, b serviceSpec) bool {
return false
}
if !reflect.DeepEqual(toMap(a.Labels), toMap(b.Labels)) {
t.Logf("actual != expected (label): %v != %v\n", a.Labels, b.Labels)
t.Logf("actual != expected (labels): %v != %v\n", a.Labels, b.Labels)
return false
}
if !reflect.DeepEqual(toMap(a.ContainerLabels), toMap(b.ContainerLabels)) {
t.Logf("actual != expected (container_labels): %v != %v\n", a.ContainerLabels, b.ContainerLabels)
return false
}
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pinger:
image: appcelerator/pinger
labels:
foo: bar
hello: world
public:
- publish_port: 3000
internal_port: 3000
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pinger:
image: appcelerator/pinger
labels:
- "foo=bar"
- "hello=world"
public:
- publish_port: 3000
internal_port: 3000
8 changes: 8 additions & 0 deletions api/rpc/stack/test_samples/sample-07-1-container-labels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pinger:
image: appcelerator/pinger
container_labels:
foo: bar
hello: world
public:
- publish_port: 3000
internal_port: 3000
8 changes: 8 additions & 0 deletions api/rpc/stack/test_samples/sample-07-2-container-labels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pinger:
image: appcelerator/pinger
container_labels:
- "foo=bar"
- "hello=world"
public:
- publish_port: 3000
internal_port: 3000
23 changes: 14 additions & 9 deletions cmd/amp/service-create.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ var (
env []string

// service labels
labels []string
serviceLabels []string

// container labels
containerLabels []string

// ports
publishSpecs []string
Expand All @@ -45,10 +48,11 @@ var (
func init() {
flags := createCmd.Flags()
flags.StringVar(&name, "name", name, "Service name")
flags.StringSliceVarP(&publishSpecs, "publish", "p", publishSpecs, "Publish a service externally. Format: [published-name|published-port:]internal-service-port[/protocol], i.e. '80:3000/tcp' or 'admin:3000'")
flags.Uint64Var(&replicas, "replicas", replicas, "Number of tasks (default none)")
flags.StringSliceVarP(&env, "env", "e", env, "Set environment variables (default [])")
flags.StringSliceVarP(&labels, "label", "l", labels, "Set service labels (default [])")
flags.StringSliceVarP(&publishSpecs, "publish", "p", publishSpecs, "Publish a service externally. Format: [published-name|published-port:]internal-service-port[/protocol], i.e. '80:3000/tcp' or 'admin:3000'")
flags.StringSliceVarP(&serviceLabels, "label", "l", serviceLabels, "Set service labels (default [])")
flags.StringSliceVar(&containerLabels, "container-label", containerLabels, "Set container labels for service replicas (default [])")

ServiceCmd.AddCommand(createCmd)
}
Expand All @@ -67,12 +71,13 @@ func create(amp *client.AMP, cmd *cobra.Command, args []string) error {
}

spec := &service.ServiceSpec{
Image: image,
Name: name,
Replicas: replicas,
Env: env,
Labels: stringmap(labels),
PublishSpecs: parsedSpecs,
Image: image,
Name: name,
Replicas: replicas,
Env: env,
Labels: stringmap(serviceLabels),
ContainerLabels: stringmap(containerLabels),
PublishSpecs: parsedSpecs,
}

request := &service.ServiceCreateRequest{
Expand Down

0 comments on commit 7bc3583

Please sign in to comment.