Skip to content

Commit b1f5e08

Browse files
authored
Merge pull request #2390 from lizardruss/local-registry-vclusters
Skip local registry use for vclusters on local kubernetes clusters
2 parents 602c322 + 2e908a1 commit b1f5e08

7 files changed

Lines changed: 329 additions & 37 deletions

File tree

pkg/devspace/build/build.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/loft-sh/devspace/pkg/devspace/build/types"
1212
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
1313
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
14-
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
1514
"github.com/loft-sh/devspace/pkg/util/stringutil"
1615

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

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

107102
// Determine whether the local registry is required / enabled
108103
isLocalReqistryRequired := !registry.HasPushPermission(imageConf)
109-
useMinikubeDocker := registry.UseMinikubeDocker(ctx, imageConf)
110-
if useMinikubeDocker {
111-
ctx.Log().Warnf("Using Minikube for image %s, skipping local registry", imageConf.Image)
112-
} else if isLocalReqistryRequired {
104+
if isLocalReqistryRequired {
113105
// Not able to deploy a local registry
114106
if kubeClient == nil {
115107
return fmt.Errorf("unable to push image %s and a valid kube context is not available", imageConf.Image)

pkg/devspace/build/builder/buildkit/buildkit.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ func (b *Builder) BuildImage(ctx devspacecontext.Context, contextPath, dockerfil
139139

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

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

@@ -235,7 +235,13 @@ func buildWithCLI(ctx context.Context, dir string, environ expand.Environ, conte
235235
// Push image to local registry
236236
for _, tag := range options.Tags {
237237
log.Info("The push refers to repository [" + tag + "]")
238-
err := registry.CopyImageToRemote(ctx, tag, writer)
238+
preferMinikube := imageConf.PreferMinikube == nil || *imageConf.PreferMinikube
239+
client, err := dockerpkg.NewClientWithMinikube(ctx, kubeClient.CurrentContext(), preferMinikube, log)
240+
if err != nil {
241+
return errors.Wrap(err, "new docker client")
242+
}
243+
244+
err = registry.CopyImageToRemote(ctx, client, tag, writer)
239245
if err != nil {
240246
return errors.Errorf("error during local registry image push: %v", err)
241247
}

pkg/devspace/build/builder/docker/docker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (b *Builder) BuildImage(ctx devspacecontext.Context, contextPath, dockerfil
212212
// Push image to local registry
213213
for _, tag := range buildOptions.Tags {
214214
ctx.Log().Info("The push refers to repository [" + tag + "]")
215-
err := registry.CopyImageToRemote(ctx.Context(), tag, writer)
215+
err := registry.CopyImageToRemote(ctx.Context(), b.client, tag, writer)
216216
if err != nil {
217217
return errors.Errorf("error during local registry image push: %v", err)
218218
}

pkg/devspace/build/registry/util.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"strings"
89

910
"github.com/docker/docker/pkg/jsonmessage"
1011
"github.com/google/go-containerregistry/pkg/authn"
@@ -14,7 +15,8 @@ import (
1415
"github.com/google/go-containerregistry/pkg/v1/remote"
1516
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
1617
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
17-
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
18+
dockerclient "github.com/loft-sh/devspace/pkg/devspace/docker"
19+
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
1820
corev1 "k8s.io/api/core/v1"
1921
)
2022

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

3133
func IsLocalRegistryFallback(config *latest.Config) bool {
32-
return config.LocalRegistry == nil || (config.LocalRegistry != nil || config.LocalRegistry.Enabled == nil)
34+
return config.LocalRegistry == nil || (config.LocalRegistry != nil && config.LocalRegistry.Enabled == nil)
3335
}
3436

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

70-
func CopyImageToRemote(ctx context.Context, imageName string, writer io.Writer) error {
72+
func CopyImageToRemote(ctx context.Context, client dockerclient.Client, imageName string, writer io.Writer) error {
7173
ref, err := name.ParseReference(imageName)
7274
if err != nil {
7375
return err
7476
}
7577

76-
image, err := daemon.Image(ref, daemon.WithContext(ctx))
78+
image, err := daemon.Image(ref, daemon.WithContext(ctx), daemon.WithClient(client.DockerAPIClient()))
7779
if err != nil {
7880
return err
7981
}
@@ -118,17 +120,26 @@ func CopyImageToRemote(ctx context.Context, imageName string, writer io.Writer)
118120
return <-errChan
119121
}
120122

121-
func UseMinikubeDocker(ctx devspacecontext.Context, image *latest.Image) bool {
122-
// preferMinikube := false
123-
var preferMinikube *bool
123+
func UseLocalRegistry(client kubectl.Client, config *latest.Config, skipPush bool) bool {
124+
if skipPush {
125+
return false
126+
}
124127

125-
if image.Docker != nil {
126-
preferMinikube = image.Docker.PreferMinikube
128+
if client == nil {
129+
return false
127130
}
128131

129-
if image.BuildKit != nil {
130-
preferMinikube = image.BuildKit.PreferMinikube
132+
if !IsLocalRegistryFallback(config) {
133+
return IsLocalRegistryEnabled(config)
131134
}
132135

133-
return ctx.KubeClient() != nil && ctx.KubeClient().CurrentContext() == "minikube" && (preferMinikube == nil || *preferMinikube)
136+
context := client.CurrentContext()
137+
138+
// Determine if this is a vcluster
139+
isVClusterContext := strings.HasPrefix(context, "vcluster_")
140+
141+
// Determine if this is a local kubernetes cluster
142+
isLocalKubernetes := kubectl.IsLocalKubernetes(context)
143+
144+
return !isLocalKubernetes && !(isVClusterContext && isLocalKubernetes)
134145
}

0 commit comments

Comments
 (0)