Skip to content

Commit

Permalink
Use either hostname or etcd.extraArgs.name for EtcdMember object name
Browse files Browse the repository at this point in the history
Signed-off-by: Jussi Nummelin <[email protected]>
  • Loading branch information
jnummelin committed Apr 30, 2024
1 parent 41d49a0 commit 6c8b264
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
12 changes: 12 additions & 0 deletions pkg/apis/k0s/v1beta1/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -161,6 +162,17 @@ func DefaultEtcdConfig() *EtcdConfig {
}
}

const etcdNameExtraArg = "name"

// GetNodeName returns the node name for the etcd peer
func (e *EtcdConfig) GetNodeName() (string, error) {
if e.ExtraArgs != nil && e.ExtraArgs[etcdNameExtraArg] != "" {
return e.ExtraArgs[etcdNameExtraArg], nil
}

return os.Hostname()
}

// DefaultKineConfig creates KineConfig with sane defaults
func DefaultKineConfig(dataDir string) *KineConfig {
// https://www.sqlite.org/c3ref/open.html#urifilenamesinsqlite3open
Expand Down
38 changes: 38 additions & 0 deletions pkg/apis/k0s/v1beta1/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package v1beta1

import (
"fmt"
"os"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -329,3 +331,39 @@ func TestStorageSuite(t *testing.T) {

suite.Run(t, storageSuite)
}

func TestEtcdConfig_GetNodeName(t *testing.T) {
require := require.New(t)

hostname, err := os.Hostname()
require.NoError(err)

tests := []struct {
name string
e *EtcdConfig
want string
}{
{
name: "no extra args - default to hostname",
e: &EtcdConfig{},
want: hostname,
},
{
name: "node name set in extra args",
e: &EtcdConfig{
ExtraArgs: map[string]string{
"name": "node-1",
},
},
want: "node-1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := tt.e.GetNodeName()
if got != tt.want {
t.Errorf("EtcdConfig.GetNodeName() = %v, want %v", got, tt.want)
}
})
}
}
3 changes: 1 addition & 2 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"
"fmt"
"net"
"os"
"strconv"
"time"

Expand Down Expand Up @@ -176,8 +175,8 @@ func (e *EtcdMemberReconciler) createMemberObject(ctx context.Context) error {

// Convert the memberID to hex string
memberIDStr := fmt.Sprintf("%x", memberID)
name, err := e.etcdConfig.GetNodeName()

name, err := os.Hostname()
if err != nil {
return err
}
Expand Down

0 comments on commit 6c8b264

Please sign in to comment.