forked from labring/sealos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf03646
commit c6a026a
Showing
4 changed files
with
230 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |