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
0c58170a
Commit
0c58170a
authored
Nov 06, 2015
by
kargakis
Committed by
Michail Kargakis
Dec 15, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move port utility out of endpoints controller
parent
114f6f76
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
159 additions
and
112 deletions
+159
-112
util.go
pkg/api/pod/util.go
+47
-0
util_test.go
pkg/api/pod/util_test.go
+110
-0
endpoints_controller.go
pkg/controller/endpoint/endpoints_controller.go
+2
-26
endpoints_controller_test.go
pkg/controller/endpoint/endpoints_controller_test.go
+0
-86
No files found.
pkg/api/pod/util.go
0 → 100644
View file @
0c58170a
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
pod
import
(
"fmt"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/util/intstr"
)
// FindPort locates the container port for the given pod and portName. If the
// targetPort is a number, use that. If the targetPort is a string, look that
// string up in all named ports in all containers in the target pod. If no
// match is found, fail.
func
FindPort
(
pod
*
api
.
Pod
,
svcPort
*
api
.
ServicePort
)
(
int
,
error
)
{
portName
:=
svcPort
.
TargetPort
switch
portName
.
Type
{
case
intstr
.
String
:
name
:=
portName
.
StrVal
for
_
,
container
:=
range
pod
.
Spec
.
Containers
{
for
_
,
port
:=
range
container
.
Ports
{
if
port
.
Name
==
name
&&
port
.
Protocol
==
svcPort
.
Protocol
{
return
port
.
ContainerPort
,
nil
}
}
}
case
intstr
.
Int
:
return
portName
.
IntValue
(),
nil
}
return
0
,
fmt
.
Errorf
(
"no suitable port for manifest: %s"
,
pod
.
UID
)
}
pkg/api/pod/util_test.go
0 → 100644
View file @
0c58170a
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
pod
import
(
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/util/intstr"
)
func
TestFindPort
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
name
string
containers
[]
api
.
Container
port
intstr
.
IntOrString
expected
int
pass
bool
}{{
name
:
"valid int, no ports"
,
containers
:
[]
api
.
Container
{{}},
port
:
intstr
.
FromInt
(
93
),
expected
:
93
,
pass
:
true
,
},
{
name
:
"valid int, with ports"
,
containers
:
[]
api
.
Container
{{
Ports
:
[]
api
.
ContainerPort
{{
Name
:
""
,
ContainerPort
:
11
,
Protocol
:
"TCP"
,
},
{
Name
:
"p"
,
ContainerPort
:
22
,
Protocol
:
"TCP"
,
}}}},
port
:
intstr
.
FromInt
(
93
),
expected
:
93
,
pass
:
true
,
},
{
name
:
"valid str, no ports"
,
containers
:
[]
api
.
Container
{{}},
port
:
intstr
.
FromString
(
"p"
),
expected
:
0
,
pass
:
false
,
},
{
name
:
"valid str, one ctr with ports"
,
containers
:
[]
api
.
Container
{{
Ports
:
[]
api
.
ContainerPort
{{
Name
:
""
,
ContainerPort
:
11
,
Protocol
:
"UDP"
,
},
{
Name
:
"p"
,
ContainerPort
:
22
,
Protocol
:
"TCP"
,
},
{
Name
:
"q"
,
ContainerPort
:
33
,
Protocol
:
"TCP"
,
}}}},
port
:
intstr
.
FromString
(
"q"
),
expected
:
33
,
pass
:
true
,
},
{
name
:
"valid str, two ctr with ports"
,
containers
:
[]
api
.
Container
{{},
{
Ports
:
[]
api
.
ContainerPort
{{
Name
:
""
,
ContainerPort
:
11
,
Protocol
:
"UDP"
,
},
{
Name
:
"p"
,
ContainerPort
:
22
,
Protocol
:
"TCP"
,
},
{
Name
:
"q"
,
ContainerPort
:
33
,
Protocol
:
"TCP"
,
}}}},
port
:
intstr
.
FromString
(
"q"
),
expected
:
33
,
pass
:
true
,
}}
for
_
,
tc
:=
range
testCases
{
port
,
err
:=
FindPort
(
&
api
.
Pod
{
Spec
:
api
.
PodSpec
{
Containers
:
tc
.
containers
}},
&
api
.
ServicePort
{
Protocol
:
"TCP"
,
TargetPort
:
tc
.
port
})
if
err
!=
nil
&&
tc
.
pass
{
t
.
Errorf
(
"unexpected error for %s: %v"
,
tc
.
name
,
err
)
}
if
err
==
nil
&&
!
tc
.
pass
{
t
.
Errorf
(
"unexpected non-error for %s: %d"
,
tc
.
name
,
port
)
}
if
port
!=
tc
.
expected
{
t
.
Errorf
(
"wrong result for %s: expected %d, got %d"
,
tc
.
name
,
tc
.
expected
,
port
)
}
}
}
pkg/controller/endpoint/endpoints_controller.go
View file @
0c58170a
...
...
@@ -19,13 +19,13 @@ limitations under the License.
package
endpoint
import
(
"fmt"
"reflect"
"time"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/endpoints"
"k8s.io/kubernetes/pkg/api/errors"
podutil
"k8s.io/kubernetes/pkg/api/pod"
"k8s.io/kubernetes/pkg/client/cache"
client
"k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/controller"
...
...
@@ -33,7 +33,6 @@ import (
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util/intstr"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/util/workqueue"
"k8s.io/kubernetes/pkg/watch"
...
...
@@ -302,7 +301,7 @@ func (e *EndpointController) syncService(key string) {
portName
:=
servicePort
.
Name
portProto
:=
servicePort
.
Protocol
portNum
,
err
:=
f
indPort
(
pod
,
servicePort
)
portNum
,
err
:=
podutil
.
F
indPort
(
pod
,
servicePort
)
if
err
!=
nil
{
glog
.
V
(
4
)
.
Infof
(
"Failed to find port for service %s/%s: %v"
,
service
.
Namespace
,
service
.
Name
,
err
)
continue
...
...
@@ -399,26 +398,3 @@ func (e *EndpointController) checkLeftoverEndpoints() {
e
.
queue
.
Add
(
key
)
}
}
// findPort locates the container port for the given pod and portName. If the
// targetPort is a number, use that. If the targetPort is a string, look that
// string up in all named ports in all containers in the target pod. If no
// match is found, fail.
func
findPort
(
pod
*
api
.
Pod
,
svcPort
*
api
.
ServicePort
)
(
int
,
error
)
{
portName
:=
svcPort
.
TargetPort
switch
portName
.
Type
{
case
intstr
.
String
:
name
:=
portName
.
StrVal
for
_
,
container
:=
range
pod
.
Spec
.
Containers
{
for
_
,
port
:=
range
container
.
Ports
{
if
port
.
Name
==
name
&&
port
.
Protocol
==
svcPort
.
Protocol
{
return
port
.
ContainerPort
,
nil
}
}
}
case
intstr
.
Int
:
return
portName
.
IntValue
(),
nil
}
return
0
,
fmt
.
Errorf
(
"no suitable port for manifest: %s"
,
pod
.
UID
)
}
pkg/controller/endpoint/endpoints_controller_test.go
View file @
0c58170a
...
...
@@ -68,92 +68,6 @@ func addPods(store cache.Store, namespace string, nPods int, nPorts int, nNotRea
}
}
func
TestFindPort
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
name
string
containers
[]
api
.
Container
port
intstr
.
IntOrString
expected
int
pass
bool
}{{
name
:
"valid int, no ports"
,
containers
:
[]
api
.
Container
{{}},
port
:
intstr
.
FromInt
(
93
),
expected
:
93
,
pass
:
true
,
},
{
name
:
"valid int, with ports"
,
containers
:
[]
api
.
Container
{{
Ports
:
[]
api
.
ContainerPort
{{
Name
:
""
,
ContainerPort
:
11
,
Protocol
:
"TCP"
,
},
{
Name
:
"p"
,
ContainerPort
:
22
,
Protocol
:
"TCP"
,
}}}},
port
:
intstr
.
FromInt
(
93
),
expected
:
93
,
pass
:
true
,
},
{
name
:
"valid str, no ports"
,
containers
:
[]
api
.
Container
{{}},
port
:
intstr
.
FromString
(
"p"
),
expected
:
0
,
pass
:
false
,
},
{
name
:
"valid str, one ctr with ports"
,
containers
:
[]
api
.
Container
{{
Ports
:
[]
api
.
ContainerPort
{{
Name
:
""
,
ContainerPort
:
11
,
Protocol
:
"UDP"
,
},
{
Name
:
"p"
,
ContainerPort
:
22
,
Protocol
:
"TCP"
,
},
{
Name
:
"q"
,
ContainerPort
:
33
,
Protocol
:
"TCP"
,
}}}},
port
:
intstr
.
FromString
(
"q"
),
expected
:
33
,
pass
:
true
,
},
{
name
:
"valid str, two ctr with ports"
,
containers
:
[]
api
.
Container
{{},
{
Ports
:
[]
api
.
ContainerPort
{{
Name
:
""
,
ContainerPort
:
11
,
Protocol
:
"UDP"
,
},
{
Name
:
"p"
,
ContainerPort
:
22
,
Protocol
:
"TCP"
,
},
{
Name
:
"q"
,
ContainerPort
:
33
,
Protocol
:
"TCP"
,
}}}},
port
:
intstr
.
FromString
(
"q"
),
expected
:
33
,
pass
:
true
,
}}
for
_
,
tc
:=
range
testCases
{
port
,
err
:=
findPort
(
&
api
.
Pod
{
Spec
:
api
.
PodSpec
{
Containers
:
tc
.
containers
}},
&
api
.
ServicePort
{
Protocol
:
"TCP"
,
TargetPort
:
tc
.
port
})
if
err
!=
nil
&&
tc
.
pass
{
t
.
Errorf
(
"unexpected error for %s: %v"
,
tc
.
name
,
err
)
}
if
err
==
nil
&&
!
tc
.
pass
{
t
.
Errorf
(
"unexpected non-error for %s: %d"
,
tc
.
name
,
port
)
}
if
port
!=
tc
.
expected
{
t
.
Errorf
(
"wrong result for %s: expected %d, got %d"
,
tc
.
name
,
tc
.
expected
,
port
)
}
}
}
type
serverResponse
struct
{
statusCode
int
obj
interface
{}
...
...
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