-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadimage.go
86 lines (72 loc) · 2.19 KB
/
loadimage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cmd
import (
"context"
"fmt"
"github.com/zdnscloud/zke/core"
"github.com/zdnscloud/zke/core/pki"
"github.com/zdnscloud/zke/pkg/docker"
"github.com/zdnscloud/zke/pkg/hosts"
"github.com/zdnscloud/zke/pkg/log"
"github.com/zdnscloud/zke/types"
"github.com/urfave/cli"
"github.com/zdnscloud/cement/errgroup"
cementlog "github.com/zdnscloud/cement/log"
)
func LoadImageCommand() cli.Command {
loadImageFlags := []cli.Flag{
cli.StringSliceFlag{
Name: "input-file",
Usage: "Specify the images tar file, example: --input-file zcloud_images.tar --input-file zke_images.tar",
},
}
return cli.Command{
Name: "loadimage",
Usage: "load images for the cluster nodes",
Action: loadImageFromCli,
Flags: loadImageFlags,
}
}
func loadImageFromCli(cliCtx *cli.Context) error {
imageFilePaths := cliCtx.StringSlice("input-file")
parentCtx := context.Background()
logger := cementlog.NewLog4jConsoleLogger(log.LogLevel)
defer logger.Close()
ctx, err := log.SetLogger(parentCtx, logger)
if err != nil {
return err
}
clusterFile, err := resolveClusterFile(pki.ClusterConfig)
if err != nil {
return fmt.Errorf("Failed to resolve cluster file: %v", err)
}
zkeConfig, err := core.ParseConfig(ctx, clusterFile)
if err != nil {
return fmt.Errorf("Failed to parse cluster file: %v", err)
}
return LoadImage(ctx, zkeConfig, imageFilePaths)
}
func LoadImage(ctx context.Context, zkeConfig *types.ZKEConfig, imageFilePaths []string) error {
kubeCluster, err := core.InitClusterObject(ctx, zkeConfig)
if err != nil {
return err
}
if err := kubeCluster.SetupDialers(ctx, hosts.DialersOptions{}); err != nil {
return err
}
err = kubeCluster.TunnelHosts(ctx)
if err != nil {
return err
}
for _, imageFilePath := range imageFilePaths {
log.Infof(ctx, "Starting load [%s]", imageFilePath)
allHosts := hosts.GetUniqueHostList(kubeCluster.ControlPlaneHosts, kubeCluster.EtcdHosts, kubeCluster.WorkerHosts, kubeCluster.EdgeHosts)
_, err = errgroup.Batch(allHosts, func(h interface{}) (interface{}, error) {
runHost := h.(*hosts.Host)
return nil, docker.LoadImage(ctx, runHost.DClient, runHost.Address, imageFilePath)
})
if err != nil {
return err
}
}
return nil
}