-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
268 lines (231 loc) · 5.48 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
package main
import (
"bytes"
"context"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"path/filepath"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
flag "github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"github.com/google/uuid"
)
const (
screenWidth = 1280
screenHeight = 640
balloonSpeed = 7
bulletSpeed = 15
)
type Balloon struct {
x, y float64
shooted int
id string
active bool
}
type Bullet struct {
x, y float64
active bool
}
type Game struct {
balloons []*Balloon
bullets []*Bullet
score int
}
var (
balloonImg *ebiten.Image
bulletImg *ebiten.Image
idMap []string
nodeList []string
)
type Node struct {
Name string
Status string
Roles string
Age string
Version string
}
func HomeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return ""
}
func getKubeConfig() *string{
var kubeConfig *string
if home := HomeDir(); home != "" {
kubeConfig = flag.String("kubeconfig", filepath.Join(home, ".kube","config"), "")
} else {
kubeConfig = flag.String("kubeconfig", "", "")
}
return kubeConfig
}
func ClientSetup() (*kubernetes.Clientset, error) {
var kubeConfig *string
if flag.Lookup("kubeconfig") == nil {
kubeConfig = getKubeConfig()
config, err := clientcmd.BuildConfigFromFlags("", *kubeConfig)
if err != nil {
fmt.Printf("Error in new client config: %s\n", err)
return nil,err
}
clientset := kubernetes.NewForConfigOrDie(config)
return clientset, nil
}
config, err := clientcmd.BuildConfigFromFlags("", flag.Lookup("kubeconfig").Value.String())
if err != nil {
fmt.Printf("Error in new client config: %s\n", err)
return nil,err
}
clientset := kubernetes.NewForConfigOrDie(config)
return clientset, nil
}
func GetNodeNames() ([]string, error) {
clientSet, err := ClientSetup()
if err != nil {
fmt.Println("error in creating client set: ", err)
return nil, err
}
nodes, err := clientSet.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
if err != nil {
fmt.Println("error in getting the list of nodes: ", err)
return nil, err
}
for _, n := range nodes.Items {
nodeList = append(nodeList, n.Name)
}
return nodeList, nil
}
func CreatePod(nodeName string) error{
id := uuid.New().String()
pod := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("nginx-%s",id),
Namespace: "default",
},
Spec: corev1.PodSpec{
RestartPolicy: "OnFailure",
Containers: []corev1.Container{
{
Name: "nginx",
Image: "nginx:latest",
},
},
NodeName: nodeName,
},
}
clientSet, err := ClientSetup()
if err != nil {
fmt.Println("error in creating client set: ", err)
return err
}
if _, err := clientSet.CoreV1().Pods("default").Create(context.Background(), &pod, metav1.CreateOptions{}); err != nil {
fmt.Printf("error in creating pod on node %s\n: ", nodeName)
return err
}
return nil
}
func (g *Game) Update() error {
nodeList, err := GetNodeNames()
if err != nil {
fmt.Println("error in getting node list: ", err)
os.Exit(1)
}
for _, b := range g.balloons {
if b.active {
b.y -= balloonSpeed
}
}
for i := 0; i < len(g.bullets); i++ {
g.bullets[i].y -= bulletSpeed
if g.bullets[i].y < 0 {
g.bullets = append(g.bullets[:i], g.bullets[i+1:]...)
i--
}
}
for _, b := range g.bullets {
for _, b2 := range g.balloons {
if b2.active && b.x >= b2.x && b.x <= b2.x+50 && b.y <= b2.y+50 {
b.active = false
b2.active = false
b2.shooted = 1
b2.id = nodeList[rand.Intn(len(nodeList))]
if err := CreatePod(b2.id); err != nil {
fmt.Printf("error in creating pod on the node %s\n: %v", b2.id, err)
os.Exit(1)
}
g.score++
}
}
}
if rand.Float64() < 0.05 {
for _, b := range g.balloons {
if !b.active {
b.active = true
b.x = rand.Float64() * (screenWidth - 50)
b.y = screenHeight
break
}
}
}
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
x, y := ebiten.CursorPosition()
g.bullets = append(g.bullets, &Bullet{x: float64(x), y: float64(y)})
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
for _, bullet := range g.bullets {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(bullet.x, bullet.y)
screen.DrawImage(bulletImg, op)
}
for _, balloon := range g.balloons {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(balloon.x, balloon.y)
screen.DrawImage(balloonImg, op)
}
for _, balloon := range g.balloons {
if balloon.shooted == 1 {
idMap = append(idMap, balloon.id)
}
balloon.shooted = 0
}
ebitenutil.DebugPrint(screen, fmt.Sprintf("Score: %d", g.score))
if len(idMap) > 0 {
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Pods scheduled on node %s", idMap[len(idMap)-1]), 0, 50)
}
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Balloon Shooter")
var err error
balloonImg, _, err = ebitenutil.NewImageFromFile("balloon.png")
if err != nil {
log.Fatal(err)
}
bulletImg, _, err = ebitenutil.NewImageFromFile("bullet.png")
if err != nil {
log.Fatal(err)
}
game := &Game{
balloons: make([]*Balloon, 60),
bullets: make([]*Bullet, 60),
}
for i := 0; i < 60; i++ {
game.balloons[i] = &Balloon{}
game.bullets[i] = &Bullet{}
}
if err := ebiten.RunGame(game); err != nil {
panic(err)
}
}