Skip to content

Commit 0a4bb60

Browse files
authored
Merge pull request #2604 from 89luca89/main
fix: do ensureNamespace only if the namespace doesn't really exist.
2 parents f7d1568 + 32cfa90 commit 0a4bb60

File tree

1 file changed

+61
-14
lines changed

1 file changed

+61
-14
lines changed

pkg/devspace/build/localregistry/local_registry.go

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"k8s.io/apimachinery/pkg/util/wait"
1717
applyv1 "k8s.io/client-go/applyconfigurations/core/v1"
1818

19+
kerrors "k8s.io/apimachinery/pkg/api/errors"
20+
1921
"github.com/google/go-containerregistry/pkg/v1/remote"
2022
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
2123
"github.com/mgutz/ansi"
@@ -36,7 +38,10 @@ type LocalRegistry struct {
3638
servicePort *corev1.ServicePort
3739
}
3840

39-
func GetOrCreateLocalRegistry(ctx devspacecontext.Context, options Options) (*LocalRegistry, error) {
41+
func GetOrCreateLocalRegistry(
42+
ctx devspacecontext.Context,
43+
options Options,
44+
) (*LocalRegistry, error) {
4045
localRegistriesLock.Lock()
4146
defer localRegistriesLock.Unlock()
4247

@@ -164,6 +169,30 @@ func (r *LocalRegistry) RewriteImageForBuilder(image string) (string, error) {
164169
}
165170

166171
func (r *LocalRegistry) ensureNamespace(ctx devspacecontext.Context) error {
172+
// If localregistry namespace is the same as devspace, we don't have
173+
// anything to do.
174+
if r.Namespace == ctx.KubeClient().Namespace() {
175+
ctx.Log().Debugf("Namespace %s is the default Devspace namespace", r.Namespace)
176+
return nil
177+
}
178+
179+
// Try to get the namespace we need
180+
_, err := ctx.KubeClient().
181+
KubeClient().
182+
CoreV1().
183+
Namespaces().
184+
Get(ctx.Context(), r.Namespace, metav1.GetOptions{})
185+
// Ignore not found errors, but if we have any other type or error, report it
186+
if err != nil && !kerrors.IsNotFound(err) {
187+
return err
188+
}
189+
// And if we don't have errors, it means the namespace already exists.
190+
if err == nil {
191+
ctx.Log().Debugf("Namespace %s already exists, skipping creation", r.Namespace)
192+
return nil
193+
}
194+
195+
ctx.Log().Debugf("Namespace %s doesn't exist, attempting creation", r.Namespace)
167196
applyConfiguration, err := applyv1.ExtractNamespace(&corev1.Namespace{
168197
ObjectMeta: metav1.ObjectMeta{
169198
Name: r.Namespace,
@@ -198,15 +227,22 @@ func (r *LocalRegistry) waitForNodePort(ctx devspacecontext.Context) (*corev1.Se
198227
var servicePort *corev1.ServicePort
199228

200229
kubeClient := ctx.KubeClient().KubeClient()
201-
err := wait.PollImmediateWithContext(ctx.Context(), time.Second, 30*time.Second, func(ctx context.Context) (done bool, err error) {
202-
service, err := kubeClient.CoreV1().Services(r.Namespace).Get(ctx, r.Name, metav1.GetOptions{})
203-
if err != nil {
204-
return false, err
205-
}
230+
err := wait.PollImmediateWithContext(
231+
ctx.Context(),
232+
time.Second,
233+
30*time.Second,
234+
func(ctx context.Context) (done bool, err error) {
235+
service, err := kubeClient.CoreV1().
236+
Services(r.Namespace).
237+
Get(ctx, r.Name, metav1.GetOptions{})
238+
if err != nil {
239+
return false, err
240+
}
206241

207-
servicePort = GetServicePort(service)
208-
return servicePort.NodePort != 0, nil
209-
})
242+
servicePort = GetServicePort(service)
243+
return servicePort.NodePort != 0, nil
244+
},
245+
)
210246

211247
return servicePort, err
212248
}
@@ -218,12 +254,18 @@ func (r *LocalRegistry) GetRegistryURL() string {
218254

219255
// startPortForwarding will forward container's port into localhost in order to access registry's container in
220256
// the cluster, locally, to push the built image afterwards
221-
func (r *LocalRegistry) startPortForwarding(ctx devspacecontext.Context, imageRegistryPod *corev1.Pod) error {
257+
func (r *LocalRegistry) startPortForwarding(
258+
ctx devspacecontext.Context,
259+
imageRegistryPod *corev1.Pod,
260+
) error {
222261
localPort := r.servicePort.NodePort
223262
remotePort := r.servicePort.TargetPort.IntVal
224263
ports := []string{fmt.Sprintf("%d:%d", localPort, remotePort)}
225264
addresses := []string{"localhost"}
226-
portsFormatted := ansi.Color(fmt.Sprintf("%d -> %d", int(localPort), int(remotePort)), "white+b")
265+
portsFormatted := ansi.Color(
266+
fmt.Sprintf("%d -> %d", int(localPort), int(remotePort)),
267+
"white+b",
268+
)
227269
readyChan := make(chan struct{})
228270
errorChan := make(chan error, 1)
229271
pf, err := kubectl.NewPortForwarder(
@@ -266,9 +308,14 @@ func (r *LocalRegistry) startPortForwarding(ctx devspacecontext.Context, imageRe
266308
}
267309

268310
func (r *LocalRegistry) waitForRegistry(ctx context.Context) error {
269-
return wait.PollImmediateWithContext(ctx, time.Second, 30*time.Second, func(ctx context.Context) (done bool, err error) {
270-
return r.ping(ctx)
271-
})
311+
return wait.PollImmediateWithContext(
312+
ctx,
313+
time.Second,
314+
30*time.Second,
315+
func(ctx context.Context) (done bool, err error) {
316+
return r.ping(ctx)
317+
},
318+
)
272319
}
273320

274321
func (r *LocalRegistry) ping(ctx context.Context) (done bool, err error) {

0 commit comments

Comments
 (0)