From e5b3912ed0f9c42dfab465262b75c0e579d652f3 Mon Sep 17 00:00:00 2001 From: Alex Collins Date: Mon, 26 Dec 2022 15:35:46 -0800 Subject: [PATCH] tidy up --- probe.go | 6 +++--- up.go | 25 ++++++++++++------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/probe.go b/probe.go index 5211108..34310e0 100644 --- a/probe.go +++ b/probe.go @@ -10,7 +10,7 @@ import ( "github.com/alexec/kit/internal/types" ) -func probeLoop(ctx context.Context, stop func(), name string, probe types.Probe, callback func(name string, ok bool, err error)) { +func probeLoop(ctx context.Context, stop func(), probe types.Probe, callback func(ok bool, err error)) { defer handleCrash(stop) initialDelay := probe.GetInitialDelay() period := probe.GetPeriod() @@ -48,9 +48,9 @@ func probeLoop(ctx context.Context, stop func(), name string, probe types.Probe, } if successes == probe.GetSuccessThreshold() { - callback(name, true, nil) + callback(true, nil) } else if failures == probe.GetFailureThreshold() { - callback(name, false, err) + callback(false, err) } time.Sleep(period) } diff --git a/up.go b/up.go index b8092fe..c86fced 100644 --- a/up.go +++ b/up.go @@ -104,7 +104,7 @@ func up() *cobra.Command { for _, c := range containers { name := c.Name - state := states[name] + state := states[c.Name] if slices.Contains(exclude, name) { state.Phase = types.ExcludedPhase @@ -118,8 +118,6 @@ func up() *cobra.Command { state.Phase = types.CreatingPhase - var pd proc.Proc - logFile, err := os.Create(filepath.Join("logs", name+".log")) if err != nil { if err != nil { @@ -128,6 +126,7 @@ func up() *cobra.Command { } stdout := io.MultiWriter(logFile, states[c.Name].Stdout()) stderr := io.MultiWriter(logFile, states[c.Name].Stderr()) + var pd proc.Proc if c.Image == "" { pd = &proc.HostProc{Container: c} } else { @@ -139,7 +138,7 @@ func up() *cobra.Command { return err } - go func(state *types.State) { + go func() { defer handleCrash(stop) wg.Add(1) defer wg.Done() @@ -172,7 +171,7 @@ func up() *cobra.Command { time.Sleep(3 * time.Second) } } - }(state) + }() go func() { <-ctx.Done() @@ -184,11 +183,11 @@ func up() *cobra.Command { }() if probe := c.LivenessProbe; probe != nil { - liveFunc := func(name string, live bool, err error) { + liveFunc := func(live bool, err error) { if live { - states[name].Phase = types.LivePhase + state.Phase = types.LivePhase } else { - states[name].Phase = types.DeadPhase + state.Phase = types.DeadPhase } if err != nil { state.Log = types.LogEntry{Level: "error", Msg: err.Error()} @@ -199,20 +198,20 @@ func up() *cobra.Command { } } } - go probeLoop(ctx, stop, name, *probe, liveFunc) + go probeLoop(ctx, stop, *probe, liveFunc) } if probe := c.ReadinessProbe; probe != nil { - readyFunc := func(name string, ready bool, err error) { + readyFunc := func(ready bool, err error) { if ready { - states[name].Phase = types.ReadyPhase + state.Phase = types.ReadyPhase } else { - states[name].Phase = types.UnreadyPhase + state.Phase = types.UnreadyPhase } if err != nil { state.Log = types.LogEntry{Level: "error", Msg: err.Error()} } } - go probeLoop(ctx, stop, name, *probe, readyFunc) + go probeLoop(ctx, stop, *probe, readyFunc) } time.Sleep(time.Second) }