Skip to content

Commit 9e899fe

Browse files
committed
Fix create-workspace --create-context pointing at the old workspace
Signed-off-by: Nelo-T. Wallus <[email protected]> Signed-off-by: Nelo-T. Wallus <[email protected]>
1 parent e1c04ac commit 9e899fe

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

cli/pkg/workspace/plugin/context.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ type CreateContextOptions struct {
4242
// KeepCurrent indicates whether to keep the current context. When creating a new context, if KeepCurrent is true, the current context will be preserved.
4343
KeepCurrent bool
4444

45+
// ClusterURL is the URL of the cluster to use for the new context.
46+
// If empty, the current context's cluster will be used.
47+
ClusterURL string
48+
4549
startingConfig *clientcmdapi.Config
4650

4751
// for testing
@@ -122,6 +126,9 @@ func (o *CreateContextOptions) Run(ctx context.Context) error {
122126

123127
newKubeConfig := o.startingConfig.DeepCopy()
124128
newCluster := *currentCluster
129+
if o.ClusterURL != "" {
130+
newCluster.Server = o.ClusterURL
131+
}
125132
newKubeConfig.Clusters[o.Name] = &newCluster
126133
newContext := *currentContext
127134
newContext.Cluster = o.Name

cli/pkg/workspace/plugin/create.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (o *CreateWorkspaceOptions) BindFlags(cmd *cobra.Command) {
112112
o.Options.BindFlags(cmd)
113113
cmd.Flags().StringVar(&o.Type, "type", o.Type, "A workspace type. The default type depends on where this child workspace is created.")
114114
cmd.Flags().BoolVar(&o.EnterAfterCreate, "enter", o.EnterAfterCreate, "Immediately enter the created workspace")
115-
cmd.Flags().BoolVar(&o.IgnoreExisting, "ignore-existing", o.IgnoreExisting, "Ignore if the workspace already exists. Requires none or absolute type path.")
115+
cmd.Flags().BoolVar(&o.IgnoreExisting, "ignore-existing", o.IgnoreExisting, "Ignore if the workspace already exists. Requires none or absolute type path. Overwrites the context if --create-context is set.")
116116
cmd.Flags().StringVar(&o.LocationSelector, "location-selector", o.LocationSelector, "A label selector to select the scheduling location of the created workspace.")
117117
cmd.Flags().StringVar(&o.CreateContextName, "create-context", o.CreateContextName, "Create a kubeconfig context for the new workspace with the given name.")
118118
}
@@ -239,7 +239,9 @@ func (o *CreateWorkspaceOptions) Run(ctx context.Context) error {
239239
if o.CreateContextName != "" {
240240
createContextOptions := NewCreateContextOptions(o.IOStreams)
241241
createContextOptions.Name = o.CreateContextName
242+
createContextOptions.ClusterURL = ws.Spec.URL
242243
createContextOptions.ClientConfig = o.ClientConfig
244+
createContextOptions.Overwrite = o.IgnoreExisting
243245

244246
// If --enter is set, switch to the new context
245247
createContextOptions.KeepCurrent = !o.EnterAfterCreate

cli/pkg/workspace/plugin/create_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,12 @@ func TestCreate(t *testing.T) {
210210
},
211211
Clusters: map[string]*clientcmdapi.Cluster{
212212
"test": {Server: "https://test/clusters/root:foo"},
213-
"bar": {Server: "https://test/clusters/root:foo"},
213+
"bar": {Server: "https://test/clusters/root:foo:bar"},
214214
},
215215
AuthInfos: map[string]*clientcmdapi.AuthInfo{"test": {Token: "test"}},
216216
},
217217
existingWorkspaces: []string{"test"},
218+
newWorkspaceName: "bar",
218219
createContextName: "bar",
219220
useAfterCreation: false,
220221
markReady: true,
@@ -239,11 +240,12 @@ func TestCreate(t *testing.T) {
239240
},
240241
Clusters: map[string]*clientcmdapi.Cluster{
241242
"test": {Server: "https://test/clusters/root:foo"},
242-
"bar": {Server: "https://test/clusters/root:foo"},
243+
"bar": {Server: "https://test/clusters/root:foo:bar"},
243244
},
244245
AuthInfos: map[string]*clientcmdapi.AuthInfo{"test": {Token: "test"}},
245246
},
246247
existingWorkspaces: []string{"test"},
248+
newWorkspaceName: "bar",
247249
createContextName: "bar",
248250
useAfterCreation: true,
249251
markReady: true,

0 commit comments

Comments
 (0)