-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
196 lines (173 loc) · 5.64 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
package main
import (
"fmt"
"os"
"log"
"strings"
"time"
"net/http"
"encoding/json"
"io/ioutil"
"github.com/gorilla/mux"
guuid "github.com/google/uuid"
agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
"agones.dev/agones/pkg/client/clientset/versioned"
"agones.dev/agones/pkg/util/runtime"
"k8s.io/apimachinery/pkg/labels"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
)
// Store client token
var clientToken map[string]string
// Knowrob server info
type KRClient struct {
Port int `json:"KRServerPort"`
Protocol string `json:"KRProtocol"`
LevelName string `json:"LevelName"`
}
func getAllGameServers(w http.ResponseWriter, r *http.Request) {
// Check if token for the client already exists
var clientIP string
if r.RemoteAddr[:strings.Index(r.RemoteAddr, ":")] == "127.0.0.1" {
hostIP := os.Getenv("HOST")
clientIP = hostIP
} else {
clientIP = r.RemoteAddr[:strings.Index(r.RemoteAddr, ":")]
}
if _, ok := clientToken[clientIP]; !ok {
fmt.Fprintf(w, "No server available\n")
}
config, err := rest.InClusterConfig()
logger := runtime.NewLoggerWithSource("main")
if err != nil {
logger.WithError(err).Fatal("Could not create in cluster config")
}
// Access to the Agones resources through the Agones Clientset
// Note that we reuse the same config as we used for the Kubernetes Clientset
agonesClient, err := versioned.NewForConfig(config)
if err != nil {
fmt.Fprintf(w, fmt.Sprintf("Could not create the agones api clientset: %v\n", err))
}
selectorSet := make(map[string]string)
selectorSet["client"] = clientToken[clientIP]
labelSelector := labels.SelectorFromSet(selectorSet)
options := metav1.ListOptions{ LabelSelector: labelSelector.String()}
gsList, err := agonesClient.AgonesV1().GameServers("default").List(options)
var ipList string
for _, gs := range gsList.Items {
ipList += gs.Status.Address + ":" + fmt.Sprint(int64(gs.Status.Ports[0].Port)) + "\n"
}
fmt.Fprintf(w, ipList)
}
//
func createGameServer(w http.ResponseWriter, r *http.Request) {
mongoIP := os.Getenv("MONGO_IP")
mongoPort := os.Getenv("MONGO_PORT")
imageRepo := os.Getenv("IMAGE_REPO")
// Check if token for the client already exists
var clientIP string
if r.RemoteAddr[:strings.Index(r.RemoteAddr, ":")] == "127.0.0.1" {
hostIP := os.Getenv("HOST")
clientIP = hostIP
} else {
clientIP = r.RemoteAddr[:strings.Index(r.RemoteAddr, ":")]
}
reqBody, _ := ioutil.ReadAll(r.Body)
var krClient KRClient
json.Unmarshal(reqBody, &krClient)
// Check if token for the client already exists
if _, ok := clientToken[clientIP]; !ok {
clientToken[clientIP] = guuid.New().String()
}
config, err := rest.InClusterConfig()
logger := runtime.NewLoggerWithSource("main")
if err != nil {
logger.WithError(err).Fatal("Could not create in cluster config")
}
// Access to the Agones resources through the Agones Clientset
// Note that we reuse the same config as we used for the Kubernetes Clientset
agonesClient, err := versioned.NewForConfig(config)
if err != nil {
fmt.Fprintf(w, fmt.Sprintf("Could not create the agones api clientset: %v\n", err))
}
// // Create a GameServer
gs := &agonesv1.GameServer{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "ue-gs-", Namespace: "default",
Labels: map[string]string { "client" : clientToken[clientIP] },
},
Spec: agonesv1.GameServerSpec{
Container: "server",
Ports: []agonesv1.GameServerPort{
{
ContainerPort: 80,
Name: "default",
PortPolicy: agonesv1.Dynamic,
Protocol: corev1.ProtocolTCP,
},
},
Health: agonesv1.Health{
InitialDelaySeconds : 120,
PeriodSeconds : 30,
},
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "server", Image: imageRepo+"/ue-signal-srv",
},
{
Name: "proxy", Image: imageRepo+"/ue-proxy",
},
{
Name: "ue-app", Image: imageRepo+"/"+strings.ToLower(krClient.LevelName),
Command: []string{"/home/ue4/project/RobCoG.sh"},
Args: []string{"-AudioMixer", "-opengl4", "-NvEncFrameRateNum=1", "-KRServerIP="+clientIP, "KRServerPort="+fmt.Sprint(int64(krClient.Port)), "KRProtocol="+krClient.Protocol, "MongoServerIP="+mongoIP, "MongoServerPort="+mongoPort},
},
},
},
},
},
}
fmt.Println("-KRServerIP="+clientIP+":"+fmt.Sprint(int64(krClient.Port)))
newGS, err := agonesClient.AgonesV1().GameServers("default").Create(gs)
if err != nil {
fmt.Fprintf(w, "Unable to create GameServer: %v\n", err)
}
name := newGS.ObjectMeta.Name
options := metav1.GetOptions{}
for {
gs, err := agonesClient.AgonesV1().GameServers("default").Get(name, options)
if err != nil {
fmt.Fprintf(w, fmt.Sprintf("Error updating gameserver: %v\n", err))
return
}
switch gs.Status.State {
case "Scheduled", "Ready", "Allocated":
fmt.Fprintf(w, gs.Status.Address + ":" + fmt.Sprint(int64(gs.Status.Ports[0].Port)) + "\n")
return
case "Error", "Unhealthy", "Shutdown":
fmt.Fprintf(w, "Error creating gameserver.\n")
return
default:
time.Sleep(time.Second * 5)
}
}
}
func main() {
// port := "9090"
// if fromEnv := os.Getenv("PORT"); fromEnv != "" {
// port = fromEnv
// }
port := os.Getenv("PORT")
clientToken = make(map[string]string)
// register hello function to handle all requests
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/game-server", createGameServer).Methods("POST")
router.HandleFunc("/game-servers", getAllGameServers)
// start the web server on port and accept requests
log.Printf("Server listening on port %s", port)
err := http.ListenAndServe(":"+port, router)
log.Fatal(err)
}