Skip to content

Commit f84306c

Browse files
committed
refactor: Standardize namespace resolution for kubectl-plugin.
1 parent 79b5c30 commit f84306c

File tree

11 files changed

+15
-51
lines changed

11 files changed

+15
-51
lines changed

kubectl-plugin/pkg/cmd/create/create_cluster.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,10 @@ func (options *CreateClusterOptions) resolveClusterName() (string, error) {
241241

242242
// resolveNamespace resolves the namespace from the CLI flag and the config file
243243
func (options *CreateClusterOptions) resolveNamespace() (string, error) {
244-
namespace := "default"
244+
namespace,_,err := options.cmdFactory.ToRawKubeConfigLoader().Namespace()
245+
if err != nil{
246+
return "", fmt.Errorf("failed to get current namespace: %w", err)
247+
}
245248

246249
if options.rayClusterConfig.Namespace != nil && *options.rayClusterConfig.Namespace != "" && options.namespace != "" && options.namespace != *options.rayClusterConfig.Namespace {
247250
return "", fmt.Errorf("the namespace in the config file %q does not match the namespace %q. You must pass --namespace=%s to perform this operation", *options.rayClusterConfig.Namespace, options.namespace, *options.rayClusterConfig.Namespace)
@@ -268,16 +271,12 @@ func (options *CreateClusterOptions) Run(ctx context.Context, k8sClient client.C
268271
}
269272
options.rayClusterConfig.Name = &name
270273

271-
namespace, err := options.resolveNamespace()
274+
namespace,_,err := options.cmdFactory.ToRawKubeConfigLoader().Namespace()
272275
if err != nil {
273276
return err
274277
}
275278
options.rayClusterConfig.Namespace = &namespace
276279
} else {
277-
if options.namespace == "" {
278-
options.namespace = "default"
279-
}
280-
281280
options.rayClusterConfig = &generation.RayClusterConfig{
282281
Namespace: &options.namespace,
283282
Name: &options.clusterName,

kubectl-plugin/pkg/cmd/create/create_workergroup.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,12 @@ func NewCreateWorkerGroupCommand(cmdFactory cmdutil.Factory, streams genericclio
108108
}
109109

110110
func (options *CreateWorkerGroupOptions) Complete(cmd *cobra.Command, args []string) error {
111-
namespace, err := cmd.Flags().GetString("namespace")
111+
namespace,_,err := options.cmdFactory.ToRawKubeConfigLoader().Namespace();
112112
if err != nil {
113113
return fmt.Errorf("failed to get namespace: %w", err)
114114
}
115115
options.namespace = namespace
116116

117-
if options.namespace == "" {
118-
options.namespace = "default"
119-
}
120-
121117
if options.rayStartParams == nil {
122118
options.rayStartParams = map[string]string{}
123119
}

kubectl-plugin/pkg/cmd/delete/delete.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,11 @@ func NewDeleteCommand(cmdFactory cmdutil.Factory, streams genericclioptions.IOSt
7878
}
7979

8080
func (options *DeleteOptions) Complete(cmd *cobra.Command, args []string) error {
81-
namespace, err := cmd.Flags().GetString("namespace")
81+
namespace,_,err := options.cmdFactory.ToRawKubeConfigLoader().Namespace()
8282
if err != nil {
8383
return fmt.Errorf("failed to get namespace: %w", err)
8484
}
8585
options.namespace = namespace
86-
if options.namespace == "" {
87-
options.namespace = "default"
88-
}
8986

9087
if options.resources == nil {
9188
options.resources = map[util.ResourceType][]string{}

kubectl-plugin/pkg/cmd/get/get_cluster.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,11 @@ func NewGetClusterCommand(cmdFactory cmdutil.Factory, streams genericclioptions.
6262
}
6363

6464
func (options *GetClusterOptions) Complete(args []string, cmd *cobra.Command) error {
65-
namespace, err := cmd.Flags().GetString("namespace")
65+
namespace,_,err := options.cmdFactory.ToRawKubeConfigLoader().Namespace();
6666
if err != nil {
6767
return fmt.Errorf("failed to get namespace: %w", err)
6868
}
6969
options.namespace = namespace
70-
if options.namespace == "" {
71-
options.namespace = "default"
72-
}
7370

7471
if len(args) >= 1 {
7572
options.cluster = args[0]

kubectl-plugin/pkg/cmd/get/get_nodes.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,11 @@ func (options *GetNodesOptions) Complete(args []string, cmd *cobra.Command) erro
109109
if options.allNamespaces {
110110
options.namespace = ""
111111
} else {
112-
namespace, err := cmd.Flags().GetString("namespace")
112+
namespace,_,err := options.cmdFactory.ToRawKubeConfigLoader().Namespace();
113113
if err != nil {
114114
return fmt.Errorf("failed to get namespace: %w", err)
115115
}
116116
options.namespace = namespace
117-
118-
if options.namespace == "" {
119-
options.namespace = "default"
120-
}
121117
}
122118

123119
if len(args) > 0 {

kubectl-plugin/pkg/cmd/get/get_token.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,11 @@ func NewGetTokenCommand(cmdFactory cmdutil.Factory, streams genericclioptions.IO
5555
}
5656

5757
func (options *GetTokenOptions) Complete(args []string, cmd *cobra.Command) error {
58-
namespace, err := cmd.Flags().GetString("namespace")
58+
namespace,_,err := options.cmdFactory.ToRawKubeConfigLoader().Namespace();
5959
if err != nil {
6060
return fmt.Errorf("failed to get namespace: %w", err)
6161
}
6262
options.namespace = namespace
63-
if options.namespace == "" {
64-
options.namespace = "default"
65-
}
6663
// guarded by cobra.ExactArgs(1)
6764
options.cluster = args[0]
6865
return nil

kubectl-plugin/pkg/cmd/get/get_workergroup.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,11 @@ func (options *GetWorkerGroupsOptions) Complete(args []string, cmd *cobra.Comman
116116
if options.allNamespaces {
117117
options.namespace = ""
118118
} else {
119-
namespace, err := cmd.Flags().GetString("namespace")
119+
namespace,_,err := options.cmdFactory.ToRawKubeConfigLoader().Namespace();
120120
if err != nil {
121121
return fmt.Errorf("failed to get namespace: %w", err)
122122
}
123123
options.namespace = namespace
124-
125-
if options.namespace == "" {
126-
options.namespace = "default"
127-
}
128124
}
129125

130126
if len(args) > 0 {

kubectl-plugin/pkg/cmd/job/job_submit.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,11 @@ func NewJobSubmitCommand(cmdFactory cmdutil.Factory, streams genericclioptions.I
187187
}
188188

189189
func (options *SubmitJobOptions) Complete(cmd *cobra.Command) error {
190-
namespace, err := cmd.Flags().GetString("namespace")
190+
namespace,_,err := options.cmdFactory.ToRawKubeConfigLoader().Namespace();
191191
if err != nil {
192192
return fmt.Errorf("failed to get namespace: %w", err)
193193
}
194194
options.namespace = namespace
195-
if options.namespace == "" {
196-
options.namespace = "default"
197-
}
198195

199196
if len(options.runtimeEnv) > 0 {
200197
options.runtimeEnv = filepath.Clean(options.runtimeEnv)

kubectl-plugin/pkg/cmd/log/log.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,12 @@ func NewClusterLogCommand(cmdFactory cmdutil.Factory, streams genericclioptions.
153153
}
154154

155155
func (options *ClusterLogOptions) Complete(cmd *cobra.Command, args []string) error {
156-
namespace, err := cmd.Flags().GetString("namespace")
156+
namespace,_,err := options.cmdFactory.ToRawKubeConfigLoader().Namespace();
157157
if err != nil {
158158
return fmt.Errorf("failed to get namespace: %w", err)
159159
}
160160
options.namespace = namespace
161161

162-
if options.namespace == "" {
163-
options.namespace = "default"
164-
}
165-
166162
typeAndName := strings.Split(args[0], "/")
167163
if len(typeAndName) == 1 {
168164
options.ResourceType = util.RayCluster

kubectl-plugin/pkg/cmd/scale/scale_cluster.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,11 @@ func NewScaleClusterCommand(cmdFactory cmdutil.Factory, streams genericclioption
9292
}
9393

9494
func (options *ScaleClusterOptions) Complete(args []string, cmd *cobra.Command) error {
95-
namespace, err := cmd.Flags().GetString("namespace")
95+
namespace,_,err := options.cmdFactory.ToRawKubeConfigLoader().Namespace();
9696
if err != nil {
9797
return fmt.Errorf("failed to get namespace: %w", err)
9898
}
9999
options.namespace = namespace
100-
if options.namespace == "" {
101-
options.namespace = "default"
102-
}
103-
104100
options.cluster = args[0]
105101

106102
return nil

0 commit comments

Comments
 (0)