Skip to content

Commit dc91159

Browse files
authored
Merge pull request #3561 from ntnn/fix-create-ws
Fix create-workspace --create-context pointing at the old workspace
2 parents 9d893bb + 547d392 commit dc91159

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

cli/pkg/workspace/plugin/context.go

Lines changed: 14 additions & 6 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
@@ -137,14 +144,15 @@ func (o *CreateContextOptions) Run(ctx context.Context) error {
137144
return err
138145
}
139146

147+
verb := "Created"
140148
if existedBefore {
141-
if o.startingConfig.CurrentContext == o.Name {
142-
_, err = fmt.Fprintf(o.Out, "Updated context %q.\n", o.Name)
143-
} else {
144-
_, err = fmt.Fprintf(o.Out, "Updated context %q and switched to it.\n", o.Name)
145-
}
149+
verb = "Updated"
150+
}
151+
152+
if o.KeepCurrent || o.startingConfig.CurrentContext == o.Name {
153+
_, err = fmt.Fprintf(o.Out, "%s context %q.\n", verb, o.Name)
146154
} else {
147-
_, err = fmt.Fprintf(o.Out, "Created context %q and switched to it.\n", o.Name)
155+
_, err = fmt.Fprintf(o.Out, "%s context %q and switched to it.\n", verb, o.Name)
148156
}
149157

150158
return err

cli/pkg/workspace/plugin/create.go

Lines changed: 3 additions & 8 deletions
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
@@ -266,13 +268,6 @@ func (o *CreateWorkspaceOptions) Run(ctx context.Context) error {
266268
return fmt.Errorf("failed to create context: %w", err)
267269
}
268270

269-
// Print output
270-
if o.EnterAfterCreate {
271-
fmt.Fprintf(o.Out, "Created context %q and switched to it.\n", o.CreateContextName)
272-
} else {
273-
fmt.Fprintf(o.Out, "Created context %q.\n", o.CreateContextName)
274-
}
275-
276271
return nil
277272
}
278273

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)