Skip to content

Commit 50b0e92

Browse files
feat: introduce pod-name flag specifically for kubernetes
1 parent afb954c commit 50b0e92

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

cmd/cosmoseed/config.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const (
1414
)
1515

1616
var (
17-
home, chainID, seeds, logLevel, nodeKeyFile, externalAddress string
18-
showVersion, showNodeID, configReadOnly bool
17+
home, chainID, seeds, logLevel, externalAddress, podName string
18+
showVersion, showNodeID, configReadOnly bool
1919
)
2020

2121
func init() {
@@ -42,15 +42,19 @@ func init() {
4242
utils.GetString("LOG_LEVEL", "info"),
4343
"logging level",
4444
)
45-
flag.StringVar(&nodeKeyFile,
46-
"node-key-file",
47-
utils.GetString("NODE_KEY_FILE", ""),
48-
"override node key file on config.yaml",
49-
)
5045
flag.StringVar(&externalAddress,
5146
"external-address",
5247
utils.GetString("EXTERNAL_ADDRESS", ""),
53-
"external address to use in format '<host>:<port>'",
48+
"external address to use in format '<host>:<port>'. "+
49+
"When pod-name is set, this can be a list separated by comma and index will be extracted "+
50+
"from pod name to chose the correct address (useful on kubernetes StatefulSets)",
51+
)
52+
flag.StringVar(&podName,
53+
"pod-name",
54+
utils.GetString("POD_NAME", ""),
55+
"name of the pod when running on kubernetes. When set, node-key-file will be set to pod "+
56+
"name and index will be extracted from it to pick the right address from"+
57+
"external-address list (comma separated) (useful on kubernetes StatefulSets)",
5458
)
5559

5660
flag.BoolVar(&showVersion, "version", false, "print version and exit")

cmd/cosmoseed/main.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"os"
77
"path"
8+
"strconv"
9+
"strings"
810

911
cosmoseed2 "github.com/NibiruChain/cosmoseed/pkg/cosmoseed"
1012
)
@@ -49,14 +51,21 @@ func main() {
4951
cfg.LogLevel = logLevel
5052
}
5153

52-
if nodeKeyFile != "" {
53-
cfg.NodeKeyFile = nodeKeyFile
54-
}
55-
5654
if externalAddress != "" {
5755
cfg.ExternalAddress = externalAddress
5856
}
5957

58+
if podName != "" {
59+
cfg.NodeKeyFile = podName
60+
if externalAddress != "" {
61+
idx, _ := extractIndexFromPodName(podName)
62+
parts := strings.Split(externalAddress, ",")
63+
if len(parts) > idx {
64+
cfg.ExternalAddress = parts[idx]
65+
}
66+
}
67+
}
68+
6069
seeder, err := cosmoseed2.NewSeeder(home, cfg)
6170
if err != nil {
6271
panic(err)
@@ -71,3 +80,16 @@ func main() {
7180
panic(err)
7281
}
7382
}
83+
84+
func extractIndexFromPodName(podName string) (int, error) {
85+
parts := strings.Split(podName, "-")
86+
if len(parts) == 0 {
87+
return 0, fmt.Errorf("invalid pod name: %s", podName)
88+
}
89+
indexStr := parts[len(parts)-1]
90+
index, err := strconv.Atoi(indexStr)
91+
if err != nil {
92+
return 0, fmt.Errorf("failed to parse index from pod name %q: %w", podName, err)
93+
}
94+
return index, nil
95+
}

0 commit comments

Comments
 (0)