-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allocator draft #28
Merged
dgkanatsios
merged 6 commits into
PlayFab:master
from
onlyralphie:onlyralphie/allocator-tool
Oct 16, 2021
Merged
allocator draft #28
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5346b7c
allocator draft
rarandasoto 5ec7738
adding tls initialization
rarandasoto 6c41527
general cleanup
rarandasoto 41b4726
adding support for tls certs and no tls certs provided by user
rarandasoto 2166a09
adding README
rarandasoto 5d97c31
readme typos
rarandasoto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Thundernetes allocator tool | ||
|
||
Thanks for using the allocator tool for thundernetes. To use it: | ||
- `kubectl` is required to be in $PATH. for more information, please refer to the following [guide](https://kubernetes.io/docs/tasks/tools/#kubectl) | ||
- Compile the main.go file (optional to provide a meaningful name like allocator, thunderallocator or something similar). | ||
- Once you the executable, you can: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: "Once you run" |
||
- run the executable with no argument for some help and details | ||
- `list` which will provide the available servers. | ||
- `allocate <build-id> <session-id> [tls-public] [tls-private]` where the tls certificates are optional, but build and session ID are mandatory. Please note that providing the certs as env variables is also supported; if so, please name them TLS_PUBLIC for the cert file and TLS_PRIVATE for the key file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/playfab/thundernetes/tools/allocator | ||
|
||
go 1.16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
); | ||
|
||
type AllocationResult struct { | ||
IPV4Address string `json:"IPv4Address"` | ||
SessionID string `json:"SessionID"` | ||
}; | ||
|
||
var ( | ||
ip string | ||
certFile string | ||
keyFile string | ||
tlsSet bool | ||
ar *AllocationResult | ||
); | ||
|
||
func main () { | ||
args := os.Args | ||
|
||
if len(args) == 1 { | ||
fmt.Println("Usage of the allocator tool (is required to have"+ | ||
" kubectl on your $PATH)") | ||
fmt.Println("\t- allocate <build-id> <session-id> [tls-public] [tls-private]"+ | ||
" # Initialize a server with the given paramaters (if tls certs"+ | ||
" are not on the TLS_PUBLIC / TLS_PRIVATE env variables, please"+ | ||
onlyralphie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
" provide them via argument)") | ||
fmt.Println("\t- list # Returns the available Game Servers") | ||
} else if strings.Compare(args[1], "allocate") == 0 { | ||
fmt.Println("Beginning the allocate process") | ||
|
||
cmd := exec.Command("kubectl","get","svc","-n","thundernetes-system","thundernetes-controller-manager", | ||
onlyralphie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"-o","jsonpath='{.status.loadBalancer.ingress[0].ip}'") | ||
|
||
output, err := cmd.CombinedOutput() | ||
|
||
if err != nil { | ||
log.Println("Is required to have kubectl on your $PATH") | ||
onlyralphie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log.Fatal(string(output)) | ||
|
||
} | ||
|
||
if len(args) < 5 { // if no more arguments are provided | ||
if certFile == "" || keyFile == "" { // If the env vars are not set | ||
tlsSet = false | ||
} else { // the env vars are set | ||
tlsSet = true | ||
} | ||
} else { // all the arguments are provided | ||
tlsSet = true | ||
} | ||
|
||
if len(output) < 3 { // basically if we don't have a valid IP | ||
if tlsSet == true { | ||
ip = "https://127.0.0.1" | ||
cert, err := tls.LoadX509KeyPair(certFile, keyFile) | ||
ar, err = allocateTls(ip, args[2], args[3], cert) | ||
|
||
if err != nil { | ||
log.Panic(err) | ||
} | ||
} else { | ||
ip = "http://127.0.0.1" | ||
ar, err = allocateNoTls(ip, args[2], args[3]) | ||
|
||
if err != nil { | ||
log.Panic(err) | ||
} | ||
} | ||
} else { // if we retrieve the ip correctly | ||
ip = string(output) | ||
|
||
if tlsSet == true { | ||
cert, err := tls.LoadX509KeyPair(certFile, keyFile) | ||
ar, err = allocateTls(ip, args[2], args[3], cert) | ||
|
||
if err != nil { | ||
log.Panic(err) | ||
} | ||
} else { | ||
ar, err = allocateNoTls(ip, args[2], args[3]) | ||
|
||
if err != nil { | ||
log.Panic(err) | ||
} | ||
} | ||
} | ||
|
||
log.Println("IP address: "+ ar.IPV4Address+". Session ID: "+ar.SessionID); | ||
|
||
} else if strings.Compare(args[1], "list") == 0 { | ||
fmt.Println("Listing the available game servers") | ||
cmd := exec.Command("kubectl", "get", "gs") | ||
output, err := cmd.CombinedOutput() | ||
|
||
if err != nil { | ||
fmt.Println(string(output)) | ||
log.Fatal("Error while fetching the servers: ", err) | ||
log.Println("It is required to have kubectl on your $PATH") | ||
fmt.Println("Please, make sure you have your cluster configured properly") | ||
} | ||
|
||
fmt.Println(string(output)) | ||
} else { | ||
fmt.Println("Sorry, but the commad "+args[1]+" is not recognized") | ||
} | ||
|
||
fmt.Println("\nThanks for using the thundernetes allocator tool") | ||
|
||
} | ||
|
||
func allocateTls(ip string, buildID string, sessionID string, cert tls.Certificate) (*AllocationResult, error) { | ||
tlsConfig := &tls.Config{ | ||
InsecureSkipVerify: true, | ||
Certificates: []tls.Certificate{cert}, | ||
} | ||
|
||
transport := &http.Transport{TLSClientConfig: tlsConfig} | ||
client := &http.Client{Transport: transport} | ||
|
||
postBody, _ := json.Marshal(map[string]interface{}{ | ||
"buildID": buildID, | ||
"sessionID": sessionID, | ||
"sessionCookie": "coolRandomCookie", | ||
"initialPlayers": []string{"player1", "player2"}, | ||
}) | ||
|
||
postBodyBytes := bytes.NewBuffer(postBody) | ||
resp, err := client.Post(ip+":5000/api/v1/allocate", "application/json", postBodyBytes) | ||
|
||
//Handle Error | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("%d", resp.StatusCode) | ||
} | ||
|
||
//Read the response body | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ar := &AllocationResult{} | ||
json.Unmarshal(body, ar) | ||
|
||
if ar.IPV4Address == "" { | ||
return nil, fmt.Errorf("invalid IPV4Address %s", ar.IPV4Address) | ||
} | ||
|
||
if ar.SessionID != sessionID { | ||
return nil, fmt.Errorf("invalid SessionID %s", ar.SessionID) | ||
} | ||
|
||
return ar, nil | ||
} | ||
|
||
func allocateNoTls(ip string, buildID string, sessionID string) (*AllocationResult, error) { | ||
|
||
transport := &http.Transport{} | ||
client := &http.Client{Transport: transport} | ||
|
||
postBody, _ := json.Marshal(map[string]interface{}{ | ||
"buildID": buildID, | ||
"sessionID": sessionID, | ||
"sessionCookie": "coolRandomCookie", | ||
"initialPlayers": []string{"player1", "player2"}, | ||
}) | ||
|
||
postBodyBytes := bytes.NewBuffer(postBody) | ||
resp, err := client.Post(ip+":5000/api/v1/allocate", "application/json", postBodyBytes) | ||
|
||
//Handle Error | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("%d", resp.StatusCode) | ||
} | ||
|
||
//Read the response body | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ar := &AllocationResult{} | ||
json.Unmarshal(body, ar) | ||
|
||
if ar.IPV4Address == "" { | ||
return nil, fmt.Errorf("invalid IPV4Address %s", ar.IPV4Address) | ||
} | ||
|
||
if ar.SessionID != sessionID { | ||
return nil, fmt.Errorf("invalid SessionID %s", ar.SessionID) | ||
} | ||
|
||
return ar, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
go run main.go
to compile it