/
kube.go
151 lines (115 loc) · 3.05 KB
/
kube.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
package main
import (
"container/list"
"fmt"
"github.com/fission/fission/pkg/crd"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"math/rand"
"os"
"sync"
"time"
)
type SyncList struct {
*list.List
sync.Mutex
}
func NewSyncList() *SyncList {
return &SyncList{
List: list.New(),
Mutex: sync.Mutex{},
}
}
func worker(s *SyncList, wg *sync.WaitGroup) {
fmt.Println("worker starting...")
for i := 0; i < 100; i++ {
s.Lock()
s.PushBack(rand.Intn(100))
s.Unlock()
}
wg.Done()
}
func getPod() *corev1.Pod{
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-busypod",
Namespace: "default",
Labels: map[string]string{
"app": "test",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "busybox",
Image: "busybox:1.28",
ImagePullPolicy: corev1.PullIfNotPresent,
Command: []string{
"sleep",
"300",
},
Ports: []corev1.ContainerPort{
{
Name: "ssh-busybox",
HostPort: 0,
ContainerPort: 22,
Protocol: "TCP",
},
},
},
},
},
}
}
func getJobPod(client *kubernetes.Clientset) {
pod, err := client.CoreV1().Pods("kubeml").List(metav1.ListOptions{})
if err != nil {panic(err)}
for _, p := range pod.Items{
fmt.Println(p.Name, p.Status.PodIP, p.Status.Phase)
}
}
func main() {
// The make fission client and so on needs the KUBECONFIG variable to be set
_ = os.Setenv("KUBECONFIG", "C:\\Users\\diego\\.kube\\config")
//// Access the kubernetes API directly from the code
//config, _ := clientcmd.BuildConfigFromFlags("", "C:\\Users\\diego\\.kube\\config")
//clienset, _ := kubernetes.NewForConfig(config)
//svcs, _ := clienset.CoreV1().Pods("fission-function").List(metav1.ListOptions{})
//for _, i := range svcs.Items {
// fmt.Println(i.Name)
//}
// Create the multiple clients from the fission stuff
_, kubeClient, _, err := crd.MakeFissionClient()
if err != nil {
panic(err)
}
getJobPod(kubeClient)
os.Exit(0)
pods, err := kubeClient.CoreV1().Pods("kubeml").List(metav1.ListOptions{})
if err != nil {panic(err)}
for _, p := range pods.Items {
fmt.Println(p.Spec.Containers[0].ReadinessProbe)
}
// try to create and delete a pod
busyPod := getPod()
podref, err := kubeClient.CoreV1().Pods(busyPod.Namespace).Create(busyPod)
if err != nil {panic(err)}
fmt.Println("Created pod", podref.Name)
// list the pods in the namespace
pods, err = kubeClient.CoreV1().Pods(busyPod.Namespace).List(metav1.ListOptions{})
if err != nil {panic(err)}
for _, p := range pods.Items{
fmt.Println(p.Name, p.Spec.Containers, p.Spec.Containers[0].Ports)
}
time.Sleep(30 * time.Second)
fmt.Println("Deleting pod...")
// delete the busybox pod
err = kubeClient.CoreV1().Pods(podref.Namespace).Delete(podref.Name, &metav1.DeleteOptions{})
if err != nil {panic(err)}
//
//env, _ := fissionClient.CoreV1().Functions("").List(metav1.ListOptions{})
//for _, e := range env.Items {
// fmt.Println(e.Name)
//}
}