Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gpctl] Talk to ws-manager hosts directly #4788

Merged
merged 1 commit into from
Jul 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 79 additions & 42 deletions dev/gpctl/cmd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ package cmd
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"path/filepath"
"strings"

"github.com/spf13/cobra"
Expand All @@ -28,75 +31,109 @@ var workspacesCmd = &cobra.Command{
}

func init() {
workspacesCmd.PersistentFlags().StringP("tls", "t", "", "TLS certificate when connecting to a secured gRPC endpoint")
workspacesCmd.PersistentFlags().Bool("tls-from-secret", false, "get TLS certificate from Kubernetes secret")
workspacesCmd.PersistentFlags().StringP("tls-path", "t", "", "TLS certificate when connecting to a secured gRPC endpoint")
workspacesCmd.PersistentFlags().Bool("tls-from-secret", true, "get TLS certificate from Kubernetes secret")
workspacesCmd.PersistentFlags().StringP("pod", "s", "ws-manager", "Pod label for the port forwarding")
workspacesCmd.PersistentFlags().StringP("port", "p", "8080", "remote port")
workspacesCmd.PersistentFlags().String("host", "", "talk to a ws-manager host directly rather than to a pod - ignores tls-from-secret")

rootCmd.AddCommand(workspacesCmd)
}

func getWorkspacesClient(ctx context.Context) (*grpc.ClientConn, api.WorkspaceManagerClient, error) {
cfg, namespace, err := getKubeconfig()
if err != nil {
return nil, nil, err
}
clientSet, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, nil, err
}
var addr string
secopt := grpc.WithInsecure()
if host, _ := workspacesCmd.PersistentFlags().GetString("host"); host == "" {
cfg, namespace, err := getKubeconfig()
if err != nil {
return nil, nil, err
}
clientSet, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, nil, err
}

podLabel, err := workspacesCmd.Flags().GetString("pod")
if err != nil {
return nil, nil, err
}
podLabel, err := workspacesCmd.Flags().GetString("pod")
if err != nil {
return nil, nil, err
}

remotePort, err := workspacesCmd.Flags().GetString("port")
if err != nil {
return nil, nil, err
}
remotePort, err := workspacesCmd.Flags().GetString("port")
if err != nil {
return nil, nil, err
}

port := fmt.Sprintf("20202:%s", remotePort)
podName, err := util.FindAnyPodForComponent(clientSet, namespace, podLabel)
if err != nil {
return nil, nil, err
}
readychan, errchan := util.ForwardPort(ctx, cfg, namespace, podName, port)
select {
case <-readychan:
case err := <-errchan:
return nil, nil, err
case <-ctx.Done():
return nil, nil, ctx.Err()
port := fmt.Sprintf("20202:%s", remotePort)
podName, err := util.FindAnyPodForComponent(clientSet, namespace, podLabel)
if err != nil {
return nil, nil, err
}
readychan, errchan := util.ForwardPort(ctx, cfg, namespace, podName, port)
select {
case <-readychan:
case err := <-errchan:
return nil, nil, err
case <-ctx.Done():
return nil, nil, ctx.Err()
}

if certFromSecret, _ := workspacesCmd.Flags().GetBool("tls-from-secret"); certFromSecret {
certPool, err := util.CertPoolFromSecret(clientSet, namespace, "ws-manager-tls", []string{"ca.crt"})
if err != nil {
return nil, nil, xerrors.Errorf("could not load ca cert: %w", err)
}
cert, err := util.CertFromSecret(clientSet, namespace, "ws-manager-client-tls", "tls.crt", "tls.key")
if err != nil {
return nil, nil, xerrors.Errorf("could not load tls cert: %w", err)
}
creds := credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: certPool,
ServerName: "ws-manager",
})

secopt = grpc.WithTransportCredentials(creds)
}

addr = "localhost:20202"
} else {
port, _ := workspacesCmd.PersistentFlags().GetString("port")
addr = fmt.Sprintf("%s:%s", host, port)
}

secopt := grpc.WithInsecure()
if certFromSecret, _ := workspacesCmd.Flags().GetBool("tls-from-secret"); certFromSecret {
certPool, err := util.CertPoolFromSecret(clientSet, namespace, "ws-manager-tls", []string{"ca.crt"})
if fn, _ := workspacesCmd.Flags().GetString("tls-path"); fn != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: having this as another function will reduce loc in this funtion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would indeed. We've reached a point where we should pull something like this into common-go anyways

crt, err := ioutil.ReadFile(filepath.Join(fn, "tls.crt"))
if err != nil {
return nil, nil, xerrors.Errorf("could not load ca cert: %w", err)
return nil, nil, err
}
cert, err := util.CertFromSecret(clientSet, namespace, "ws-manager-client-tls", "tls.crt", "tls.key")
key, err := ioutil.ReadFile(filepath.Join(fn, "tls.key"))
if err != nil {
return nil, nil, xerrors.Errorf("could not load tls cert: %w", err)
return nil, nil, err
}
cert, err := tls.X509KeyPair(crt, key)
if err != nil {
return nil, nil, err
}

ca, err := ioutil.ReadFile(filepath.Join(fn, "ca.crt"))
if err != nil {
return nil, nil, err
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(ca)

creds := credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: certPool,
ServerName: "ws-manager",
})

secopt = grpc.WithTransportCredentials(creds)
}
if cert, _ := workspacesCmd.Flags().GetString("tls"); cert != "" {
creds, err := credentials.NewClientTLSFromFile(cert, "")
if err != nil {
return nil, nil, xerrors.Errorf("could not load tls cert: %w", err)
}

secopt = grpc.WithTransportCredentials(creds)
}
conn, err := grpc.Dial("localhost:20202", secopt)
conn, err := grpc.Dial(addr, secopt)
if err != nil {
return nil, nil, err
}
Expand Down