Skip to content

Commit 3b2c534

Browse files
committed
Fix create-workspace on an existing workspace throwing a panic
Signed-off-by: Nelo-T. Wallus <[email protected]>
1 parent 67ae63a commit 3b2c534

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

cli/pkg/workspace/plugin/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (o *CreateWorkspaceOptions) Run(ctx context.Context) error {
176176

177177
workspaceReference := fmt.Sprintf("Workspace %q (type %s)", o.Name, logicalcluster.NewPath(ws.Spec.Type.Path).Join(string(ws.Spec.Type.Name)).String())
178178
if preExisting {
179-
if ws.Spec.Type.Name != "" && ws.Spec.Type.Name != structuredWorkspaceType.Name || ws.Spec.Type.Path != structuredWorkspaceType.Path {
179+
if structuredWorkspaceType != nil && (ws.Spec.Type.Name != "" && ws.Spec.Type.Name != structuredWorkspaceType.Name || ws.Spec.Type.Path != structuredWorkspaceType.Path) {
180180
wsTypeString := logicalcluster.NewPath(ws.Spec.Type.Path).Join(string(ws.Spec.Type.Name)).String()
181181
structuredWorkspaceTypeString := logicalcluster.NewPath(structuredWorkspaceType.Path).Join(string(structuredWorkspaceType.Name)).String()
182182
return fmt.Errorf("workspace %q cannot be created with type %s, it already exists with different type %s", o.Name, structuredWorkspaceTypeString, wsTypeString)

cli/pkg/workspace/plugin/create_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func TestCreate(t *testing.T) {
4747
config clientcmdapi.Config
4848

4949
existingWorkspaces []string // existing workspaces
50+
skipInitialType bool
5051
markReady bool
5152

5253
newWorkspaceName string
@@ -107,6 +108,18 @@ func TestCreate(t *testing.T) {
107108
AuthInfos: map[string]*clientcmdapi.AuthInfo{"test": {Token: "test"}},
108109
},
109110
existingWorkspaces: []string{"bar"},
111+
skipInitialType: true,
112+
newWorkspaceName: "bar",
113+
ignoreExisting: true,
114+
},
115+
{
116+
name: "create, already existing, use after creation",
117+
config: clientcmdapi.Config{CurrentContext: "test",
118+
Contexts: map[string]*clientcmdapi.Context{"test": {Cluster: "test", AuthInfo: "test"}},
119+
Clusters: map[string]*clientcmdapi.Cluster{"test": {Server: "https://test/clusters/root:foo"}},
120+
AuthInfos: map[string]*clientcmdapi.AuthInfo{"test": {Token: "test"}},
121+
},
122+
existingWorkspaces: []string{"bar"},
110123
newWorkspaceName: "bar",
111124
useAfterCreation: true,
112125
wantErr: true,
@@ -234,7 +247,9 @@ func TestCreate(t *testing.T) {
234247

235248
opts := NewCreateWorkspaceOptions(genericclioptions.NewTestIOStreamsDiscard())
236249
opts.Name = tt.newWorkspaceName
237-
opts.Type = workspaceType.Path + ":" + string(workspaceType.Name)
250+
if !tt.skipInitialType {
251+
opts.Type = workspaceType.Path + ":" + string(workspaceType.Name)
252+
}
238253
opts.IgnoreExisting = tt.ignoreExisting
239254
opts.EnterAfterCreate = tt.useAfterCreation
240255
opts.ReadyWaitTimeout = time.Second

0 commit comments

Comments
 (0)