Skip to content

Commit

Permalink
stage
Browse files Browse the repository at this point in the history
  • Loading branch information
oldthreefeng committed Sep 3, 2020
1 parent bf03646 commit c6a026a
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 3 deletions.
51 changes: 51 additions & 0 deletions cmd/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// execCmd represents the exec command
var execCmd = &cobra.Command{
Use: "exec",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("exec called")
},
}

func init() {
rootCmd.AddCommand(execCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// execCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// execCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ require (
go.uber.org/zap v1.10.0
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
gopkg.in/yaml.v2 v2.2.8
k8s.io/api v0.17.3
k8s.io/apimachinery v0.17.3
k8s.io/client-go v0.17.3
k8s.io/api v0.18.0
k8s.io/apimachinery v0.18.0
k8s.io/client-go v0.18.0
)

replace (
Expand Down
82 changes: 82 additions & 0 deletions install/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package install

import (
"github.com/fanux/sealos/k8s"
"sync"
)

type ExecFlag struct {
Dst string
Src string
Cmd string
Label string
Nodes []string
SealConfig
}

func GetExecFlag() *ExecFlag {
return &ExecFlag{}
}

// IsLabeled return true when is not labeled
func (e *ExecFlag) IsLabeled() bool {
return e.Label == ""
}

func (e *ExecFlag) IsNode() bool {
return e.Nodes == nil
}

// Copy is cp src file to dst file
func (e *ExecFlag) Copy() {

}

// Exec is cp src file to dst file
func (e *ExecFlag) Exec() {

}

// copyByNode is cp src file to dst file
func (e *ExecFlag) copyByNode() {
var wg sync.WaitGroup
for _, n := range e.Nodes {
wg.Add(1)
go func(node string) {
defer wg.Done()
SSHConfig.Copy(node, e.Src, e.Dst)
}(n)
}
wg.Wait()
}

// execByNode is exec cmd in Node
func (e *ExecFlag) execByNode() {
var wg sync.WaitGroup
for _, n := range e.Nodes {
wg.Add(1)
go func(node string) {
defer wg.Done()
CmdWorkSpace(node, e.Cmd, TMPDIR)
}(n)
}
wg.Wait()
}

func (e *ExecFlag) getNodesByLabel() error {
k8sClient, err := k8s.NewClient(k8s.KubeDefaultConfigPath, nil)
if err != nil {
return err
}
nodes, err := k8s.GetNodeByLabel(k8sClient, e.Label)
if err != nil {
return err
}
e.Nodes = nodes
return nil
}

// execByNode is exec cmd in Node
func (e *ExecFlag) execByLabel() {

}
94 changes: 94 additions & 0 deletions k8s/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package k8s


import (
"context"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/transport"
"strings"
"time"
)

const (
HostnameLabel = "kubernetes.io/hostname"
NodeRoleLabel = "node-role.kubernetes.io/master"
MaxRetries = 5
RetryInterval = 5
WrapTransportTimeout = 30
KubeDefaultConfigPath = "/root/.kube/config"
)

func NewClient(kubeConfigPath string, k8sWrapTransport transport.WrapperFunc) (*kubernetes.Clientset, error) {
// use the current admin kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
if err != nil {
return nil, err
}
if k8sWrapTransport != nil {
config.WrapTransport = k8sWrapTransport
}
config.Timeout = time.Second * time.Duration(WrapTransportTimeout)
K8sClientSet, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return K8sClientSet, nil
}

func GetNodeList(k8sClient *kubernetes.Clientset) (*v1.NodeList, error) {
return k8sClient.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
}

func GetNode(k8sClient *kubernetes.Clientset, nodeName string) (*v1.Node, error) {
var listErr error
for retries := 0; retries < MaxRetries; retries++ {
nodes, err := GetNodeList(k8sClient)
if err != nil {
listErr = err
time.Sleep(time.Second * RetryInterval)
continue
}
// reset listErr back to nil
listErr = nil
for _, node := range nodes.Items {
if strings.ToLower(node.Labels[HostnameLabel]) == strings.ToLower(nodeName) {
return &node, nil
}
}
time.Sleep(time.Second * RetryInterval)
}
if listErr != nil {
return nil, listErr
}
return nil, apierrors.NewNotFound(schema.GroupResource{}, nodeName)
}

func GetNodeByLabel(k8sClient *kubernetes.Clientset, label string) ([]string, error) {
var listErr error
nodes, err := GetNodeList(k8sClient)
if err != nil {
return nil, listErr
}
var ns []string
for _, node := range nodes.Items {
if _, ok := node.Labels[label]; ok {
ns = append(ns, node.Name)
}
}
return ns, nil
}

func IsNodeReady(node v1.Node) bool {
nodeConditions := node.Status.Conditions
for _, condition := range nodeConditions {
if condition.Type == v1.NodeReady && condition.Status == v1.ConditionTrue {
return true
}
}
return false
}

0 comments on commit c6a026a

Please sign in to comment.