Skip to content

Commit 1ce408d

Browse files
authored
Merge pull request #2494 from lizardruss/main
Local Registry being used with insecure registry
2 parents c457f35 + 93fde49 commit 1ce408d

2 files changed

Lines changed: 85 additions & 15 deletions

File tree

pkg/devspace/build/registry/util.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ 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"
86
"io"
9-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
107
"net/http"
118
"runtime"
129
"strings"
1310

11+
"github.com/loft-sh/devspace/pkg/devspace/build/builder"
12+
"github.com/loft-sh/devspace/pkg/devspace/build/builder/kaniko"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
1415
"github.com/docker/docker/pkg/jsonmessage"
1516
"github.com/google/go-containerregistry/pkg/authn"
1617
"github.com/google/go-containerregistry/pkg/name"
@@ -31,6 +32,17 @@ func HasPushPermission(image *latest.Image) bool {
3132
}
3233

3334
pushErr := remote.CheckPushPermission(ref, authn.DefaultKeychain, http.DefaultTransport)
35+
36+
if isInsecureRegistry(pushErr) {
37+
// Retry with insecure registry
38+
ref, err := name.ParseReference(image.Image, name.Insecure)
39+
if err != nil {
40+
panic(err)
41+
}
42+
43+
pushErr = remote.CheckPushPermission(ref, authn.DefaultKeychain, http.DefaultTransport)
44+
}
45+
3446
return pushErr == nil
3547
}
3648

@@ -132,7 +144,7 @@ func UseLocalRegistry(client kubectl.Client, config *latest.Config, imageConfig
132144
}
133145

134146
// check if node architecture equals our architecture
135-
if runtime.GOARCH != "amd64" {
147+
if runtime.GOARCH != "amd64" && client.KubeClient() != nil {
136148
nodes, err := client.KubeClient().CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
137149
if err != nil {
138150
return false
@@ -160,11 +172,6 @@ func UseLocalRegistry(client kubectl.Client, config *latest.Config, imageConfig
160172
} else if imageConfig.BuildKit != nil && imageConfig.BuildKit.InCluster != nil {
161173
return false
162174
}
163-
164-
imageWithoutPort := strings.Split(imageConfig.Image, ":")[0]
165-
if imageWithoutPort == "" || imageWithoutPort == "localhost" || imageWithoutPort == "127.0.0.1" || strings.HasSuffix(imageWithoutPort, ".local") || strings.HasSuffix(imageWithoutPort, ".localhost") {
166-
return false
167-
}
168175
}
169176

170177
// check if fallback
@@ -180,3 +187,11 @@ func UseLocalRegistry(client kubectl.Client, config *latest.Config, imageConfig
180187
isLocalKubernetes := kubectl.IsLocalKubernetes(context)
181188
return !isLocalKubernetes && !(isVClusterContext && isLocalKubernetes)
182189
}
190+
191+
func isInsecureRegistry(err error) bool {
192+
if err == nil {
193+
return false
194+
}
195+
196+
return strings.Contains(err.Error(), "http: server gave HTTP response to HTTPS client")
197+
}

pkg/devspace/build/registry/util_test.go

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import (
1111
)
1212

1313
type useLocalRegistryTestCase struct {
14-
name string
15-
client kubectl.Client
16-
config *latest.Config
17-
skipPush bool
18-
expected bool
14+
name string
15+
client kubectl.Client
16+
config *latest.Config
17+
imageConfig *latest.Image
18+
skipPush bool
19+
expected bool
1920
}
2021

2122
func TestUseLocalRegistry(t *testing.T) {
@@ -304,10 +305,64 @@ func TestUseLocalRegistry(t *testing.T) {
304305
},
305306
expected: false,
306307
},
308+
{
309+
name: "Remote Cluster BuildKit In Cluster Build Config",
310+
client: &kubectltesting.Client{
311+
Context: "arn:aws:eks:us-west-2:1234567890:cluster/remote-eks",
312+
},
313+
config: &latest.Config{
314+
LocalRegistry: nil,
315+
},
316+
imageConfig: &latest.Image{
317+
BuildKit: &latest.BuildKitConfig{
318+
InCluster: &latest.BuildKitInClusterConfig{},
319+
},
320+
},
321+
expected: false,
322+
},
323+
{
324+
name: "Remote Cluster BuildKit Build Config",
325+
client: &kubectltesting.Client{
326+
Context: "arn:aws:eks:us-west-2:1234567890:cluster/remote-eks",
327+
},
328+
config: &latest.Config{
329+
LocalRegistry: nil,
330+
},
331+
imageConfig: &latest.Image{
332+
BuildKit: &latest.BuildKitConfig{},
333+
},
334+
expected: true,
335+
},
336+
{
337+
name: "Remote Cluster Kaniko Build Config",
338+
client: &kubectltesting.Client{
339+
Context: "arn:aws:eks:us-west-2:1234567890:cluster/remote-eks",
340+
},
341+
config: &latest.Config{
342+
LocalRegistry: nil,
343+
},
344+
imageConfig: &latest.Image{
345+
Kaniko: &latest.KanikoConfig{},
346+
},
347+
expected: false,
348+
},
349+
{
350+
name: "Remote Cluster Custom Build Config",
351+
client: &kubectltesting.Client{
352+
Context: "arn:aws:eks:us-west-2:1234567890:cluster/remote-eks",
353+
},
354+
config: &latest.Config{
355+
LocalRegistry: nil,
356+
},
357+
imageConfig: &latest.Image{
358+
Custom: &latest.CustomConfig{},
359+
},
360+
expected: false,
361+
},
307362
}
308363

309364
for _, testCase := range testCases {
310-
actual := UseLocalRegistry(testCase.client, testCase.config, nil, nil, testCase.skipPush)
365+
actual := UseLocalRegistry(testCase.client, testCase.config, testCase.imageConfig, nil, testCase.skipPush)
311366
assert.Equal(t, actual, testCase.expected, "Unexpected result in test case %s", testCase.name)
312367
}
313368
}

0 commit comments

Comments
 (0)