Skip to content

Commit

Permalink
OKG Network Information Getting Shell Script Update (#212)
Browse files Browse the repository at this point in the history
Signed-off-by: 宋康 <sk01199367@alibaba-inc.com>
Co-authored-by: 宋康 <sk01199367@alibaba-inc.com>
  • Loading branch information
songkang7 and 宋康 committed Sep 25, 2024
1 parent cc4ea61 commit 023ff4e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,33 @@ func getNetwork() {

```

在获取网络信息时,可能面临着网络状态未准备好的情况,可以在程序中加入一个循环,持续检测网络状态,直至网络状态为Ready后获取网络信息数据。以下是相应的shell脚本。

```shell
# 初始化JSON字符串为空
json=''

# 循环直到拿到数据
while [ -z "$json" ]; do
# 从文件中读取JSON字符串
json=$(cat /etc/podinfo/network)

# 检查currentNetworkState是否为Ready
currentNetworkState=$(echo $json | jq -r '.currentNetworkState')
if [ "$currentNetworkState" != "Ready" ]; then
echo "currentNetworkState is not Ready, sleeping 1 second..."
sleep 1
json=''
fi
done

# 解析externalAddresses的ip和port
echo "externalAddresses:"
ip=$(echo $json | jq -r '.externalAddresses[0].ip')
port=$(echo $json | jq -r '.externalAddresses[0].ports[0].port')
echo " IP: $ip, Port: $port"
```

## FAQ

Q: 如何更改网络插件配置?
Expand Down
27 changes: 27 additions & 0 deletions kruisegame/user-manuals/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,33 @@ func getNetwork() {
```

When getting network information, you may face the situation that the network status is not ready, you can add a loop in the programme to continuously detect the network status until the network status is Ready and then get the network information data. The following is the corresponding shell script.

```shell
# Initialise JSON string to empty
json=''
# Loop until you get the data
while [ -z "$json" ]; do
# Reading a JSON string from a file
json=$(cat /etc/podinfo/network)
# Check if currentNetworkState is Ready
currentNetworkState=$(echo $json | jq -r '.currentNetworkState')
if [ "$currentNetworkState" != "Ready" ]; then
echo "currentNetworkState is not Ready, sleeping 1 second..."
sleep 1
json=''
fi
done
# Parsing the ip and port of externalAddresses
echo "externalAddresses:"
ip=$(echo $json | jq -r '.externalAddresses[0].ip')
port=$(echo $json | jq -r '.externalAddresses[0].ports[0].port')
echo " IP: $ip, Port: $port"
```

## FAQ

Q: How to change the network plugin configuration?
Expand Down

0 comments on commit 023ff4e

Please sign in to comment.