Sfoglia il codice sorgente

fix: connect term sizing to stream io

Joseph Gilley 4 anni fa
parent
commit
8a13ebae96
4 ha cambiato i file con 66 aggiunte e 46 eliminazioni
  1. 3 0
      .gitignore
  2. 11 10
      CONTRIBUTING.md
  3. 52 33
      cli/cmd/run.go
  4. 0 3
      go.sum

+ 3 - 0
.gitignore

@@ -55,3 +55,6 @@ override.tf.json
 # Ignore CLI configuration files
 .terraformrc
 terraform.rc
+
+# Ignore .gitignore files
+.vscode

+ 11 - 10
CONTRIBUTING.md

@@ -4,16 +4,17 @@ First off, thanks for considering contributing to Porter. There are many types o
 
 Before you contribute, make sure to read these guidelines thoroughly, so that you can get your pull request reviewed and finalized as quickly as possible. 
 
-- [Reporting Issues](#reporting-issues)
-- [Development Process Overview](#development-process-overview)
-  * [Good first issues and bug fixes](#good-first-issues-and-bug-fixes)
-  * [Improving Documentation and Writing Tutorials](#improving-documentation-and-writing-tutorials)
-  * [Features](#features)
-- [Writing Code](#writing-code)
-  * [Navigating the Codebase](#navigating-the-codebase)
-  * [Getting started](#getting-started)
-  * [Testing](#testing)
-- [Making the PR](#making-the-pr)
+- [Contributing to Porter](#contributing-to-porter)
+  - [Reporting Issues](#reporting-issues)
+  - [Development Process Overview](#development-process-overview)
+    - [Good first issues and bug fixes](#good-first-issues-and-bug-fixes)
+    - [Improving Documentation and Writing Tutorials](#improving-documentation-and-writing-tutorials)
+    - [Features](#features)
+  - [Writing Code](#writing-code)
+    - [Navigating the Codebase](#navigating-the-codebase)
+    - [Getting started](#getting-started)
+    - [Testing](#testing)
+  - [Making the PR](#making-the-pr)
 
 > **Note:** we're still working on our contributing process, as we're a young project. If you'd like to suggest or discuss changes to this document or the process in general, we're very open to suggestions and would appreciate if you reach out on Discord or at [contact@getporter.dev](mailto:contact@getporter.dev)! To suggest additions to this document, feel free to raise an issue or make a PR with the changes. 
 

+ 52 - 33
cli/cmd/run.go

@@ -14,6 +14,7 @@ import (
 	"github.com/spf13/cobra"
 	v1 "k8s.io/api/core/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"k8s.io/apimachinery/pkg/fields"
 	"k8s.io/kubectl/pkg/util/term"
 
 	"k8s.io/apimachinery/pkg/runtime"
@@ -277,28 +278,51 @@ func executeRun(config *PorterRunSharedConfig, namespace, name, container string
 	return nil
 }
 
-func executeRunEphemeral(config *PorterRunSharedConfig, namespace, name, container string, args []string) error {
-	existing, err := getExistingPod(config, name, namespace)
-
+func waitForPod(config *PorterRunSharedConfig, pod *v1.Pod) error {
+	watch, err := config.Clientset.CoreV1().Pods(pod.Namespace).Watch(context.Background(), metav1.ListOptions{
+		FieldSelector: fields.OneTermEqualSelector("metadata.name", pod.Name).String(),
+	})
 	if err != nil {
 		return err
 	}
+	defer watch.Stop()
+	for {
+		select {
+		case evt := <-watch.ResultChan():
+			pod, ok := evt.Object.(*v1.Pod)
+			if !ok {
+				return fmt.Errorf("unexpected object type: %T", evt.Object)
+			}
+			ready := false
+			conditions := pod.Status.Conditions
+			for i := range conditions {
+				if conditions[i].Type == v1.PodReady {
+					ready = pod.Status.Conditions[i].Status == v1.ConditionTrue
+				}
+			}
+			if ready {
+				return nil
+			}
+		case <-time.After(time.Second * 30):
+			return fmt.Errorf("timed out waiting for pod")
+		}
+	}
+}
 
-	newPod, err := createPodFromExisting(config, existing, args)
+func executeRunEphemeral(config *PorterRunSharedConfig, namespace, name, container string, args []string) error {
+	existing, err := getExistingPod(config, name, namespace)
 
 	if err != nil {
 		return err
 	}
 
+	newPod, err := createPodFromExisting(config, existing, args)
 	podName := newPod.ObjectMeta.Name
 
-	t := term.TTY{
-		In:  os.Stdin,
-		Out: os.Stdout,
-		Raw: true,
-	}
+	err = waitForPod(config, newPod)
 
-	fn := func() error {
+	if err == nil {
+		color.New(color.FgYellow).Println("Attempting connection to the container, this may take up to 10 seconds. If you don't see a command prompt, try pressing enter.")
 		req := config.RestClient.Post().
 			Resource("pods").
 			Name(podName).
@@ -310,33 +334,28 @@ func executeRunEphemeral(config *PorterRunSharedConfig, namespace, name, contain
 		req.Param("tty", "true")
 		req.Param("container", container)
 
-		exec, err := remotecommand.NewSPDYExecutor(config.RestConf, "POST", req.URL())
-
-		if err != nil {
-			return err
+		t := term.TTY{
+			In:  os.Stdin,
+			Out: os.Stdout,
+			Raw: true,
 		}
-
-		return exec.Stream(remotecommand.StreamOptions{
-			Stdin:  os.Stdin,
-			Stdout: os.Stdout,
-			Stderr: os.Stderr,
-			Tty:    true,
+		size := t.GetSize()
+		sizeQueue := t.MonitorSize(size)
+		err = t.Safe(func() error {
+			exec, err := remotecommand.NewSPDYExecutor(config.RestConf, "POST", req.URL())
+			if err != nil {
+				return err
+			}
+			return exec.Stream(remotecommand.StreamOptions{
+				Stdin:             os.Stdin,
+				Stdout:            os.Stdout,
+				Stderr:            os.Stderr,
+				Tty:               true,
+				TerminalSizeQueue: sizeQueue,
+			})
 		})
 	}
 
-	color.New(color.FgYellow).Println("Attempting connection to the container, this may take up to 10 seconds. If you don't see a command prompt, try pressing enter.")
-
-	for i := 0; i < 5; i++ {
-		err = t.Safe(fn)
-
-		if err == nil {
-			break
-		}
-
-		time.Sleep(2 * time.Second)
-
-	}
-
 	// ugly way to catch no TTY errors, such as when running command "echo \"hello\""
 	if err != nil {
 		color.New(color.FgYellow).Println("Could not open a shell to this container. Container logs:\n")

+ 0 - 3
go.sum

@@ -527,7 +527,6 @@ github.com/google/go-github/v29 v29.0.3/go.mod h1:CHKiKKPHJ0REzfwc14QMklvtHwCveD
 github.com/google/go-github/v30 v30.1.0/go.mod h1:n8jBpHl45a/rlBUtRJMOG4GhNADUQFEufcolZ95JfU8=
 github.com/google/go-github/v33 v33.0.0 h1:qAf9yP0qc54ufQxzwv+u9H0tiVOnPJxo0lI/JXqw3ZM=
 github.com/google/go-github/v33 v33.0.0/go.mod h1:GMdDnVZY/2TsWgp/lkYnpSAh6TrzhANBBwm6k6TTEXg=
-github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
 github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
 github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
 github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
@@ -1413,8 +1412,6 @@ golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d/go.mod h1:h1NjWce9XRLGQEsW7w
 golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c=
 golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
-golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=