This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
/
docker.go
76 lines (65 loc) · 1.62 KB
/
docker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package e2e
import (
"context"
"fmt"
"io"
"log"
"os"
"path"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
"github.com/google/uuid"
"sigs.k8s.io/e2e-framework/pkg/envconf"
)
type dockerImageContext struct{}
var dockerImageContextKey = dockerImageContext{}
const (
envvarExtraDockerImages = envvarPrefix + "DOCKER_IMAGES"
)
func BuildDockerImage(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
log.Print("Building docker image")
tag := fmt.Sprintf("consul-api-gateway:%s", uuid.New().String())
dockerClient, err := client.NewClientWithOpts(
client.FromEnv,
)
if err != nil {
return nil, err
}
tar, err := archive.TarWithOptions(path.Join("..", "..", ".."), &archive.TarOptions{})
if err != nil {
return nil, err
}
r, err := dockerClient.ImageBuild(ctx, tar, types.ImageBuildOptions{
Dockerfile: "Dockerfile.local",
Tags: []string{tag},
Remove: true,
})
if err != nil {
return nil, err
}
defer r.Body.Close()
b, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
log.Print(string(b))
return context.WithValue(ctx, dockerImageContextKey, tag), nil
}
func DockerImage(ctx context.Context) string {
image := ctx.Value(dockerImageContextKey)
if image == nil {
panic("must run this with an integration test that has called BuildDockerImage")
}
return image.(string)
}
func ExtraDockerImages() []string {
images := os.Getenv(envvarExtraDockerImages)
if images != "" {
return strings.Split(images, ",")
}
return nil
}