From 529361cb92cacf9bfe27566f71e5294c21973da0 Mon Sep 17 00:00:00 2001 From: Manuel Alejandro de Brito Fontes Date: Mon, 11 Oct 2021 09:11:01 -0300 Subject: [PATCH] Check for existing image builds --- components/ws-manager/pkg/manager/manager.go | 33 ++++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/components/ws-manager/pkg/manager/manager.go b/components/ws-manager/pkg/manager/manager.go index 53d2d0e5b5c58d..90f32a6d4e4694 100644 --- a/components/ws-manager/pkg/manager/manager.go +++ b/components/ws-manager/pkg/manager/manager.go @@ -153,13 +153,34 @@ func (m *Manager) StartWorkspace(ctx context.Context, req *api.StartWorkspaceReq defer tracing.FinishSpan(span, &err) // Make sure the objects we're about to create do not exist already - exists, err := m.workspaceExists(ctx, req.Id) - if err != nil { - return nil, xerrors.Errorf("cannot start workspace: %w", err) - } - if exists { - return nil, status.Error(codes.AlreadyExists, "workspace instance already exists") + switch req.Type { + case api.WorkspaceType_IMAGEBUILD: + wss, err := m.GetWorkspaces(ctx, &api.GetWorkspacesRequest{ + MustMatch: &api.MetadataFilter{ + Annotations: req.Metadata.Annotations, + }, + }) + if err != nil { + return nil, xerrors.Errorf("cannot start workspace: %w", err) + } + + if len(wss.Status) >= 1 { + status := wss.Status[0] + return &api.StartWorkspaceResponse{ + Url: status.Spec.Url, + OwnerToken: status.Metadata.Owner, + }, nil + } + default: + exists, err := m.workspaceExists(ctx, req.Id) + if err != nil { + return nil, xerrors.Errorf("cannot start workspace: %w", err) + } + if exists { + return nil, status.Error(codes.AlreadyExists, "workspace instance already exists") + } } + span.LogKV("event", "workspace does not exist") err = validateStartWorkspaceRequest(req) if err != nil {