/
cmd.go
136 lines (120 loc) · 3.3 KB
/
cmd.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
"github.com/spf13/cobra"
// "k8s.io/client-go/kubernetes"
// "k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
)
/* To use this kubectl-plugin
$ go build -o kubectl-migrate
$ sudo cp kubectl-migrate /usr/local/bin
*/
const (
example = `
# Checkpoint a Pod
kubectl migrate POD_NAME destHost
kubectl migrate POD_NAME --namespace string destHost
`
longDesc = `
migrate POD_NAME to destHost
`
)
type PodmigrationEndpoint struct {
client client.Client
}
type MigrateArgs struct {
// Pod select options
Namespace string
SourcePodName string
DestHost string
}
type Podmigration struct {
Name string `json:"name"`
DestHost string `json:"destHost"`
Replicas int `json:"replicas"`
// Selector *metav1.LabelSelector `json:"selector"`
Action string `json:"action"`
SnapshotPath string `json:"snapshotPath"`
SourcePod string `json:"sourcePod"`
// Template corev1.PodTemplateSpec `json:"template"`
}
func NewPluginCmd() *cobra.Command {
var Margs MigrateArgs
cmd := &cobra.Command{
Use: "migrate [OPTIONS] POD_NAME destHost",
Short: "migrate a Pod",
Long: longDesc,
Example: example,
Run: func(c *cobra.Command, args []string) {
if err := Margs.Complete(c, args); err != nil {
fmt.Println(err)
}
if err := Margs.Run(); err != nil {
fmt.Println(err)
}
},
}
cmd.Flags().StringVar(&Margs.Namespace, "namespace", "default",
"default namespace is \"default\"")
return cmd
}
func (a *MigrateArgs) Complete(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("error pod not specified")
}
if len(args) == 1 {
return fmt.Errorf("destHost not specified")
}
a.SourcePodName = args[0]
a.DestHost = args[1]
return nil
}
func (a *MigrateArgs) Run() error {
// ctx := context.Background()
//read the config file, so the plugin can talk to API-server
// config, _ := clientcmd.BuildConfigFromFlags("", "/home/dcn/fault-detection/docs/anisble-playbook/kubernetes-the-hard-way/admin.kubeconfig")
// clientset, _ := kubernetes.NewForConfig(config)
// pod, _ := clientset.CoreV1().Pods("default").Get(ctx, "pingpod-worker1", metav1.GetOptions{})
url := "http://localhost:5000/Podmigrations"
method := "POST"
crdName := a.getCrdName()
action := "live-migration"
sourcePod := a.SourcePodName
destHost := a.DestHost
data := fmt.Sprintf(`{"name": "%s","action": "%s","sourcePod": "%s","destHost":"%s"}`, crdName, action, sourcePod, destHost)
payload := strings.NewReader(data)
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
}
req.Header.Add("contentType", "application/json")
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
body, err := ioutil.ReadAll(res.Body)
fmt.Println("response Status:", res.Status)
fmt.Println(string(body))
fmt.Println("testing .............")
if err != nil {
return err
}
defer res.Body.Close()
return nil
}
func (a *MigrateArgs) getCrdName() string {
s1 := rand.NewSource(time.Now().UnixNano())
number := rand.New(s1)
crdName := a.SourcePodName + "-migration-controller-" + strconv.Itoa(number.Intn(100))
return crdName
}
func main() {
cmd := NewPluginCmd()
cmd.Execute()
}