Skip to content

Commit

Permalink
Merge pull request #384 from coveooss/fix/DT-5198-DT-5199-min-docker-…
Browse files Browse the repository at this point in the history
…version

don't set the API_VERSION anymore, ensure minimum version through code
  • Loading branch information
jonapich authored Oct 17, 2022
2 parents 724b286 + 297cf01 commit 6a5aa3d
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.fleet
.pkg
dist
vendor
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Check for new version at https://github.com/coveooss/tgf/releases/latest.
Any docker image could be used, but TGF specialized images could be found at: https://hub.docker.com/r/coveo/tgf/tags.
Terragrunt documentation could be found at https://github.com/coveo/terragrunt/blob/master/README.md (Coveo fork)
Terragrunt documentation could be found at https://github.com/coveo/terragrunt/blob/master/README.md (Coveo fork).
Terraform documentation could be found at https://www.terraform.io/docs/index.html.
Expand Down
40 changes: 32 additions & 8 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ import (
)

const (
minimumDockerVersion = "1.25"
tgfImageVersion = "TGF_IMAGE_VERSION"
dockerSocketFile = "/var/run/docker.sock"
dockerfilePattern = "TGF_dockerfile"
maxDockerTagLength = 128
dockerMountImagePath = "/var/tgf"
dockerVolumeName = "tgf"
allowedDockerVersions = ">=1.32.0"
tgfImageVersion = "TGF_IMAGE_VERSION"
dockerSocketFile = "/var/run/docker.sock"
dockerfilePattern = "TGF_dockerfile"
maxDockerTagLength = 128
dockerMountImagePath = "/var/tgf"
dockerVolumeName = "tgf"
)

type dockerConfig struct{ *TGFConfig }
Expand Down Expand Up @@ -415,12 +415,36 @@ func (docker *dockerConfig) GetActualImageVersion() string {
return getActualImageVersionInternal(docker.getImage())
}

func validateDockerVersion(version string) (bool, error) {
v, err := semver.ParseTolerant(version)
if err != nil {
return false, fmt.Errorf("Cannot parse %s: %w", version, err)
}

expectedRange, err := semver.ParseRange(allowedDockerVersions)
if err != nil {
// this is a developer error, allowedDockerVersions is a const
log.Panicf("Unable to parse range: %s: %v", allowedDockerVersions, err)
}

return expectedRange(v), nil
}

func getDockerClient() (*client.Client, context.Context) {
if dockerClient == nil {
os.Setenv("DOCKER_API_VERSION", minimumDockerVersion)
dockerClient = must(client.NewClientWithOpts(client.FromEnv)).(*client.Client)
dockerContext = context.Background()
}

currentVersion := dockerClient.ClientVersion()
valid, err := validateDockerVersion(currentVersion)

if err != nil {
log.Errorf("Skipping Docker API validation because of an error: %v", err)
} else if !valid {
log.Fatalf("Docker API reported version %s which is too old, required version range is %s", currentVersion, allowedDockerVersions)
}

return dockerClient, dockerContext
}

Expand Down
67 changes: 67 additions & 0 deletions docker_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDockerVersion(t *testing.T) {
dockerClient, _ := getDockerClient()

tests := []struct {
name string
version string
valid bool
}{
{
name: "Current docker version",
version: dockerClient.ClientVersion(),
valid: true, // this unit test depends on the environment
},
{
name: "Minimum version",
version: "1.32",
valid: true,
},
{
name: "They patched the minimum version",
version: "1.32.1",
valid: true,
},
{
name: "More recent version",
version: "1.35",
valid: true,
},
{
name: "New major",
version: "2.0",
valid: true,
},

{
name: "Too old",
version: "1.31.0",
valid: false,
},
}

assert := assert.New(t)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
valid, err := validateDockerVersion(tt.version)
assert.NoError(err)
assert.Equal(valid, tt.valid)
})
}
}

func TestGetDockerClient(t *testing.T) {
dockerClient, _ := getDockerClient()
valid, err := validateDockerVersion(dockerClient.ClientVersion())

assert.NoError(t, err)
assert.True(t, valid)
}

0 comments on commit 6a5aa3d

Please sign in to comment.