Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,21 @@ var RetryTimes = 0

var IsMasterNode bool

// NodeName 节点名称,从 NODE_NAME 环境变量读取;
// 用于审计日志中标识节点身份,在容器/K8s 部署时比自动探测到的容器内网 IP 更具可读性。
const (
NodeNameSourceManual = "manual"
NodeNameSourceHostname = "hostname"
)

// NodeName 节点名称,优先从 NODE_NAME 环境变量读取,未配置时回退主机名。
// 用于审计日志和后台任务中标识节点身份;多实例部署时建议显式配置稳定 NODE_NAME。
var NodeName = ""

// NodeNameSource records how NodeName was chosen so future instance-management
// reporting can distinguish operator-configured names from automatic fallback.
var NodeNameSource = NodeNameSourceHostname

var NodeNameManuallyConfigured bool

var requestInterval int
var RequestInterval time.Duration

Expand Down
4 changes: 1 addition & 3 deletions common/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ func InitEnv() {
DebugEnabled = os.Getenv("DEBUG") == "true"
MemoryCacheEnabled = os.Getenv("MEMORY_CACHE_ENABLED") == "true"
IsMasterNode = os.Getenv("NODE_TYPE") != "slave"
// NodeName 优先用 NODE_NAME,未配置时回退主机名(容器下=容器 ID/Pod 名,自动扩容天然唯一)。
hostname, _ := os.Hostname()
NodeName = GetEnvOrDefaultString("NODE_NAME", hostname)
initNodeNameIdentity()
TLSInsecureSkipVerify = GetEnvOrDefaultBool("TLS_INSECURE_SKIP_VERIFY", false)
if TLSInsecureSkipVerify {
if tr, ok := http.DefaultTransport.(*http.Transport); ok && tr != nil {
Expand Down
33 changes: 33 additions & 0 deletions common/node_identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package common

import "os"

type NodeIdentity struct {
Name string `json:"name"`
Source string `json:"source"`
ManuallyConfigured bool `json:"manually_configured"`
ShouldConfigureManually bool `json:"should_configure_manually"`
}

func initNodeNameIdentity() {
if envNodeName := os.Getenv("NODE_NAME"); envNodeName != "" {
NodeName = envNodeName
NodeNameSource = NodeNameSourceManual
NodeNameManuallyConfigured = true
return
}

hostname, _ := os.Hostname()
NodeName = hostname
NodeNameSource = NodeNameSourceHostname
NodeNameManuallyConfigured = false
}

func GetNodeIdentity() NodeIdentity {
return NodeIdentity{
Name: NodeName,
Source: NodeNameSource,
ManuallyConfigured: NodeNameManuallyConfigured,
ShouldConfigureManually: !NodeNameManuallyConfigured,
}
}
Loading