From 3e6917b1cc9dd24e936f9e9a48775284b2a5c152 Mon Sep 17 00:00:00 2001 From: Allen Ray Date: Thu, 22 Sep 2022 16:19:09 -0400 Subject: [PATCH] ETCD-319: adding transient systemd unit --- etcd/cmd/microshift-etcd/run.go | 1 + hack/cleanup.sh | 3 +++ pkg/controllers/etcd.go | 29 +++++++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/etcd/cmd/microshift-etcd/run.go b/etcd/cmd/microshift-etcd/run.go index e973ddba9e..a9017d2c61 100644 --- a/etcd/cmd/microshift-etcd/run.go +++ b/etcd/cmd/microshift-etcd/run.go @@ -97,6 +97,7 @@ func (s *EtcdService) Run() error { } <-e.Server.ReadyNotify() + // Wait to be stopped. sigTerm := make(chan os.Signal, 1) signal.Notify(sigTerm, os.Interrupt, syscall.SIGTERM) sig := <-sigTerm diff --git a/hack/cleanup.sh b/hack/cleanup.sh index dbaf47749d..68732ff0c7 100755 --- a/hack/cleanup.sh +++ b/hack/cleanup.sh @@ -18,6 +18,9 @@ sudo bash -c " systemctl disable microshift 2>/dev/null pkill -9 microshift + systemctl stop --now microshift-etcd 2>/dev/null + systemctl reset-failed microshift-etcd 2>/dev/null + echo Removing crio container and image storage crio wipe -f &>/dev/null || true systemctl restart crio diff --git a/pkg/controllers/etcd.go b/pkg/controllers/etcd.go index 6f7f4282d7..7479b64c43 100644 --- a/pkg/controllers/etcd.go +++ b/pkg/controllers/etcd.go @@ -46,15 +46,39 @@ func (s *EtcdService) Name() string { return "etcd" } func (s *EtcdService) Dependencies() []string { return []string{} } func (s *EtcdService) Run(ctx context.Context, ready chan<- struct{}, stopped chan<- struct{}) error { + defer close(stopped) + + // Check to see if we should run as a systemd run or directly as a binary. + runningAsSvc := os.Getenv("INVOCATION_ID") != "" + + // Get the path to the etcd binary based on the MicroShift binary location. microshiftExecPath, err := os.Executable() if err != nil { return fmt.Errorf("%v failed to get exec path: %v", s.Name(), err) } etcdPath := filepath.Join(filepath.Dir(microshiftExecPath), "microshift-etcd") + // Not running the etcd binary directly, the proper etcd arguments are handled + // in etcd/cmd/microshift-etcd/run.go. args := []string{"run"} + // If we're launching MicroShift as a service, we need to do the same + // with etcd, so wrap it in a transient systemd-unit that's tied + // to the MicroShift service lifetime. + var exe string + if runningAsSvc { + args = append([]string{ + "--uid=root", + "--scope", + "--property", "BindsTo=microshift.service", + "--unit", "microshift-etcd", + etcdPath, + }, args...) + exe = "systemd-run" + } else { + exe = etcdPath + } // Not using context as canceling ctx sends SIGKILL to process - cmd := exec.Command(etcdPath, args...) + cmd := exec.Command(exe, args...) wd, err := os.Getwd() if err != nil { @@ -63,7 +87,7 @@ func (s *EtcdService) Run(ctx context.Context, ready chan<- struct{}, stopped ch cmd.Dir = wd cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - if err := cmd.Start(); err != nil { + if err = cmd.Start(); err != nil { return fmt.Errorf("%s failed to start: %v", s.Name(), err) } @@ -87,6 +111,7 @@ func (s *EtcdService) Run(ctx context.Context, ready chan<- struct{}, stopped ch klog.Info("etcd is ready!") close(ready) + // Wait for MicroShift to be done <-ctx.Done() return ctx.Err() }