-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6811 from josedonizetti/kic-add-cmd-service
kic on mac: add service cmd support
- Loading branch information
Showing
4 changed files
with
202 additions
and
25 deletions.
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,81 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kic | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/golang/glog" | ||
"github.com/pkg/errors" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
typed_core "k8s.io/client-go/kubernetes/typed/core/v1" | ||
) | ||
|
||
// ServiceTunnel ... | ||
type ServiceTunnel struct { | ||
sshPort string | ||
sshKey string | ||
v1Core typed_core.CoreV1Interface | ||
sshConn *sshConn | ||
} | ||
|
||
// NewServiceTunnel ... | ||
func NewServiceTunnel(sshPort, sshKey string, v1Core typed_core.CoreV1Interface) *ServiceTunnel { | ||
return &ServiceTunnel{ | ||
sshPort: sshPort, | ||
sshKey: sshKey, | ||
v1Core: v1Core, | ||
} | ||
} | ||
|
||
// Start ... | ||
func (t *ServiceTunnel) Start(svcName, namespace string) ([]string, error) { | ||
svc, err := t.v1Core.Services(namespace).Get(svcName, metav1.GetOptions{}) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "getting service") | ||
} | ||
|
||
t.sshConn, err = createSSHConnWithRandomPorts(svcName, t.sshPort, t.sshKey, svc) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "creating ssh conn") | ||
} | ||
|
||
go func() { | ||
err = t.sshConn.startAndWait() | ||
if err != nil { | ||
glog.Errorf("error starting ssh tunnel: %v", err) | ||
} | ||
}() | ||
|
||
urls := make([]string, 0, len(svc.Spec.Ports)) | ||
for _, port := range t.sshConn.ports { | ||
urls = append(urls, fmt.Sprintf("http://127.0.0.1:%d", port)) | ||
} | ||
|
||
return urls, nil | ||
} | ||
|
||
// Stop ... | ||
func (t *ServiceTunnel) Stop() error { | ||
err := t.sshConn.stop() | ||
if err != nil { | ||
return errors.Wrap(err, "stopping ssh tunnel") | ||
} | ||
|
||
return nil | ||
} |
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 |
---|---|---|
|
@@ -20,16 +20,19 @@ import ( | |
"fmt" | ||
"os/exec" | ||
|
||
"github.com/phayes/freeport" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
type sshConn struct { | ||
name string | ||
service string | ||
cmd *exec.Cmd | ||
ports []int | ||
} | ||
|
||
func createSSHConn(name, sshPort, sshKey string, svc v1.Service) *sshConn { | ||
func createSSHConn(name, sshPort, sshKey string, svc *v1.Service) *sshConn { | ||
// extract sshArgs | ||
sshArgs := []string{ | ||
// TODO: document the options here | ||
|
@@ -61,6 +64,47 @@ func createSSHConn(name, sshPort, sshKey string, svc v1.Service) *sshConn { | |
} | ||
} | ||
|
||
func createSSHConnWithRandomPorts(name, sshPort, sshKey string, svc *v1.Service) (*sshConn, error) { | ||
// extract sshArgs | ||
sshArgs := []string{ | ||
// TODO: document the options here | ||
"-o", "UserKnownHostsFile=/dev/null", | ||
"-o", "StrictHostKeyChecking no", | ||
"-N", | ||
"[email protected]", | ||
"-p", sshPort, | ||
"-i", sshKey, | ||
} | ||
|
||
usedPorts := make([]int, 0, len(svc.Spec.Ports)) | ||
|
||
for _, port := range svc.Spec.Ports { | ||
freeport, err := freeport.GetFreePort() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
arg := fmt.Sprintf( | ||
"-L %d:%s:%d", | ||
freeport, | ||
svc.Spec.ClusterIP, | ||
port.Port, | ||
) | ||
|
||
sshArgs = append(sshArgs, arg) | ||
usedPorts = append(usedPorts, freeport) | ||
} | ||
|
||
cmd := exec.Command("ssh", sshArgs...) | ||
|
||
return &sshConn{ | ||
name: name, | ||
service: svc.Name, | ||
cmd: cmd, | ||
ports: usedPorts, | ||
}, nil | ||
} | ||
|
||
func (c *sshConn) startAndWait() error { | ||
fmt.Printf("starting tunnel for %s\n", c.service) | ||
err := c.cmd.Start() | ||
|
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