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

Add GatewayPort configuration #67

Merged
merged 8 commits into from Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -28,6 +28,7 @@ Screenshots from keynote / video - find out more over at https://www.openfaas.co
| `basic_auth` | When `true` basic auth is used to post any function statistics back to the gateway | `false` |
| `write_debug` | Print verbose logs | `false` |
| `faas_gateway_address` | Address of gateway DNS name | `gateway` |
| `faas_gateway_port` | Port of gateway service | `8080` |
| `faas_function_suffix` | When `gateway_invoke` is `false`, this suffix is used to contact a function, it may correspond to a Kubernetes namespace | `` |
| `faas_max_reconnect` | An integer of the amount of reconnection attempts when the NATS connection is lost | `120` |
| `faas_nats_address` | The DNS entry for NATS | `nats` |
Expand Down
4 changes: 2 additions & 2 deletions main.go
Expand Up @@ -37,7 +37,7 @@ func makeFunctionURL(req *queue.Request, config *QueueWorkerConfig, path, queryS
qs)

if config.GatewayInvoke {
functionURL = fmt.Sprintf("http://%s:8080/function/%s%s%s",
functionURL = fmt.Sprintf("http://%s/function/%s%s%s",
config.GatewayAddress,
strings.Trim(req.Function, "/"),
pathVal,
Expand Down Expand Up @@ -317,7 +317,7 @@ func postReport(client *http.Client, function string, statusCode int, timeTaken
TimeTaken: timeTaken,
}

targetPostback := "http://" + gatewayAddress + ":8080/system/async-report"
targetPostback := fmt.Sprintf("http://%s/system/async-report", gatewayAddress)
reqBytes, _ := json.Marshal(req)
request, err := http.NewRequest(http.MethodPost, targetPostback, bytes.NewReader(reqBytes))

Expand Down
4 changes: 4 additions & 0 deletions main_test.go
Expand Up @@ -11,6 +11,7 @@ func Test_makeFunctionURL_DefaultPathQS_GatewayInvoke_IncludesGWAddress(t *testi
FunctionSuffix: "",
GatewayInvoke: true,
GatewayAddress: "gateway",
GatewayPort: "8080",
}
req := queue.Request{
Function: "function1",
Expand All @@ -29,6 +30,7 @@ func Test_makeFunctionURL_DefaultPathQS_GatewayInvoke_WithQS(t *testing.T) {
FunctionSuffix: "",
GatewayInvoke: true,
GatewayAddress: "gateway",
GatewayPort: "8080",
}
req := queue.Request{
Function: "function1",
Expand All @@ -47,6 +49,7 @@ func Test_makeFunctionURL_DefaultPathQS_GatewayInvoke_WithPath(t *testing.T) {
FunctionSuffix: "",
GatewayInvoke: true,
GatewayAddress: "gateway",
GatewayPort: "8080",
}
req := queue.Request{
Function: "function1",
Expand All @@ -65,6 +68,7 @@ func Test_makeFunctionURL_DefaultPathQS_GatewayInvokeOff_UsesDirectInvocation(t
FunctionSuffix: ".openfaas-fn",
GatewayInvoke: false,
GatewayAddress: "gateway",
GatewayPort: "8080",
}
req := queue.Request{
Function: "function1",
Expand Down
15 changes: 15 additions & 0 deletions readconfig.go
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"os"
"strconv"
Expand Down Expand Up @@ -33,6 +34,19 @@ func (ReadConfig) Read() QueueWorkerConfig {
cfg.GatewayAddress = "gateway"
}

if value, exists := os.LookupEnv("faas_gateway_port"); exists {
val, err := strconv.Atoi(value)
if err != nil {
log.Println("converting faas_gateway_port to int error:", err)
} else {
cfg.GatewayPort = val
}
} else {
cfg.GatewayPort = 8080
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably concatenate the value once here instead of passing two arguments everywhere.

I think the new env var makes sense to prevent issues upgrading versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


cfg.GatewayAddress = fmt.Sprintf("%s:%d", cfg.GatewayAddress, cfg.GatewayPort)

if val, exists := os.LookupEnv("faas_function_suffix"); exists {
cfg.FunctionSuffix = val
}
Expand Down Expand Up @@ -114,6 +128,7 @@ func (ReadConfig) Read() QueueWorkerConfig {
type QueueWorkerConfig struct {
NatsAddress string
GatewayAddress string
GatewayPort int
FunctionSuffix string
DebugPrintBody bool
WriteDebug bool
Expand Down
73 changes: 40 additions & 33 deletions readconfig_test.go
Expand Up @@ -15,7 +15,7 @@ func Test_ReadConfig_GatewayInvokeDefault(t *testing.T) {

gatewayInvokeWant := false
if cfg.GatewayInvoke != gatewayInvokeWant {
t.Errorf("gatewayInvokeWant want %v, but got %v", gatewayInvokeWant, cfg.GatewayInvoke)
t.Errorf("gatewayInvokeWant want %v, got %v", gatewayInvokeWant, cfg.GatewayInvoke)
}
}

Expand All @@ -28,7 +28,7 @@ func Test_ReadConfig_GatewayInvokeSetToTrue(t *testing.T) {

gatewayInvokeWant := true
if cfg.GatewayInvoke != gatewayInvokeWant {
t.Errorf("gatewayInvokeWant want %v, but got %v", gatewayInvokeWant, cfg.GatewayInvoke)
t.Errorf("gatewayInvokeWant want %v, got %v", gatewayInvokeWant, cfg.GatewayInvoke)
}
}

Expand All @@ -40,7 +40,7 @@ func Test_ReadConfig_BasicAuthDefaultIsFalse(t *testing.T) {

want := false
if cfg.BasicAuth != want {
t.Errorf("basicAuth want %v, but got %v", want, cfg.BasicAuth)
t.Errorf("basicAuth want %v, got %v", want, cfg.BasicAuth)
}
}

Expand All @@ -52,7 +52,7 @@ func Test_ReadConfig_BasicAuthSetToTrue(t *testing.T) {

want := true
if cfg.BasicAuth != want {
t.Errorf("basicAuth want %v, but got %v", want, cfg.BasicAuth)
t.Errorf("basicAuth want %v, got %v", want, cfg.BasicAuth)
}
}

Expand All @@ -62,6 +62,7 @@ func Test_ReadConfig(t *testing.T) {

os.Setenv("faas_nats_address", "test_nats")
os.Setenv("faas_gateway_address", "test_gatewayaddr")
os.Setenv("faas_gateway_port", "8080")
os.Setenv("faas_function_suffix", "test_suffix")
os.Setenv("faas_print_body", "true")
os.Setenv("write_debug", "true")
Expand All @@ -70,43 +71,49 @@ func Test_ReadConfig(t *testing.T) {

config := readConfig.Read()

expected := "test_nats"
if config.NatsAddress != expected {
t.Logf("Expected NatsAddress `%s` actual `%s`\n", expected, config.NatsAddress)
want := "test_nats"
if config.NatsAddress != want {
t.Logf("NatsAddress want `%s`, got `%s`\n", want, config.NatsAddress)
t.Fail()
}

expected = "test_gatewayaddr"
if config.GatewayAddress != expected {
t.Logf("Expected GatewayAddress `%s` actual `%s`\n", expected, config.GatewayAddress)
want = "test_gatewayaddr:8080"
if config.GatewayAddress != want {
t.Logf("GatewayAddress want `%s`, got `%s`\n", want, config.GatewayAddress)
t.Fail()
}

expected = "test_suffix"
if config.FunctionSuffix != expected {
t.Logf("Expected FunctionSuffix `%s` actual `%s`\n", expected, config.FunctionSuffix)
wantGatewayPort := 8080
if config.GatewayPort != wantGatewayPort {
t.Logf("GatewayPort want `%d`, got `%d`\n", wantGatewayPort, config.GatewayPort)
t.Fail()
}

want = "test_suffix"
if config.FunctionSuffix != want {
t.Logf("FunctionSuffix want `%s`, got `%s`\n", want, config.FunctionSuffix)
t.Fail()
}

if config.DebugPrintBody != true {
t.Logf("Expected DebugPrintBody `%v` actual `%v`\n", true, config.DebugPrintBody)
t.Logf("DebugPrintBody want `%v`, got `%v`\n", true, config.DebugPrintBody)
t.Fail()
}

if config.WriteDebug != true {
t.Logf("Expected WriteDebug `%v` actual `%v`\n", true, config.WriteDebug)
t.Logf("WriteDebug want `%v`, got `%v`\n", true, config.WriteDebug)
t.Fail()
}

expectedMaxInflight := 10
if config.MaxInflight != expectedMaxInflight {
t.Logf("Expected maxInflight `%v` actual `%v`\n", expectedMaxInflight, config.MaxInflight)
wantMaxInflight := 10
if config.MaxInflight != wantMaxInflight {
t.Logf("maxInflight want `%v`, got `%v`\n", wantMaxInflight, config.MaxInflight)
t.Fail()
}

expectedAckWait := time.Millisecond * 10
if config.AckWait != expectedAckWait {
t.Logf("Expected maxInflight `%v` actual `%v`\n", expectedAckWait, config.AckWait)
wantAckWait := time.Millisecond * 10
if config.AckWait != wantAckWait {
t.Logf("maxInflight want `%v`, got `%v`\n", wantAckWait, config.AckWait)
t.Fail()
}

Expand All @@ -115,15 +122,15 @@ func Test_ReadConfig(t *testing.T) {

config = readConfig.Read()

expectedMaxInflight = 1
if config.MaxInflight != expectedMaxInflight {
t.Logf("Expected maxInflight `%v` actual `%v`\n", expectedMaxInflight, config.MaxInflight)
wantMaxInflight = 1
if config.MaxInflight != wantMaxInflight {
t.Logf("maxInflight want `%v`, got `%v`\n", wantMaxInflight, config.MaxInflight)
t.Fail()
}

expectedAckWait = time.Second * 30
if config.AckWait != expectedAckWait {
t.Logf("Expected maxInflight `%v` actual `%v`\n", expectedAckWait, config.AckWait)
wantAckWait = time.Second * 30
if config.AckWait != wantAckWait {
t.Logf("maxInflight want `%v`, got `%v`\n", wantAckWait, config.AckWait)
t.Fail()
}

Expand All @@ -132,15 +139,15 @@ func Test_ReadConfig(t *testing.T) {

config = readConfig.Read()

expectedMaxInflight = 1
if config.MaxInflight != expectedMaxInflight {
t.Logf("Expected maxInflight `%v` actual `%v`\n", expectedMaxInflight, config.MaxInflight)
wantMaxInflight = 1
if config.MaxInflight != wantMaxInflight {
t.Logf("maxInflight want `%v`, got `%v`\n", wantMaxInflight, config.MaxInflight)
t.Fail()
}

expectedAckWait = time.Second * 30
if config.AckWait != expectedAckWait {
t.Logf("Expected ackWait `%v` actual `%v`\n", expectedAckWait, config.AckWait)
wantAckWait = time.Second * 30
if config.AckWait != wantAckWait {
t.Logf("ackWait want `%v`, got `%v`\n", wantAckWait, config.AckWait)
t.Fail()
}
}