-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (78 loc) · 2.38 KB
/
main.go
File metadata and controls
89 lines (78 loc) · 2.38 KB
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
package main
// exemple of usage :
// go run main.go -c ~/.kube/config -k pod -n etcd0 -ns default
import (
"errors"
"flag"
"fmt"
"kube_helper/model"
"time"
"k8s.io/client-go/kubernetes"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
var (
kubeconfig = flag.String("c", "./config", "absolute path to the kubeconfig file")
resourceKind = flag.String("k", "rc", "resource kind name : pod, svc, rc, petset ...")
resourceName = flag.String("n", "mysuperpod", "resource name e.g. pod name, rc name ...")
namespace = flag.String("ns", "namespace", "namespace of the resource")
maxwait = flag.Int("t", 15, "wait for status in seconds")
)
func waitResource(resource model.KubeResource, clientSet clientset.Interface) (bool, error) {
timeout := time.After(time.Duration(*maxwait) * time.Second)
tick := time.Tick(2 * time.Second)
fmt.Printf("Waiting for resource")
for {
select {
case <-timeout:
msg := fmt.Sprintf("Timeout after %d seconds", *maxwait)
return false, errors.New(msg)
case <-tick:
ok, err := resource.CheckState(clientSet)
fmt.Printf(".")
if err != nil {
fmt.Printf("\n")
return false, err
} else if ok {
fmt.Printf("\n")
return true, nil
}
}
}
}
func makeInstance(kind string, resourceName string, namespace string) interface{} {
switch kind {
case "pod":
return model.Pod{Namespace: namespace, Name: resourceName}
case "rc":
return model.ReplicationController{Namespace: namespace, Name: resourceName}
case "svc":
return model.Service{Namespace: namespace, Name: resourceName}
case "job":
return model.Job{Namespace: namespace, Name: resourceName}
case "statefulset":
return model.StatefulSet{Namespace: namespace, Name: resourceName}
default:
fmt.Println(fmt.Errorf("Don't know what to do with kind : %s", kind))
return nil
}
}
func main() {
flag.Parse()
fmt.Println("Resource Kind is :", *resourceKind)
fmt.Println("Resource Name is : ", *resourceName)
fmt.Println("Namespace is : ", *namespace)
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
kubeResource := makeInstance(*resourceKind, *resourceName, *namespace).(model.KubeResource)
_, podErr := waitResource(kubeResource, clientset)
if podErr != nil {
panic(podErr.Error())
}
}