Skip to content

Commit 70e35d7

Browse files
committed
refactor: disable local registry in some cases
1 parent ef1a491 commit 70e35d7

3 files changed

Lines changed: 59 additions & 46 deletions

File tree

pkg/devspace/build/build.go

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"strings"
88

99
"github.com/loft-sh/devspace/pkg/devspace/build/builder"
10-
"github.com/loft-sh/devspace/pkg/devspace/build/builder/buildkit"
11-
"github.com/loft-sh/devspace/pkg/devspace/build/builder/docker"
1210
"github.com/loft-sh/devspace/pkg/devspace/build/registry"
1311
"github.com/loft-sh/devspace/pkg/devspace/build/types"
1412
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
@@ -151,38 +149,34 @@ func (c *controller) Build(ctx devspacecontext.Context, images []string, options
151149
imageCache.ImageName = imageName
152150
imageCache.LocalRegistryImageName = ""
153151

154-
if registry.UseLocalRegistry(kubeClient, conf, options.SkipPush) && !registry.HasPushPermission(imageConf) {
155-
if SupportsLocalRegistry(builder) {
156-
// Not able to deploy a local registry without a valid kube context
157-
if kubeClient == nil {
158-
return fmt.Errorf("unable to push image %s and a valid kube context is not available", imageConf.Image)
159-
}
152+
if registry.UseLocalRegistry(kubeClient, conf, imageConf, builder, options.SkipPush) && !registry.HasPushPermission(imageConf) {
153+
// Not able to deploy a local registry without a valid kube context
154+
if kubeClient == nil {
155+
return fmt.Errorf("unable to push image %s and a valid kube context is not available", imageConf.Image)
156+
}
160157

161-
registryOptions := registry.NewDefaultOptions().
162-
WithNamespace(kubeClient.Namespace()).
163-
WithLocalRegistryConfig(conf.LocalRegistry)
158+
registryOptions := registry.NewDefaultOptions().
159+
WithNamespace(kubeClient.Namespace()).
160+
WithLocalRegistryConfig(conf.LocalRegistry)
164161

165-
// Create and start a local registry if one isn't already running
166-
localRegistry, err := registry.GetOrCreateLocalRegistry(ctx, registryOptions)
167-
if err != nil {
168-
return errors.Wrap(err, "get or create local registry")
169-
}
162+
// Create and start a local registry if one isn't already running
163+
localRegistry, err := registry.GetOrCreateLocalRegistry(ctx, registryOptions)
164+
if err != nil {
165+
return errors.Wrap(err, "get or create local registry")
166+
}
170167

171-
// Update cache for local registry use
172-
imageCache.LocalRegistryImageName, err = localRegistry.RewriteImage(imageName)
173-
if err != nil {
174-
return errors.Wrap(err, "rewrite image")
175-
}
176-
ctx.Config().LocalCache().SetImageCache(imageConfigName, imageCache)
168+
// Update cache for local registry use
169+
imageCache.LocalRegistryImageName, err = localRegistry.RewriteImage(imageName)
170+
if err != nil {
171+
return errors.Wrap(err, "rewrite image")
172+
}
173+
ctx.Config().LocalCache().SetImageCache(imageConfigName, imageCache)
177174

178-
// Reset the builder for local registry usage
179-
// TODO: refactor so this isn't necessary!
180-
builder, err = c.createBuilder(ctx, imageConfigName, imageConf, imageTags, options)
181-
if err != nil {
182-
return errors.Wrap(err, "create builder")
183-
}
184-
} else {
185-
ctx.Log().Warnf("unable to push image %s and only docker and buildkit builds support using a local registry", imageConf.Image)
175+
// Reset the builder for local registry usage
176+
// TODO: refactor so this isn't necessary!
177+
builder, err = c.createBuilder(ctx, imageConfigName, imageConf, imageTags, options)
178+
if err != nil {
179+
return errors.Wrap(err, "create builder")
186180
}
187181
}
188182

@@ -430,14 +424,3 @@ func (c *controller) waitForBuild(ctx devspacecontext.Context, errChan <-chan er
430424

431425
return nil
432426
}
433-
434-
func SupportsLocalRegistry(builder builder.Interface) bool {
435-
switch builder.(type) {
436-
case *buildkit.Builder:
437-
return true
438-
case *docker.Builder:
439-
return true
440-
default:
441-
return false
442-
}
443-
}

pkg/devspace/build/registry/util.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package registry
33
import (
44
"context"
55
"fmt"
6+
"github.com/loft-sh/devspace/pkg/devspace/build/builder"
7+
"github.com/loft-sh/devspace/pkg/devspace/build/builder/kaniko"
68
"io"
79
"net/http"
10+
"runtime"
811
"strings"
912

1013
"github.com/docker/docker/pkg/jsonmessage"
@@ -120,22 +123,49 @@ func CopyImageToRemote(ctx context.Context, client dockerclient.Client, imageNam
120123
return <-errChan
121124
}
122125

123-
func UseLocalRegistry(client kubectl.Client, config *latest.Config, skipPush bool) bool {
126+
func UseLocalRegistry(client kubectl.Client, config *latest.Config, imageConfig *latest.Image, imageBuilder builder.Interface, skipPush bool) bool {
124127
if skipPush {
125128
return false
129+
} else if client == nil {
130+
return false
126131
}
127132

128-
if client == nil {
133+
// TODO: better check for arm64 architectures
134+
if runtime.GOARCH != "amd64" {
129135
return false
130136
}
131137

138+
// check if builder is kaniko
139+
if imageBuilder != nil {
140+
_, ok := imageBuilder.(*kaniko.Builder)
141+
if ok {
142+
return false
143+
}
144+
}
145+
146+
// check if image looks weird like localhost / cluster.local
147+
if imageConfig != nil {
148+
if imageConfig.Kaniko != nil {
149+
return false
150+
} else if imageConfig.Custom != nil {
151+
return false
152+
} else if imageConfig.BuildKit != nil && imageConfig.BuildKit.InCluster != nil {
153+
return false
154+
}
155+
156+
imageWithoutPort := strings.Split(imageConfig.Image, ":")[0]
157+
if imageWithoutPort == "" || imageWithoutPort == "localhost" || imageWithoutPort == "127.0.0.1" || strings.HasSuffix(imageWithoutPort, ".cluster.local") {
158+
return false
159+
}
160+
}
161+
162+
// check if fallback
132163
if !IsLocalRegistryFallback(config) {
133164
return IsLocalRegistryEnabled(config)
134165
}
135166

136-
context := client.CurrentContext()
137-
138167
// Determine if this is a vcluster
168+
context := client.CurrentContext()
139169
isVClusterContext := strings.Contains(context, "vcluster_")
140170

141171
// Determine if this is a local kubernetes cluster

pkg/devspace/build/registry/util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func TestUseLocalRegistry(t *testing.T) {
307307
}
308308

309309
for _, testCase := range testCases {
310-
actual := UseLocalRegistry(testCase.client, testCase.config, testCase.skipPush)
310+
actual := UseLocalRegistry(testCase.client, testCase.config, nil, nil, testCase.skipPush)
311311
assert.Equal(t, actual, testCase.expected, "Unexpected result in test case %s", testCase.name)
312312
}
313313
}

0 commit comments

Comments
 (0)