Skip to content

Commit

Permalink
Fix etcd peer URL to support ipv6
Browse files Browse the repository at this point in the history
IPv6 URL need to have brackets [] around the ipv6 to separate it from
the port. Eg https://[xx:yy:zz]:2380

Fix this by using net/url's String() formatter.

Signed-off-by: Natanael Copa <[email protected]>
  • Loading branch information
ncopa committed Nov 5, 2024
1 parent b393d37 commit 1469e0c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
10 changes: 10 additions & 0 deletions pkg/apis/k0s/v1beta1/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1beta1
import (
"encoding/json"
"fmt"
"net"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -179,6 +180,15 @@ func (e *EtcdConfig) GetNodeName() (string, error) {
return os.Hostname()
}

// GetPeerURL returns the URL of PeerAddress
func (e *EtcdConfig) GetPeerURL() string {
u := &url.URL{
Scheme: "https",
Host: net.JoinHostPort(e.PeerAddress, "2380"),
}
return u.String()
}

// DefaultKineConfig creates KineConfig with sane defaults
func DefaultKineConfig(dataDir string) *KineConfig {
return &KineConfig{
Expand Down
8 changes: 7 additions & 1 deletion pkg/backup/etcd_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package backup
import (
"context"
"fmt"
"net"
"net/url"
"os"
"path/filepath"

Expand Down Expand Up @@ -84,7 +86,11 @@ func (e etcdStep) Restore(restoreFrom, _ string) error {
if err != nil {
return err
}
peerURL := fmt.Sprintf("https://%s:2380", e.peerAddress)
u := &url.URL{
Scheme: "https",
Host: net.JoinHostPort(e.peerAddress, "2380"),
}
peerURL := u.String()
restoreConfig := utilsnapshot.RestoreConfig{
SnapshotPath: snapshotPath,
OutputDataDir: e.etcdDataDir,
Expand Down
2 changes: 1 addition & 1 deletion pkg/component/controller/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (e *Etcd) Start(ctx context.Context) error {
name = hostName
}

peerURL := fmt.Sprintf("https://%s:2380", e.Config.PeerAddress)
peerURL := e.Config.GetPeerURL()

args := stringmap.StringMap{
"--data-dir": e.K0sVars.EtcdDataDir,
Expand Down
5 changes: 1 addition & 4 deletions pkg/component/controller/etcd_member_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"errors"
"fmt"
"net"
"strconv"
"time"

Expand Down Expand Up @@ -177,9 +176,7 @@ func (e *EtcdMemberReconciler) createMemberObject(ctx context.Context) error {
return err
}

peerURL := fmt.Sprintf("https://%s", net.JoinHostPort(e.etcdConfig.PeerAddress, "2380"))

memberID, err := etcdClient.GetPeerIDByAddress(ctx, peerURL)
memberID, err := etcdClient.GetPeerIDByAddress(ctx, e.etcdConfig.GetPeerURL())
if err != nil {
return err
}
Expand Down

0 comments on commit 1469e0c

Please sign in to comment.