Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add labels to marathon and environment for schedulers. #18

Merged
merged 1 commit into from
Feb 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions framework/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (a *Application) Run(context *Context, client marathon.Marathon, stateStora
return err
}

application := a.createApplication()
application := a.createApplication(context)
_, err = client.CreateApplication(application)
if err != nil {
return err
Expand Down Expand Up @@ -298,10 +298,10 @@ func (a *Application) checkRunningAndHealthy(client marathon.Marathon) error {
return nil
}

func (a *Application) createApplication() *marathon.Application {
func (a *Application) createApplication(context *Context) *marathon.Application {
application := &marathon.Application{
ID: a.ID,
Cmd: a.getLaunchCommand(),
Cmd: a.getLaunchCommand(context),
Instances: a.getInstances(),
CPUs: a.Cpu,
Mem: a.Mem,
Expand All @@ -311,16 +311,37 @@ func (a *Application) createApplication() *marathon.Application {
User: a.User,
HealthChecks: a.getHealthchecks(),
Constraints: a.Constraints,
Labels: a.getLabelsFromContext(context),
}
return application
}

func (a *Application) getLaunchCommand() string {
func (a *Application) getLabelsFromContext(context *Context) map[string]string {
keys := []string{"zone", "stack"}
labels := make(map[string]string)
for _, key := range keys {
val, _ := context.Get(key)
if val != "" {
labels[key] = val
}
}
return labels
}

func (a *Application) getLaunchCommand(context *Context) string {
cmd := a.LaunchCommand
for k, v := range a.Scheduler {
cmd += fmt.Sprintf(" --%s %s", k, fmt.Sprint(v))
}

labelStrings := make([]string, 0)
for key, val := range a.getLabelsFromContext(context) {
labelStrings = append(labelStrings, fmt.Sprintf("%s=%s", key, val))
}
stackLabels := strings.Join(labelStrings, ";")
if stackLabels != "" {
env := fmt.Sprintf("STACK_LABELS=\"%s\" ", stackLabels)
cmd = env + cmd
}
return cmd
}

Expand Down
4 changes: 2 additions & 2 deletions framework/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,13 @@ func TestApplication(t *testing.T) {
app := new(Application)
app.LaunchCommand = "./script.sh"

So(app.getLaunchCommand(), ShouldEqual, "./script.sh")
So(app.getLaunchCommand(&Context{}), ShouldEqual, "./script.sh")

app.Scheduler = map[string]string{
"foo": "bar",
}

So(app.getLaunchCommand(), ShouldEqual, "./script.sh --foo bar")
So(app.getLaunchCommand(&Context{}), ShouldEqual, "./script.sh --foo bar")
})

Convey("Custom shell commands should run correctly", t, func() {
Expand Down
2 changes: 1 addition & 1 deletion framework/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (*MockStorage) GetStack(name string) (*Stack, error) {

type FakeStack struct{}

func (*FakeStack) Run(*Context, string, marathon.Marathon, StateStorage, int) (*Context, error) {
func (*FakeStack) Run(*RunRequest, *Context, marathon.Marathon, StateStorage) (*Context, error) {
return &Context{}, nil
}
func (*FakeStack) GetStack() *Stack {
Expand Down
2 changes: 2 additions & 0 deletions framework/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func (s *Stack) Run(request *RunRequest, context *Context, client marathon.Marat
return nil, err
}
context.Set("mesos.master", info.MarathonConfig.Master)
context.Set("zone", request.Zone)
context.Set("stack", s.Name)

err = s.markSkippedApps(request.SkipApplications, runningApps, statuses)
if err != nil {
Expand Down