Skip to content

Commit

Permalink
Restore the custom naming based on config location (#44)
Browse files Browse the repository at this point in the history
The initial goal was to easily identify custom images instead of just having an anonymous hash to identify them. We then added a hash to ensure that we rebuild the image if the custom DockerFile or the custom build folder content changed and finally, we removed the name, thus  loosing the identification of the custom image and returning to an anonymous image as we did originally.

It is still important to not rebuild the custom image if there is no change and rebuild it if there is a change. So, I simply added the hash into a label and instead putting the hash into the name, we simply check if the label has changed to determine if the image should be rebuilt.
  • Loading branch information
jocgir authored Jan 15, 2019
1 parent eccc433 commit 75f77cf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/md5"
"errors"
"fmt"
"github.com/hashicorp/go-getter"
"io"
"io/ioutil"
"os"
Expand All @@ -21,6 +20,7 @@ import (
"github.com/blang/semver"
"github.com/coveo/gotemplate/collections"
"github.com/gruntwork-io/terragrunt/aws_helper"
"github.com/hashicorp/go-getter"
yaml "gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -107,7 +107,7 @@ func (cb TGFConfigBuild) GetTag() string {
if cb.Tag != "" {
return cb.Tag
}
return cb.hash()
return filepath.Base(filepath.Dir(cb.source))
}

// InitConfig returns a properly initialized TGF configuration struct
Expand Down
12 changes: 1 addition & 11 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package main

import (
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
Expand Down Expand Up @@ -110,7 +108,7 @@ func TestSetConfigDefaultValues(t *testing.T) {
assert.Equal(t, "RUN ls test", config.imageBuildConfigs[1].Instructions)
assert.Equal(t, "/abspath/my-folder", config.imageBuildConfigs[1].Folder)
assert.Equal(t, "/abspath/my-folder", config.imageBuildConfigs[1].Dir())
assert.Equal(t, getHash([]string{"AWS", config.imageBuildConfigs[1].Instructions}), config.imageBuildConfigs[1].GetTag())
assert.Equal(t, "AWS", config.imageBuildConfigs[1].GetTag())

assert.Equal(t, "coveo/stuff", config.Image)
assert.Equal(t, "test", *config.ImageTag)
Expand Down Expand Up @@ -193,11 +191,3 @@ func randInt() int {
random := rand.New(source)
return random.Int()
}

func getHash(values []string) string {
h := md5.New()
for _, value := range values {
io.WriteString(h, value)
}
return fmt.Sprintf("%x", h.Sum(nil))
}
34 changes: 26 additions & 8 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"context"
"encoding/base64"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -19,6 +16,9 @@ import (
"strings"
"syscall"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/blang/semver"
"github.com/coveo/gotemplate/utils"
"github.com/docker/docker/api/types"
Expand All @@ -33,6 +33,7 @@ const (
tgfImageVersion = "TGF_IMAGE_VERSION"
dockerSocketFile = "/var/run/docker.sock"
dockerfilePattern = "TGF_dockerfile"
maxDockerTagLength = 128
)

func callDocker(withDockerMount bool, args ...string) int {
Expand Down Expand Up @@ -242,8 +243,12 @@ func getImage() (name string) {
}

name = name + "-" + ib.GetTag()
if refresh || getActualImageVersionInternal(name) == "" {
args := []string{"build", ".", "-f", dockerfilePattern, "--quiet", "--force-rm"}
if image, tag := Split2(name, ":"); len(tag) > maxDockerTagLength {
name = image + ":" + tag[0:maxDockerTagLength]
}
if refresh || getImageHash(name) != ib.hash() {
label := fmt.Sprintf("hash=%s", ib.hash())
args := []string{"build", ".", "-f", dockerfilePattern, "--quiet", "--force-rm", "--label", label}
if i == 0 && refresh && !useLocalImage {
args = append(args, "--pull")
}
Expand Down Expand Up @@ -341,17 +346,30 @@ func getDockerClient() (*client.Client, context.Context) {
var dockerClient *client.Client
var dockerContext context.Context

func getActualImageVersionInternal(imageName string) string {
func getImageSummary(imageName string) *types.ImageSummary {
cli, ctx := getDockerClient()
// Find image
filters := filters.NewArgs()
filters.Add("reference", imageName)
images, err := cli.ImageList(ctx, types.ImageListOptions{Filters: filters})
if err != nil || len(images) != 1 {
return ""
return nil
}
return &images[0]
}

func getActualImageVersionInternal(imageName string) string {
if image := getImageSummary(imageName); image != nil {
return getActualImageVersionFromImageID(image.ID)
}
return ""
}

return getActualImageVersionFromImageID(images[0].ID)
func getImageHash(imageName string) string {
if image := getImageSummary(imageName); image != nil {
return image.Labels["hash"]
}
return ""
}

func getActualImageVersionFromImageID(imageID string) string {
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

// Version is initialized at build time through -ldflags "-X main.Version=<version number>"
var version = "1.18.1"
var version = "1.18.5"

var description = `
DESCRIPTION:
Expand Down Expand Up @@ -83,6 +83,7 @@ var (
ErrPrintf = utils.ColorErrorPrintf
ErrPrintln = utils.ColorErrorPrintln
ErrPrint = utils.ColorErrorPrint
Split2 = collections.Split2
)

// Environment variables
Expand Down

0 comments on commit 75f77cf

Please sign in to comment.