Skip to content

Commit

Permalink
make restart code a bit more idiomatic
Browse files Browse the repository at this point in the history
  • Loading branch information
phyrog committed Mar 1, 2024
1 parent ccda80d commit 23c8518
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
19 changes: 8 additions & 11 deletions pkg/containerd/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,24 @@ func (c *Config) RestartRuntime() error {
}

func getPid() (int, error) {
processList, err := psProcesses()
processes, err := psProcesses()
if err != nil {
slog.Info("psProcesses() Failed, are you using windows?")
return -1, fmt.Errorf("could not get processes: %+v", err)
return 0, fmt.Errorf("could not get processes: %+v", err)
}

var containerdProcessList = []ps.Process{}
var containerdProcesses = []ps.Process{}

for x := range processList {
process := processList[x]
for _, process := range processes {
if process.Executable() == "containerd" {
containerdProcessList = append(containerdProcessList, process)
containerdProcesses = append(containerdProcesses, process)
}

}

if len(containerdProcessList) == 1 {
return containerdProcessList[0].Pid(), nil
} else if len(containerdProcessList) == 0 {
return -1, fmt.Errorf("no containerd process found")
if len(containerdProcesses) == 1 {
return containerdProcesses[0].Pid(), nil
} else {
panic("multiple containerd processes found")
return 0, fmt.Errorf("need exactly one containerd process, found: %d", len(containerdProcesses))
}
}
4 changes: 2 additions & 2 deletions pkg/containerd/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Test_getPid(t *testing.T) {
}{
{"no containerd process found", func() ([]ps.Process, error) {
return []ps.Process{}, nil
}, -1, true},
}, 0, true},
{"single containerd process found", func() ([]ps.Process, error) {
return []ps.Process{
&mockProcess{executable: "containerd", pid: 123},
Expand All @@ -48,7 +48,7 @@ func Test_getPid(t *testing.T) {
}, 0, true},
{"error getting processes", func() ([]ps.Process, error) {
return nil, fmt.Errorf("error getting processes")
}, -1, true},
}, 0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 23c8518

Please sign in to comment.