Skip to content

Commit

Permalink
portName is renamed to svcPort and port number will be supported in a…
Browse files Browse the repository at this point in the history
…ddition to port name in the future
  • Loading branch information
takumakume committed Aug 5, 2023
1 parent 8c48ab5 commit acc3ed6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
11 changes: 7 additions & 4 deletions pkg/cmd/open-svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ var (
# Open service/kubernetes-dashboard in namespace/kube-system
kubectl open-svc kubernetes-dashboard -n kube-system
# Open http-monitoring port name of service/istiod in namespace/istio-system
kubectl open-svc istiod -n istio-system --svc-port http-monitoring
# Use "https" scheme with --scheme option for connections between the apiserver
# and service/rook-ceph-mgr-dashboard in namespace/rook-ceph
kubectl open-svc rook-ceph-mgr-dashboard -n rook-ceph --scheme https
Expand All @@ -60,7 +63,7 @@ type OpenServiceOptions struct {

args []string
port int
portName string
svcPort string
address string
keepalive time.Duration
scheme string
Expand Down Expand Up @@ -233,22 +236,22 @@ func (o *OpenServiceOptions) getServiceProxyPath(svc *v1.Service) (string, error

var port v1.ServicePort

if len(o.portName) == 0 {
if len(o.svcPort) == 0 {
port = svc.Spec.Ports[0]

if l > 1 {
fmt.Fprintf(o.ErrOut, "service/%s has %d ports, defaulting port %d\n", svc.GetName(), l, port.Port)
}
} else {
for _, p := range svc.Spec.Ports {
if p.Name == o.portName {
if p.Name == o.svcPort {
port = p
break
}
}

if len(port.Name) == 0 {
return "", fmt.Errorf("port %s not found for service/%s", o.portName, svc.GetName())
return "", fmt.Errorf("port %s not found in service/%s", o.svcPort, svc.GetName())
}
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/cmd/open-svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ func TestOpenServiceOptionsGetServiceProxyPath(t *testing.T) {
"Looks like service/nginx is a headless service",
},
{
"no ports by portName",
"no ports by service port name",
&OpenServiceOptions{
IOStreams: genericclioptions.NewTestIOStreamsDiscard(),
portName: "noport",
svcPort: "noport",
},
&v1.Service{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -230,13 +230,13 @@ func TestOpenServiceOptionsGetServiceProxyPath(t *testing.T) {
},
},
"",
"port noport not found for service/nginx",
"port noport not found in service/nginx",
},
{
"not specified scheme by portName",
"not specified scheme by service port name",
&OpenServiceOptions{
IOStreams: genericclioptions.NewTestIOStreamsDiscard(),
portName: "metrics",
svcPort: "metrics",
},
&v1.Service{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -260,10 +260,10 @@ func TestOpenServiceOptionsGetServiceProxyPath(t *testing.T) {
"",
},
{
"specified scheme by portName",
"specified scheme by service port name",
&OpenServiceOptions{
IOStreams: genericclioptions.NewTestIOStreamsDiscard(),
portName: "https",
svcPort: "https",
},
&v1.Service{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -283,7 +283,7 @@ func TestOpenServiceOptionsGetServiceProxyPath(t *testing.T) {
},
},
},
"/api/v1/namespaces/default/services/nginx:https:https/proxy",
"/api/v1/namespaces/default/services/https:nginx:https/proxy",
"",
},
}
Expand Down

0 comments on commit acc3ed6

Please sign in to comment.