Skip to content

Commit ef04e9f

Browse files
authored
Merge pull request #3518 from ntnn/fix-cli-panic
Fix `create-workspace` on an existing workspace throwing a panic
2 parents 77d614c + 3b2c534 commit ef04e9f

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
@@ -177,7 +177,7 @@ func (o *CreateWorkspaceOptions) Run(ctx context.Context) error {
177177

178178
workspaceReference := fmt.Sprintf("Workspace %q (type %s)", o.Name, logicalcluster.NewPath(ws.Spec.Type.Path).Join(string(ws.Spec.Type.Name)).String())
179179
if preExisting {
180-
if ws.Spec.Type.Name != "" && ws.Spec.Type.Name != structuredWorkspaceType.Name || ws.Spec.Type.Path != structuredWorkspaceType.Path {
180+
if structuredWorkspaceType != nil && (ws.Spec.Type.Name != "" && ws.Spec.Type.Name != structuredWorkspaceType.Name || ws.Spec.Type.Path != structuredWorkspaceType.Path) {
181181
wsTypeString := logicalcluster.NewPath(ws.Spec.Type.Path).Join(string(ws.Spec.Type.Name)).String()
182182
structuredWorkspaceTypeString := logicalcluster.NewPath(structuredWorkspaceType.Path).Join(string(structuredWorkspaceType.Name)).String()
183183
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
@@ -48,6 +48,7 @@ func TestCreate(t *testing.T) {
4848
config clientcmdapi.Config
4949

5050
existingWorkspaces []string // existing workspaces
51+
skipInitialType bool
5152
markReady bool
5253

5354
newWorkspaceName string
@@ -109,6 +110,18 @@ func TestCreate(t *testing.T) {
109110
AuthInfos: map[string]*clientcmdapi.AuthInfo{"test": {Token: "test"}},
110111
},
111112
existingWorkspaces: []string{"bar"},
113+
skipInitialType: true,
114+
newWorkspaceName: "bar",
115+
ignoreExisting: true,
116+
},
117+
{
118+
name: "create, already existing, use after creation",
119+
config: clientcmdapi.Config{CurrentContext: "test",
120+
Contexts: map[string]*clientcmdapi.Context{"test": {Cluster: "test", AuthInfo: "test"}},
121+
Clusters: map[string]*clientcmdapi.Cluster{"test": {Server: "https://test/clusters/root:foo"}},
122+
AuthInfos: map[string]*clientcmdapi.AuthInfo{"test": {Token: "test"}},
123+
},
124+
existingWorkspaces: []string{"bar"},
112125
newWorkspaceName: "bar",
113126
useAfterCreation: true,
114127
wantErr: true,
@@ -257,7 +270,9 @@ func TestCreate(t *testing.T) {
257270

258271
opts := NewCreateWorkspaceOptions(genericclioptions.NewTestIOStreamsDiscard())
259272
opts.Name = tt.newWorkspaceName
260-
opts.Type = workspaceType.Path + ":" + string(workspaceType.Name)
273+
if !tt.skipInitialType {
274+
opts.Type = workspaceType.Path + ":" + string(workspaceType.Name)
275+
}
261276
opts.IgnoreExisting = tt.ignoreExisting
262277
opts.EnterAfterCreate = tt.useAfterCreation
263278
opts.ReadyWaitTimeout = time.Second

0 commit comments

Comments
 (0)