@@ -20,6 +20,7 @@ package join
20
20
import (
21
21
"fmt"
22
22
"io"
23
+ "os"
23
24
"strings"
24
25
25
26
"github.com/pkg/errors"
@@ -38,6 +39,7 @@ import (
38
39
yurtconstants "github.com/openyurtio/openyurt/pkg/yurtadm/constants"
39
40
"github.com/openyurtio/openyurt/pkg/yurtadm/util/edgenode"
40
41
yurtadmutil "github.com/openyurtio/openyurt/pkg/yurtadm/util/kubernetes"
42
+ "github.com/openyurtio/openyurt/pkg/yurtadm/util/localnode"
41
43
"github.com/openyurtio/openyurt/pkg/yurtadm/util/yurthub"
42
44
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/yurtstaticset/util"
43
45
)
@@ -52,6 +54,8 @@ type joinOptions struct {
52
54
organizations string
53
55
pauseImage string
54
56
yurthubImage string
57
+ yurthubBinary string
58
+ hostControlPlaneAddr string // hostControlPlaneAddr is the address (ip:port) of host kubernetes cluster that used for yurthub local mode.
55
59
namespace string
56
60
caCertHashes []string
57
61
unsafeSkipCAVerification bool
@@ -124,7 +128,7 @@ func addJoinConfigFlags(flagSet *flag.FlagSet, joinOptions *joinOptions) {
124
128
)
125
129
flagSet .StringVar (
126
130
& joinOptions .nodeType , yurtconstants .NodeType , joinOptions .nodeType ,
127
- "Sets the node is edge or cloud " ,
131
+ "Sets the node is edge, cloud or local " ,
128
132
)
129
133
flagSet .StringVar (
130
134
& joinOptions .nodeName , yurtconstants .NodeName , joinOptions .nodeName ,
@@ -154,6 +158,14 @@ func addJoinConfigFlags(flagSet *flag.FlagSet, joinOptions *joinOptions) {
154
158
& joinOptions .yurthubImage , yurtconstants .YurtHubImage , joinOptions .yurthubImage ,
155
159
"Sets the image version of yurthub component" ,
156
160
)
161
+ flagSet .StringVar (
162
+ & joinOptions .yurthubBinary , yurtconstants .YurtHubBinary , joinOptions .yurthubBinary ,
163
+ "Sets the binary path of yurthub, this is used for deploying local mode yurthub in systemd" ,
164
+ )
165
+ flagSet .StringVar (
166
+ & joinOptions .hostControlPlaneAddr , yurtconstants .HostControlPlaneAddr , joinOptions .hostControlPlaneAddr ,
167
+ "Sets the address of hostControlPlaneAddr, which is the address (ip:port) of host kubernetes cluster that used for yurthub local mode" ,
168
+ )
157
169
flagSet .StringSliceVar (
158
170
& joinOptions .caCertHashes , yurtconstants .TokenDiscoveryCAHash , joinOptions .caCertHashes ,
159
171
"For token-based discovery, validate that the root CA public key matches this hash (format: \" <type>:<value>\" )." ,
@@ -227,6 +239,9 @@ type joinData struct {
227
239
organizations string
228
240
pauseImage string
229
241
yurthubImage string
242
+ yurthubBinary string
243
+ hostControlPlaneAddr string
244
+ tenantApiServerEndpoints string
230
245
yurthubTemplate string
231
246
yurthubManifest string
232
247
kubernetesVersion string
@@ -257,6 +272,25 @@ func newJoinData(args []string, opt *joinOptions) (*joinData, error) {
257
272
apiServerEndpoint = args [0 ]
258
273
}
259
274
275
+ if opt .nodeType == yurtconstants .LocalNode {
276
+ // in local mode, it is necessary to prepare yurthub binary file for deploying systemd yurthub.
277
+ if len (opt .yurthubBinary ) == 0 {
278
+ return nil , errors .New ("yurthub binary filepath is empty, so unable to run systemd yurthub in local mode." )
279
+ }
280
+ _ , err := os .Stat (opt .yurthubBinary )
281
+ if err != nil {
282
+ if os .IsNotExist (err ) {
283
+ return nil , errors .New ("yurthub binary file does not exist." )
284
+ }
285
+ return nil , errors .Wrapf (err , "stat yurthub binary file %s fail" , opt .yurthubBinary )
286
+ }
287
+
288
+ // in local mode, hostControlPlaneAddr is needed for systemd yurthub accessing host kubernetes cluster.
289
+ if len (opt .hostControlPlaneAddr ) == 0 {
290
+ return nil , errors .New ("host control plane address is empty, so unable to run systemd yurthub in local mode." )
291
+ }
292
+ }
293
+
260
294
if len (opt .token ) == 0 {
261
295
return nil , errors .New ("join token is empty, so unable to bootstrap worker node." )
262
296
}
@@ -265,8 +299,8 @@ func newJoinData(args []string, opt *joinOptions) (*joinData, error) {
265
299
return nil , errors .Errorf ("the bootstrap token %s was not of the form %s" , opt .token , yurtconstants .BootstrapTokenPattern )
266
300
}
267
301
268
- if opt .nodeType != yurtconstants .EdgeNode && opt .nodeType != yurtconstants .CloudNode {
269
- return nil , errors .Errorf ("node type(%s) is invalid, only \" edge and cloud \" are supported" , opt .nodeType )
302
+ if opt .nodeType != yurtconstants .EdgeNode && opt .nodeType != yurtconstants .CloudNode && opt . nodeType != yurtconstants . LocalNode {
303
+ return nil , errors .Errorf ("node type(%s) is invalid, only \" edge, cloud and local \" are supported" , opt .nodeType )
270
304
}
271
305
272
306
if opt .unsafeSkipCAVerification && len (opt .caCertHashes ) != 0 {
@@ -298,6 +332,8 @@ func newJoinData(args []string, opt *joinOptions) (*joinData, error) {
298
332
ignorePreflightErrors : ignoreErrors ,
299
333
pauseImage : opt .pauseImage ,
300
334
yurthubImage : opt .yurthubImage ,
335
+ yurthubBinary : opt .yurthubBinary ,
336
+ hostControlPlaneAddr : opt .hostControlPlaneAddr ,
301
337
yurthubServer : opt .yurthubServer ,
302
338
caCertHashes : opt .caCertHashes ,
303
339
organizations : opt .organizations ,
@@ -327,13 +363,37 @@ func newJoinData(args []string, opt *joinOptions) (*joinData, error) {
327
363
}
328
364
}
329
365
366
+ // if the node type is local, before get tls bootstrap config, we need to deploy systemd yurthub for maintaining iptables rules
367
+ if opt .nodeType == yurtconstants .LocalNode {
368
+ // deploy systemd yurthub
369
+ if err := localnode .DeployYurthubInSystemd (data .HostControlPlaneAddr (), data .ServerAddr (), data .YurtHubBinary (), data .NodeRegistration ().Name ); err != nil {
370
+ klog .Errorf ("could not deploy local yurthub in systemd, %v" , err )
371
+ return nil , err
372
+ }
373
+
374
+ // check systemd yurthub is ready or not
375
+ if err := localnode .CheckYurthubStatus (); err != nil {
376
+ return nil , err
377
+ }
378
+ klog .V (1 ).Infof ("systemd yurthub agent is ready" )
379
+
380
+ tenantApiServerEndpoints , err := localnode .GetTenantApiServerEndpoints ()
381
+ if err != nil {
382
+ klog .Errorf ("could not get tenantApiServerEndpoints, %v" , err )
383
+ return nil , err
384
+ }
385
+ data .tenantApiServerEndpoints = tenantApiServerEndpoints
386
+ klog .V (1 ).Infof ("get tenantApiServerEndpoints: %s" , tenantApiServerEndpoints )
387
+ }
388
+
330
389
// get tls bootstrap config
331
390
cfg , err := yurtadmutil .RetrieveBootstrapConfig (data )
332
391
if err != nil {
333
392
klog .Errorf ("could not retrieve bootstrap config, %v" , err )
334
393
return nil , err
335
394
}
336
395
data .tlsBootstrapCfg = cfg
396
+ klog .Infof ("RetrieveBootstrapConfig: %#+v" , * cfg )
337
397
338
398
// get kubernetes version
339
399
client , err := kubeconfigutil .ToClientSet (cfg )
@@ -342,6 +402,7 @@ func newJoinData(args []string, opt *joinOptions) (*joinData, error) {
342
402
return nil , err
343
403
}
344
404
data .clientSet = client
405
+ klog .Infof ("ToClientSet: %#+v" , * client )
345
406
346
407
k8sVersion , err := yurtadmutil .GetKubernetesVersionFromCluster (client )
347
408
if err != nil {
@@ -350,6 +411,9 @@ func newJoinData(args []string, opt *joinOptions) (*joinData, error) {
350
411
}
351
412
data .kubernetesVersion = k8sVersion
352
413
414
+ // test for get k8s version
415
+ klog .Infof ("GetKubernetesVersionFromCluster: %s" , k8sVersion )
416
+
353
417
// check whether specified nodePool exists
354
418
if len (opt .nodePoolName ) != 0 {
355
419
np , err := apiclient .GetNodePoolInfoWithRetry (cfg , opt .nodePoolName )
@@ -439,6 +503,19 @@ func (j *joinData) YurtHubImage() string {
439
503
return j .yurthubImage
440
504
}
441
505
506
+ // YurtHubBinary returns the YurtHub binary.
507
+ func (j * joinData ) YurtHubBinary () string {
508
+ return j .yurthubBinary
509
+ }
510
+
511
+ func (j * joinData ) HostControlPlaneAddr () string {
512
+ return j .hostControlPlaneAddr
513
+ }
514
+
515
+ func (j * joinData ) TenantApiServerEndpoints () string {
516
+ return j .tenantApiServerEndpoints
517
+ }
518
+
442
519
// YurtHubServer returns the YurtHub server addr.
443
520
func (j * joinData ) YurtHubServer () string {
444
521
return j .yurthubServer
0 commit comments