diff --git a/pkg/cli/etcdsnapshot/etcd_snapshot.go b/pkg/cli/etcdsnapshot/etcd_snapshot.go index d1d88af66a..18f9959cab 100644 --- a/pkg/cli/etcdsnapshot/etcd_snapshot.go +++ b/pkg/cli/etcdsnapshot/etcd_snapshot.go @@ -80,10 +80,11 @@ func commandSetup(app *cli.Context, cfg *cmds.Server, config *server.Config) (*e // command uses the same endpoint selection logic as it does when starting up the full server. Specifically, // we need to set an IPv6 service CIDR on IPv6-only or IPv6-first nodes, as the etcd default endpoints check // the service CIDR primary addresss family to determine what loopback address to use. - _, nodeIPs, err := util.GetHostnameAndIPs(cmds.AgentConfig.NodeName, cmds.AgentConfig.NodeIP) + nodeName, nodeIPs, err := util.GetHostnameAndIPs(cmds.AgentConfig.NodeName, cmds.AgentConfig.NodeIP) if err != nil { return nil, err } + config.ControlConfig.ServerNodeName = nodeName // configure ClusterIPRanges. Use default 10.42.0.0/16 or fd00:42::/56 if user did not set it _, defaultClusterCIDR, defaultServiceCIDR, _ := util.GetDefaultAddresses(nodeIPs[0]) diff --git a/pkg/etcd/etcd.go b/pkg/etcd/etcd.go index d9d7356442..6a4801dff1 100644 --- a/pkg/etcd/etcd.go +++ b/pkg/etcd/etcd.go @@ -623,6 +623,9 @@ func (e *ETCD) setName(force bool) error { fileName := nameFile(e.config) data, err := os.ReadFile(fileName) if os.IsNotExist(err) || force { + if e.config.ServerNodeName == "" { + return errors.New("server node name not set") + } e.name = e.config.ServerNodeName + "-" + uuid.New().String()[:8] if err := os.MkdirAll(filepath.Dir(fileName), 0700); err != nil { return err @@ -1106,7 +1109,7 @@ func (e *ETCD) manageLearners(ctx context.Context) { var node *v1.Node for _, n := range nodes { - if strings.HasPrefix(member.Name, n.Name+"-") { + if member.Name == n.Annotations[NodeNameAnnotation] { node = n nodeIsMember[n.Name] = true break diff --git a/pkg/etcd/etcd_test.go b/pkg/etcd/etcd_test.go index f7d6e2da48..a28dee46e1 100644 --- a/pkg/etcd/etcd_test.go +++ b/pkg/etcd/etcd_test.go @@ -27,6 +27,7 @@ func mustGetAddress() string { } func generateTestConfig() *config.Control { + hostname, _ := os.Hostname() containerRuntimeReady := make(chan struct{}) close(containerRuntimeReady) criticalControlArgs := config.CriticalControlArgs{ @@ -37,6 +38,7 @@ func generateTestConfig() *config.Control { ServiceIPRange: testutil.ServiceIPNet(), } return &config.Control{ + ServerNodeName: hostname, Runtime: config.NewRuntime(containerRuntimeReady), HTTPSPort: 6443, SupervisorPort: 6443,