diff --git a/README.md b/README.md index c685642..3d97799 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,12 @@ Examples: # Open service/kubernetes-dashboard in namespace/kube-system kubectl open-svc kubernetes-dashboard -n kube-system - # Open port 15014 of service/istiod in namespace/istio-system - kubectl open-svc istiod -n istio-system --svc-port 15014 - # Open port "http-monitoring" of service/istiod in namespace/istio-system kubectl open-svc istiod -n istio-system --svc-port http-monitoring + # Open port 15014 of service/istiod in namespace/istio-system + kubectl open-svc istiod -n istio-system --svc-port 15014 + # 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 diff --git a/pkg/cmd/open-svc.go b/pkg/cmd/open-svc.go index 90d4b95..c6169b2 100644 --- a/pkg/cmd/open-svc.go +++ b/pkg/cmd/open-svc.go @@ -48,12 +48,12 @@ var ( # Open service/kubernetes-dashboard in namespace/kube-system kubectl open-svc kubernetes-dashboard -n kube-system - # Open port 15014 of service/istiod in namespace/istio-system - kubectl open-svc istiod -n istio-system --svc-port 15014 - # Open port "http-monitoring" of service/istiod in namespace/istio-system kubectl open-svc istiod -n istio-system --svc-port http-monitoring + # Open port 15014 of service/istiod in namespace/istio-system + kubectl open-svc istiod -n istio-system --svc-port 15014 + # 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 @@ -238,25 +238,25 @@ func (o *OpenServiceOptions) getServiceProxyPath(svc *v1.Service) (string, error return "", fmt.Errorf("Looks like service/%s is a headless service", svc.GetName()) } - var port v1.ServicePort + var port *v1.ServicePort if o.svcPort == "" { - port = svc.Spec.Ports[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) + fmt.Fprintf(o.ErrOut, "service/%s has %d ports, defaulting port %d. You can use the another port with --svc-port flag.\n", svc.GetName(), l, port.Port) } } else { var isMatchingPort func(p v1.ServicePort) bool portNumber, err := strconv.ParseInt(o.svcPort, 10, 32) if err == nil { - klog.V(4).Info("treat svc-port %q as a port number", o.svcPort) + klog.V(4).Infof("treat svc-port %q as a port number", o.svcPort) isMatchingPort = func(p v1.ServicePort) bool { return p.Port == int32(portNumber) } } else { - klog.V(4).Info("treat svc-port %q as a port name", o.svcPort) + klog.V(4).Infof("treat svc-port %q as a port name", o.svcPort) isMatchingPort = func(p v1.ServicePort) bool { return p.Name == o.svcPort } @@ -264,12 +264,12 @@ func (o *OpenServiceOptions) getServiceProxyPath(svc *v1.Service) (string, error for _, p := range svc.Spec.Ports { if isMatchingPort(p) { - port = p + port = &p break } } - if port.Name == "" { + if port == nil { return "", fmt.Errorf("port %q not found in service/%s", o.svcPort, svc.GetName()) } } diff --git a/pkg/cmd/open-svc_test.go b/pkg/cmd/open-svc_test.go index 2640e32..6fce6c5 100644 --- a/pkg/cmd/open-svc_test.go +++ b/pkg/cmd/open-svc_test.go @@ -313,6 +313,28 @@ func TestOpenServiceOptionsGetServiceProxyPath(t *testing.T) { "/api/v1/namespaces/default/services/nginx:metrics/proxy", "", }, + { + "no port name with service port number", + &OpenServiceOptions{ + IOStreams: genericclioptions.NewTestIOStreamsDiscard(), + svcPort: "10254", + }, + &v1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "nginx", + Namespace: "default", + }, + Spec: v1.ServiceSpec{ + Ports: []v1.ServicePort{ + { + Port: 10254, + }, + }, + }, + }, + "/api/v1/namespaces/default/services/nginx/proxy", + "", + }, } for _, tt := range tests {