@@ -31,33 +31,31 @@ import (
31
31
32
32
var joinExample = templates .Examples (i18n .T (`
33
33
# Join cluster resource from a directory containing cluster.yaml, e.g:
34
- kosmosctl join cluster --cluster- name=[cluster-name] --master-kubeconfig=[master-kubeconfig] --cluster-kubeconfig=[cluster-kubeconfig]
34
+ kosmosctl join cluster --name=[cluster-name] --master-kubeconfig=[master-kubeconfig] --cluster-kubeconfig=[cluster-kubeconfig]
35
35
36
36
# Join cluster resource without master-kubeconfig, e.g:
37
- kosmosctl join cluster --cluster- name=[cluster-name] --cluster-kubeconfig=[cluster-kubeconfig]
37
+ kosmosctl join cluster --name=[cluster-name] --cluster-kubeconfig=[cluster-kubeconfig]
38
38
39
39
# Join knode resource, e.g:
40
- kosmosctl join knode --knode- name=[knode-name] --master-kubeconfig=[master-kubeconfig] --cluster-kubeconfig=[cluster-kubeconfig]
40
+ kosmosctl join knode --name=[knode-name] --master-kubeconfig=[master-kubeconfig] --cluster-kubeconfig=[cluster-kubeconfig]
41
41
42
42
# Join knode resource without master-kubeconfig, e.g:
43
- kosmosctl join knode --knode- name=[knode-name] --cluster-kubeconfig=[cluster-kubeconfig]
43
+ kosmosctl join knode --name=[knode-name] --cluster-kubeconfig=[cluster-kubeconfig]
44
44
` ))
45
45
46
46
type CommandJoinOptions struct {
47
47
MasterKubeConfig string
48
48
MasterKubeConfigStream []byte
49
49
ClusterKubeConfig string
50
50
51
- ClusterName string
51
+ Name string
52
52
CNI string
53
53
DefaultNICName string
54
54
ImageRegistry string
55
55
NetworkType string
56
56
UseProxy string
57
57
WaitTime int
58
58
59
- KnodeName string
60
-
61
59
Client kubernetes.Interface
62
60
DynamicClient * dynamic.DynamicClient
63
61
}
@@ -84,12 +82,11 @@ func NewCmdJoin(f ctlutil.Factory) *cobra.Command {
84
82
flags := cmd .Flags ()
85
83
flags .StringVar (& o .MasterKubeConfig , "master-kubeconfig" , "" , "Absolute path to the master kubeconfig file." )
86
84
flags .StringVar (& o .ClusterKubeConfig , "cluster-kubeconfig" , "" , "Absolute path to the cluster kubeconfig file." )
87
- flags .StringVar (& o .ClusterName , "cluster- name" , "" , "Specify the name of the member cluster to join." )
85
+ flags .StringVar (& o .Name , "name" , "" , "Specify the name of the resource to join." )
88
86
flags .StringVar (& o .CNI , "cni" , "" , "The cluster is configured using cni and currently supports calico and flannel." )
89
87
flags .StringVar (& o .DefaultNICName , "default-nic" , "" , "Set default network interface card." )
90
88
flags .StringVar (& o .ImageRegistry , "private-image-registry" , utils .DefaultImageRepository , "Private image registry where pull images from. If set, all required images will be downloaded from it, it would be useful in offline installation scenarios. In addition, you still can use --kube-image-registry to specify the registry for Kubernetes's images." )
91
- flags .StringVar (& o .NetworkType , "network-type" , "gateway" , "Set the cluster network connection mode, which supports gateway and p2p modes. Gateway is used by default." )
92
- flags .StringVar (& o .KnodeName , "knode-name" , "" , "Specify the name of the knode to join." )
89
+ flags .StringVar (& o .NetworkType , "network-type" , utils .NetworkTypeP2P , "Set the cluster network connection mode, which supports gateway and p2p modes, p2p is used by default." )
93
90
flags .StringVar (& o .UseProxy , "use-proxy" , "false" , "Set whether to enable proxy." )
94
91
flags .IntVarP (& o .WaitTime , "wait-time" , "" , 120 , "Wait the specified time for the Kosmos install ready." )
95
92
@@ -142,16 +139,20 @@ func (o *CommandJoinOptions) Complete(f ctlutil.Factory) error {
142
139
}
143
140
144
141
func (o * CommandJoinOptions ) Validate (args []string ) error {
142
+ if len (o .Name ) == 0 {
143
+ return fmt .Errorf ("kosmosctl join validate error, resource name is not valid" )
144
+ }
145
+
145
146
switch args [0 ] {
146
147
case "cluster" :
147
- _ , err := o .DynamicClient .Resource (util .ClusterGVR ).Get (context .TODO (), o .ClusterName , metav1.GetOptions {})
148
+ _ , err := o .DynamicClient .Resource (util .ClusterGVR ).Get (context .TODO (), o .Name , metav1.GetOptions {})
148
149
if err != nil {
149
150
if apierrors .IsAlreadyExists (err ) {
150
151
return fmt .Errorf ("kosmosctl join validate error, clsuter already exists: %s" , err )
151
152
}
152
153
}
153
154
case "knode" :
154
- _ , err := o .DynamicClient .Resource (util .KnodeGVR ).Get (context .TODO (), o .KnodeName , metav1.GetOptions {})
155
+ _ , err := o .DynamicClient .Resource (util .KnodeGVR ).Get (context .TODO (), o .Name , metav1.GetOptions {})
155
156
if err != nil && apierrors .IsAlreadyExists (err ) {
156
157
if apierrors .IsAlreadyExists (err ) {
157
158
return fmt .Errorf ("kosmosctl join validate error, knode already exists: %s" , err )
@@ -183,7 +184,7 @@ func (o *CommandJoinOptions) runCluster() error {
183
184
klog .Info ("Start registering cluster to kosmos control plane..." )
184
185
// 1. create cluster in master
185
186
clusterByte , err := util .GenerateCustomResource (manifest .ClusterCR , manifest.ClusterReplace {
186
- ClusterName : o .ClusterName ,
187
+ ClusterName : o .Name ,
187
188
CNI : o .CNI ,
188
189
DefaultNICName : o .DefaultNICName ,
189
190
ImageRepository : o .ImageRegistry ,
@@ -200,15 +201,16 @@ func (o *CommandJoinOptions) runCluster() error {
200
201
}
201
202
_ , err = o .DynamicClient .Resource (util .ClusterGVR ).Namespace ("" ).Create (context .TODO (), obj , metav1.CreateOptions {})
202
203
if err != nil {
203
- return fmt .Errorf ("(cluster) kosmosctl join run error, create cluster failed: %s" , err )
204
+ return fmt .Errorf ("kosmosctl join run error, create cluster failed: %s" , err )
204
205
}
206
+ klog .Info ("Cluster: " + o .Name + " has been created." )
205
207
206
208
// 2. create namespace in member
207
209
namespace := & corev1.Namespace {}
208
210
namespace .Name = utils .DefaultNamespace
209
211
_ , err = o .Client .CoreV1 ().Namespaces ().Create (context .TODO (), namespace , metav1.CreateOptions {})
210
212
if err != nil && ! apierrors .IsAlreadyExists (err ) {
211
- return fmt .Errorf ("(cluster namespace) kosmosctl join run error, create namespace failed: %s" , err )
213
+ return fmt .Errorf ("kosmosctl join run error, create namespace failed: %s" , err )
212
214
}
213
215
214
216
// 3. create secret in member
@@ -224,59 +226,66 @@ func (o *CommandJoinOptions) runCluster() error {
224
226
}
225
227
_ , err = o .Client .CoreV1 ().Secrets (secret .Namespace ).Create (context .TODO (), secret , metav1.CreateOptions {})
226
228
if err != nil && ! apierrors .IsAlreadyExists (err ) {
227
- return fmt .Errorf ("(cluster secret) kosmosctl join run error, create secret failed: %s" , err )
229
+ return fmt .Errorf ("kosmosctl join run error, create secret failed: %s" , err )
228
230
}
231
+ klog .Info ("Secret: " + secret .Name + " has been created." )
229
232
230
233
// 4. create rbac in member
231
234
clusterRole , err := util .GenerateClusterRole (manifest .ClusterlinkClusterRole , nil )
232
235
if err != nil {
233
- return fmt .Errorf ("(cluster rbac) kosmosctl join run error, generate clusterrole failed: %s" , err )
236
+ return fmt .Errorf ("kosmosctl join run error, generate clusterrole failed: %s" , err )
234
237
}
235
238
_ , err = o .Client .RbacV1 ().ClusterRoles ().Create (context .TODO (), clusterRole , metav1.CreateOptions {})
236
239
if err != nil && ! apierrors .IsAlreadyExists (err ) {
237
- return fmt .Errorf ("(cluster rbac) kosmosctl join run error, create clusterrole failed: %s" , err )
240
+ return fmt .Errorf ("kosmosctl join run error, create clusterrole failed: %s" , err )
238
241
}
242
+ klog .Info ("ClusterRole: " + clusterRole .Name + " has been created." )
243
+
239
244
clusterRoleBinding , err := util .GenerateClusterRoleBinding (manifest .ClusterlinkClusterRoleBinding , manifest.ClusterRoleBindingReplace {
240
245
Namespace : utils .DefaultNamespace ,
241
246
})
242
247
if err != nil {
243
- return fmt .Errorf ("(cluster rbac) kosmosctl join run error, generate clusterrolebinding failed: %s" , err )
248
+ return fmt .Errorf ("kosmosctl join run error, generate clusterrolebinding failed: %s" , err )
244
249
}
245
250
_ , err = o .Client .RbacV1 ().ClusterRoleBindings ().Create (context .TODO (), clusterRoleBinding , metav1.CreateOptions {})
246
251
if err != nil && ! apierrors .IsAlreadyExists (err ) {
247
- return fmt .Errorf ("(cluster rbac) kosmosctl join run error, create clusterrolebinding failed: %s" , err )
252
+ return fmt .Errorf ("kosmosctl join run error, create clusterrolebinding failed: %s" , err )
248
253
}
254
+ klog .Info ("ClusterRoleBinding: " + clusterRoleBinding .Name + " has been created." )
249
255
250
256
// 5. create operator in member
251
257
serviceAccount , err := util .GenerateServiceAccount (manifest .ClusterlinkOperatorServiceAccount , manifest.ServiceAccountReplace {
252
258
Namespace : utils .DefaultNamespace ,
253
259
})
254
260
if err != nil {
255
- return fmt .Errorf ("(cluster operator) kosmosctl join run error, generate serviceaccount failed: %s" , err )
261
+ return fmt .Errorf ("kosmosctl join run error, generate serviceaccount failed: %s" , err )
256
262
}
257
263
_ , err = o .Client .CoreV1 ().ServiceAccounts (serviceAccount .Namespace ).Create (context .TODO (), serviceAccount , metav1.CreateOptions {})
258
264
if err != nil && ! apierrors .IsAlreadyExists (err ) {
259
- return fmt .Errorf ("(cluster operator) kosmosctl join run error, create serviceaccount failed: %s" , err )
265
+ return fmt .Errorf ("kosmosctl join run error, create serviceaccount failed: %s" , err )
260
266
}
267
+ klog .Info ("ServiceAccount: " + serviceAccount .Name + " has been created." )
261
268
262
269
deployment , err := util .GenerateDeployment (manifest .ClusterlinkOperatorDeployment , manifest.ClusterlinkDeploymentReplace {
263
270
Namespace : utils .DefaultNamespace ,
264
271
Version : version .GetReleaseVersion ().PatchRelease (),
265
- ClusterName : o .ClusterName ,
272
+ ClusterName : o .Name ,
266
273
UseProxy : o .UseProxy ,
267
274
ImageRepository : o .ImageRegistry ,
268
275
})
269
276
if err != nil {
270
- return fmt .Errorf ("(cluster operator) kosmosctl join run error, generate deployment failed: %s" , err )
277
+ return fmt .Errorf ("kosmosctl join run error, generate deployment failed: %s" , err )
271
278
}
272
279
_ , err = o .Client .AppsV1 ().Deployments (deployment .Namespace ).Create (context .TODO (), deployment , metav1.CreateOptions {})
273
280
if err != nil && ! apierrors .IsAlreadyExists (err ) {
274
- return fmt .Errorf ("(cluster operator) kosmosctl join run error, create deployment failed: %s" , err )
281
+ return fmt .Errorf ("kosmosctl join run error, create deployment failed: %s" , err )
275
282
}
276
- if err = util .WaitDeploymentReady (o .Client , deployment , o .WaitTime ); err != nil {
277
- return fmt .Errorf ("(cluster operator) kosmosctl join run error, create deployment failed: %s" , err )
283
+ label := map [string ]string {"app" : deployment .Labels ["app" ]}
284
+ if err = util .WaitPodReady (o .Client , deployment .Namespace , util .MapToString (label ), o .WaitTime ); err != nil {
285
+ return fmt .Errorf ("kosmosctl join run error, create deployment failed: %s" , err )
278
286
} else {
279
- klog .Info ("Cluster registration successful." )
287
+ klog .Info ("Deployment: " + deployment .Name + " has been created." )
288
+ klog .Info ("Cluster [" + o .Name + "] registration successful." )
280
289
}
281
290
282
291
return nil
@@ -286,11 +295,11 @@ func (o *CommandJoinOptions) runKnode() error {
286
295
klog .Info ("Start registering knode to kosmos control plane..." )
287
296
clusterKubeConfigByte , err := os .ReadFile (o .ClusterKubeConfig )
288
297
if err != nil {
289
- return fmt .Errorf ("(knode) kosmosctl join run error, decode knode cr failed: %s" , err )
298
+ return fmt .Errorf ("kosmosctl join run error, decode knode cr failed: %s" , err )
290
299
}
291
300
base64ClusterKubeConfig := base64 .StdEncoding .EncodeToString (clusterKubeConfigByte )
292
301
knodeByte , err := util .GenerateCustomResource (manifest .KnodeCR , manifest.KnodeReplace {
293
- KnodeName : o .KnodeName ,
302
+ KnodeName : o .Name ,
294
303
KnodeKubeConfig : base64ClusterKubeConfig ,
295
304
})
296
305
if err != nil {
@@ -300,13 +309,14 @@ func (o *CommandJoinOptions) runKnode() error {
300
309
obj := & unstructured.Unstructured {}
301
310
_ , _ , err = decoder .Decode (knodeByte , nil , obj )
302
311
if err != nil {
303
- return fmt .Errorf ("(knode) kosmosctl join run error, decode knode cr failed: %s" , err )
312
+ return fmt .Errorf ("kosmosctl join run error, decode knode cr failed: %s" , err )
304
313
}
305
314
_ , err = o .DynamicClient .Resource (util .KnodeGVR ).Namespace ("" ).Create (context .TODO (), obj , metav1.CreateOptions {})
306
315
if err != nil && ! apierrors .IsAlreadyExists (err ) {
307
- return fmt .Errorf ("(knode) kosmosctl join run error, create knode failed: %s" , err )
316
+ return fmt .Errorf ("kosmosctl join run error, create knode failed: %s" , err )
308
317
}
309
- klog .Info ("Knode registration successful." )
318
+ klog .Info ("Knode: " + obj .GetName () + " has been created." )
319
+ klog .Info ("Knode [" + obj .GetName () + "] registration successful." )
310
320
311
321
return nil
312
322
}
0 commit comments