Skip to content

Commit

Permalink
Introduce LocalDaemon type
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Dec 28, 2018
1 parent 2043aaf commit ac6bb3a
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 159 deletions.
13 changes: 4 additions & 9 deletions pkg/skaffold/build/local/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"path/filepath"
"strings"

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

resp, err := b.api.ImageLoad(ctx, imageTar, false)
if err != nil {
return "", errors.Wrap(err, "loading image into docker daemon")
}
defer resp.Body.Close()
ref := buildImageTag(a.BuildTarget)

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

return docker.ImageID(ctx, b.api, buildImageTag(a.BuildTarget))
return imageID, nil
}

func bazelBin(ctx context.Context, workspace string) (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/build/local/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func (b *Builder) buildDocker(ctx context.Context, out io.Writer, workspace stri
return "", errors.Wrap(err, "running build")
}

return docker.ImageID(ctx, b.api, initialTag)
return b.localDocker.ImageID(ctx, initialTag)
}

return docker.Build(ctx, out, b.api, workspace, a, initialTag)
return b.localDocker.Build(ctx, out, workspace, a, initialTag)
}
2 changes: 1 addition & 1 deletion pkg/skaffold/build/local/jib_gradle.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (b *Builder) buildJibGradleToDocker(ctx context.Context, out io.Writer, wor
return "", err
}

return docker.ImageID(ctx, b.api, skaffoldImage)
return b.localDocker.ImageID(ctx, skaffoldImage)
}

func (b *Builder) buildJibGradleToRegistry(ctx context.Context, out io.Writer, workspace string, artifact *latest.Artifact) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/local/jib_maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (b *Builder) buildJibMavenToDocker(ctx context.Context, out io.Writer, work
return "", err
}

return docker.ImageID(ctx, b.api, skaffoldImage)
return b.localDocker.ImageID(ctx, skaffoldImage)
}

func (b *Builder) buildJibMavenToRegistry(ctx context.Context, out io.Writer, workspace string, artifact *latest.Artifact) (string, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/skaffold/build/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (b *Builder) Build(ctx context.Context, out io.Writer, tagger tag.Tagger, a
if b.localCluster {
color.Default.Fprintf(out, "Found [%s] context, using local docker daemon.\n", b.kubeContext)
}
defer b.api.Close()
defer b.localDocker.Close()

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

if err := b.api.ImageTag(ctx, initialTag, newTag); err != nil {
if err := b.localDocker.Tag(ctx, initialTag, newTag); err != nil {
return err
}

if b.pushImages {
if _, err := docker.RunPush(ctx, out, b.api, newTag); err != nil {
if _, err := b.localDocker.Push(ctx, out, newTag); err != nil {
return errors.Wrap(err, "pushing")
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestLocalRun(t *testing.T) {
t.Run(test.description, func(t *testing.T) {
l := Builder{
cfg: &latest.LocalBuild{},
api: &test.api,
localDocker: docker.NewLocalDaemon(&test.api),
localCluster: test.localCluster,
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/skaffold/build/local/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
type Builder struct {
cfg *latest.LocalBuild

api docker.APIClient
localDocker docker.LocalDaemon
localCluster bool
pushImages bool
kubeContext string
Expand All @@ -41,7 +41,7 @@ type Builder struct {

// NewBuilder returns an new instance of a local Builder.
func NewBuilder(cfg *latest.LocalBuild, kubeContext string) (*Builder, error) {
api, err := docker.NewAPIClient()
localDocker, err := docker.NewAPIClient()
if err != nil {
return nil, errors.Wrap(err, "getting docker client")
}
Expand All @@ -58,7 +58,7 @@ func NewBuilder(cfg *latest.LocalBuild, kubeContext string) (*Builder, error) {
return &Builder{
cfg: cfg,
kubeContext: kubeContext,
api: api,
localDocker: localDocker,
localCluster: localCluster,
pushImages: pushImages,
}, nil
Expand All @@ -70,7 +70,7 @@ func (b *Builder) Labels() map[string]string {
constants.Labels.Builder: "local",
}

v, err := b.api.ServerVersion(context.Background())
v, err := b.localDocker.ServerVersion(context.Background())
if err == nil {
labels[constants.Labels.DockerAPIVersion] = fmt.Sprintf("%v", v.APIVersion)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/skaffold/docker/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (credsHelper) GetAllAuthConfigs() (map[string]types.AuthConfig, error) {
return cf.GetCredentialsStore("").GetAll()
}

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

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

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

// The daemon `/info` endpoint informs us of the default registry being used.
info, err := cli.Info(ctx)
info, err := l.apiClient.Info(ctx)
switch {
case err != nil:
logrus.Warnf("failed to get default registry endpoint from daemon (%v). Using system default: %s\n", err, serverAddress)
Expand Down
4 changes: 3 additions & 1 deletion pkg/skaffold/docker/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ func TestGetEncodedRegistryAuth(t *testing.T) {
defer func(h AuthConfigHelper) { DefaultAuthHelper = h }(DefaultAuthHelper)
DefaultAuthHelper = test.authType

out, err := encodedRegistryAuth(context.Background(), nil, test.authType, test.image)
l := &localDaemon{}
out, err := l.encodedRegistryAuth(context.Background(), test.authType, test.image)

testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expected, out)
})
}
Expand Down
21 changes: 9 additions & 12 deletions pkg/skaffold/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,42 @@ import (
"strings"
"sync"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
"github.com/docker/docker/api"
"github.com/docker/docker/client"
"github.com/docker/go-connections/tlsconfig"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

type APIClient interface {
client.CommonAPIClient
}

var (
dockerAPIClientOnce sync.Once
dockerAPIClient APIClient
dockerAPIClient LocalDaemon
dockerAPIClientErr error
)

// NewAPIClient guesses the docker client to use based on current kubernetes context.
func NewAPIClient() (APIClient, error) {
func NewAPIClient() (LocalDaemon, error) {
dockerAPIClientOnce.Do(func() {
kubeContext, err := kubectx.CurrentContext()
if err != nil {
dockerAPIClientErr = errors.Wrap(err, "getting current cluster context")
return
}

dockerAPIClient, dockerAPIClientErr = newAPIClient(kubeContext)
apiClient, err := newAPIClient(kubeContext)
dockerAPIClient = NewLocalDaemon(apiClient)
dockerAPIClientErr = err
})

return dockerAPIClient, dockerAPIClientErr
}

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

// newMinikubeAPIClient returns a docker client using the environment variables
// provided by minikube.
func newMinikubeAPIClient() (APIClient, error) {
func newMinikubeAPIClient() (client.CommonAPIClient, error) {
env, err := getMinikubeDockerEnv()
if err != nil {
logrus.Warnf("Could not get minikube docker env, falling back to local docker daemon: %s", err)
Expand Down
3 changes: 2 additions & 1 deletion pkg/skaffold/docker/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/testutil"
"github.com/docker/docker/client"
)

func TestNewEnvClient(t *testing.T) {
Expand Down Expand Up @@ -61,7 +62,7 @@ func TestNewMinikubeImageAPIClient(t *testing.T) {
description string
cmd util.Command

expected APIClient
expected client.CommonAPIClient
shouldErr bool
}{
{
Expand Down
Loading

0 comments on commit ac6bb3a

Please sign in to comment.