Skip to content

Commit

Permalink
sidecar: support multiple destionation images
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Mar 29, 2018
1 parent d0f7a0d commit ada04e0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 34 deletions.
16 changes: 12 additions & 4 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ func setupSidecar(dockerClient *docker.Client, config Config) (*docker.Sidecar,

// pushSidecar commits the sidecar primary container, tags and pushes its image
func pushSidecar(dockerClient *docker.Client, sideCar *docker.Sidecar, config Config, w io.Writer) error {
if len(config.DestinationImages) == 0 {
return nil
}
fmt.Fprintln(w, "---- Building application image ----")
img, err := sideCar.CommitPrimaryContainer(context.Background(), config.DestinationImage)
imgID, err := sideCar.CommitPrimaryContainer(context.Background(), config.DestinationImages[0])
if err != nil {
return fmt.Errorf("failed to commit main container: %v", err)
}
Expand All @@ -82,11 +85,16 @@ func pushSidecar(dockerClient *docker.Client, sideCar *docker.Sidecar, config Co
Email: config.RegistryAuthEmail,
ServerAddress: config.RegistryAddress,
}
return tagAndPush(dockerClient, img, authConfig, config.RegistryPushRetries, w)
for _, destImg := range config.DestinationImages {
if err := tagAndPush(dockerClient, imgID, destImg, authConfig, config.RegistryPushRetries, w); err != nil {
return err
}
}
return nil
}

func tagAndPush(dockerClient *docker.Client, img docker.Image, auth docker.AuthConfig, retries int, w io.Writer) error {
err := dockerClient.Tag(context.Background(), img)
func tagAndPush(dockerClient *docker.Client, imgID, imageName string, auth docker.AuthConfig, retries int, w io.Writer) error {
img, err := dockerClient.Tag(context.Background(), imgID, imageName)
if err != nil {
return fmt.Errorf("error tagging image %v: %v", img, err)
}
Expand Down
14 changes: 8 additions & 6 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *Client) ListContainersByLabels(ctx context.Context, labels map[string]s
return conts, err
}

func (c *Client) Commit(ctx context.Context, containerID, image string) (Image, error) {
func (c *Client) Commit(ctx context.Context, containerID, image string) (string, error) {
registry, repo, tag := splitImageName(image)
img := Image{
registry: registry,
Expand All @@ -88,14 +88,16 @@ func (c *Client) Commit(ctx context.Context, containerID, image string) (Image,
Context: ctx,
})
if err != nil {
return Image{}, err
return "", err
}
img.ID = commitedImg.ID
return img, err
return commitedImg.ID, err
}

func (c *Client) Tag(ctx context.Context, img Image) error {
return c.api.TagImage(img.ID, docker.TagImageOptions{
// Tag tags the image given by imgID with the given imageName
func (c *Client) Tag(ctx context.Context, imgID, imageName string) (Image, error) {
reg, rep, tag := splitImageName(imageName)
img := Image{registry: reg, repository: rep, tag: tag, ID: imgID}
return img, c.api.TagImage(img.ID, docker.TagImageOptions{
Repo: img.Name(),
Tag: img.tag,
Force: true,
Expand Down
15 changes: 5 additions & 10 deletions internal/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,17 @@ func (s *S) TestClientCommit(c *check.C) {
c.Assert(err, check.IsNil)
dockerImage, err := client.api.InspectImage("admin/app-myapp:v23-builder")
c.Assert(err, check.IsNil)
c.Assert(img, check.DeepEquals, Image{
ID: dockerImage.ID,
registry: "10.200.10.1:5000",
repository: "admin/app-myapp",
tag: "v23-builder",
})
c.Assert(img, check.DeepEquals, dockerImage.ID)
}

func (s *S) TestClientTag(c *check.C) {
client, err := NewClient(s.dockerserver.URL())
c.Assert(err, check.IsNil)
contID := s.addTestContainer(c, "my-cont", "my-img")

img, err := client.Commit(context.Background(), contID, "10.200.10.1:5000/admin/app-myapp:v23-builder")
imgID, err := client.Commit(context.Background(), contID, "10.200.10.1:5000/admin/app-myapp:v23-builder")
c.Assert(err, check.IsNil)
err = client.Tag(context.Background(), img)
img, err := client.Tag(context.Background(), imgID, "10.200.10.1:5000/admin/app-myapp:v23-builder")
c.Assert(err, check.IsNil)
dockerImage, err := client.api.InspectImage("10.200.10.1:5000/admin/app-myapp:v23-builder")
c.Assert(err, check.IsNil)
Expand All @@ -156,9 +151,9 @@ func (s *S) TestClientPush(c *check.C) {
c.Assert(err, check.IsNil)
contID := s.addTestContainer(c, "my-cont", "my-img")

img, err := client.Commit(context.Background(), contID, "10.200.10.1:5000/admin/app-myapp:v23-builder")
imgID, err := client.Commit(context.Background(), contID, "10.200.10.1:5000/admin/app-myapp:v23-builder")
c.Assert(err, check.IsNil)
err = client.Tag(context.Background(), img)
img, err := client.Tag(context.Background(), imgID, "10.200.10.1:5000/admin/app-myapp:v23-builder")
c.Assert(err, check.IsNil)
err = client.Push(context.Background(), AuthConfig{}, img)
c.Assert(err, check.IsNil)
Expand Down
8 changes: 4 additions & 4 deletions internal/docker/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func NewSidecar(client *Client, user string) (*Sidecar, error) {
return &sidecar, nil
}

func (s *Sidecar) CommitPrimaryContainer(ctx context.Context, image string) (Image, error) {
img, err := s.client.Commit(ctx, s.primaryContainer.ID, image)
func (s *Sidecar) CommitPrimaryContainer(ctx context.Context, image string) (string, error) {
id, err := s.client.Commit(ctx, s.primaryContainer.ID, image)
if err != nil {
return Image{}, fmt.Errorf("error commiting image %v: %v", image, err)
return "", fmt.Errorf("error commiting image %v: %v", image, err)
}
return img, nil
return id, nil
}

// UploadToPrimaryContainer uploads a file to the primary container
Expand Down
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ import (
const version = "0.2.8"

type Config struct {
DockerHost string `envconfig:"DOCKER_HOST"`
RunAsSidecar bool `split_words:"true"`
DestinationImage string `split_words:"true"`
InputFile string `split_words:"true"`
RegistryPushRetries int `split_words:"true" default:"3"`
RegistryAuthEmail string `split_words:"true"`
RegistryAuthPass string `split_words:"true"`
RegistryAuthUser string `split_words:"true"`
RegistryAddress string `split_words:"true"`
RunAsUser string `split_words:"true"`
DockerHost string `envconfig:"DOCKER_HOST"`
RunAsSidecar bool `split_words:"true"`
DestinationImages []string `split_words:"true"`
InputFile string `split_words:"true"`
RegistryPushRetries int `split_words:"true" default:"3"`
RegistryAuthEmail string `split_words:"true"`
RegistryAuthPass string `split_words:"true"`
RegistryAuthUser string `split_words:"true"`
RegistryAddress string `split_words:"true"`
RunAsUser string `split_words:"true"`
}

func main() {
Expand Down

0 comments on commit ada04e0

Please sign in to comment.