Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
8620b736
Commit
8620b736
authored
Aug 02, 2018
by
David Eads
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
switch kubectl portfoward to external types
parent
23111ad4
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
123 additions
and
128 deletions
+123
-128
BUILD
pkg/kubectl/cmd/BUILD
+1
-1
portforward.go
pkg/kubectl/cmd/portforward.go
+6
-12
portforward_test.go
pkg/kubectl/cmd/portforward_test.go
+45
-44
BUILD
pkg/kubectl/util/BUILD
+2
-2
service_port.go
pkg/kubectl/util/service_port.go
+5
-5
service_port_test.go
pkg/kubectl/util/service_port_test.go
+64
-64
No files found.
pkg/kubectl/cmd/BUILD
View file @
8620b736
...
@@ -59,7 +59,6 @@ go_library(
...
@@ -59,7 +59,6 @@ go_library(
"//pkg/api/legacyscheme:go_default_library",
"//pkg/api/legacyscheme:go_default_library",
"//pkg/apis/certificates:go_default_library",
"//pkg/apis/certificates:go_default_library",
"//pkg/apis/core:go_default_library",
"//pkg/apis/core:go_default_library",
"//pkg/apis/core/v1:go_default_library",
"//pkg/client/clientset_generated/internalclientset:go_default_library",
"//pkg/client/clientset_generated/internalclientset:go_default_library",
"//pkg/client/clientset_generated/internalclientset/typed/batch/internalversion:go_default_library",
"//pkg/client/clientset_generated/internalclientset/typed/batch/internalversion:go_default_library",
"//pkg/client/clientset_generated/internalclientset/typed/core/internalversion:go_default_library",
"//pkg/client/clientset_generated/internalclientset/typed/core/internalversion:go_default_library",
...
@@ -125,6 +124,7 @@ go_library(
...
@@ -125,6 +124,7 @@ go_library(
"//staging/src/k8s.io/client-go/discovery:go_default_library",
"//staging/src/k8s.io/client-go/discovery:go_default_library",
"//staging/src/k8s.io/client-go/dynamic:go_default_library",
"//staging/src/k8s.io/client-go/dynamic:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/scheme:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/typed/autoscaling/v1:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/typed/autoscaling/v1:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
"//staging/src/k8s.io/client-go/rest:go_default_library",
"//staging/src/k8s.io/client-go/rest:go_default_library",
...
...
pkg/kubectl/cmd/portforward.go
View file @
8620b736
...
@@ -28,13 +28,13 @@ import (
...
@@ -28,13 +28,13 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/cobra"
"k8s.io/api/core/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
restclient
"k8s.io/client-go/rest"
restclient
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
"k8s.io/client-go/transport/spdy"
"k8s.io/kubernetes/pkg/api/legacyscheme"
api
"k8s.io/kubernetes/pkg/apis/core"
api
"k8s.io/kubernetes/pkg/apis/core"
apiv1
"k8s.io/kubernetes/pkg/apis/core/v1"
coreclient
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
coreclient
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil
"k8s.io/kubernetes/pkg/kubectl/cmd/util"
cmdutil
"k8s.io/kubernetes/pkg/kubectl/cmd/util"
...
@@ -143,7 +143,7 @@ func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, opts Po
...
@@ -143,7 +143,7 @@ func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, opts Po
// It rewrites ports as needed if the Service port declares targetPort.
// It rewrites ports as needed if the Service port declares targetPort.
// It returns an error when a named targetPort can't find a match in the pod, or the Service did not declare
// It returns an error when a named targetPort can't find a match in the pod, or the Service did not declare
// the port.
// the port.
func
translateServicePortToTargetPort
(
ports
[]
string
,
svc
api
.
Service
,
pod
api
.
Pod
)
([]
string
,
error
)
{
func
translateServicePortToTargetPort
(
ports
[]
string
,
svc
v1
.
Service
,
pod
v1
.
Pod
)
([]
string
,
error
)
{
var
translated
[]
string
var
translated
[]
string
for
_
,
port
:=
range
ports
{
for
_
,
port
:=
range
ports
{
// port is in the form of [LOCAL PORT]:REMOTE PORT
// port is in the form of [LOCAL PORT]:REMOTE PORT
...
@@ -184,7 +184,7 @@ func (o *PortForwardOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, arg
...
@@ -184,7 +184,7 @@ func (o *PortForwardOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, arg
}
}
builder
:=
f
.
NewBuilder
()
.
builder
:=
f
.
NewBuilder
()
.
WithScheme
(
legacyscheme
.
Scheme
)
.
WithScheme
(
scheme
.
Scheme
,
scheme
.
Scheme
.
PrioritizedVersionsAllGroups
()
...
)
.
ContinueOnError
()
.
ContinueOnError
()
.
NamespaceParam
(
o
.
Namespace
)
.
DefaultNamespace
()
NamespaceParam
(
o
.
Namespace
)
.
DefaultNamespace
()
...
@@ -210,14 +210,8 @@ func (o *PortForwardOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, arg
...
@@ -210,14 +210,8 @@ func (o *PortForwardOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, arg
// handle service port mapping to target port if needed
// handle service port mapping to target port if needed
switch
t
:=
obj
.
(
type
)
{
switch
t
:=
obj
.
(
type
)
{
case
*
api
.
Service
:
case
*
v1
.
Service
:
// TODO(juanvallejo): remove this once we convert this command to work with externals
o
.
Ports
,
err
=
translateServicePortToTargetPort
(
args
[
1
:
],
*
t
,
*
forwardablePod
)
internalPod
:=
&
api
.
Pod
{}
if
err
:=
apiv1
.
Convert_v1_Pod_To_core_Pod
(
forwardablePod
,
internalPod
,
nil
);
err
!=
nil
{
return
err
}
o
.
Ports
,
err
=
translateServicePortToTargetPort
(
args
[
1
:
],
*
t
,
*
internalPod
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
...
pkg/kubectl/cmd/portforward_test.go
View file @
8620b736
...
@@ -25,6 +25,7 @@ import (
...
@@ -25,6 +25,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/cobra"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/rest/fake"
"k8s.io/client-go/rest/fake"
...
@@ -145,17 +146,17 @@ func TestPortForward(t *testing.T) {
...
@@ -145,17 +146,17 @@ func TestPortForward(t *testing.T) {
func
TestTranslateServicePortToTargetPort
(
t
*
testing
.
T
)
{
func
TestTranslateServicePortToTargetPort
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
cases
:=
[]
struct
{
name
string
name
string
svc
api
.
Service
svc
v1
.
Service
pod
api
.
Pod
pod
v1
.
Pod
ports
[]
string
ports
[]
string
translated
[]
string
translated
[]
string
err
bool
err
bool
}{
}{
{
{
name
:
"test success 1 (int port)"
,
name
:
"test success 1 (int port)"
,
svc
:
api
.
Service
{
svc
:
v1
.
Service
{
Spec
:
api
.
ServiceSpec
{
Spec
:
v1
.
ServiceSpec
{
Ports
:
[]
api
.
ServicePort
{
Ports
:
[]
v1
.
ServicePort
{
{
{
Port
:
80
,
Port
:
80
,
TargetPort
:
intstr
.
FromInt
(
8080
),
TargetPort
:
intstr
.
FromInt
(
8080
),
...
@@ -163,11 +164,11 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
...
@@ -163,11 +164,11 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
},
},
},
},
},
},
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"http"
,
Name
:
"http"
,
ContainerPort
:
int32
(
8080
)},
ContainerPort
:
int32
(
8080
)},
...
@@ -182,10 +183,10 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
...
@@ -182,10 +183,10 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
},
},
{
{
name
:
"test success 2 (clusterIP: None)"
,
name
:
"test success 2 (clusterIP: None)"
,
svc
:
api
.
Service
{
svc
:
v1
.
Service
{
Spec
:
api
.
ServiceSpec
{
Spec
:
v1
.
ServiceSpec
{
ClusterIP
:
"None"
,
ClusterIP
:
"None"
,
Ports
:
[]
api
.
ServicePort
{
Ports
:
[]
v1
.
ServicePort
{
{
{
Port
:
80
,
Port
:
80
,
TargetPort
:
intstr
.
FromInt
(
8080
),
TargetPort
:
intstr
.
FromInt
(
8080
),
...
@@ -193,11 +194,11 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
...
@@ -193,11 +194,11 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
},
},
},
},
},
},
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"http"
,
Name
:
"http"
,
ContainerPort
:
int32
(
8080
)},
ContainerPort
:
int32
(
8080
)},
...
@@ -212,9 +213,9 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
...
@@ -212,9 +213,9 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
},
},
{
{
name
:
"test success 3 (named port)"
,
name
:
"test success 3 (named port)"
,
svc
:
api
.
Service
{
svc
:
v1
.
Service
{
Spec
:
api
.
ServiceSpec
{
Spec
:
v1
.
ServiceSpec
{
Ports
:
[]
api
.
ServicePort
{
Ports
:
[]
v1
.
ServicePort
{
{
{
Port
:
80
,
Port
:
80
,
TargetPort
:
intstr
.
FromString
(
"http"
),
TargetPort
:
intstr
.
FromString
(
"http"
),
...
@@ -226,11 +227,11 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
...
@@ -226,11 +227,11 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
},
},
},
},
},
},
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"http"
,
Name
:
"http"
,
ContainerPort
:
int32
(
8080
)},
ContainerPort
:
int32
(
8080
)},
...
@@ -248,20 +249,20 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
...
@@ -248,20 +249,20 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
},
},
{
{
name
:
"test success (targetPort omitted)"
,
name
:
"test success (targetPort omitted)"
,
svc
:
api
.
Service
{
svc
:
v1
.
Service
{
Spec
:
api
.
ServiceSpec
{
Spec
:
v1
.
ServiceSpec
{
Ports
:
[]
api
.
ServicePort
{
Ports
:
[]
v1
.
ServicePort
{
{
{
Port
:
80
,
Port
:
80
,
},
},
},
},
},
},
},
},
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"http"
,
Name
:
"http"
,
ContainerPort
:
int32
(
80
)},
ContainerPort
:
int32
(
80
)},
...
@@ -276,9 +277,9 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
...
@@ -276,9 +277,9 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
},
},
{
{
name
:
"test failure 1 (named port lookup failure)"
,
name
:
"test failure 1 (named port lookup failure)"
,
svc
:
api
.
Service
{
svc
:
v1
.
Service
{
Spec
:
api
.
ServiceSpec
{
Spec
:
v1
.
ServiceSpec
{
Ports
:
[]
api
.
ServicePort
{
Ports
:
[]
v1
.
ServicePort
{
{
{
Port
:
80
,
Port
:
80
,
TargetPort
:
intstr
.
FromString
(
"http"
),
TargetPort
:
intstr
.
FromString
(
"http"
),
...
@@ -286,11 +287,11 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
...
@@ -286,11 +287,11 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
},
},
},
},
},
},
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"https"
,
Name
:
"https"
,
ContainerPort
:
int32
(
443
)},
ContainerPort
:
int32
(
443
)},
...
@@ -305,9 +306,9 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
...
@@ -305,9 +306,9 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
},
},
{
{
name
:
"test failure 2 (service port not declared)"
,
name
:
"test failure 2 (service port not declared)"
,
svc
:
api
.
Service
{
svc
:
v1
.
Service
{
Spec
:
api
.
ServiceSpec
{
Spec
:
v1
.
ServiceSpec
{
Ports
:
[]
api
.
ServicePort
{
Ports
:
[]
v1
.
ServicePort
{
{
{
Port
:
80
,
Port
:
80
,
TargetPort
:
intstr
.
FromString
(
"http"
),
TargetPort
:
intstr
.
FromString
(
"http"
),
...
@@ -315,11 +316,11 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
...
@@ -315,11 +316,11 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
},
},
},
},
},
},
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"https"
,
Name
:
"https"
,
ContainerPort
:
int32
(
443
)},
ContainerPort
:
int32
(
443
)},
...
...
pkg/kubectl/util/BUILD
View file @
8620b736
...
@@ -15,7 +15,7 @@ go_library(
...
@@ -15,7 +15,7 @@ go_library(
importpath = "k8s.io/kubernetes/pkg/kubectl/util",
importpath = "k8s.io/kubernetes/pkg/kubectl/util",
visibility = ["//build/visible_to:pkg_kubectl_util_CONSUMERS"],
visibility = ["//build/visible_to:pkg_kubectl_util_CONSUMERS"],
deps = [
deps = [
"//
pkg/apis/core
:go_default_library",
"//
staging/src/k8s.io/api/core/v1
:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
...
@@ -83,7 +83,7 @@ go_test(
...
@@ -83,7 +83,7 @@ go_test(
],
],
embed = [":go_default_library"],
embed = [":go_default_library"],
deps = [
deps = [
"//
pkg/apis/core
:go_default_library",
"//
staging/src/k8s.io/api/core/v1
:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
],
],
)
)
pkg/kubectl/util/service_port.go
View file @
8620b736
...
@@ -19,12 +19,12 @@ package util
...
@@ -19,12 +19,12 @@ package util
import
(
import
(
"fmt"
"fmt"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/intstr"
api
"k8s.io/kubernetes/pkg/apis/core"
)
)
// Lookup containerPort number by its named port name
// Lookup containerPort number by its named port name
func
LookupContainerPortNumberByName
(
pod
api
.
Pod
,
name
string
)
(
int32
,
error
)
{
func
lookupContainerPortNumberByName
(
pod
v1
.
Pod
,
name
string
)
(
int32
,
error
)
{
for
_
,
ctr
:=
range
pod
.
Spec
.
Containers
{
for
_
,
ctr
:=
range
pod
.
Spec
.
Containers
{
for
_
,
ctrportspec
:=
range
ctr
.
Ports
{
for
_
,
ctrportspec
:=
range
ctr
.
Ports
{
if
ctrportspec
.
Name
==
name
{
if
ctrportspec
.
Name
==
name
{
...
@@ -40,12 +40,12 @@ func LookupContainerPortNumberByName(pod api.Pod, name string) (int32, error) {
...
@@ -40,12 +40,12 @@ func LookupContainerPortNumberByName(pod api.Pod, name string) (int32, error) {
// It implements the handling of resolving container named port, as well as ignoring targetPort when clusterIP=None
// It implements the handling of resolving container named port, as well as ignoring targetPort when clusterIP=None
// It returns an error when a named port can't find a match (with -1 returned), or when the service does not
// It returns an error when a named port can't find a match (with -1 returned), or when the service does not
// declare such port (with the input port number returned).
// declare such port (with the input port number returned).
func
LookupContainerPortNumberByServicePort
(
svc
api
.
Service
,
pod
api
.
Pod
,
port
int32
)
(
int32
,
error
)
{
func
LookupContainerPortNumberByServicePort
(
svc
v1
.
Service
,
pod
v1
.
Pod
,
port
int32
)
(
int32
,
error
)
{
for
_
,
svcportspec
:=
range
svc
.
Spec
.
Ports
{
for
_
,
svcportspec
:=
range
svc
.
Spec
.
Ports
{
if
svcportspec
.
Port
!=
port
{
if
svcportspec
.
Port
!=
port
{
continue
continue
}
}
if
svc
.
Spec
.
ClusterIP
==
api
.
ClusterIPNone
{
if
svc
.
Spec
.
ClusterIP
==
v1
.
ClusterIPNone
{
return
port
,
nil
return
port
,
nil
}
}
if
svcportspec
.
TargetPort
.
Type
==
intstr
.
Int
{
if
svcportspec
.
TargetPort
.
Type
==
intstr
.
Int
{
...
@@ -56,7 +56,7 @@ func LookupContainerPortNumberByServicePort(svc api.Service, pod api.Pod, port i
...
@@ -56,7 +56,7 @@ func LookupContainerPortNumberByServicePort(svc api.Service, pod api.Pod, port i
return
int32
(
svcportspec
.
TargetPort
.
IntValue
()),
nil
return
int32
(
svcportspec
.
TargetPort
.
IntValue
()),
nil
}
}
}
else
{
}
else
{
return
L
ookupContainerPortNumberByName
(
pod
,
svcportspec
.
TargetPort
.
String
())
return
l
ookupContainerPortNumberByName
(
pod
,
svcportspec
.
TargetPort
.
String
())
}
}
}
}
return
port
,
fmt
.
Errorf
(
"Service %s does not have a service port %d"
,
svc
.
Name
,
port
)
return
port
,
fmt
.
Errorf
(
"Service %s does not have a service port %d"
,
svc
.
Name
,
port
)
...
...
pkg/kubectl/util/service_port_test.go
View file @
8620b736
...
@@ -19,25 +19,25 @@ package util
...
@@ -19,25 +19,25 @@ package util
import
(
import
(
"testing"
"testing"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/intstr"
api
"k8s.io/kubernetes/pkg/apis/core"
)
)
func
TestLookupContainerPortNumberByName
(
t
*
testing
.
T
)
{
func
TestLookupContainerPortNumberByName
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
tests
:=
[]
struct
{
name
string
name
string
pod
api
.
Pod
pod
v1
.
Pod
portname
string
portname
string
portnum
int32
portnum
int32
err
bool
err
bool
}{
}{
{
{
name
:
"test success 1"
,
name
:
"test success 1"
,
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"https"
,
Name
:
"https"
,
ContainerPort
:
int32
(
443
)},
ContainerPort
:
int32
(
443
)},
...
@@ -55,11 +55,11 @@ func TestLookupContainerPortNumberByName(t *testing.T) {
...
@@ -55,11 +55,11 @@ func TestLookupContainerPortNumberByName(t *testing.T) {
},
},
{
{
name
:
"test faulure 1"
,
name
:
"test faulure 1"
,
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"https"
,
Name
:
"https"
,
ContainerPort
:
int32
(
443
)},
ContainerPort
:
int32
(
443
)},
...
@@ -76,7 +76,7 @@ func TestLookupContainerPortNumberByName(t *testing.T) {
...
@@ -76,7 +76,7 @@ func TestLookupContainerPortNumberByName(t *testing.T) {
for
_
,
tt
:=
range
tests
{
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
portnum
,
err
:=
L
ookupContainerPortNumberByName
(
tt
.
pod
,
tt
.
portname
)
portnum
,
err
:=
l
ookupContainerPortNumberByName
(
tt
.
pod
,
tt
.
portname
)
if
err
!=
nil
{
if
err
!=
nil
{
if
tt
.
err
{
if
tt
.
err
{
return
return
...
@@ -101,17 +101,17 @@ func TestLookupContainerPortNumberByName(t *testing.T) {
...
@@ -101,17 +101,17 @@ func TestLookupContainerPortNumberByName(t *testing.T) {
func
TestLookupContainerPortNumberByServicePort
(
t
*
testing
.
T
)
{
func
TestLookupContainerPortNumberByServicePort
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
tests
:=
[]
struct
{
name
string
name
string
svc
api
.
Service
svc
v1
.
Service
pod
api
.
Pod
pod
v1
.
Pod
port
int32
port
int32
containerPort
int32
containerPort
int32
err
bool
err
bool
}{
}{
{
{
name
:
"test success 1 (int port)"
,
name
:
"test success 1 (int port)"
,
svc
:
api
.
Service
{
svc
:
v1
.
Service
{
Spec
:
api
.
ServiceSpec
{
Spec
:
v1
.
ServiceSpec
{
Ports
:
[]
api
.
ServicePort
{
Ports
:
[]
v1
.
ServicePort
{
{
{
Port
:
80
,
Port
:
80
,
TargetPort
:
intstr
.
FromInt
(
8080
),
TargetPort
:
intstr
.
FromInt
(
8080
),
...
@@ -119,11 +119,11 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
...
@@ -119,11 +119,11 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
},
},
},
},
},
},
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"http"
,
Name
:
"http"
,
ContainerPort
:
int32
(
8080
)},
ContainerPort
:
int32
(
8080
)},
...
@@ -138,10 +138,10 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
...
@@ -138,10 +138,10 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
},
},
{
{
name
:
"test success 2 (clusterIP: None)"
,
name
:
"test success 2 (clusterIP: None)"
,
svc
:
api
.
Service
{
svc
:
v1
.
Service
{
Spec
:
api
.
ServiceSpec
{
Spec
:
v1
.
ServiceSpec
{
ClusterIP
:
api
.
ClusterIPNone
,
ClusterIP
:
v1
.
ClusterIPNone
,
Ports
:
[]
api
.
ServicePort
{
Ports
:
[]
v1
.
ServicePort
{
{
{
Port
:
80
,
Port
:
80
,
TargetPort
:
intstr
.
FromInt
(
8080
),
TargetPort
:
intstr
.
FromInt
(
8080
),
...
@@ -149,11 +149,11 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
...
@@ -149,11 +149,11 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
},
},
},
},
},
},
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"http"
,
Name
:
"http"
,
ContainerPort
:
int32
(
8080
)},
ContainerPort
:
int32
(
8080
)},
...
@@ -168,9 +168,9 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
...
@@ -168,9 +168,9 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
},
},
{
{
name
:
"test success 3 (named port)"
,
name
:
"test success 3 (named port)"
,
svc
:
api
.
Service
{
svc
:
v1
.
Service
{
Spec
:
api
.
ServiceSpec
{
Spec
:
v1
.
ServiceSpec
{
Ports
:
[]
api
.
ServicePort
{
Ports
:
[]
v1
.
ServicePort
{
{
{
Port
:
80
,
Port
:
80
,
TargetPort
:
intstr
.
FromString
(
"http"
),
TargetPort
:
intstr
.
FromString
(
"http"
),
...
@@ -178,11 +178,11 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
...
@@ -178,11 +178,11 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
},
},
},
},
},
},
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"http"
,
Name
:
"http"
,
ContainerPort
:
int32
(
8080
)},
ContainerPort
:
int32
(
8080
)},
...
@@ -197,20 +197,20 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
...
@@ -197,20 +197,20 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
},
},
{
{
name
:
"test success (targetPort omitted)"
,
name
:
"test success (targetPort omitted)"
,
svc
:
api
.
Service
{
svc
:
v1
.
Service
{
Spec
:
api
.
ServiceSpec
{
Spec
:
v1
.
ServiceSpec
{
Ports
:
[]
api
.
ServicePort
{
Ports
:
[]
v1
.
ServicePort
{
{
{
Port
:
80
,
Port
:
80
,
},
},
},
},
},
},
},
},
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"http"
,
Name
:
"http"
,
ContainerPort
:
int32
(
80
)},
ContainerPort
:
int32
(
80
)},
...
@@ -225,9 +225,9 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
...
@@ -225,9 +225,9 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
},
},
{
{
name
:
"test failure 1 (cannot find a matching named port)"
,
name
:
"test failure 1 (cannot find a matching named port)"
,
svc
:
api
.
Service
{
svc
:
v1
.
Service
{
Spec
:
api
.
ServiceSpec
{
Spec
:
v1
.
ServiceSpec
{
Ports
:
[]
api
.
ServicePort
{
Ports
:
[]
v1
.
ServicePort
{
{
{
Port
:
80
,
Port
:
80
,
TargetPort
:
intstr
.
FromString
(
"http"
),
TargetPort
:
intstr
.
FromString
(
"http"
),
...
@@ -235,11 +235,11 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
...
@@ -235,11 +235,11 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
},
},
},
},
},
},
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"https"
,
Name
:
"https"
,
ContainerPort
:
int32
(
443
)},
ContainerPort
:
int32
(
443
)},
...
@@ -254,9 +254,9 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
...
@@ -254,9 +254,9 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
},
},
{
{
name
:
"test failure 2 (cannot find a matching service port)"
,
name
:
"test failure 2 (cannot find a matching service port)"
,
svc
:
api
.
Service
{
svc
:
v1
.
Service
{
Spec
:
api
.
ServiceSpec
{
Spec
:
v1
.
ServiceSpec
{
Ports
:
[]
api
.
ServicePort
{
Ports
:
[]
v1
.
ServicePort
{
{
{
Port
:
80
,
Port
:
80
,
TargetPort
:
intstr
.
FromString
(
"http"
),
TargetPort
:
intstr
.
FromString
(
"http"
),
...
@@ -264,11 +264,11 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
...
@@ -264,11 +264,11 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
},
},
},
},
},
},
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"https"
,
Name
:
"https"
,
ContainerPort
:
int32
(
443
)},
ContainerPort
:
int32
(
443
)},
...
@@ -283,10 +283,10 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
...
@@ -283,10 +283,10 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
},
},
{
{
name
:
"test failure 2 (cannot find a matching service port, but ClusterIP: None)"
,
name
:
"test failure 2 (cannot find a matching service port, but ClusterIP: None)"
,
svc
:
api
.
Service
{
svc
:
v1
.
Service
{
Spec
:
api
.
ServiceSpec
{
Spec
:
v1
.
ServiceSpec
{
ClusterIP
:
api
.
ClusterIPNone
,
ClusterIP
:
v1
.
ClusterIPNone
,
Ports
:
[]
api
.
ServicePort
{
Ports
:
[]
v1
.
ServicePort
{
{
{
Port
:
80
,
Port
:
80
,
TargetPort
:
intstr
.
FromString
(
"http"
),
TargetPort
:
intstr
.
FromString
(
"http"
),
...
@@ -294,11 +294,11 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
...
@@ -294,11 +294,11 @@ func TestLookupContainerPortNumberByServicePort(t *testing.T) {
},
},
},
},
},
},
pod
:
api
.
Pod
{
pod
:
v1
.
Pod
{
Spec
:
api
.
PodSpec
{
Spec
:
v1
.
PodSpec
{
Containers
:
[]
api
.
Container
{
Containers
:
[]
v1
.
Container
{
{
{
Ports
:
[]
api
.
ContainerPort
{
Ports
:
[]
v1
.
ContainerPort
{
{
{
Name
:
"http"
,
Name
:
"http"
,
ContainerPort
:
int32
(
80
)},
ContainerPort
:
int32
(
80
)},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment