Skip to content

Commit

Permalink
Merge pull request #739 from porter-dev/0.4.0-fix-container-select
Browse files Browse the repository at this point in the history
[0.4.0] Spawn container selection for multi-container pods for `porter run`
  • Loading branch information
abelanger5 authored May 26, 2021
2 parents 0e5e80d + 78bb949 commit 4848de5
Showing 1 changed file with 56 additions and 11 deletions.
67 changes: 56 additions & 11 deletions cli/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,55 @@ func init() {
func run(_ *api.AuthCheckResponse, client *api.Client, args []string) error {
color.New(color.FgGreen).Println("Running", strings.Join(args[1:], " "), "for release", args[0])

podNames, err := getPods(client, namespace, args[0])
podsSimple, err := getPods(client, namespace, args[0])

if err != nil {
return fmt.Errorf("Could not retrieve list of pods: %s", err.Error())
}

// if length of pods is 0, throw error
pod := ""
var selectedPod podSimple

if len(podNames) == 0 {
if len(podsSimple) == 0 {
return fmt.Errorf("At least one pod must exist in this deployment.")
} else if len(podNames) == 1 {
pod = podNames[0]
} else if len(podsSimple) == 1 {
selectedPod = podsSimple[0]
} else {
pod, err = utils.PromptSelect("Select the pod:", podNames)
podNames := make([]string, 0)

for _, podSimple := range podsSimple {
podNames = append(podNames, podSimple.Name)
}

selectedPodName, err := utils.PromptSelect("Select the pod:", podNames)

if err != nil {
return err
}

// find selected pod
for _, podSimple := range podsSimple {
if selectedPodName == podSimple.Name {
selectedPod = podSimple
}
}
}

var selectedContainerName string

// if the selected pod has multiple container, spawn selector
if len(selectedPod.ContainerNames) == 0 {
return fmt.Errorf("At least one pod must exist in this deployment.")
} else if len(selectedPod.ContainerNames) == 1 {
selectedContainerName = selectedPod.ContainerNames[0]
} else {
selectedContainer, err := utils.PromptSelect("Select the container:", selectedPod.ContainerNames)

if err != nil {
return err
}

selectedContainerName = selectedContainer
}

restConf, err := getRESTConfig(client)
Expand All @@ -83,7 +113,7 @@ func run(_ *api.AuthCheckResponse, client *api.Client, args []string) error {
return fmt.Errorf("Could not retrieve kube credentials: %s", err.Error())
}

return executeRun(restConf, namespace, pod, args[1:])
return executeRun(restConf, namespace, selectedPod.Name, selectedContainerName, args[1:])
}

func getRESTConfig(client *api.Client) (*rest.Config, error) {
Expand Down Expand Up @@ -120,7 +150,12 @@ func getRESTConfig(client *api.Client) (*rest.Config, error) {
return restConf, nil
}

func getPods(client *api.Client, namespace, releaseName string) ([]string, error) {
type podSimple struct {
Name string
ContainerNames []string
}

func getPods(client *api.Client, namespace, releaseName string) ([]podSimple, error) {
pID := getProjectID()
cID := getClusterID()

Expand All @@ -130,16 +165,25 @@ func getPods(client *api.Client, namespace, releaseName string) ([]string, error
return nil, err
}

res := make([]string, 0)
res := make([]podSimple, 0)

for _, pod := range resp {
res = append(res, pod.ObjectMeta.Name)
containerNames := make([]string, 0)

for _, container := range pod.Spec.Containers {
containerNames = append(containerNames, container.Name)
}

res = append(res, podSimple{
Name: pod.ObjectMeta.Name,
ContainerNames: containerNames,
})
}

return res, nil
}

func executeRun(config *rest.Config, namespace, name string, args []string) error {
func executeRun(config *rest.Config, namespace, name, container string, args []string) error {
restClient, err := rest.RESTClientFor(config)

if err != nil {
Expand All @@ -159,6 +203,7 @@ func executeRun(config *rest.Config, namespace, name string, args []string) erro
req.Param("stdin", "true")
req.Param("stdout", "true")
req.Param("tty", "true")
req.Param("container", container)

t := term.TTY{
In: os.Stdin,
Expand Down

0 comments on commit 4848de5

Please sign in to comment.