diff --git a/CHANGELOG.md b/CHANGELOG.md index c366c7b6..1716ef93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index 76cb378e..c02ff10c 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -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 diff --git a/pkg/controllers/gameserver/gameserver_manager.go b/pkg/controllers/gameserver/gameserver_manager.go index 3d17c56c..c7b96c4c 100644 --- a/pkg/controllers/gameserver/gameserver_manager.go +++ b/pkg/controllers/gameserver/gameserver_manager.go @@ -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 ( diff --git a/pkg/util/env.go b/pkg/util/env.go new file mode 100644 index 00000000..5635acb9 --- /dev/null +++ b/pkg/util/env.go @@ -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 +} diff --git a/pkg/util/env_test.go b/pkg/util/env_test.go new file mode 100644 index 00000000..f36f4cfd --- /dev/null +++ b/pkg/util/env_test.go @@ -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()) + } + } +}