Skip to content

Commit e51af33

Browse files
committed
fix: do ensureNamespace only if the namespace doesn't really exist.
Signed-off-by: Luca Di Maio <[email protected]>
1 parent 28b3872 commit e51af33

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
@@ -36,7 +36,10 @@ type LocalRegistry struct {
3636
servicePort *corev1.ServicePort
3737
}
3838

39-
func GetOrCreateLocalRegistry(ctx devspacecontext.Context, options Options) (*LocalRegistry, error) {
39+
func GetOrCreateLocalRegistry(
40+
ctx devspacecontext.Context,
41+
options Options,
42+
) (*LocalRegistry, error) {
4043
localRegistriesLock.Lock()
4144
defer localRegistriesLock.Unlock()
4245

@@ -164,6 +167,32 @@ func (r *LocalRegistry) RewriteImageForBuilder(image string) (string, error) {
164167
}
165168

166169
func (r *LocalRegistry) ensureNamespace(ctx devspacecontext.Context) error {
170+
// If localregistry namespace is the same as devspace, we don't have
171+
// anything to do.
172+
if r.Namespace == ctx.KubeClient().Namespace() {
173+
return nil
174+
}
175+
176+
// List all namespaces, this will already return an error in case of
177+
// user's permissions problems.
178+
namespaces, err := ctx.KubeClient().
179+
KubeClient().
180+
CoreV1().
181+
Namespaces().
182+
List(ctx.Context(), metav1.ListOptions{})
183+
if err != nil {
184+
return err
185+
}
186+
187+
// Check if the namespace already is there, if not we'll try to create it.
188+
for _, namespace := range namespaces.Items {
189+
if r.Namespace == namespace.Name {
190+
ctx.Log().Debugf("Namespace %s already exists, skipping creation", r.Namespace)
191+
return nil
192+
}
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)