2020-05-05 22:02:16 +00:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
2021-09-08 17:56:18 +00:00
|
|
|
"context"
|
2021-07-02 19:55:47 +00:00
|
|
|
"errors"
|
2022-10-08 00:36:57 +00:00
|
|
|
"os"
|
2020-12-07 20:30:44 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
daemonconfig "github.com/k3s-io/k3s/pkg/daemons/config"
|
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2020-05-05 22:02:16 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-07-02 19:55:47 +00:00
|
|
|
"go.etcd.io/etcd/server/v3/embed"
|
|
|
|
"go.etcd.io/etcd/server/v3/etcdserver/api/rafthttp"
|
2020-05-05 22:02:16 +00:00
|
|
|
)
|
|
|
|
|
2022-02-24 22:35:08 +00:00
|
|
|
type Embedded struct {
|
|
|
|
nodeConfig *daemonconfig.Node
|
2020-05-05 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 22:35:08 +00:00
|
|
|
func (e *Embedded) ETCD(ctx context.Context, args ETCDConfig, extraArgs []string) error {
|
2021-11-12 05:03:15 +00:00
|
|
|
configFile, err := args.ToConfigFile(extraArgs)
|
2020-05-05 22:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cfg, err := embed.ConfigFromFile(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-10 12:33:42 +00:00
|
|
|
|
2020-05-05 22:02:16 +00:00
|
|
|
etcd, err := embed.StartEtcd(cfg)
|
|
|
|
if err != nil {
|
2021-10-12 06:13:10 +00:00
|
|
|
return err
|
2020-05-05 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2020-10-27 18:06:26 +00:00
|
|
|
select {
|
2020-12-07 20:30:44 +00:00
|
|
|
case err := <-etcd.Server.ErrNotify():
|
2021-07-02 19:55:47 +00:00
|
|
|
if errors.Is(err, rafthttp.ErrMemberRemoved) {
|
2020-12-07 20:30:44 +00:00
|
|
|
tombstoneFile := filepath.Join(args.DataDir, "tombstone")
|
2022-10-08 00:36:57 +00:00
|
|
|
if err := os.WriteFile(tombstoneFile, []byte{}, 0600); err != nil {
|
2020-12-07 20:30:44 +00:00
|
|
|
logrus.Fatalf("failed to write tombstone file to %s", tombstoneFile)
|
|
|
|
}
|
|
|
|
logrus.Infof("this node has been removed from the cluster please restart %s to rejoin the cluster", version.Program)
|
|
|
|
return
|
|
|
|
}
|
2021-09-08 17:56:18 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
logrus.Infof("stopping etcd")
|
|
|
|
etcd.Close()
|
2020-10-27 18:06:26 +00:00
|
|
|
case <-etcd.Server.StopNotify():
|
2020-12-07 20:30:44 +00:00
|
|
|
logrus.Fatalf("etcd stopped")
|
2020-10-27 18:06:26 +00:00
|
|
|
case err := <-etcd.Err():
|
|
|
|
logrus.Fatalf("etcd exited: %v", err)
|
|
|
|
}
|
2020-05-05 22:02:16 +00:00
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|