Skip to content
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

Configurable Allocation API Port #419

Merged
merged 28 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2a6cbae
Modified Files to allow for port as env variable
nottagg Sep 23, 2022
9eae5ff
Separated listening ports into int and str
nottagg Sep 26, 2022
563e001
Modified Files to allow for port as env variable
nottagg Sep 23, 2022
96c33a1
Separated listening ports into int and str
nottagg Sep 26, 2022
4b800a7
Merge branch 'AllocationAPIPorts' of https://github.com/nottagg/thund…
nottagg Sep 26, 2022
6e10601
Merge branch 'PlayFab:main' into AllocationAPIPorts
nottagg Sep 26, 2022
73b5a1e
Rebuild Installfiles and adjusted default port
nottagg Sep 26, 2022
9e8cd55
Merge branch 'AllocationAPIPorts' of https://github.com/nottagg/thund…
nottagg Sep 26, 2022
70d0ef8
Changes to get e2e tests to have variable port
nottagg Sep 27, 2022
678c983
Merge branch 'PlayFab:main' into AllocationAPIPorts
nottagg Sep 27, 2022
d1bea18
Set default back to 5000
nottagg Sep 27, 2022
4a0fb7f
Fix typo
nottagg Sep 27, 2022
63aeaba
Merge branch 'main' into AllocationAPIPorts
nottagg Sep 30, 2022
bf2aa64
Changed name of vars, replaced shell cmds
nottagg Oct 3, 2022
8667ca5
Merge branch 'AllocationAPIPorts' of https://github.com/nottagg/thund…
nottagg Oct 3, 2022
a947b34
Remove dynamic port from unit tests and makefile
nottagg Oct 4, 2022
f4b54e2
Merge branch 'main' into AllocationAPIPorts
nottagg Oct 4, 2022
6bc5fcc
Fix camel case, make const for test
nottagg Oct 4, 2022
bc250e1
Adjust e2e test for new alloc api utility
nottagg Oct 7, 2022
273bd52
md for Changing port
nottagg Oct 7, 2022
186ae19
Merge branch 'PlayFab:main' into AllocationAPIPorts
nottagg Oct 7, 2022
4447aa4
Added fmt
nottagg Oct 7, 2022
0b279f6
Commiting modified installfile
nottagg Oct 7, 2022
55779a8
Move allocation port to suite_test
nottagg Oct 10, 2022
184c478
Updated Doc
nottagg Oct 10, 2022
360f2ff
Made api port come from suite_test
nottagg Oct 10, 2022
8b24c02
Fixed controller suite_test
nottagg Oct 10, 2022
8d7c44b
Adjusted controller and doc
nottagg Oct 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions cmd/allocator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ type AllocationResult struct {
}

var (
ip string
certFile string
keyFile string
tlsSet bool
ar *AllocationResult
ip string
certFile string
keyFile string
tlsSet bool
allocationAPISVCPort string
ar *AllocationResult
)

func main() {
Expand All @@ -40,15 +41,16 @@ func main() {
} else if strings.Compare(args[1], "allocate") == 0 {
fmt.Println("Beginning the allocate process")

//pulls ip and port into output
cmd := exec.Command("kubectl", "get", "svc", "-n", "thundernetes-system", "thundernetes-controller-manager",
"-o", "jsonpath='{.status.loadBalancer.ingress[0].ip}'")
"-o", "jsonpath={.status.loadBalancer.ingress[0].ip},{.spec.ports[0].port}")

output, err := cmd.CombinedOutput()

//if there's an error it's likely because no path
if err != nil {
log.Println("Is required to have kubectl on your $PATH")
log.Fatal(string(output))

}

if len(args) < 5 { // if no more arguments are provided
Expand All @@ -61,7 +63,18 @@ func main() {
tlsSet = true
}

if len(output) < 3 { // basically if we don't have a valid IP
//Read output until comma for ip
var s strings.Builder
for i := 0; i < len(output); i++ {
if output[i] == ',' {
ip = s.String()
s.Reset()
}
s.WriteString(string(output[i]))
}
allocationAPISVCPort = s.String()

if len(ip) < 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)
Expand Down Expand Up @@ -137,7 +150,7 @@ func allocateTls(ip string, buildID string, sessionID string, cert tls.Certifica
})

postBodyBytes := bytes.NewBuffer(postBody)
resp, err := client.Post(ip+":5000/api/v1/allocate", "application/json", postBodyBytes)
resp, err := client.Post(ip+":"+allocationAPISVCPort+"/api/v1/allocate", "application/json", postBodyBytes)

//Handle Error
if err != nil {
Expand Down Expand Up @@ -182,7 +195,7 @@ func allocateNoTls(ip string, buildID string, sessionID string) (*AllocationResu
})

postBodyBytes := bytes.NewBuffer(postBody)
resp, err := client.Post(ip+":5000/api/v1/allocate", "application/json", postBodyBytes)
resp, err := client.Post(ip+":"+allocationAPISVCPort+"/api/v1/allocate", "application/json", postBodyBytes)

//Handle Error
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion cmd/e2e/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
)

var connectedPlayers = []string{"Amie", "Ken", "Dimitris"} // this should the same as in the netcore sample
var allocationAPISVCPort string
nottagg marked this conversation as resolved.
Show resolved Hide resolved

const (
testNamespace = "e2e"
Expand Down Expand Up @@ -269,14 +270,18 @@ func allocate(buildID, sessionID string, cert tls.Certificate) error {
transport := &http.Transport{TLSClientConfig: tlsConfig}
client := &http.Client{Transport: transport}

//Grabs the port from the running service
svc := corev1.Service{}
allocationAPISVCPort = string(svc.Spec.Ports[0].Port)

postBody, _ := json.Marshal(map[string]interface{}{
"buildID": buildID,
"sessionID": sessionID,
"sessionCookie": "randomCookie",
"initialPlayers": []string{"player1", "player2"},
})
postBodyBytes := bytes.NewBuffer(postBody)
resp, err := client.Post("https://localhost:5000/api/v1/allocate", "application/json", postBodyBytes)
resp, err := client.Post("https://localhost:"+allocationAPISVCPort+"/api/v1/allocate", "application/json", postBodyBytes)
//Handle Error
if err != nil {
return err
Expand Down
9,768 changes: 7,927 additions & 1,841 deletions installfiles/operator.yaml

Large diffs are not rendered by default.

9,768 changes: 7,927 additions & 1,841 deletions installfiles/operator_with_monitoring.yaml

Large diffs are not rendered by default.

9,768 changes: 7,927 additions & 1,841 deletions installfiles/operator_with_security.yaml

Large diffs are not rendered by default.

9,768 changes: 7,927 additions & 1,841 deletions installfiles/operator_with_security_and_monitoring.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/operator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
KUSTOMIZE_VERSION ?= v4.5.7
CONTROLLER_TOOLS_VERSION ?= v0.9.0

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
Expand Down
2 changes: 2 additions & 0 deletions pkg/operator/config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ spec:
value: ${LOG_LEVEL}
- name: API_SERVICE_SECURITY
value: ${API_SERVICE_SECURITY}
- name: ALLOC_API_SVC_PORT
value: "5000"
- name: THUNDERNETES_INIT_CONTAINER_IMAGE
value: ${IMAGE_NAME_INIT_CONTAINER}:${IMAGE_TAG}
- name: THUNDERNETES_INIT_CONTAINER_IMAGE_WIN
Expand Down
24 changes: 12 additions & 12 deletions pkg/operator/controllers/allocation_api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
)

const (
// listeningPort is the port the API server will listen on
listeningPort = 5000
allocationTries = 3
// statusSessionId is the field name used to index GameServer objects by their session ID
statusSessionId string = "status.sessionID"
Expand All @@ -48,17 +46,19 @@ type AllocationApiServer struct {
gameServerQueue *GameServersQueue
// events is a buffered channel of GenericEvent
// it is used to re-enqueue GameServer objects that their allocation failed for whatever reason
events chan event.GenericEvent
logger logr.Logger
events chan event.GenericEvent
logger logr.Logger
listeningPort int32
}

func NewAllocationApiServer(crt, key []byte, cl client.Client) *AllocationApiServer {
func NewAllocationApiServer(crt, key []byte, cl client.Client, port int32) *AllocationApiServer {
return &AllocationApiServer{
CrtBytes: crt,
KeyBytes: key,
Client: cl,
events: make(chan event.GenericEvent, 100),
logger: log.Log.WithName("allocation-api"),
CrtBytes: crt,
KeyBytes: key,
Client: cl,
events: make(chan event.GenericEvent, 100),
logger: log.Log.WithName("allocation-api"),
listeningPort: port,
}
}

Expand All @@ -68,7 +68,7 @@ func NewAllocationApiServer(crt, key []byte, cl client.Client) *AllocationApiSer
func (s *AllocationApiServer) Start(ctx context.Context) error {
addr := os.Getenv("API_LISTEN")
if addr == "" {
addr = fmt.Sprintf(":%d", listeningPort)
addr = fmt.Sprintf(":%d", s.listeningPort)
}

// create the queue for game servers
Expand All @@ -77,7 +77,7 @@ func (s *AllocationApiServer) Start(ctx context.Context) error {
mux := http.NewServeMux()
mux.HandleFunc("/api/v1/allocate", s.handleAllocationRequest)

s.logger.Info("serving allocation API service", "addr", addr, "port", listeningPort)
s.logger.Info("serving allocation API service", "addr", addr, "port", s.listeningPort)

srv := &http.Server{
Addr: addr,
Expand Down
14 changes: 7 additions & 7 deletions pkg/operator/controllers/allocation_api_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var _ = Describe("allocation API service input validation tests", func() {
It("empty body should return error", func() {
req := httptest.NewRequest(http.MethodPost, "/api/v1/allocate", nil)
w := httptest.NewRecorder()
h := NewAllocationApiServer(nil, nil, nil)
h := NewAllocationApiServer(nil, nil, nil, int32(5000))
nottagg marked this conversation as resolved.
Show resolved Hide resolved
h.handleAllocationRequest(w, req)
res := w.Result()
defer res.Body.Close()
Expand All @@ -38,7 +38,7 @@ var _ = Describe("allocation API service input validation tests", func() {
It("GET method should return error", func() {
req := httptest.NewRequest(http.MethodGet, "/api/v1/allocate", nil)
w := httptest.NewRecorder()
h := NewAllocationApiServer(nil, nil, nil)
h := NewAllocationApiServer(nil, nil, nil, int32(5000))
h.handleAllocationRequest(w, req)
res := w.Result()
defer res.Body.Close()
Expand All @@ -49,7 +49,7 @@ var _ = Describe("allocation API service input validation tests", func() {
It("bad body should return error", func() {
req := httptest.NewRequest(http.MethodPost, "/api/v1/allocate", bytes.NewBufferString("{\"foo\":\"bar\"}"))
w := httptest.NewRecorder()
h := NewAllocationApiServer(nil, nil, nil)
h := NewAllocationApiServer(nil, nil, nil, int32(5000))
h.handleAllocationRequest(w, req)
res := w.Result()
defer res.Body.Close()
Expand All @@ -60,7 +60,7 @@ var _ = Describe("allocation API service input validation tests", func() {
It("buildID should be a GUID", func() {
req := httptest.NewRequest(http.MethodPost, "/api/v1/allocate", bytes.NewBufferString("{\"buildID\":\"NOT_A_GUID\",\"sessionID\":\"9bb3bbb2-5031-42fd-8982-5a3f76ef2c8a\"}"))
w := httptest.NewRecorder()
h := NewAllocationApiServer(nil, nil, nil)
h := NewAllocationApiServer(nil, nil, nil, int32(5000))
h.handleAllocationRequest(w, req)
res := w.Result()
defer res.Body.Close()
Expand All @@ -71,7 +71,7 @@ var _ = Describe("allocation API service input validation tests", func() {
It("should return NotFound on an empty list", func() {
req := httptest.NewRequest(http.MethodPost, "/api/v1/allocate", bytes.NewBufferString("{\"sessionID\":\"9bb3bbb2-5031-42fd-8982-5a3f76ef2c8a\",\"buildID\":\"9bb3bbb2-5031-42fd-8982-5a3f76ef2c8a\"}"))
w := httptest.NewRecorder()
h := NewAllocationApiServer(nil, nil, testNewSimpleK8sClient())
h := NewAllocationApiServer(nil, nil, testNewSimpleK8sClient(), int32(5000))
h.handleAllocationRequest(w, req)
res := w.Result()
defer res.Body.Close()
Expand All @@ -85,7 +85,7 @@ var _ = Describe("allocation API service input validation tests", func() {
Expect(err).ToNot(HaveOccurred())
req := httptest.NewRequest(http.MethodPost, "/api/v1/allocate", bytes.NewBufferString(fmt.Sprintf("{\"sessionID\":\"%s\",\"buildID\":\"%s\"}", sessionID1, buildID1)))
w := httptest.NewRecorder()
h := NewAllocationApiServer(nil, nil, client)
h := NewAllocationApiServer(nil, nil, client, int32(5000))
h.handleAllocationRequest(w, req)
res := w.Result()
defer res.Body.Close()
Expand All @@ -105,7 +105,7 @@ var _ = Describe("allocation API service input validation tests", func() {
Expect(err).ToNot(HaveOccurred())
req := httptest.NewRequest(http.MethodPost, "/api/v1/allocate", bytes.NewBufferString(fmt.Sprintf("{\"sessionID\":\"%s\",\"buildID\":\"%s\"}", sessionID1, buildID1)))
w := httptest.NewRecorder()
h := NewAllocationApiServer(nil, nil, client)
h := NewAllocationApiServer(nil, nil, client, int32(5000))
h.handleAllocationRequest(w, req)
res := w.Result()
defer res.Body.Close()
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ var _ = BeforeSuite(func() {
Expect(err).ToNot(HaveOccurred())

// allocation api service is a controller, so add it to the manager
testAllocationApiServer = NewAllocationApiServer(nil, nil, k8sManager.GetClient())
testAllocationApiServer = NewAllocationApiServer(nil, nil, k8sManager.GetClient(), 5000)
err = testAllocationApiServer.SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

Expand Down
4 changes: 3 additions & 1 deletion pkg/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Config struct {
LogLevel string `env:"LOG_LEVEL" envDefault:"info"`
MinPort int32 `env:"MIN_PORT" envDefault:"10000"`
MaxPort int32 `env:"MAX_PORT" envDefault:"12000"`
AllocationAPISVCPort int32 `env:"ALLOC_API_SVC_PORT" envDefault:"5000"`
InitContainerImageLinux string `env:"THUNDERNETES_INIT_CONTAINER_IMAGE,notEmpty"`
InitContainerImageWin string `env:"THUNDERNETES_INIT_CONTAINER_IMAGE_WIN,notEmpty"`
}
Expand All @@ -82,6 +83,7 @@ func main() {
if err := env.Parse(cfg); err != nil {
log.Fatal(err, "Cannot load configuration from environment variables")
}
allocationAPISVCPort := cfg.AllocationAPISVCPort

// load the rest of the configuration from command-line flags
var metricsAddr string
Expand Down Expand Up @@ -124,7 +126,7 @@ func main() {
crt, key := getCrtKeyIfTlsEnabled(k8sClient, cfg)

// initialize the allocation API service, which is also a controller. So we add it to the manager
aas := controllers.NewAllocationApiServer(crt, key, mgr.GetClient())
aas := controllers.NewAllocationApiServer(crt, key, mgr.GetClient(), int32(allocationAPISVCPort))
if err = aas.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create HTTP allocation API Server", "Allocation API Server", "HTTP Allocation API Server")
os.Exit(1)
Expand Down