Sfoglia il codice sorgente

add timeout for up/down

Signed-off-by: aiordache <[email protected]>
aiordache 4 anni fa
parent
commit
b3d39931b3
3 ha cambiato i file con 23 aggiunte e 26 eliminazioni
  1. 2 2
      kube/client/client.go
  2. 3 1
      kube/client/utils.go
  3. 18 23
      kube/compose.go

+ 2 - 2
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)

+ 3 - 1
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
 }

+ 18 - 23
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