Skip to content

Commit

Permalink
Configurable Allocation API Port (#419)
Browse files Browse the repository at this point in the history
* Modified Files to allow for port as env variable

* Separated listening ports into int and str

* Modified Files to allow for port as env variable

* Separated listening ports into int and str

* Rebuild Installfiles and adjusted default port

* Changes to get e2e tests to have variable port

* Set default back to 5000

* Fix typo

* Changed name of vars, replaced shell cmds

* Remove dynamic port from unit tests and makefile

* Fix camel case, make const for test

* Adjust e2e test for new alloc api utility

* md for Changing port

* Added fmt

* Commiting modified installfile

* Move allocation port to suite_test

* Updated Doc

* Made api port come from suite_test

* Fixed controller suite_test

* Adjusted controller and doc
  • Loading branch information
nottagg authored Oct 10, 2022
1 parent 2777c30 commit d215d8e
Show file tree
Hide file tree
Showing 14 changed files with 32,245 additions and 7,565 deletions.
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
30 changes: 25 additions & 5 deletions cmd/e2e/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package main

import (
"context"
"os"
"strconv"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
)

var (
img string
certFile string
keyFile string
fakeCertFile string
fakeKeyFile string
img string
certFile string
keyFile string
fakeCertFile string
fakeKeyFile string
allocationApiSvcPort string
)

func TestEndToEnd(t *testing.T) {
Expand All @@ -34,4 +40,18 @@ var _ = BeforeSuite(func() {
Expect(fakeCertFile).ToNot(BeEmpty())
fakeKeyFile = os.Getenv("FAKE_TLS_PRIVATE")
Expect(fakeKeyFile).ToNot(BeEmpty())
allocationApiSvcPort = GetAllocationApiSvcPort()
Expect(allocationApiSvcPort).ToNot(BeEmpty())
})

// Function to pull allocation api svc port from the controller
func GetAllocationApiSvcPort() string {
kubeConfig := ctrl.GetConfigOrDie()
kubeClient, err := createKubeClient(kubeConfig)
Expect(err).ToNot(HaveOccurred())
ctx := context.Background()
svc := corev1.Service{}
err = kubeClient.Get(ctx, types.NamespacedName{Namespace: "thundernetes-system", Name: "thundernetes-controller-manager"}, &svc)
Expect(err).ToNot(HaveOccurred())
return strconv.Itoa(int(svc.Spec.Ports[0].Port))
}
2 changes: 1 addition & 1 deletion cmd/e2e/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func allocate(buildID, sessionID string, cert tls.Certificate) error {
"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
50 changes: 50 additions & 0 deletions docs/troubleshooting/defaultportinuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
layout: default
title: Port 5000 is already in use
parent: Troubleshooting
nav_order: 5
---

# How can I change the port that Thundernetes uses?

By default, Thundernetes's Allocation API listens on port 5000 which is opened with the kind config set-up [here](../quickstart/installing-kind.md). This port can already be in use by another service thus causing Thundernetes to fail.

## Kind Changes
The first step (if using kind) is changing the `kind-config.yaml` to use the port desired. For example:

{% include code-block-start.md %}
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
extraPortMappings:
- containerPort: 5000
hostPort: 5000
listenAddress: "0.0.0.0"
protocol: tcp
- containerPort: 10000
hostPort: 10000
listenAddress: "0.0.0.0"
protocol: tcp
- containerPort: 10001
hostPort: 10001
listenAddress: "0.0.0.0"
protocol: tcp
{% include code-block-end.md %}

## YAML Changes
The necessary YAML changes are found within the `manager.yaml` file. A find and replace of `5000` with `{DESIRED_PORT}` will change where the Allocation API listens. In total, there are 5 instances.

Once this file is modified, you can generate new installfiles with `make create-install-files` and verify your changes in `operator.yaml`

### Development - End to end tests
End to end tests also run and listen on port 5000. Once you complete the above yaml change, you also need to modify `e2e/kind-config.yaml` to listen on your desired port. The other needed change is modifying allocationApiSvcPort in `pkg/operator/controllers/sute_test.go`

## Verify changes

Once these changes are made and thundernetes is running, you can verify the port within the logs using the following:
`kubectl -n thundernetes-system logs {thundernetes-controller-manager} | grep addr`

Resulting in the following output:
`2022-10-07T17:01:07Z INFO allocation-api serving allocation API service {"addr": ":5005", "port": 5005}`
Loading

0 comments on commit d215d8e

Please sign in to comment.