From b3d39931b3f567ec32c0a5d05dd6663ab996116b Mon Sep 17 00:00:00 2001 From: aiordache Date: Tue, 23 Feb 2021 13:26:45 +0100 Subject: [PATCH] add timeout for up/down Signed-off-by: aiordache --- kube/client/client.go | 4 ++-- kube/client/utils.go | 4 +++- kube/compose.go | 41 ++++++++++++++++++----------------------- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/kube/client/client.go b/kube/client/client.go index 5af1c6fd..c4a02a4e 100644 --- a/kube/client/client.go +++ b/kube/client/client.go @@ -118,8 +118,8 @@ func (kc *KubeClient) GetLogs(ctx context.Context, projectName string, consumer // WaitForRunningPodState blocks until pods are in running state func (kc KubeClient) WaitForPodState(ctx context.Context, opts WaitForStatusOptions) error { var timeout time.Duration = time.Duration(60) * time.Second - if opts.Timeout > 0 { - timeout = time.Duration(opts.Timeout) * time.Second + if opts.Timeout != nil { + timeout = *opts.Timeout } selector := fmt.Sprintf("%s=%s", compose.ProjectTag, opts.ProjectName) diff --git a/kube/client/utils.go b/kube/client/utils.go index aad7ff40..e40bd3c2 100644 --- a/kube/client/utils.go +++ b/kube/client/utils.go @@ -19,6 +19,8 @@ package client import ( + "time" + "github.com/docker/compose-cli/api/compose" corev1 "k8s.io/api/core/v1" ) @@ -40,6 +42,6 @@ type WaitForStatusOptions struct { ProjectName string Services []string Status string - Timeout int + Timeout *time.Duration Log LogFunc } diff --git a/kube/compose.go b/kube/compose.go index 1a17b17d..0094dba4 100644 --- a/kube/compose.go +++ b/kube/compose.go @@ -92,20 +92,17 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options w.Event(progress.NewEvent(eventName, progress.Done, "")) - logF := func(pod string, stateReached bool, message string) { - state := progress.Done - if !stateReached { - state = progress.Working - } - w.Event(progress.NewEvent(pod, state, message)) - } - return s.client.WaitForPodState(ctx, client.WaitForStatusOptions{ ProjectName: project.Name, Services: project.ServiceNames(), Status: compose.RUNNING, - Timeout: 60, - Log: logF, + Log: func(pod string, stateReached bool, message string) { + state := progress.Done + if !stateReached { + state = progress.Working + } + w.Event(progress.NewEvent(pod, state, message)) + }, }) } @@ -133,23 +130,21 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c } events := []string{} - logF := func(pod string, stateReached bool, message string) { - state := progress.Done - if !stateReached { - state = progress.Working - } - w.Event(progress.NewEvent(pod, state, message)) - if !utils.StringContains(events, pod) { - events = append(events, pod) - } - } - err = s.client.WaitForPodState(ctx, client.WaitForStatusOptions{ ProjectName: projectName, Services: nil, Status: compose.REMOVING, - Timeout: 60, - Log: logF, + Timeout: options.Timeout, + Log: func(pod string, stateReached bool, message string) { + state := progress.Done + if !stateReached { + state = progress.Working + } + w.Event(progress.NewEvent(pod, state, message)) + if !utils.StringContains(events, pod) { + events = append(events, pod) + } + }, }) if err != nil { return err