Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions pkg/devspace/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/loft-sh/devspace/pkg/devspace/build/types"
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
"github.com/loft-sh/devspace/pkg/util/stringutil"

"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
Expand Down Expand Up @@ -89,12 +88,8 @@ func (c *controller) Build(ctx devspacecontext.Context, images []string, options

// Determine if we need to use the local registry to build any images.
kubeClient := ctx.KubeClient()
isKindContext := kubeClient != nil && kubectl.GetKindContext(kubeClient.CurrentContext()) != ""
useKindLoad := !registry.IsLocalRegistryEnabled(conf) && isKindContext
var localRegistry *registry.LocalRegistry
if !options.SkipPush &&
!useKindLoad &&
(registry.IsLocalRegistryEnabled(conf) || registry.IsLocalRegistryFallback(conf)) {
if registry.UseLocalRegistry(kubeClient, conf, options.SkipPush) {
ctx := ctx.WithLogger(ctx.Log().WithPrefix("local-registry: "))
for key, imageConf := range conf.Images {
imageName := imageConf.Image
Expand All @@ -106,10 +101,7 @@ func (c *controller) Build(ctx devspacecontext.Context, images []string, options

// Determine whether the local registry is required / enabled
isLocalReqistryRequired := !registry.HasPushPermission(imageConf)
useMinikubeDocker := registry.UseMinikubeDocker(ctx, imageConf)
if useMinikubeDocker {
ctx.Log().Warnf("Using Minikube for image %s, skipping local registry", imageConf.Image)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can bring back this edge case if desired, but it was somewhat unexpected to have localRegistry.enabled = true and it still not be used because of minikube.

This did require using a docker client configured for the minikube environment to work, see later comments.

} else if isLocalReqistryRequired {
if isLocalReqistryRequired {
// Not able to deploy a local registry
if kubeClient == nil {
return fmt.Errorf("unable to push image %s and a valid kube context is not available", imageConf.Image)
Expand Down
14 changes: 10 additions & 4 deletions pkg/devspace/build/builder/buildkit/buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ func (b *Builder) BuildImage(ctx devspacecontext.Context, contextPath, dockerfil

// We skip pushing when using a local registry
imageCache, _ := ctx.Config().LocalCache().GetImageCache(b.helper.ImageConfigName)
if !usingLocalKubernetes && imageCache.IsLocalRegistryImage() {
b.skipPush = true
if usingLocalKubernetes && imageCache.IsLocalRegistryImage() {
b.skipPush = false
}

// Should we use the minikube docker daemon?
useMinikubeDocker := false
if ctx.KubeClient() != nil && ctx.KubeClient().CurrentContext() == "minikube" && (buildKitConfig.PreferMinikube == nil || *buildKitConfig.PreferMinikube) {
if ctx.KubeClient() != nil && kubectl.IsMinikubeKubernetes(ctx.KubeClient().CurrentContext()) && (buildKitConfig.PreferMinikube == nil || *buildKitConfig.PreferMinikube) {
useMinikubeDocker = true
}

Expand Down Expand Up @@ -235,7 +235,13 @@ func buildWithCLI(ctx context.Context, dir string, environ expand.Environ, conte
// Push image to local registry
for _, tag := range options.Tags {
log.Info("The push refers to repository [" + tag + "]")
err := registry.CopyImageToRemote(ctx, tag, writer)
preferMinikube := imageConf.PreferMinikube == nil || *imageConf.PreferMinikube
client, err := dockerpkg.NewClientWithMinikube(ctx, kubeClient.CurrentContext(), preferMinikube, log)
if err != nil {
return errors.Wrap(err, "new docker client")
}

err = registry.CopyImageToRemote(ctx, client, tag, writer)
if err != nil {
return errors.Errorf("error during local registry image push: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/devspace/build/builder/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (b *Builder) BuildImage(ctx devspacecontext.Context, contextPath, dockerfil
// Push image to local registry
for _, tag := range buildOptions.Tags {
ctx.Log().Info("The push refers to repository [" + tag + "]")
err := registry.CopyImageToRemote(ctx.Context(), tag, writer)
err := registry.CopyImageToRemote(ctx.Context(), b.client, tag, writer)
if err != nil {
return errors.Errorf("error during local registry image push: %v", err)
}
Expand Down
35 changes: 23 additions & 12 deletions pkg/devspace/build/registry/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"strings"

"github.com/docker/docker/pkg/jsonmessage"
"github.com/google/go-containerregistry/pkg/authn"
Expand All @@ -14,7 +15,8 @@ import (
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
dockerclient "github.com/loft-sh/devspace/pkg/devspace/docker"
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
corev1 "k8s.io/api/core/v1"
)

Expand All @@ -29,7 +31,7 @@ func HasPushPermission(image *latest.Image) bool {
}

func IsLocalRegistryFallback(config *latest.Config) bool {
return config.LocalRegistry == nil || (config.LocalRegistry != nil || config.LocalRegistry.Enabled == nil)
return config.LocalRegistry == nil || (config.LocalRegistry != nil && config.LocalRegistry.Enabled == nil)
}

func IsLocalRegistryEnabled(config *latest.Config) bool {
Expand Down Expand Up @@ -67,13 +69,13 @@ func IsImageAvailableRemotely(ctx context.Context, imageName string) (bool, erro
return image != nil, nil
}

func CopyImageToRemote(ctx context.Context, imageName string, writer io.Writer) error {
func CopyImageToRemote(ctx context.Context, client dockerclient.Client, imageName string, writer io.Writer) error {
ref, err := name.ParseReference(imageName)
if err != nil {
return err
}

image, err := daemon.Image(ref, daemon.WithContext(ctx))
image, err := daemon.Image(ref, daemon.WithContext(ctx), daemon.WithClient(client.DockerAPIClient()))
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This update allows the local registry push to work with either the local docker daemon, or the Minikube docker daemon.

if err != nil {
return err
}
Expand Down Expand Up @@ -118,17 +120,26 @@ func CopyImageToRemote(ctx context.Context, imageName string, writer io.Writer)
return <-errChan
}

func UseMinikubeDocker(ctx devspacecontext.Context, image *latest.Image) bool {
// preferMinikube := false
var preferMinikube *bool
func UseLocalRegistry(client kubectl.Client, config *latest.Config, skipPush bool) bool {
if skipPush {
return false
}

if image.Docker != nil {
preferMinikube = image.Docker.PreferMinikube
if client == nil {
return false
}

if image.BuildKit != nil {
preferMinikube = image.BuildKit.PreferMinikube
if !IsLocalRegistryFallback(config) {
return IsLocalRegistryEnabled(config)
}

return ctx.KubeClient() != nil && ctx.KubeClient().CurrentContext() == "minikube" && (preferMinikube == nil || *preferMinikube)
context := client.CurrentContext()

// Determine if this is a vcluster
isVClusterContext := strings.HasPrefix(context, "vcluster_")

// Determine if this is a local kubernetes cluster
isLocalKubernetes := kubectl.IsLocalKubernetes(context)

return !isLocalKubernetes && !(isVClusterContext && isLocalKubernetes)
}
Loading