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 20, 2024
1 parent b93fd98 commit aec8aac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
17 changes: 16 additions & 1 deletion pkg/cluster/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"time"

"github.com/gorilla/mux"
"github.com/k3s-io/k3s/pkg/cluster/managed"
"github.com/k3s-io/k3s/pkg/etcd"
"github.com/k3s-io/k3s/pkg/nodepassword"
Expand Down Expand Up @@ -96,7 +97,7 @@ func (c *Cluster) start(ctx context.Context) error {
// management of etcd cluster membership without being disrupted when a member is removed from the cluster.
func (c *Cluster) registerDBHandlers(handler http.Handler) (http.Handler, error) {
if c.managedDB == nil {
return handler, nil
return handlerNoEtcd(handler), nil
}

return c.managedDB.Register(handler)
Expand Down Expand Up @@ -168,3 +169,17 @@ func (c *Cluster) deleteNodePasswdSecret(ctx context.Context) {
logrus.Warnf("failed to delete old node password secret: %v", err)
}
}

// handlerNoEtcd wraps a handler with an error message indicating that etcd is not deployed.
func handlerNoEtcd(handler http.Handler) http.Handler {
r := mux.NewRouter().SkipClean(true)
r.NotFoundHandler = handler

ir := r.Path("/db/info").Subrouter()
ir.HandleFunc("", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("K3s is not deployed with an etcd datastore"))
})

return r
}
11 changes: 0 additions & 11 deletions tests/e2e/snapshotrestore/snapshotrestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,6 @@ var _ = Describe("Verify snapshots and cluster restores work", Ordered, func() {
Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Error().NotTo(HaveOccurred())
})

It("Resets non bootstrap nodes", func() {
for _, nodeName := range serverNodeNames {
if nodeName != serverNodeNames[0] {
cmd := "k3s server --cluster-reset"
response, err := e2e.RunCmdOnNode(cmd, nodeName)
Expect(err).NotTo(HaveOccurred())
Expect(response).Should(ContainSubstring("Managed etcd cluster membership has been reset, restart without --cluster-reset flag now"))
}
}
})

It("Checks that other servers are not ready", func() {
fmt.Printf("\nFetching node status\n")
Eventually(func(g Gomega) {
Expand Down

0 comments on commit aec8aac

Please sign in to comment.