Skip to content

Commit

Permalink
Specify service port with port number
Browse files Browse the repository at this point in the history
  • Loading branch information
superbrothers committed Aug 6, 2023
1 parent 3dd372c commit e689016
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions pkg/cmd/open-svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -238,38 +238,38 @@ 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
}
}

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())
}
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/cmd/open-svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e689016

Please sign in to comment.