Skip to content

Add port name to service struct used in minikube service #4011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions pkg/minikube/service/service.go
Original file line number Diff line number Diff line change
@@ -170,29 +170,36 @@ func printURLsForService(c corev1.CoreV1Interface, ip, service, namespace string
if err != nil {
return nil, errors.Wrapf(err, "service '%s' could not be found running", service)
}
var nodePorts []int32
if len(svc.Spec.Ports) > 0 {
for _, port := range svc.Spec.Ports {
if port.NodePort > 0 {
nodePorts = append(nodePorts, port.NodePort)

e := c.Endpoints(namespace)
endpoints, err := e.Get(service, metav1.GetOptions{})
m := make(map[int32]string)
if endpoints != nil && len(endpoints.Subsets) > 0 {
for _, ept := range endpoints.Subsets {
for _, p := range ept.Ports {
m[int32(p.Port)] = p.Name
}
}
}

urls := []string{}
for _, port := range nodePorts {
var doc bytes.Buffer
err = t.Execute(&doc, struct {
IP string
Port int32
}{
ip,
port,
})
if err != nil {
return nil, err
for _, port := range svc.Spec.Ports {
if port.NodePort > 0 {
var doc bytes.Buffer
err = t.Execute(&doc, struct {
IP string
Port int32
Name string
}{
ip,
port.NodePort,
m[port.TargetPort.IntVal],
})
if err != nil {
return nil, err
}
urls = append(urls, doc.String())
}

urls = append(urls, doc.String())
}
return urls, nil
}
64 changes: 55 additions & 9 deletions pkg/minikube/service/service_test.go
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ import (
"github.com/pkg/errors"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/kubernetes/typed/core/v1/fake"
@@ -36,12 +37,14 @@ import (
)

type MockClientGetter struct {
servicesMap map[string]corev1.ServiceInterface
servicesMap map[string]corev1.ServiceInterface
endpointsMap map[string]corev1.EndpointsInterface
}

func (m *MockClientGetter) GetCoreClient() (corev1.CoreV1Interface, error) {
return &MockCoreClient{
servicesMap: m.servicesMap,
servicesMap: m.servicesMap,
endpointsMap: m.endpointsMap,
}, nil
}

@@ -51,7 +54,8 @@ func (m *MockClientGetter) GetClientset(timeout time.Duration) (*kubernetes.Clie

type MockCoreClient struct {
fake.FakeCoreV1
servicesMap map[string]corev1.ServiceInterface
servicesMap map[string]corev1.ServiceInterface
endpointsMap map[string]corev1.EndpointsInterface
}

var serviceNamespaces = map[string]corev1.ServiceInterface{
@@ -68,8 +72,18 @@ var defaultNamespaceServiceInterface = &MockServiceInterface{
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{
{NodePort: int32(1111)},
{NodePort: int32(2222)},
{
NodePort: int32(1111),
TargetPort: intstr.IntOrString{
IntVal: int32(11111),
},
},
{
NodePort: int32(2222),
TargetPort: intstr.IntOrString{
IntVal: int32(22222),
},
},
},
},
},
@@ -86,8 +100,14 @@ var defaultNamespaceServiceInterface = &MockServiceInterface{
},
}

var endpointNamespaces = map[string]corev1.EndpointsInterface{
"default": defaultNamespaceEndpointInterface,
}

var defaultNamespaceEndpointInterface = &MockEndpointsInterface{}

func (m *MockCoreClient) Endpoints(namespace string) corev1.EndpointsInterface {
return &MockEndpointsInterface{}
return m.endpointsMap[namespace]
}

func (m *MockCoreClient) Services(namespace string) corev1.ServiceInterface {
@@ -124,6 +144,22 @@ var endpointMap = map[string]*v1.Endpoints{
},
},
},
"mock-dashboard": {
Subsets: []v1.EndpointSubset{
{
Ports: []v1.EndpointPort{
{
Name: "port1",
Port: int32(11111),
},
{
Name: "port2",
Port: int32(22222),
},
},
},
},
},
}

func (e MockEndpointsInterface) Get(name string, _ metav1.GetOptions) (*v1.Endpoints, error) {
@@ -195,7 +231,8 @@ func TestGetServiceListFromServicesByLabel(t *testing.T) {
func TestPrintURLsForService(t *testing.T) {
defaultTemplate := template.Must(template.New("svc-template").Parse("http://{{.IP}}:{{.Port}}"))
client := &MockCoreClient{
servicesMap: serviceNamespaces,
servicesMap: serviceNamespaces,
endpointsMap: endpointNamespaces,
}
var tests = []struct {
description string
@@ -219,6 +256,13 @@ func TestPrintURLsForService(t *testing.T) {
tmpl: template.Must(template.New("svc-arbitrary-template").Parse("{{.IP}}:{{.Port}}")),
expectedOutput: []string{"127.0.0.1:1111", "127.0.0.1:2222"},
},
{
description: "should get the name of all target ports with arbitrary format",
serviceName: "mock-dashboard",
namespace: "default",
tmpl: template.Must(template.New("svc-arbitrary-template").Parse("{{.Name}}={{.IP}}:{{.Port}}")),
expectedOutput: []string{"port1=127.0.0.1:1111", "port2=127.0.0.1:2222"},
},
{
description: "empty slice for no node ports",
serviceName: "mock-dashboard-no-ports",
@@ -361,7 +405,8 @@ func TestGetServiceURLs(t *testing.T) {
t.Parallel()

K8s = &MockClientGetter{
servicesMap: serviceNamespaces,
servicesMap: serviceNamespaces,
endpointsMap: endpointNamespaces,
}
urls, err := GetServiceURLs(test.api, test.namespace, defaultTemplate)
if err != nil && !test.err {
@@ -428,7 +473,8 @@ func TestGetServiceURLsForService(t *testing.T) {
t.Run(test.description, func(t *testing.T) {
t.Parallel()
K8s = &MockClientGetter{
servicesMap: serviceNamespaces,
servicesMap: serviceNamespaces,
endpointsMap: endpointNamespaces,
}
urls, err := GetServiceURLsForService(test.api, test.namespace, test.service, defaultTemplate)
if err != nil && !test.err {