Skip to content

Commit

Permalink
If no etcd was deployed, fail etcd-snapshot with a useful error
Browse files Browse the repository at this point in the history
Signed-off-by: manuelbuil <[email protected]>
  • Loading branch information
manuelbuil committed Nov 13, 2024
1 parent b93fd98 commit a2eee54
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/cli/etcdsnapshot/etcd_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,26 @@ func commandSetup(app *cli.Context, cfg *cmds.Server) (*etcd.SnapshotRequest, *c
}
cfg.Token = string(bytes.TrimRight(tokenByte, "\n"))
}

if !etcdRunning(dataDir) {
return nil, nil, errors.New("K3s is not deployed with an etcd datastore")
}

info, err := clientaccess.ParseAndValidateToken(cmds.ServerConfig.ServerURL, cfg.Token, clientaccess.WithUser("server"))
return sr, info, err
}

// etcdRunning checks if etcd is the datastore
func etcdRunning(dataDir string) bool {
// if kine.sock is present and db/etcd/config is not, we are not using etcd. Only one of them missing could also mean a wrong dataDir
_, errEtcd := os.Stat(filepath.Join(dataDir, "db/etcd/config"))
_, errKine := os.Stat(filepath.Join(dataDir, "kine.sock"))
if os.IsNotExist(errEtcd) && !os.IsNotExist(errKine) {
return false
}
return true
}

func wrapServerError(err error) error {
if errors.Is(err, context.DeadlineExceeded) {
// if the request timed out the server log likely won't contain anything useful,
Expand Down

0 comments on commit a2eee54

Please sign in to comment.