This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
429 lines (370 loc) · 9.3 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package main
import (
"bufio"
"bytes"
"context"
"errors"
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/biosugar0/tele/params"
"github.com/biosugar0/tele/pkg/util"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
const (
// PodTerminating means that the pod is being terminated by the system.
PodTerminating v1.PodPhase = "Terminating"
)
var (
rootCmd = &cobra.Command{
Example: "tele --port 8080:8080 go run main.go",
DisableFlagsInUseLine: true,
Version: params.Version,
Use: `tele [flags] <shell command>`,
Short: "simple Telepresence wrapper tool for development microservices",
Long: `A simple Telepresence wrapper tool for microservice development.
Find more information at: https://github.com/biosugar0/tele
`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires a command string. example: tele go run main.go")
}
return nil
},
RunE: Run,
}
)
type k8sClient struct {
Client *kubernetes.Clientset
ResourceName *string
NameSpace string
}
type Resource struct {
NameSpace string
Deployment *string
}
var (
resourceStore = Resource{}
)
var stopSignalReceived = make(chan bool, 1)
var completeCommand = make(chan bool, 1)
func init() {
rootCmd.SetOutput(os.Stdout)
}
func execute(cmdstr string) (string, error) {
cmd := exec.Command("bash", "-c", cmdstr)
cmd.Env = os.Environ()
var sout bytes.Buffer
var serr bytes.Buffer
cmd.Stdout = &sout
cmd.Stderr = &serr
err := cmd.Run()
if err != nil {
return "", errors.New(serr.String())
}
result := strings.TrimRight(sout.String(), "\n")
return result, nil
}
func executeStream(cmdstr string) error {
cmd := exec.Command("bash", "-c", cmdstr)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
cmd.Env = os.Environ()
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
err = cmd.Start()
if err != nil {
return err
}
streamReader := func(scanner *bufio.Scanner, output chan string, done chan bool) {
defer close(output)
defer close(done)
for scanner.Scan() {
output <- scanner.Text()
}
done <- true
}
stdoutScanner := bufio.NewScanner(stdout)
stdoutOutputChan := make(chan string)
stdoutDoneChan := make(chan bool)
stderrScanner := bufio.NewScanner(stderr)
stderrOutputChan := make(chan string)
stderrDoneChan := make(chan bool)
go streamReader(stdoutScanner, stdoutOutputChan, stdoutDoneChan)
go streamReader(stderrScanner, stderrOutputChan, stderrDoneChan)
go func() {
for range stopSignalReceived {
if cmd.ProcessState == nil || !cmd.ProcessState.Exited() {
fmt.Println("\n[tele]: shut down command")
if e := cmd.Process.Signal(os.Interrupt); e != nil {
fmt.Printf("err: %s", e)
err = e
}
}
completeCommand <- false
}
}()
go func() {
for {
if cmd.ProcessState != nil {
if cmd.ProcessState.Exited() {
completeCommand <- true
return
}
}
}
}()
go func() {
runnning := true
for runnning {
select {
case <-stdoutDoneChan:
runnning = false
case line := <-stdoutOutputChan:
fmt.Println(line)
case line := <-stderrOutputChan:
fmt.Println(line)
}
}
}()
if e := cmd.Wait(); e != nil {
fmt.Printf("err: %s", e)
err = e
}
if err != nil {
return err
}
return nil
}
func Run(cmd *cobra.Command, args []string) error {
repository, err := execute("git rev-parse --show-toplevel")
if err != nil {
return err
}
repo := filepath.Base(repository)
cmd.Printf("[repository]:\n %s\n", repo)
branch, err := execute(`git rev-parse --abbrev-ref @`)
if err != nil {
return err
}
cmd.Printf("[branch]:\n %s\n", branch)
user := params.User
cmd.Printf("[user]:\n %s\n", user)
namespace := params.NameSpace
cmd.Printf("[namespace]:\n %s\n", namespace)
port := params.ServerPort
cmd.Printf("[port]:\n %s\n", port)
deployment := strings.Join([]string{
user,
repo,
branch,
}, "-")
deployment = util.ToValidName(deployment)
cmd.Printf("[deployment]:\n %s\n", deployment)
cmdArgs := []string{}
for _, v := range args {
v := util.SpecialStr(v)
cmdArgs = append(cmdArgs, v)
}
run := strings.Join(cmdArgs, " ")
cmd.Printf("[request command]:\n %s\n", run)
telepresence := fmt.Sprintf(
"telepresence --namespace %s --method inject-tcp --new-deployment %s",
namespace,
deployment,
)
resourceStore.Deployment = &deployment
resourceStore.NameSpace = namespace
if len(port) > 0 {
telepresence += fmt.Sprintf(" --expose %s", port)
}
telepresence += fmt.Sprintf(
" --run bash -c \"%s\"",
run,
)
if params.Sudo {
telepresence = fmt.Sprintf("sudo %s", telepresence)
}
cmd.Printf("[Telepreesence command]:\n %s\n", telepresence)
cmd.Printf("[result]:\n")
err = executeStream(telepresence)
if err != nil {
return err
}
complete := <-completeCommand
cleanup(resourceStore)
switch complete {
case true:
fmt.Println("\n[tele]: complete")
default:
fmt.Println("\n[tele]: command killed")
}
return nil
}
func newClient(name *string) k8sClient {
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
fmt.Println(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
fmt.Println(err.Error())
}
return k8sClient{
Client: clientset,
ResourceName: name,
NameSpace: params.NameSpace,
}
}
func (r *k8sClient) cleanService() error {
if r.ResourceName == nil {
return nil
}
service, err := r.Client.CoreV1().Services(r.NameSpace).Get(context.TODO(), *r.ResourceName, metav1.GetOptions{})
if err != nil {
if k8sErrors.IsNotFound(err) {
return nil
}
return err
}
if service != nil {
err = r.Client.CoreV1().Services(r.NameSpace).Delete(context.TODO(), *r.ResourceName, metav1.DeleteOptions{})
if err != nil {
return err
}
fmt.Println("service has been deleted")
}
return nil
}
func (r *k8sClient) podPhase() *v1.PodPhase {
pod, err := r.Client.CoreV1().Pods(r.NameSpace).Get(context.TODO(), *r.ResourceName, metav1.GetOptions{})
if err != nil {
if k8sErrors.IsNotFound(err) {
return nil
}
fmt.Println(err.Error())
return nil
}
if pod != nil {
st := pod.Status
if pod.ObjectMeta.DeletionTimestamp != nil {
v := PodTerminating
return &v
}
return &st.Phase
}
fmt.Printf("%v:%v", pod.Name, pod.Status.Phase)
return nil
}
func (r *k8sClient) cleanPod() error {
time.Sleep(1 * time.Second)
pods, err := r.Client.CoreV1().Pods(r.NameSpace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
if pods != nil {
podlist := *pods
terminating := 0
for _, v := range podlist.Items {
if v.Name == *r.ResourceName {
runningTime := 0
for {
finish := false
time.Sleep(1 * time.Second)
runningTime++
switch phase := r.podPhase(); {
case phase == nil:
fmt.Println("pod has been terminated")
finish = true
case *phase == PodTerminating:
terminating++
if terminating == 1 {
fmt.Println("waiting treminate pod...")
}
case *phase == v1.PodPending:
err = r.Client.CoreV1().Pods(r.NameSpace).Delete(context.TODO(), *r.ResourceName, metav1.DeleteOptions{})
if err != nil {
fmt.Println(err.Error())
}
finish = true
case *phase == v1.PodRunning:
if runningTime > 3 {
err = r.Client.CoreV1().Pods(r.NameSpace).Delete(context.TODO(), *r.ResourceName, metav1.DeleteOptions{})
if err != nil {
fmt.Println(err.Error())
}
}
default:
finish = true
}
if finish {
break
}
}
if err != nil {
return err
}
}
}
}
return nil
}
func cleanup(resource Resource) {
fmt.Println("tele resource clening")
client := newClient(resource.Deployment)
if resource.Deployment != nil {
err := client.cleanPod()
if err != nil {
fmt.Println(err.Error())
}
err = client.cleanService()
if err != nil {
fmt.Println(err.Error())
}
}
}
func main() {
homedir := filepath.Base(os.Getenv("HOME"))
rootCmd.Flags().SetInterspersed(false)
rootCmd.PersistentFlags().SortFlags = false
rootCmd.PersistentFlags().StringVar(¶ms.ServerPort, "port", "", "expose http server port")
rootCmd.PersistentFlags().StringVar(¶ms.User, "user", homedir, "user name for prefix of deployment name. default is home directory name")
rootCmd.PersistentFlags().StringVar(¶ms.NameSpace, "namespace", "default", "name space of kubernetes")
rootCmd.PersistentFlags().BoolVar(¶ms.Sudo, "sudo", false, "execute commands as a super user")
go func() {
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
stopSignalReceived <- true
}()
if err := rootCmd.Execute(); err != nil {
rootCmd.SetOutput(os.Stderr)
rootCmd.Println(err)
os.Exit(1)
}
}