diff --git a/cmd/command/opts.go b/cmd/command/opts.go index 51fa764450042294b4a768ad804d0112056ad4ab..5e299140f0122dcdff6c7c8f06f7635e1cc199f4 100644 --- a/cmd/command/opts.go +++ b/cmd/command/opts.go @@ -1,5 +1,9 @@ package command +var ( + RootOptDir string +) + // 部署集群可选配置参数集合 var ClusterOpts struct { ClusterId string diff --git a/cmd/deploy.go b/cmd/deploy.go index 6d31738c9a1ffb898f985d57bc4ee23aa5bb4748..b997fe4fec46e1fa7bb005626f25194eafe0761a 100755 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -16,9 +16,17 @@ limitations under the License. package cmd import ( + "context" "nestos-kubernetes-deployer/cmd/command" + "path/filepath" + "time" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" + wait "k8s.io/apimachinery/pkg/util/wait" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" ) func NewDeployCommand() *cobra.Command { @@ -40,6 +48,17 @@ func NewDeployCommand() *cobra.Command { func runDeployCmd(cmd *cobra.Command, args []string) error { + //todo:部署集群 + + configPath := filepath.Join(command.RootOptDir, "auth", "kubeconfig") + config, err := clientcmd.BuildConfigFromFlags("", configPath) + if err != nil { + logrus.Errorf("error to load kubeconfig: %v", err) + return err + } + if err := waitForAPIReady(context.Background(), config); err != nil { + return err + } return nil } @@ -50,16 +69,38 @@ func runInstallconfig() error { } func runDeployCluster() error { + return nil } -// 等待集群安装完成 -func waitForClusterComplete(config string) error { +func waitForAPIReady(ctx context.Context, config *rest.Config) error { + client, err := kubernetes.NewForConfig(config) + if err != nil { + logrus.Errorf("failed to create a kubernetes client: %v", err) + return err + } - return nil + discovery := client.Discovery() + + apiTimeout := 10 * time.Minute + apiContext, cancel := context.WithTimeout(ctx, apiTimeout) + logrus.Infof("Waiting up to %v for the Kubernetes API at %s...", apiTimeout, config.Host) + defer cancel() + + wait.Until(func() { + version, err := discovery.ServerVersion() + if err == nil { + logrus.Infof("The Kubernetes API %s up", version) + cancel() + } else { + logrus.Debugf("Still waiting for Kubernetes API ready: %v", err) + } + }, 2*time.Second, apiContext.Done()) + + return waitForPodsRunning(ctx, client) } -// check 集群running状态 -func checkPod() error { +func waitForPodsRunning(ctx context.Context, client *kubernetes.Clientset) error { + //todo: 等待Pods Running return nil } diff --git a/nkd.go b/nkd.go index 3c0bb2c6ed809208e46d13baf83935da674839e2..a6b5297c4fba7e87b267a3c06e9eb85b6d0e5e8d 100755 --- a/nkd.go +++ b/nkd.go @@ -17,6 +17,7 @@ package main import ( "nestos-kubernetes-deployer/cmd" + "nestos-kubernetes-deployer/cmd/command" "github.com/spf13/cobra" ) @@ -45,5 +46,7 @@ func newRootCmd() *cobra.Command { Use: "nkd", Short: "Creates Kubernetes Clusters", } + cmd.PersistentFlags().StringVar(&command.RootOptDir, "dir", ".", "assets directory") + return cmd }