mirror of
https://github.com/k3s-io/k3s.git
synced 2024-06-07 19:41:36 +00:00
52 lines
1006 B
Go
52 lines
1006 B
Go
package dqlite
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/canonical/go-dqlite/client"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func (d *DQLite) Test(ctx context.Context) error {
|
|
var ips []string
|
|
peers, err := d.NodeStore.Get(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, peer := range peers {
|
|
ips = append(ips, peer.Address)
|
|
}
|
|
|
|
logrus.Infof("Testing connection to peers %v", ips)
|
|
return d.Join(ctx, nil)
|
|
}
|
|
|
|
func (d *DQLite) Join(ctx context.Context, nodes []client.NodeInfo) error {
|
|
if len(nodes) > 0 {
|
|
if err := d.NodeStore.Set(ctx, nodes); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
client, err := client.FindLeader(ctx, d.NodeStore, d.clientOpts...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer client.Close()
|
|
|
|
current, err := client.Cluster(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, testNode := range current {
|
|
if testNode.Address == d.NodeInfo.Address {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
logrus.Infof("Joining dqlite cluster as address=%s, id=%d", d.NodeInfo.Address, d.NodeInfo.ID)
|
|
return client.Add(ctx, d.NodeInfo)
|
|
}
|