feat(src): Enable buildkit when client has support. (#387)

* feat(src): Enable buildkit when client has support.

* fix: Increase timeout of testsuite to 12min

* fix: docker_image datasource tests use smaller busybox image.

* fix: Use image version which are available on arm64.

* fix: Remove session dialer from buildkit implementation.

Did not do really anything

* feat: Buildkit is now working.

* fix: Disable linting for one specific line.

* fix: Fixes TestAccDockerImage_basic test.

The used image names in the two subtests must be different.
This commit is contained in:
Martin 2022-06-23 10:32:31 +02:00 committed by GitHub
parent 37b40325d9
commit dc8757e5f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 47 additions and 17 deletions

View file

@ -16,7 +16,7 @@ env:
DEBIAN_FRONTEND: noninteractive
DOCKER_CE_VERSION: "5:20.10.10~3-0~ubuntu-focal"
GO_VERSION: "1.17"
TESTSUITE_TIMEOUT: "480s"
TESTSUITE_TIMEOUT: "720s"
jobs:
acc-test:

4
go.mod
View file

@ -27,6 +27,7 @@ require (
github.com/containerd/cgroups v0.0.0-20200824123100-0b889c03f102 // indirect
github.com/containerd/containerd v1.5.0-beta.1 // indirect
github.com/containerd/continuity v0.0.0-20210208174643-50096c924a4e // indirect
github.com/containerd/typeurl v1.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/docker-credential-helpers v0.6.3 // indirect
github.com/fatih/color v1.13.0 // indirect
@ -36,6 +37,8 @@ require (
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.2.0 // indirect
@ -76,6 +79,7 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v1.0.0-rc93 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/russross/blackfriday v1.6.0 // indirect

4
go.sum
View file

@ -287,6 +287,7 @@ github.com/containerd/ttrpc v1.0.1/go.mod h1:UAxOpgT9ziI0gJrmKvgcZivgxOp8iFPSk8h
github.com/containerd/ttrpc v1.0.2/go.mod h1:UAxOpgT9ziI0gJrmKvgcZivgxOp8iFPSk8httJEt98Y=
github.com/containerd/typeurl v0.0.0-20180627222232-a93fcdb778cd/go.mod h1:Cm3kwCdlkCfMSHURc+r6fwoGH6/F1hH3S4sg0rLFWPc=
github.com/containerd/typeurl v0.0.0-20190911142611-5eb25027c9fd/go.mod h1:GeKYzf2pQcqv7tJ0AoCuuhtnqhva5LNU3U+OyKxxJpk=
github.com/containerd/typeurl v1.0.1 h1:PvuK4E3D5S5q6IqsPDCy928FhP0LUIGcmZ/Yhgp5Djw=
github.com/containerd/typeurl v1.0.1/go.mod h1:TB1hUtrpaiO88KEK56ijojHS1+NeF0izUACaJW2mdXg=
github.com/containerd/zfs v0.0.0-20200918131355-0a33824f23a2/go.mod h1:8IgZOBdv8fAgXddBT4dBXJPtxyRsejFIpXoklgxgEjw=
github.com/containernetworking/cni v0.7.1/go.mod h1:LGwApLUm2FpoOfxTDEeq8T9ipbpZ61X79hmU3w8FmsY=
@ -602,6 +603,7 @@ github.com/gotestyourself/gotestyourself v2.2.0+incompatible/go.mod h1:zZKM6oeNM
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0 h1:0IKlLyQ3Hs9nDaiK5cSHAGmcQEIC8l2Ts1u6x5Dfrqg=
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0/go.mod h1:mJzapYve32yjrKlk9GbyCZHuPgZsrbyIbyKhSzOpg6s=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
@ -611,6 +613,7 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/grpc-ecosystem/grpc-gateway v1.9.2/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 h1:MJG/KsmcqMwFAkh8mTnAwhyKoB+sTAnY4CACC110tbU=
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645/go.mod h1:6iZfnjpejD4L/4DwD7NryNaJyCQdzwWwH2MWhCA90Kw=
github.com/hanwen/go-fuse v1.0.0/go.mod h1:unqXarDXqzAk0rt98O2tVndEPIpUgLD9+rwFisZH3Ok=
github.com/hanwen/go-fuse/v2 v2.0.3/go.mod h1:0EQM6aH2ctVpvZ6a+onrQ/vaykxh2GH7hy3e13vzTUY=
@ -932,6 +935,7 @@ github.com/opencontainers/selinux v1.6.0/go.mod h1:VVGKuOLlE7v4PJyT6h7mNWvq1rzqi
github.com/opencontainers/selinux v1.8.0/go.mod h1:RScLhm78qiWa2gbVCcGkC7tCGdgk3ogry1nUQF8Evvo=
github.com/opentracing-contrib/go-stdlib v1.0.0/go.mod h1:qtI1ogk+2JhVPIXVc6q+NHziSmy2W5GbdQZFUHADCBU=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
github.com/openzipkin/zipkin-go v0.1.3/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=

View file

@ -19,7 +19,7 @@ var imageNameWithTagAndDigestRegexp = regexp.MustCompile(`^.*:.*@sha256:[A-Fa-f0
func TestAccDockerImageDataSource_withSpecificTag(t *testing.T) {
ctx := context.Background()
imageName := "nginx:1.17.6"
imageName := "busybox:1.35.0"
resource.Test(t, resource.TestCase{
PreCheck: func() {
@ -32,7 +32,7 @@ func TestAccDockerImageDataSource_withSpecificTag(t *testing.T) {
Config: loadTestConfiguration(t, DATA_SOURCE, "docker_image", "testAccDockerImageDataSourceWithSpecificTag"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.docker_image.foo", "name", imageName),
resource.TestCheckResourceAttr("data.docker_image.foo", "repo_digest", "nginx@sha256:b2d89d0a210398b4d1120b3e3a7672c16a4ba09c2c4a0395f18b9f7999b768f2"),
resource.TestCheckResourceAttr("data.docker_image.foo", "repo_digest", "busybox@sha256:8c40df61d40166f5791f44b3d90b77b4c7f59ed39a992fd9046886d3126ffa68"),
),
},
},
@ -44,7 +44,7 @@ func TestAccDockerImageDataSource_withSpecificTag(t *testing.T) {
func TestAccDockerImageDataSource_withDefaultTag(t *testing.T) {
ctx := context.Background()
imageName := "nginx"
imageName := "busybox"
resource.Test(t, resource.TestCase{
PreCheck: func() {
@ -69,7 +69,7 @@ func TestAccDockerImageDataSource_withDefaultTag(t *testing.T) {
func TestAccDockerImageDataSource_withSha256Digest(t *testing.T) {
ctx := context.Background()
imageName := "nginx@sha256:36b74457bccb56fbf8b05f79c85569501b721d4db813b684391d63e02287c0b2"
imageName := "busybox@sha256:8c40df61d40166f5791f44b3d90b77b4c7f59ed39a992fd9046886d3126ffa68"
resource.Test(t, resource.TestCase{
PreCheck: func() {
@ -93,7 +93,7 @@ func TestAccDockerImageDataSource_withSha256Digest(t *testing.T) {
}
func TestAccDockerImageDataSource_withTagAndSha256Digest(t *testing.T) {
ctx := context.Background()
imageName := "nginx:1.19.1@sha256:36b74457bccb56fbf8b05f79c85569501b721d4db813b684391d63e02287c0b2"
imageName := "busybox:1.35.0@sha256:8c40df61d40166f5791f44b3d90b77b4c7f59ed39a992fd9046886d3126ffa68"
resource.Test(t, resource.TestCase{
PreCheck: func() {

View file

@ -8,19 +8,24 @@ import (
"fmt"
"io"
"log"
"net"
"path/filepath"
"strings"
"github.com/docker/cli/cli/command/image/build"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mitchellh/go-homedir"
"github.com/moby/buildkit/session"
)
const minBuildkitDockerVersion = "1.39"
func resourceDockerImageCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*ProviderConfig).DockerClient
imageName := d.Get("name").(string)
@ -290,7 +295,6 @@ func findImage(ctx context.Context, imageName string, client *client.Client, aut
func buildDockerImage(ctx context.Context, rawBuild map[string]interface{}, imageName string, client *client.Client) error {
buildOptions := types.ImageBuildOptions{}
buildOptions.Version = types.BuilderV1
buildOptions.Dockerfile = rawBuild["dockerfile"].(string)
tags := []string{imageName}
@ -319,6 +323,24 @@ func buildDockerImage(ctx context.Context, rawBuild map[string]interface{}, imag
buildOptions.Labels = labels
log.Printf("[DEBUG] Labels: %v\n", labels)
dockerClientVersion := client.ClientVersion()
log.Printf("[DEBUG] DockerClientVersion: %v, minBuildKitDockerVersion: %v\n", dockerClientVersion, minBuildkitDockerVersion)
if versions.GreaterThanOrEqualTo(dockerClientVersion, minBuildkitDockerVersion) {
// docker client supports BuildKit
log.Printf("[DEBUG] Enabling BuildKit")
s, _ := session.NewSession(ctx, "docker-provider", "")
dialSession := func(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error) {
return client.DialHijack(ctx, "/session", proto, meta)
}
//nolint
go s.Run(ctx, dialSession)
defer s.Close()
buildOptions.SessionID = s.ID()
buildOptions.Version = types.BuilderBuildKit
} else {
buildOptions.Version = types.BuilderV1
}
contextDir := rawBuild["path"].(string)
excludes, err := build.ReadDockerignore(contextDir)
if err != nil {

View file

@ -22,7 +22,7 @@ func TestAccDockerImage_basic(t *testing.T) {
// run a Docker container which refers the Docker image to test "force_remove" option
containerName := "test-docker-image-force-remove"
ctx := context.Background()
if err := exec.Command("docker", "run", "--rm", "-d", "--name", containerName, "alpine:3.11.5", "tail", "-f", "/dev/null").Run(); err != nil {
if err := exec.Command("docker", "run", "--rm", "-d", "--name", containerName, "alpine:3.16.0", "tail", "-f", "/dev/null").Run(); err != nil {
t.Fatal(err)
}
defer func() {

View file

@ -1,3 +1,3 @@
data "docker_image" "foo" {
name = "nginx"
name = "busybox"
}

View file

@ -1,3 +1,3 @@
data "docker_image" "foo" {
name = "nginx@sha256:36b74457bccb56fbf8b05f79c85569501b721d4db813b684391d63e02287c0b2"
name = "busybox@sha256:8c40df61d40166f5791f44b3d90b77b4c7f59ed39a992fd9046886d3126ffa68"
}

View file

@ -1,3 +1,3 @@
data "docker_image" "foo" {
name = "nginx:1.17.6"
name = "busybox:1.35.0"
}

View file

@ -1,3 +1,3 @@
data "docker_image" "foo" {
name = "nginx:1.19.1@sha256:36b74457bccb56fbf8b05f79c85569501b721d4db813b684391d63e02287c0b2"
name = "busybox:1.35.0@sha256:8c40df61d40166f5791f44b3d90b77b4c7f59ed39a992fd9046886d3126ffa68"
}

View file

@ -1,3 +1,3 @@
resource "docker_image" "foo" {
name = "alpine:3.1"
name = "alpine:3.15.0"
}

View file

@ -1,5 +1,5 @@
data "docker_registry_image" "foobarbaz" {
name = "alpine:3.1"
name = "alpine:3.16.0"
}
resource "docker_image" "foobarbaz" {
name = data.docker_registry_image.foobarbaz.name

View file

@ -1,5 +1,5 @@
data "docker_registry_image" "foobarbazoo" {
name = "alpine:3.1"
name = "alpine:3.16.0"
}
resource "docker_image" "foobarbazoo" {
name = data.docker_registry_image.foobarbazoo.name

View file

@ -1,4 +1,4 @@
resource "docker_image" "foobarzoo" {
name = "crux:3.1"
name = "busybox:latest"
keep_locally = true
}

View file

@ -1,4 +1,4 @@
resource "docker_image" "test" {
name = "alpine:3.14.2"
name = "alpine:3.16.0"
force_remove = true
}