-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
all: adds image deploy support for kubernetes
This commit adds support for sidecar image deployment. When SOURCE_IMAGE is provided, the sidecar will inspect the given image and also a potential Procfile and tsuru.yaml on the primary container. This is sent to stdout so tsuru API can parse and process them. The primary container is commited and it's image is pushed.
- Loading branch information
Showing
7 changed files
with
252 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/tsuru/deploy-agent/internal/docker" | ||
"github.com/tsuru/deploy-agent/internal/docker/testing" | ||
"github.com/tsuru/deploy-agent/internal/tsuru" | ||
"github.com/tsuru/tsuru/exec" | ||
"gopkg.in/check.v1" | ||
) | ||
|
||
const primaryImage = "tsuru/base-platform" | ||
|
||
func checkSkip(c *check.C) { | ||
if os.Getenv("DEPLOYAGENT_INTEGRATION") == "" { | ||
c.Skip("skipping integration tests") | ||
} | ||
} | ||
|
||
func (s *S) TestInspect(c *check.C) { | ||
checkSkip(c) | ||
|
||
dClient, err := docker.NewClient("") | ||
c.Assert(err, check.IsNil) | ||
|
||
_, cleanup, err := testing.SetupPrimaryContainer(c) | ||
defer cleanup() | ||
|
||
sidecar, err := docker.NewSidecar(dClient, "root") | ||
c.Assert(err, check.IsNil) | ||
|
||
outW := new(bytes.Buffer) | ||
errW := new(bytes.Buffer) | ||
|
||
yamlData := ` | ||
hooks: | ||
build: | ||
- ps | ||
` | ||
|
||
err = sidecar.Execute(exec.ExecuteOptions{ | ||
Cmd: "/bin/sh", | ||
Args: []string{"-c", fmt.Sprintf("mkdir -p /home/application/current/ && echo '%s' > /home/application/current/tsuru.yaml", yamlData)}, | ||
Stdout: outW, | ||
Stderr: errW, | ||
}) | ||
c.Assert(err, check.IsNil) | ||
c.Assert(outW.String(), check.DeepEquals, "") | ||
c.Assert(errW.String(), check.DeepEquals, "") | ||
|
||
for _, loc := range []string{"/", "/app/user/", "/home/application/current/"} { | ||
outW.Reset() | ||
errW.Reset() | ||
|
||
err = sidecar.ExecuteAsUser("root", exec.ExecuteOptions{ | ||
Cmd: "/bin/sh", | ||
Args: []string{"-c", fmt.Sprintf(`mkdir -p %s && echo '%s' > %sProcfile`, loc, loc, loc)}, | ||
Stdout: outW, | ||
Stderr: errW, | ||
}) | ||
c.Assert(err, check.IsNil) | ||
c.Assert(outW.String(), check.DeepEquals, "") | ||
c.Assert(errW.String(), check.DeepEquals, "") | ||
|
||
outW.Reset() | ||
errW.Reset() | ||
|
||
err = inspect(dClient, "tsuru/base-platform", &executorFS{executor: sidecar}, outW, errW) | ||
c.Assert(err, check.IsNil) | ||
|
||
m := struct { | ||
Procfile string | ||
TsuruYaml tsuru.TsuruYaml | ||
Image docker.ImageInspect | ||
}{} | ||
err = json.NewDecoder(outW).Decode(&m) | ||
c.Assert(err, check.IsNil) | ||
c.Assert(outW.String(), check.DeepEquals, "") | ||
c.Assert(m.Procfile, check.DeepEquals, loc+"\n") | ||
c.Assert(m.TsuruYaml, check.DeepEquals, tsuru.TsuruYaml{Hooks: tsuru.Hook{BuildHooks: []string{"ps"}}}) | ||
c.Assert(m.Image.ID, check.Not(check.DeepEquals), "") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package testing | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/fsouza/go-dockerclient" | ||
"gopkg.in/check.v1" | ||
) | ||
|
||
const primaryImage = "tsuru/base-platform" | ||
|
||
func SetupPrimaryContainer(c *check.C) (string, func(), error) { | ||
dClient, err := docker.NewClient("unix:///var/run/docker.sock") | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
hostname, err := os.Hostname() | ||
if err != nil { | ||
return "", nil, fmt.Errorf("error getting hostname: %v", err) | ||
} | ||
|
||
err = dClient.PullImage(docker.PullImageOptions{Repository: primaryImage}, docker.AuthConfiguration{}) | ||
if err != nil { | ||
return "", nil, fmt.Errorf("error pulling image %v: %v", primaryImage, err) | ||
} | ||
|
||
pCont, err := dClient.CreateContainer(docker.CreateContainerOptions{ | ||
Config: &docker.Config{ | ||
Image: primaryImage, | ||
Cmd: []string{"/bin/sh", "-lc", "while true; do sleep 10; done"}, | ||
Labels: map[string]string{ | ||
"io.kubernetes.container.name": hostname, | ||
"io.kubernetes.pod.name": hostname, | ||
}, | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
return "", nil, fmt.Errorf("error creating primary container: %v", err) | ||
} | ||
|
||
cleanup := func() { | ||
dClient.RemoveContainer(docker.RemoveContainerOptions{ID: pCont.ID, Force: true}) | ||
} | ||
|
||
err = dClient.StartContainer(pCont.ID, nil) | ||
if err != nil { | ||
return pCont.ID, cleanup, fmt.Errorf("error starting primary container: %v", err) | ||
} | ||
|
||
timeout := time.After(time.Second * 15) | ||
for { | ||
c, err := dClient.InspectContainer(pCont.ID) | ||
if err != nil { | ||
return pCont.ID, cleanup, fmt.Errorf("error inspecting primary container: %v", err) | ||
} | ||
if c.State.Running { | ||
break | ||
} | ||
select { | ||
case <-time.After(time.Second): | ||
case <-timeout: | ||
return pCont.ID, cleanup, fmt.Errorf("timeout waiting for primary container to run") | ||
} | ||
} | ||
|
||
return pCont.ID, cleanup, nil | ||
} |
Oops, something went wrong.