Skip to content

Commit ac6bb3a

Browse files
committed
Introduce LocalDaemon type
Signed-off-by: David Gageot <[email protected]>
1 parent 2043aaf commit ac6bb3a

16 files changed

+238
-159
lines changed

pkg/skaffold/build/local/bazel.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"path/filepath"
2626
"strings"
2727

28-
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
2928
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
3029
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
3130
"github.com/pkg/errors"
@@ -56,18 +55,14 @@ func (b *Builder) buildBazel(ctx context.Context, out io.Writer, workspace strin
5655
}
5756
defer imageTar.Close()
5857

59-
resp, err := b.api.ImageLoad(ctx, imageTar, false)
60-
if err != nil {
61-
return "", errors.Wrap(err, "loading image into docker daemon")
62-
}
63-
defer resp.Body.Close()
58+
ref := buildImageTag(a.BuildTarget)
6459

65-
err = docker.StreamDockerMessages(out, resp.Body, nil)
60+
imageID, err := b.localDocker.Load(ctx, out, imageTar, ref)
6661
if err != nil {
67-
return "", errors.Wrap(err, "reading from image load response")
62+
return "", errors.Wrap(err, "loading image into docker daemon")
6863
}
6964

70-
return docker.ImageID(ctx, b.api, buildImageTag(a.BuildTarget))
65+
return imageID, nil
7166
}
7267

7368
func bazelBin(ctx context.Context, workspace string) (string, error) {

pkg/skaffold/build/local/docker.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func (b *Builder) buildDocker(ctx context.Context, out io.Writer, workspace stri
5151
return "", errors.Wrap(err, "running build")
5252
}
5353

54-
return docker.ImageID(ctx, b.api, initialTag)
54+
return b.localDocker.ImageID(ctx, initialTag)
5555
}
5656

57-
return docker.Build(ctx, out, b.api, workspace, a, initialTag)
57+
return b.localDocker.Build(ctx, out, workspace, a, initialTag)
5858
}

pkg/skaffold/build/local/jib_gradle.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (b *Builder) buildJibGradleToDocker(ctx context.Context, out io.Writer, wor
4444
return "", err
4545
}
4646

47-
return docker.ImageID(ctx, b.api, skaffoldImage)
47+
return b.localDocker.ImageID(ctx, skaffoldImage)
4848
}
4949

5050
func (b *Builder) buildJibGradleToRegistry(ctx context.Context, out io.Writer, workspace string, artifact *latest.Artifact) (string, error) {

pkg/skaffold/build/local/jib_maven.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (b *Builder) buildJibMavenToDocker(ctx context.Context, out io.Writer, work
5252
return "", err
5353
}
5454

55-
return docker.ImageID(ctx, b.api, skaffoldImage)
55+
return b.localDocker.ImageID(ctx, skaffoldImage)
5656
}
5757

5858
func (b *Builder) buildJibMavenToRegistry(ctx context.Context, out io.Writer, workspace string, artifact *latest.Artifact) (string, error) {

pkg/skaffold/build/local/local.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (b *Builder) Build(ctx context.Context, out io.Writer, tagger tag.Tagger, a
3535
if b.localCluster {
3636
color.Default.Fprintf(out, "Found [%s] context, using local docker daemon.\n", b.kubeContext)
3737
}
38-
defer b.api.Close()
38+
defer b.localDocker.Close()
3939

4040
// TODO(dgageot): parallel builds
4141
return build.InSequence(ctx, out, tagger, artifacts, b.buildArtifact)
@@ -98,12 +98,12 @@ func (b *Builder) retagAndPush(ctx context.Context, out io.Writer, initialTag st
9898
return nil
9999
}
100100

101-
if err := b.api.ImageTag(ctx, initialTag, newTag); err != nil {
101+
if err := b.localDocker.Tag(ctx, initialTag, newTag); err != nil {
102102
return err
103103
}
104104

105105
if b.pushImages {
106-
if _, err := docker.RunPush(ctx, out, b.api, newTag); err != nil {
106+
if _, err := b.localDocker.Push(ctx, out, newTag); err != nil {
107107
return errors.Wrap(err, "pushing")
108108
}
109109
}

pkg/skaffold/build/local/local_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func TestLocalRun(t *testing.T) {
147147
t.Run(test.description, func(t *testing.T) {
148148
l := Builder{
149149
cfg: &latest.LocalBuild{},
150-
api: &test.api,
150+
localDocker: docker.NewLocalDaemon(&test.api),
151151
localCluster: test.localCluster,
152152
}
153153

pkg/skaffold/build/local/types.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
type Builder struct {
3232
cfg *latest.LocalBuild
3333

34-
api docker.APIClient
34+
localDocker docker.LocalDaemon
3535
localCluster bool
3636
pushImages bool
3737
kubeContext string
@@ -41,7 +41,7 @@ type Builder struct {
4141

4242
// NewBuilder returns an new instance of a local Builder.
4343
func NewBuilder(cfg *latest.LocalBuild, kubeContext string) (*Builder, error) {
44-
api, err := docker.NewAPIClient()
44+
localDocker, err := docker.NewAPIClient()
4545
if err != nil {
4646
return nil, errors.Wrap(err, "getting docker client")
4747
}
@@ -58,7 +58,7 @@ func NewBuilder(cfg *latest.LocalBuild, kubeContext string) (*Builder, error) {
5858
return &Builder{
5959
cfg: cfg,
6060
kubeContext: kubeContext,
61-
api: api,
61+
localDocker: localDocker,
6262
localCluster: localCluster,
6363
pushImages: pushImages,
6464
}, nil
@@ -70,7 +70,7 @@ func (b *Builder) Labels() map[string]string {
7070
constants.Labels.Builder: "local",
7171
}
7272

73-
v, err := b.api.ServerVersion(context.Background())
73+
v, err := b.localDocker.ServerVersion(context.Background())
7474
if err == nil {
7575
labels[constants.Labels.DockerAPIVersion] = fmt.Sprintf("%v", v.APIVersion)
7676
}

pkg/skaffold/docker/auth.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (credsHelper) GetAllAuthConfigs() (map[string]types.AuthConfig, error) {
8282
return cf.GetCredentialsStore("").GetAll()
8383
}
8484

85-
func encodedRegistryAuth(ctx context.Context, cli APIClient, a AuthConfigHelper, image string) (string, error) {
85+
func (l *localDaemon) encodedRegistryAuth(ctx context.Context, a AuthConfigHelper, image string) (string, error) {
8686
ref, err := reference.ParseNormalizedNamed(image)
8787
if err != nil {
8888
return "", errors.Wrap(err, "parsing image name for registry")
@@ -96,7 +96,7 @@ func encodedRegistryAuth(ctx context.Context, cli APIClient, a AuthConfigHelper,
9696
index := repoInfo.Index
9797
configKey := index.Name
9898
if index.Official {
99-
configKey = officialRegistry(ctx, cli)
99+
configKey = l.officialRegistry(ctx)
100100
}
101101

102102
ac, err := a.GetAuthConfig(configKey)
@@ -112,11 +112,11 @@ func encodedRegistryAuth(ctx context.Context, cli APIClient, a AuthConfigHelper,
112112
return base64.URLEncoding.EncodeToString(buf), nil
113113
}
114114

115-
func officialRegistry(ctx context.Context, cli APIClient) string {
115+
func (l *localDaemon) officialRegistry(ctx context.Context) string {
116116
serverAddress := registry.IndexServer
117117

118118
// The daemon `/info` endpoint informs us of the default registry being used.
119-
info, err := cli.Info(ctx)
119+
info, err := l.apiClient.Info(ctx)
120120
switch {
121121
case err != nil:
122122
logrus.Warnf("failed to get default registry endpoint from daemon (%v). Using system default: %s\n", err, serverAddress)

pkg/skaffold/docker/auth_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ func TestGetEncodedRegistryAuth(t *testing.T) {
7878
defer func(h AuthConfigHelper) { DefaultAuthHelper = h }(DefaultAuthHelper)
7979
DefaultAuthHelper = test.authType
8080

81-
out, err := encodedRegistryAuth(context.Background(), nil, test.authType, test.image)
81+
l := &localDaemon{}
82+
out, err := l.encodedRegistryAuth(context.Background(), test.authType, test.image)
83+
8284
testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expected, out)
8385
})
8486
}

pkg/skaffold/docker/client.go

+9-12
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,42 @@ import (
2828
"strings"
2929
"sync"
3030

31-
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
32-
3331
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
3432
kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
3533
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
34+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
3635
"github.com/docker/docker/api"
3736
"github.com/docker/docker/client"
3837
"github.com/docker/go-connections/tlsconfig"
3938
"github.com/pkg/errors"
4039
"github.com/sirupsen/logrus"
4140
)
4241

43-
type APIClient interface {
44-
client.CommonAPIClient
45-
}
46-
4742
var (
4843
dockerAPIClientOnce sync.Once
49-
dockerAPIClient APIClient
44+
dockerAPIClient LocalDaemon
5045
dockerAPIClientErr error
5146
)
5247

5348
// NewAPIClient guesses the docker client to use based on current kubernetes context.
54-
func NewAPIClient() (APIClient, error) {
49+
func NewAPIClient() (LocalDaemon, error) {
5550
dockerAPIClientOnce.Do(func() {
5651
kubeContext, err := kubectx.CurrentContext()
5752
if err != nil {
5853
dockerAPIClientErr = errors.Wrap(err, "getting current cluster context")
5954
return
6055
}
6156

62-
dockerAPIClient, dockerAPIClientErr = newAPIClient(kubeContext)
57+
apiClient, err := newAPIClient(kubeContext)
58+
dockerAPIClient = NewLocalDaemon(apiClient)
59+
dockerAPIClientErr = err
6360
})
6461

6562
return dockerAPIClient, dockerAPIClientErr
6663
}
6764

6865
// newAPIClient guesses the docker client to use based on current kubernetes context.
69-
func newAPIClient(kubeContext string) (APIClient, error) {
66+
func newAPIClient(kubeContext string) (client.CommonAPIClient, error) {
7067
if kubeContext == constants.DefaultMinikubeContext {
7168
return newMinikubeAPIClient()
7269
}
@@ -76,7 +73,7 @@ func newAPIClient(kubeContext string) (APIClient, error) {
7673
// newEnvAPIClient returns a docker client based on the environment variables set.
7774
// It will "negotiate" the highest possible API version supported by both the client
7875
// and the server if there is a mismatch.
79-
func newEnvAPIClient() (APIClient, error) {
76+
func newEnvAPIClient() (client.CommonAPIClient, error) {
8077
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithHTTPHeaders(getUserAgentHeader()))
8178
if err != nil {
8279
return nil, fmt.Errorf("error getting docker client: %s", err)
@@ -88,7 +85,7 @@ func newEnvAPIClient() (APIClient, error) {
8885

8986
// newMinikubeAPIClient returns a docker client using the environment variables
9087
// provided by minikube.
91-
func newMinikubeAPIClient() (APIClient, error) {
88+
func newMinikubeAPIClient() (client.CommonAPIClient, error) {
9289
env, err := getMinikubeDockerEnv()
9390
if err != nil {
9491
logrus.Warnf("Could not get minikube docker env, falling back to local docker daemon: %s", err)

pkg/skaffold/docker/client_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
2323
"github.com/GoogleContainerTools/skaffold/testutil"
24+
"github.com/docker/docker/client"
2425
)
2526

2627
func TestNewEnvClient(t *testing.T) {
@@ -61,7 +62,7 @@ func TestNewMinikubeImageAPIClient(t *testing.T) {
6162
description string
6263
cmd util.Command
6364

64-
expected APIClient
65+
expected client.CommonAPIClient
6566
shouldErr bool
6667
}{
6768
{

0 commit comments

Comments
 (0)