Skip to content

Commit

Permalink
Merge pull request #57 from chrisliu1995/feat/network_params
Browse files Browse the repository at this point in the history
add params for network plugins
  • Loading branch information
chrisliu1995 authored May 26, 2023
2 parents 7a4a40b + 7d4aefe commit 543e02a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Add more print columns for GameServer & GameServerSet. https://github.com/openkruise/kruise-game/pull/48
- Add default serviceName for GameServerSet. https://github.com/openkruise/kruise-game/pull/51
- Add new networkType Kubernetes-Ingress. https://github.com/openkruise/kruise-game/pull/54
- Add network-related environment variables to allow users to adjust the network waiting time and detection interval. https://github.com/openkruise/kruise-game/pull/57

### Others

Expand Down
5 changes: 5 additions & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ spec:
- --provider-config=/etc/kruise-game/config.toml
image: controller:latest
name: manager
env:
- name: "NETWORK_TOTAL_WAIT_TIME"
value: "60"
- name: "NETWORK_PROBE_INTERVAL_TIME"
value: "5"
ports:
- name: https
containerPort: 8080
Expand Down
9 changes: 6 additions & 3 deletions pkg/controllers/gameserver/gameserver_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ import (
"time"
)

var (
NetworkTotalWaitTime = util.GetNetworkTotalWaitTime()
NetworkIntervalTime = util.GetNetworkIntervalTime()
)

const (
NetworkTotalWaitTime = 60 * time.Second
NetworkIntervalTime = 5 * time.Second
TimeFormat = "2006-01-02 15:04:05"
TimeFormat = "2006-01-02 15:04:05"
)

const (
Expand Down
32 changes: 32 additions & 0 deletions pkg/util/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package util

import (
"k8s.io/klog/v2"
"os"
"strconv"
"time"
)

func GetNetworkTotalWaitTime() time.Duration {
networkTotalWaitTime := 60 * time.Second
if num := os.Getenv("NETWORK_TOTAL_WAIT_TIME"); len(num) > 0 {
if p, err := strconv.ParseInt(num, 10, 32); err == nil {
networkTotalWaitTime = time.Duration(p) * time.Second
} else {
klog.Fatalf("failed to convert NETWORK_TOTAL_WAIT_TIME=%v in env: %v", p, err)
}
}
return networkTotalWaitTime
}

func GetNetworkIntervalTime() time.Duration {
networkIntervalTime := 5 * time.Second
if num := os.Getenv("NETWORK_PROBE_INTERVAL_TIME"); len(num) > 0 {
if p, err := strconv.ParseInt(num, 10, 32); err == nil {
networkIntervalTime = time.Duration(p) * time.Second
} else {
klog.Fatalf("failed to convert NETWORK_PROBE_INTERVAL_TIME=%v in env: %v", p, err)
}
}
return networkIntervalTime
}
45 changes: 45 additions & 0 deletions pkg/util/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package util

import (
"os"
"testing"
"time"
)

func TestGetNetworkTotalWaitTime(t *testing.T) {
tests := []struct {
networkTotalWaitTime string
result time.Duration
}{
{
networkTotalWaitTime: "60",
result: 60 * time.Second,
},
}

for _, test := range tests {
os.Setenv("NETWORK_TOTAL_WAIT_TIME", test.networkTotalWaitTime)
if GetNetworkTotalWaitTime() != test.result {
t.Errorf("expect %v but got %v", test.result, GetNetworkTotalWaitTime())
}
}
}

func TestGetNetworkIntervalTime(t *testing.T) {
tests := []struct {
networkIntervalTime string
result time.Duration
}{
{
networkIntervalTime: "5",
result: 5 * time.Second,
},
}

for _, test := range tests {
os.Setenv("NETWORK_PROBE_INTERVAL_TIME", test.networkIntervalTime)
if GetNetworkIntervalTime() != test.result {
t.Errorf("expect %v but got %v", test.result, GetNetworkIntervalTime())
}
}
}

0 comments on commit 543e02a

Please sign in to comment.