Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions pkg/controller/k0scontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/csv"
"fmt"
"io"
"os"
"os/user"
"path/filepath"
Expand All @@ -28,8 +27,7 @@ import (
type K0sController struct {
Options config.K0sControllerOptions

supervisor supervisor.Supervisor
Output io.Writer
supervisor *supervisor.Supervisor
uid int
gid int
}
Expand Down Expand Up @@ -68,16 +66,9 @@ func (k *K0sController) Init(_ context.Context) error {
if k.Options.CmdLogLevels != nil {
args = append(args, fmt.Sprintf("--logging=%s", createS2SFlag(k.Options.CmdLogLevels)))
}
k.supervisor = supervisor.Supervisor{
Name: "k0s",
UID: k.uid,
GID: k.gid,
BinPath: assets.BinPath("k0s", k.Options.BinDir()),
Output: k.Output,
RunDir: k.Options.RunDir(),
DataDir: k.Options.DataDir,
KeepEnvPrefix: true,
Args: args,
k0spath := assets.BinPath("k0s", k.Options.BinDir())
if k.supervisor, err = supervisor.New(k0spath, args); err != nil {
return fmt.Errorf("failed to create supervisor: %w", err)
}
return nil
}
Expand Down
27 changes: 0 additions & 27 deletions pkg/supervisor/detachattr.go

This file was deleted.

82 changes: 0 additions & 82 deletions pkg/supervisor/logwriter.go

This file was deleted.

53 changes: 53 additions & 0 deletions pkg/supervisor/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package supervisor

import (
"fmt"
"time"
)

// Option sets an option on a Supervisor reference.
type Option func(*Supervisor)

// WithName sets the name of the Supervisor.
func WithName(name string) Option {
return func(s *Supervisor) {
s.name = name
s.log = s.log.WithField("component", name)
s.pidFile = fmt.Sprintf("/run/replicated/%s.pid", name)
}
}

// WithTimeoutRespawn sets the timeout for respawning a process.
func WithTimeoutRespawn(timeoutRespawn time.Duration) Option {
return func(s *Supervisor) {
s.timeoutRespawn = timeoutRespawn
}
}

// WithTimeoutStop sets the timeout for stopping a process.
func WithTimeoutStop(timeoutStop time.Duration) Option {
return func(s *Supervisor) {
s.timeoutStop = timeoutStop
}
}

// WithUID sets the UID of the Supervisor.
func WithUID(uid int) Option {
return func(s *Supervisor) {
s.uid = uid
}
}

// WithGID sets the GID of the Supervisor.
func WithGID(gid int) Option {
return func(s *Supervisor) {
s.gid = gid
}
}

// WithPIDFile sets the PID file of the Supervisor.
func WithPIDFile(pidFile string) Option {
return func(s *Supervisor) {
s.pidFile = pidFile
}
}
45 changes: 45 additions & 0 deletions pkg/supervisor/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package supervisor

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestWithName(t *testing.T) {
s, err := New("/bin/cat", []string{"-"}, WithName("CAT"))
assert.NoError(t, err)
assert.Equal(t, "CAT", s.name)
assert.Equal(t, "/run/replicated/CAT.pid", s.pidFile)
}

func TestWithTimeoutRespawn(t *testing.T) {
s, err := New("/bin/cat", []string{"-"}, WithTimeoutRespawn(time.Second))
assert.NoError(t, err)
assert.Equal(t, time.Second, s.timeoutRespawn)
}

func TestWithTimeoutStop(t *testing.T) {
s, err := New("/bin/cat", []string{"-"}, WithTimeoutStop(time.Second))
assert.NoError(t, err)
assert.Equal(t, time.Second, s.timeoutStop)
}

func TestWithGID(t *testing.T) {
s, err := New("/bin/cat", []string{"-"}, WithGID(1000))
assert.NoError(t, err)
assert.Equal(t, 1000, s.gid)
}

func TestWithUID(t *testing.T) {
s, err := New("/bin/cat", []string{"-"}, WithUID(1000))
assert.NoError(t, err)
assert.Equal(t, 1000, s.uid)
}

func TestWithPIDFile(t *testing.T) {
s, err := New("/bin/cat", []string{"-"}, WithPIDFile("/run/abc.pid"))
assert.NoError(t, err)
assert.Equal(t, "/run/abc.pid", s.pidFile)
}
Loading