diff --git a/pkg/apis/k0s/v1beta1/storage.go b/pkg/apis/k0s/v1beta1/storage.go index e1eecc2ec451..99301954c638 100644 --- a/pkg/apis/k0s/v1beta1/storage.go +++ b/pkg/apis/k0s/v1beta1/storage.go @@ -19,6 +19,7 @@ package v1beta1 import ( "encoding/json" "fmt" + "net" "net/url" "os" "path/filepath" @@ -173,6 +174,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 { // https://www.sqlite.org/c3ref/open.html#urifilenamesinsqlite3open diff --git a/pkg/backup/etcd_unix.go b/pkg/backup/etcd_unix.go index 13465677f598..a89cec4b75d5 100644 --- a/pkg/backup/etcd_unix.go +++ b/pkg/backup/etcd_unix.go @@ -21,6 +21,8 @@ package backup import ( "context" "fmt" + "net" + "net/url" "os" "path/filepath" @@ -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, diff --git a/pkg/component/controller/etcd.go b/pkg/component/controller/etcd.go index ce98196784d9..fc84d03fd89f 100644 --- a/pkg/component/controller/etcd.go +++ b/pkg/component/controller/etcd.go @@ -154,7 +154,7 @@ func (e *Etcd) Start(ctx context.Context) error { return err } - peerURL := fmt.Sprintf("https://%s:2380", e.Config.PeerAddress) + peerURL := e.Config.GetPeerURL() args := stringmap.StringMap{ "--data-dir": e.K0sVars.EtcdDataDir, diff --git a/pkg/component/controller/etcd_member_reconciler.go b/pkg/component/controller/etcd_member_reconciler.go index ea384017c055..ae32be0f0d1e 100644 --- a/pkg/component/controller/etcd_member_reconciler.go +++ b/pkg/component/controller/etcd_member_reconciler.go @@ -20,7 +20,6 @@ import ( "context" "errors" "fmt" - "net" "strconv" "time" @@ -186,9 +185,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 }