Skip to content

Commit

Permalink
issued a fix for trim prefix(#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
omrikiei authored and Omri Eival committed Feb 12, 2020
1 parent cf850f7 commit 53579cc
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 110 deletions.
10 changes: 5 additions & 5 deletions cmd/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
var exposeCmd = &cobra.Command{
Use: "expose [flags] SERVICE_NAME [ports]",
Short: "Expose local machine as a service on the kubernetes cluster",
Long: `This command would inject a new service and deployment to the cluster, and open the tunnel to the server
Long: `This command would inject a new service and deployment to the cluster, and open the tunnel to the server
forwarding tunnel ingress traffic to the the same port on localhost`,
Args: cobra.MinimumNArgs(2),
Args: cobra.MinimumNArgs(2),
Example: `
# Expose a local application running on port 8000 via http
ktunnel expose kewlapp 80:8000
Expand Down Expand Up @@ -55,7 +55,7 @@ ktunnel expose redis 6379
}()

log.Info("waiting for deployment to be ready")
<- readyChan
<-readyChan

// Port-Forward
strPort := strconv.FormatInt(int64(Port), 10)
Expand All @@ -75,7 +75,7 @@ ktunnel expose redis 6379
log.Fatalf("Failed to run client: %v", err)
}
prt := int(p)
err = client.RunClient(&Host, &prt, Scheme ,&Tls, &CaFile, &ServerHostOverride, args[1:], closeChan)
err = client.RunClient(&Host, &prt, Scheme, &Tls, &CaFile, &ServerHostOverride, args[1:], closeChan)
if err != nil {
log.Fatalf("Failed to run client: %v", err)
}
Expand All @@ -89,6 +89,6 @@ func init() {
exposeCmd.Flags().StringVarP(&CaFile, "ca-file", "c", "", "TLS cert auth file")
exposeCmd.Flags().StringVarP(&Scheme, "scheme", "s", "tcp", "Connection scheme")
exposeCmd.Flags().StringVarP(&ServerHostOverride, "server-host-override", "o", "", "Server name use to verify the hostname returned by the TLS handshake")
exposeCmd.Flags().StringVarP(&Namespace, "namespace","n", "default", "Namespace")
exposeCmd.Flags().StringVarP(&Namespace, "namespace", "n", "default", "Namespace")
rootCmd.AddCommand(exposeCmd)
}
14 changes: 7 additions & 7 deletions cmd/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ var Namespace string
var injectCmd = &cobra.Command{
Use: "inject",
Short: "Inject server sidecar to the cluster and run the ktunnel client to establish a connection",
Long: `This command accepts a pod/deployment and injects the tunnel sidecar to that artifact,
Long: `This command accepts a pod/deployment and injects the tunnel sidecar to that artifact,
it then establishes a reverse tunnel`,
}

var injectDeploymentCmd = &cobra.Command{
Use: "deployment [flags] DEPLOYMENT_NAME [ports]",
Use: "deployment [flags] DEPLOYMENT_NAME [ports]",
Short: "Inject server sidecar to a deployment and run the ktunnel client to establish a connection",
Args: cobra.MinimumNArgs(2),
Args: cobra.MinimumNArgs(2),
Example: `
# Inject a back tunnel from a running deployment to local mysql and redis
ktunnel inject deploymeny mydeployment 3306 6379
Expand All @@ -48,7 +48,7 @@ ktunnel inject deploymeny mydeployment 3306 6379
stopChan := make(chan struct{}, 1)

go func() {
o.Do(func(){
o.Do(func() {
<-sigs
log.Info("Stopping streams")
close(closeChan)
Expand Down Expand Up @@ -85,7 +85,7 @@ ktunnel inject deploymeny mydeployment 3306 6379
log.Fatalf("Failed to run client: %v", err)
}
prt := int(p)
err = client.RunClient(&Host, &prt, Scheme ,&Tls, &CaFile, &ServerHostOverride, args[1:], closeChan)
err = client.RunClient(&Host, &prt, Scheme, &Tls, &CaFile, &ServerHostOverride, args[1:], closeChan)
if err != nil {
log.Fatalf("Failed to run client: %v", err)
}
Expand All @@ -99,11 +99,11 @@ func init() {
injectCmd.Flags().StringVarP(&CaFile, "ca-file", "c", "", "TLS cert auth file")
injectCmd.Flags().StringVarP(&Scheme, "scheme", "s", "tcp", "Connection scheme")
injectCmd.Flags().StringVarP(&ServerHostOverride, "server-host-override", "o", "", "Server name use to verify the hostname returned by the TLS handshake")
injectCmd.Flags().StringVarP(&Namespace, "namespace","n", "default", "Namespace")
injectCmd.Flags().StringVarP(&Namespace, "namespace", "n", "default", "Namespace")
injectDeploymentCmd.Flags().StringVarP(&CaFile, "ca-file", "c", "", "TLS cert auth file")
injectDeploymentCmd.Flags().StringVarP(&Scheme, "scheme", "s", "tcp", "Connection scheme")
injectDeploymentCmd.Flags().StringVarP(&ServerHostOverride, "server-host-override", "o", "", "Server name use to verify the hostname returned by the TLS handshake")
injectDeploymentCmd.Flags().StringVarP(&Namespace, "namespace","n", "default", "Namespace")
injectDeploymentCmd.Flags().StringVarP(&Namespace, "namespace", "n", "default", "Namespace")
injectCmd.AddCommand(injectDeploymentCmd)
rootCmd.AddCommand(injectCmd)
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
version = "1.1.8"
version = "1.1.9"
)

var Port int
Expand Down
2 changes: 1 addition & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ func init() {
serverCmd.Flags().StringVar(&CertFile, "cert", "", "tls certificate file")
serverCmd.Flags().StringVar(&KeyFile, "key", "", "tls key file")
rootCmd.AddCommand(serverCmd)
}
}
17 changes: 12 additions & 5 deletions ktunnel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
class Ktunnel < Formula
desc "Network tunneling tool for kubernetes."
homepage "https://github.com/omrikiei/ktunnel"
version "1.1.8"
version "1.1.9"
bottle :unneeded

if OS.mac?
url "https://github.com/omrikiei/ktunnel/releases/download/v1.1.8/ktunnel_1.1.8_Darwin_x86_64.tar.gz"
sha256 "dceadbf708b76ffc49edcf4cdae7cba0534df724a298cf4d4f8e231d5fbc29f6"
url "https://github.com/omrikiei/ktunnel/releases/download/v1.1.9/ktunnel_1.1.9_Darwin_x86_64.tar.gz"
sha256 "da35758a40f97f9f0d8498776fbba67e3deaaadef5f9f9e26d9611672b323140"
elsif OS.linux?
if Hardware::CPU.intel?
url "https://github.com/omrikiei/ktunnel/releases/download/v1.1.8/ktunnel_1.1.8_Linux_x86_64.tar.gz"
sha256 "ff4380b114d68ed673c1ea74fa2641a6ca72d0bdd8f0462cbce47d9e70b1a490"
url "https://github.com/omrikiei/ktunnel/releases/download/v1.1.9/ktunnel_1.1.9_Linux_x86_64.tar.gz"
sha256 "e3821abb032b39ee5c86b31248d86ec6c68d4e017476f44884fd574b084723c9"
end
if Hardware::CPU.arm?
if Hardware::CPU.is_64_bit?
url "https://github.com/omrikiei/ktunnel/releases/download/v1.1.9/ktunnel_1.1.9_Linux_arm64.tar.gz"
sha256 "de80e6100bdd82e4c9e3b572ee34fbfe83f6c477693333f4a53c4eb459650133"
else
end
end
end

Expand Down
14 changes: 7 additions & 7 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

const (
bufferSize = 1024*32
bufferSize = 1024 * 32
)

type Message struct {
Expand Down Expand Up @@ -47,7 +47,7 @@ func ReceiveData(st *pb.Tunnel_InitTunnelClient, closeStream <-chan bool, sessio
if m.ShouldClose != true {
log.Infof("%s; new session; connecting to port %d", m.RequestId, port)
// new session
conn ,err := net.Dial(strings.ToLower(scheme), fmt.Sprintf("localhost:%d", port))
conn, err := net.Dial(strings.ToLower(scheme), fmt.Sprintf("localhost:%d", port))
if err != nil {
log.Errorf("failed connecting to localhost on port %d scheme %s", port, scheme)
continue
Expand Down Expand Up @@ -187,9 +187,9 @@ func RunClient(host *string, port *int, scheme string, tls *bool, caFile, server
log.Fatalf("unsupported connection scheme %s", scheme)
}
req := &pb.SocketDataRequest{
Port: tunnelData.Source,
LogLevel: 0,
Scheme: pb.TunnelScheme(tunnelScheme),
Port: tunnelData.Source,
LogLevel: 0,
Scheme: pb.TunnelScheme(tunnelScheme),
}
stream, err := client.InitTunnel(ctx)
if err != nil {
Expand All @@ -203,7 +203,7 @@ func RunClient(host *string, port *int, scheme string, tls *bool, caFile, server
//closeStream := make(chan bool, 1)
go ReceiveData(&stream, closeStream, sessions, tunnelData.Target, scheme)
go SendData(&stream, sessions, closeStream)
<- closeStream
<-closeStream
_ = stream.CloseSend()
}
}
Expand All @@ -213,4 +213,4 @@ func RunClient(host *string, port *int, scheme string, tls *bool, caFile, server
}
wg.Wait()
return nil
}
}
17 changes: 8 additions & 9 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
"strconv"
"strings"
"sync"

)

type Session struct {
Id uuid.UUID
Id uuid.UUID
Conn *net.Conn
Buf *bytes.Buffer
Buf *bytes.Buffer
Open bool
Lock sync.Mutex
}
Expand All @@ -29,8 +28,8 @@ type RedirectRequest struct {
func NewSession(conn *net.Conn) *Session {
r := &Session{
Id: uuid.New(),
Conn: conn,
Buf: &bytes.Buffer{},
Conn: conn,
Buf: &bytes.Buffer{},
Open: true,
}
ok, err := AddSession(r)
Expand All @@ -43,8 +42,8 @@ func NewSession(conn *net.Conn) *Session {
func NewSessionFromStream(id *uuid.UUID, conn *net.Conn) *Session {
r := &Session{
Id: *id,
Conn: conn,
Buf: &bytes.Buffer{},
Conn: conn,
Buf: &bytes.Buffer{},
Open: true,
}
ok, err := AddSession(r)
Expand All @@ -62,7 +61,7 @@ func AddSession(r *Session) (bool, error) {
return true, nil
}

func GetSession(id *uuid.UUID) (*Session, bool){
func GetSession(id *uuid.UUID) (*Session, bool) {
request, ok := openSessions[id.String()]
return request, ok
}
Expand Down Expand Up @@ -112,4 +111,4 @@ func ParsePorts(s string) (*RedirectRequest, error) {
}, nil
}
return nil, errors.New(fmt.Sprintf("Error, bad tunnel format: %s", s))
}
}
72 changes: 35 additions & 37 deletions pkg/k8s/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func getKubeConfig() *rest.Config {
}

func getClients(namespace *string) {
deploymentOnce.Do(func(){
deploymentOnce.Do(func() {
clientset, err := kubernetes.NewForConfig(kubeconfig)
if err != nil {
log.Errorf("Failed to get k8s client: %v", err)
Expand All @@ -72,24 +72,24 @@ func getAllPods(namespace, deployment *string) (*apiv1.PodList, error) {
return pods, nil
}

func waitForReady(name *string, ti *time.Time, numPods int32, readyChan chan<-bool) {
func waitForReady(name *string, ti *time.Time, numPods int32, readyChan chan<- bool) {
go func() {
/*
watcher, err := podsClient.Watch(metav1.ListOptions{
TypeMeta: metav1.TypeMeta{
Kind: "pod",
},
Watch: true,
})
if err != nil {
return
}
for e := range watcher.ResultChan(){
if e.Type == watch.Deleted {
break
watcher, err := podsClient.Watch(metav1.ListOptions{
TypeMeta: metav1.TypeMeta{
Kind: "pod",
},
Watch: true,
})
if err != nil {
return
}
}*/
for e := range watcher.ResultChan(){
if e.Type == watch.Deleted {
break
}
}*/
for {
count := int32(0)
pods, err := podsClient.List(metav1.ListOptions{})
Expand All @@ -106,7 +106,7 @@ func waitForReady(name *string, ti *time.Time, numPods int32, readyChan chan<-bo
break
}
}
time.Sleep(time.Millisecond*300)
time.Sleep(time.Millisecond * 300)
}
}()
}
Expand All @@ -120,48 +120,48 @@ func hasSidecar(podSpec apiv1.PodSpec) bool {
return false
}

func newContainer(port int) *apiv1.Container{
args := []string{ "server", "-p", strconv.FormatInt(int64(port), 10)}
func newContainer(port int) *apiv1.Container {
args := []string{"server", "-p", strconv.FormatInt(int64(port), 10)}
if Verbose == true {
args = append(args, "-v")
}
return &apiv1.Container{
Name: "ktunnel",
Image: image,
Name: "ktunnel",
Image: image,
Command: []string{"/ktunnel/ktunnel"},
Args: args,
Args: args,
}
}

func newDeployment(namespace, name string, port int) *appsv1.Deployment {
replicas := int32(1)
co := newContainer(port)
return &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{},
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Name: name,
Namespace: namespace,
Labels: map[string]string{
"app.kubernetes.io/name": name,
"app.kubernetes.io/name": name,
"app.kubernetes.io/instance": name,
},
},
Spec: appsv1.DeploymentSpec{
Spec: appsv1.DeploymentSpec{
Replicas: &replicas,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/name": name,
MatchLabels: map[string]string{
"app.kubernetes.io/name": name,
"app.kubernetes.io/instance": name,
},
},
Template: apiv1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app.kubernetes.io/name": name,
"app.kubernetes.io/name": name,
"app.kubernetes.io/instance": name,
},
},
Spec: apiv1.PodSpec{
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
*co,
},
Expand All @@ -173,21 +173,19 @@ func newDeployment(namespace, name string, port int) *appsv1.Deployment {

func newService(namespace, name string, ports []apiv1.ServicePort) *apiv1.Service {
return &apiv1.Service{
TypeMeta: metav1.TypeMeta{
TypeMeta: metav1.TypeMeta{
Kind: "Service",

},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Name: name,
Namespace: namespace,

},
Spec: apiv1.ServiceSpec{
Spec: apiv1.ServiceSpec{
Ports: ports,
Selector: map[string]string{
"app.kubernetes.io/name": name,
"app.kubernetes.io/name": name,
"app.kubernetes.io/instance": name,
},
},
}
}
}
Loading

0 comments on commit 53579cc

Please sign in to comment.