@@ -37,6 +37,7 @@ import (
37
37
type (
38
38
ClusterAddOptions struct {
39
39
runtimeName string
40
+ clusterName string
40
41
kubeContext string
41
42
kubeconfig string
42
43
dryRun bool
@@ -97,13 +98,21 @@ func newClusterAddCommand() *cobra.Command {
97
98
}
98
99
99
100
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
+
100
108
return err
101
109
},
102
110
RunE : func (cmd * cobra.Command , args []string ) error {
103
111
return runClusterAdd (cmd .Context (), & opts )
104
112
},
105
113
}
106
114
115
+ cmd .Flags ().StringVar (& opts .clusterName , "name" , "" , "Name of the cluster. If omitted, will use the context name" )
107
116
cmd .Flags ().BoolVar (& opts .dryRun , "dry-run" , false , "" )
108
117
opts .kubeFactory = kube .AddFlags (cmd .Flags ())
109
118
@@ -136,7 +145,7 @@ func runClusterAdd(ctx context.Context, opts *ClusterAddOptions) error {
136
145
}
137
146
138
147
csdpToken := cfConfig .GetCurrentContext ().Token
139
- k := createAddClusterKustomization (ingressUrl , opts .kubeContext , server , csdpToken , * runtime .RuntimeVersion )
148
+ k := createAddClusterKustomization (ingressUrl , opts .clusterName , server , csdpToken , * runtime .RuntimeVersion )
140
149
141
150
manifests , err := kustutil .BuildKustomization (k )
142
151
if err != nil {
@@ -156,6 +165,20 @@ func runClusterAdd(ctx context.Context, opts *ClusterAddOptions) error {
156
165
return kubeutil .WaitForJob (ctx , opts .kubeFactory , "kube-system" , store .Get ().AddClusterJobName )
157
166
}
158
167
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
+
159
182
func createAddClusterKustomization (ingressUrl , contextName , server , csdpToken , version string ) * kusttypes.Kustomization {
160
183
resourceUrl := store .AddClusterDefURL
161
184
if strings .HasPrefix (resourceUrl , "http" ) {
0 commit comments