Skip to content

Commit

Permalink
prep for protocols other than HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Hamilton committed May 23, 2020
1 parent 11c743b commit 1989cf3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
8 changes: 7 additions & 1 deletion cmd/kubectl-tap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const (
annotationOriginalTargetPort = "kubetap.io/original-port"
annotationConfigMap = "kubetap.io/proxy-config"
annotationIsTapped = "kubetap.io/tapped"

defaultImageHTTP = "gcr.io/soluble-oss/kubetap-mitmproxy:latest"
)

// die exit the program, printing the error.
Expand Down Expand Up @@ -68,11 +70,12 @@ func main() {
listCmd := NewListCmd(client)

onCmd.Flags().StringP("port", "p", "", "target Service port")
onCmd.Flags().StringP("image", "i", "gcr.io/soluble-oss/kubetap-mitmproxy:latest", "image to run in proxy container")
onCmd.Flags().StringP("image", "i", defaultImageHTTP, "image to run in proxy container")
onCmd.Flags().Bool("https", false, "enable if target listener uses HTTPS")
onCmd.Flags().String("command-args", "mitmweb", "specify command arguments for the proxy sidecar container")
onCmd.Flags().Bool("port-forward", false, "enable to automatically kubctl port-forward to services")
onCmd.Flags().Bool("browser", false, "enable to open browser windows to service and proxy. Also enables --port-forward")
onCmd.Flags().String("protocol", "http", "specify a protocol. Supported protocols: [ http ]")

rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(onCmd)
Expand Down Expand Up @@ -104,6 +107,9 @@ func bindTapFlags(cmd *cobra.Command, _ []string) error {
if err := viper.BindPFlag("browser", cmd.Flags().Lookup("browser")); err != nil {
return err
}
if err := viper.BindPFlag("protocol", cmd.Flags().Lookup("protocol")); err != nil {
return err
}
return nil
}

Expand Down
51 changes: 34 additions & 17 deletions cmd/kubectl-tap/tap.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ web_open_browser: false

interactiveTimeoutSeconds = 90
configMapAnnotationPrefix = "target-"

protocolHTTP Protocol = "http"
//protocolTCP = "tcp"
//protocolUDP = "udp"
//protocolGRPC = "grpc"
)

var (
Expand All @@ -81,12 +86,17 @@ var (
ErrCreateResourceMismatch = errors.New("the created resource did not match the desired state")
)

// Protocol is a supported tap method, and ultimately determines what container
// is injected as a sidecar.
type Protocol string

// ProxyOptions are options used to configure the mitmproxy configmap
// We will eventually provide explicit support for modes, and methods
// which validate the configuration for a given mode will likely exist
// in the future.
type ProxyOptions struct {
Target string
Protocol Protocol
UpstreamHTTPS bool
UpstreamPort string
Mode string
Expand Down Expand Up @@ -143,23 +153,6 @@ func NewListCommand(client kubernetes.Interface, viper *viper.Viper) func(*cobra
}
}

// hasNamespace checks if a given Namespace exists.
func hasNamespace(client kubernetes.Interface, namespace string) (bool, error) {
if namespace == "" {
return false, os.ErrInvalid
}
ns, err := client.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
}
for _, n := range ns.Items {
if n.Name == namespace {
return true, nil
}
}
return false, nil
}

// NewTapCommand identifies a target employment through service selectors and modifies that
// deployment to add a mitmproxy sidecar and configmap, then adjusts the service targetPort
// to point to the mitmproxy sidecar. Mitmproxy's ConfigMap sets the upstream to the original
Expand All @@ -168,12 +161,19 @@ func NewTapCommand(client kubernetes.Interface, config *rest.Config, viper *vipe
return func(cmd *cobra.Command, args []string) error {
targetSvcName := args[0]

protocol := viper.GetString("protocol")
targetSvcPort := viper.GetInt32("proxyPort")
namespace := viper.GetString("namespace")
image := viper.GetString("proxyImage")
https := viper.GetBool("https")
portForward := viper.GetBool("portForward")
openBrowser := viper.GetBool("browser")
if Protocol(protocol) != protocolHTTP {
// only automatically adjust the image if it hasn't been overridden
if image == defaultImageHTTP { //nolint: staticcheck
//TODO: set image by protocol type
}
}
if openBrowser {
portForward = true
}
Expand Down Expand Up @@ -833,3 +833,20 @@ func untapSvc(svcClient corev1.ServiceInterface, svcName string) error {
}
return nil
}

// hasNamespace checks if a given Namespace exists.
func hasNamespace(client kubernetes.Interface, namespace string) (bool, error) {
if namespace == "" {
return false, os.ErrInvalid
}
ns, err := client.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
}
for _, n := range ns.Items {
if n.Name == namespace {
return true, nil
}
}
return false, nil
}

0 comments on commit 1989cf3

Please sign in to comment.