-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/qovery/qovery-cli/pkg" | ||
"github.com/qovery/qovery-cli/utils" | ||
"github.com/spf13/cobra" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
var serviceListPods = &cobra.Command{ | ||
Use: "list-pods", | ||
Short: "List the pods of a service with their pods", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
utils.Capture(cmd) | ||
|
||
var portForwardRequest *pkg.PortForwardRequest | ||
var err error | ||
if len(args) > 0 { | ||
portForwardRequest, err = portForwardRequestWithApplicationUrl(args) | ||
} else { | ||
portForwardRequest, err = portForwardRequestWithoutArg() | ||
} | ||
if err != nil { | ||
utils.PrintlnError(err) | ||
return | ||
} | ||
|
||
pods, err := pkg.ExecListPods(portForwardRequest) | ||
if err != nil { | ||
utils.PrintlnError(err) | ||
return | ||
} | ||
|
||
var data [][]string | ||
for _, pod := range pods.Pods { | ||
ports := make([]string, len(pod.Ports)) | ||
for i, x := range pod.Ports { | ||
ports[i] = strconv.FormatUint(uint64(x), 10) | ||
} | ||
data = append(data, []string{pod.Name, strings.Join(ports, ", ")}) | ||
} | ||
_ = utils.PrintTable([]string{"Pod Name", "Ports"}, data) | ||
}, | ||
} | ||
|
||
func init() { | ||
var serviceListPodsCmd = serviceListPods | ||
rootCmd.AddCommand(serviceListPodsCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package pkg | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"github.com/appscode/go-querystring/query" | ||
"github.com/gorilla/websocket" | ||
"github.com/qovery/qovery-cli/utils" | ||
"net/http" | ||
"net/url" | ||
"regexp" | ||
) | ||
|
||
type PodResponse struct { | ||
Name string | ||
Ports []uint16 | ||
} | ||
type ListPodResponse struct { | ||
Pods []PodResponse | ||
} | ||
|
||
func ExecListPods(req *PortForwardRequest) (*ListPodResponse, error) { | ||
command, err := query.Values(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
wsURL, err := url.Parse("wss://ws.qovery.com/service/pods") | ||
if err != nil { | ||
return nil, err | ||
} | ||
pattern := regexp.MustCompile("%5B([0-9]+)%5D=") | ||
wsURL.RawQuery = pattern.ReplaceAllString(command.Encode(), "[${1}]=") | ||
|
||
tokenType, token, err := utils.GetAccessToken() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
headers := http.Header{"Authorization": {utils.GetAuthorizationHeaderValue(tokenType, token)}} | ||
wsConn, _, err := websocket.DefaultDialer.Dial(wsURL.String(), headers) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { | ||
_ = wsConn.Close() | ||
}() | ||
|
||
msgType, payload, err := wsConn.ReadMessage() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch msgType { | ||
case websocket.TextMessage: | ||
var data ListPodResponse | ||
err = json.Unmarshal(payload, &data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &data, nil | ||
default: | ||
return nil, errors.New("received invalid message while listing pods: " + string(rune(msgType)) + " " + string(payload)) | ||
} | ||
} |