The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/7637

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===

Signed-off-by: Free Ekanayaka <free.ekanay...@canonical.com>
From c495f3ecf068142ea97f2177444e0c2a49dbb000 Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanay...@canonical.com>
Date: Thu, 9 Jul 2020 18:13:10 +0200
Subject: [PATCH] lxd: Add clustering_fix_raft_address_zero patch to fix node
 with "0" as address

Signed-off-by: Free Ekanayaka <free.ekanay...@canonical.com>
---
 lxd/cluster/gateway.go |  2 +-
 lxd/db/node/schema.go  |  2 +-
 lxd/db/node/update.go  | 51 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/lxd/cluster/gateway.go b/lxd/cluster/gateway.go
index face315dde..b57db673c5 100644
--- a/lxd/cluster/gateway.go
+++ b/lxd/cluster/gateway.go
@@ -878,7 +878,7 @@ func (g *Gateway) currentRaftNodes() ([]db.RaftNode, error) 
{
 // Translate a raft address to a node address. They are always the same except
 // for the bootstrap node, which has address "1".
 func (g *Gateway) nodeAddress(raftAddress string) (string, error) {
-       if raftAddress != "1" {
+       if raftAddress != "1" && raftAddress != "0" {
                return raftAddress, nil
        }
        var address string
diff --git a/lxd/db/node/schema.go b/lxd/db/node/schema.go
index aaf0e09676..907e3ba5e9 100644
--- a/lxd/db/node/schema.go
+++ b/lxd/db/node/schema.go
@@ -25,5 +25,5 @@ CREATE TABLE raft_nodes (
     UNIQUE (address)
 );
 
-INSERT INTO schema (version, updated_at) VALUES (39, strftime("%s"))
+INSERT INTO schema (version, updated_at) VALUES (40, strftime("%s"))
 `
diff --git a/lxd/db/node/update.go b/lxd/db/node/update.go
index b5787d0dd9..4a1c35a9d4 100644
--- a/lxd/db/node/update.go
+++ b/lxd/db/node/update.go
@@ -96,6 +96,7 @@ var updates = map[int]schema.Update{
        37: updateFromV36,
        38: updateFromV37,
        39: updateFromV38,
+       40: updateFromV39,
 }
 
 // UpdateFromPreClustering is the last schema version where clustering support
@@ -104,6 +105,56 @@ const UpdateFromPreClustering = 36
 
 // Schema updates begin here
 
+// Fix the address of the bootstrap node being set to "0" in the raft_nodes
+// table.
+func updateFromV39(tx *sql.Tx) error {
+       nodes := []struct {
+               ID      uint64
+               Address string
+       }{}
+       dest := func(i int) []interface{} {
+               nodes = append(nodes, struct {
+                       ID      uint64
+                       Address string
+               }{})
+               return []interface{}{&nodes[i].ID, &nodes[i].Address}
+       }
+       stmt, err := tx.Prepare("SELECT id, address FROM raft_nodes")
+       if err != nil {
+               return err
+       }
+
+       defer stmt.Close()
+
+       err = query.SelectObjects(stmt, dest)
+       if err != nil {
+               return errors.Wrap(err, "Failed to fetch raft nodes")
+       }
+
+       if len(nodes) != 1 {
+               return nil
+       }
+
+       info := nodes[0]
+       if info.ID != 1 || info.Address != "0" {
+               return nil
+       }
+
+       config, err := query.SelectConfig(tx, "config", "")
+       if err != nil {
+               return err
+       }
+       address := config["cluster.https_address"]
+       if address != "" {
+               _, err := tx.Exec("UPDATE raft_nodes SET address=? WHERE id=1", 
address)
+               if err != nil {
+                       return err
+               }
+       }
+
+       return nil
+}
+
 // Add role column to raft_nodes table. All existing entries will have role "0"
 // which means voter.
 func updateFromV38(tx *sql.Tx) error {
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to