@@ -37,6 +37,7 @@ import (
3737type (
3838 ClusterAddOptions struct {
3939 runtimeName string
40+ clusterName string
4041 kubeContext string
4142 kubeconfig string
4243 dryRun bool
@@ -97,13 +98,21 @@ func newClusterAddCommand() *cobra.Command {
9798 }
9899
99100 opts .kubeContext , err = ensureKubeContextName (cmd .Flag ("context" ), cmd .Flag ("kubeconfig" ))
101+ if err != nil {
102+ return err
103+ }
104+
105+ setClusterName (& opts )
106+ err = validateClusterName (opts .clusterName )
107+
100108 return err
101109 },
102110 RunE : func (cmd * cobra.Command , args []string ) error {
103111 return runClusterAdd (cmd .Context (), & opts )
104112 },
105113 }
106114
115+ cmd .Flags ().StringVar (& opts .clusterName , "name" , "" , "Name of the cluster. If omitted, will use the context name" )
107116 cmd .Flags ().BoolVar (& opts .dryRun , "dry-run" , false , "" )
108117 opts .kubeFactory = kube .AddFlags (cmd .Flags ())
109118
@@ -136,7 +145,7 @@ func runClusterAdd(ctx context.Context, opts *ClusterAddOptions) error {
136145 }
137146
138147 csdpToken := cfConfig .GetCurrentContext ().Token
139- k := createAddClusterKustomization (ingressUrl , opts .kubeContext , server , csdpToken , * runtime .RuntimeVersion )
148+ k := createAddClusterKustomization (ingressUrl , opts .clusterName , server , csdpToken , * runtime .RuntimeVersion )
140149
141150 manifests , err := kustutil .BuildKustomization (k )
142151 if err != nil {
@@ -156,6 +165,20 @@ func runClusterAdd(ctx context.Context, opts *ClusterAddOptions) error {
156165 return kubeutil .WaitForJob (ctx , opts .kubeFactory , "kube-system" , store .Get ().AddClusterJobName )
157166}
158167
168+ func setClusterName (opts * ClusterAddOptions ) {
169+ if opts .clusterName != "" {
170+ return
171+ }
172+ opts .clusterName = opts .kubeContext
173+ }
174+
175+ func validateClusterName (name string ) error {
176+ if strings .ContainsAny (name , "%`" ) {
177+ return fmt .Errorf ("cluster name '%s' is invalid. '%%' and '`' are not allowed" , name )
178+ }
179+ return nil
180+ }
181+
159182func createAddClusterKustomization (ingressUrl , contextName , server , csdpToken , version string ) * kusttypes.Kustomization {
160183 resourceUrl := store .AddClusterDefURL
161184 if strings .HasPrefix (resourceUrl , "http" ) {
0 commit comments