Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #361 from justinsb/etcd-version-for-adoption
Browse files Browse the repository at this point in the history
Restart the server using the recommended etcd version
  • Loading branch information
k8s-ci-robot authored Feb 4, 2023
2 parents d3c7c31 + 647e80d commit 5c8e13c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
35 changes: 28 additions & 7 deletions etcd-manager/pkg/etcd/etcdserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/etcdadm/etcd-manager/pkg/backup"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/contextutil"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/dns"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/etcdversions"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/legacy"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/pki"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/privateapi"
Expand Down Expand Up @@ -87,7 +88,7 @@ func NewEtcdServer(baseDir string, clusterName string, listenAddress string, lis
}

// Make sure we have read state from disk before serving
if err := s.initState(); err != nil {
if err := s.initStateOnStartup(); err != nil {
return nil, err
}

Expand Down Expand Up @@ -117,7 +118,11 @@ func (s *EtcdServer) Run(ctx context.Context) {
})
}

func readState(baseDir string) (*protoetcd.EtcdState, error) {
// readState loads the saved state from the directory.
// If no state is found, returns (nil, nil).
// If replaceEtcdVersion is true, we will "bump" old etcd patch versions to a supported version.
// replaceEtcdVersion means we don't need to have every etcd version available.
func readState(baseDir string, replaceEtcdVersion bool) (*protoetcd.EtcdState, error) {
p := filepath.Join(baseDir, "state")
b, err := os.ReadFile(p)
if err != nil {
Expand All @@ -133,6 +138,16 @@ func readState(baseDir string) (*protoetcd.EtcdState, error) {
return nil, fmt.Errorf("error parsing state file: %v", err)
}

// Maybe use the recommended etcd version
if replaceEtcdVersion && state.EtcdVersion != "" {
etcdVersion := state.EtcdVersion
startWith := etcdversions.EtcdVersionForAdoption(etcdVersion)
if startWith != "" && startWith != etcdVersion {
klog.Warningf("starting server from etcd %q, will start with %q", etcdVersion, startWith)
state.EtcdVersion = startWith
}
}

return state, nil
}

Expand All @@ -150,12 +165,16 @@ func writeState(baseDir string, state *protoetcd.EtcdState) error {
return nil
}

func (s *EtcdServer) initState() error {
// initStateOnStartup populates the state from local disk.
// It should only be called on initial startup; after that
// we are under control of the leader.
func (s *EtcdServer) initStateOnStartup() error {
s.mutex.Lock()
defer s.mutex.Unlock()

if s.state == nil {
state, err := readState(s.baseDir)
replaceEtcdVersion := true
state, err := readState(s.baseDir, replaceEtcdVersion)
if err != nil {
return err
}
Expand All @@ -169,7 +188,7 @@ func (s *EtcdServer) initState() error {
}

func (s *EtcdServer) runOnce() error {
if err := s.initState(); err != nil {
if err := s.initStateOnStartup(); err != nil {
return err
}

Expand Down Expand Up @@ -329,6 +348,7 @@ func (s *EtcdServer) JoinCluster(ctx context.Context, request *protoetcd.JoinClu
Nodes: request.Nodes,
}
s.state.Quarantined = true

s.state.EtcdVersion = request.EtcdVersion

if err := writeState(s.baseDir, s.state); err != nil {
Expand Down Expand Up @@ -604,12 +624,13 @@ func (s *EtcdServer) startEtcdProcess(state *protoetcd.EtcdState) error {
return err
}

binDir, err := BindirForEtcdVersion(state.EtcdVersion, "etcd")
p.EtcdVersion = state.EtcdVersion

binDir, err := BindirForEtcdVersion(p.EtcdVersion, "etcd")
if err != nil {
return err
}
p.BinDir = binDir
p.EtcdVersion = state.EtcdVersion

if state.NewCluster {
p.CreateNewCluster = true
Expand Down
8 changes: 4 additions & 4 deletions etcd-manager/test/integration/resize_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestResizeCluster(t *testing.T) {
t.Fatalf("unable to set test key: %v", err)
}

n1.AssertVersion(t, etcdVersion)
n1.AssertVersion(t, etcdversions.EtcdVersionForAdoption(etcdVersion))
}

n2 := h.NewNode("127.0.0.2")
Expand Down Expand Up @@ -107,9 +107,9 @@ func TestResizeCluster(t *testing.T) {
t.Fatalf("unexpected test key value after upgrade: %q", v)
}

n1.AssertVersion(t, etcdVersion)
n2.AssertVersion(t, etcdVersion)
n3.AssertVersion(t, etcdVersion)
n1.AssertVersion(t, etcdversions.EtcdVersionForAdoption(etcdVersion))
n2.AssertVersion(t, etcdversions.EtcdVersionForAdoption(etcdVersion))
n3.AssertVersion(t, etcdversions.EtcdVersionForAdoption(etcdVersion))
}

cancel()
Expand Down

0 comments on commit 5c8e13c

Please sign in to comment.