mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-18 07:34:12 +00:00
v1beta3 Pod refactor
This commit is contained in:
@@ -64,17 +64,18 @@ func (e *EndpointController) SyncServiceEndpoints() error {
|
||||
continue
|
||||
}
|
||||
endpoints := []string{}
|
||||
|
||||
for _, pod := range pods.Items {
|
||||
port, err := findPort(&pod.DesiredState.Manifest, service.Spec.ContainerPort)
|
||||
port, err := findPort(&pod, service.Spec.ContainerPort)
|
||||
if err != nil {
|
||||
glog.Errorf("Failed to find port for service: %v, %v", service, err)
|
||||
continue
|
||||
}
|
||||
if len(pod.CurrentState.PodIP) == 0 {
|
||||
if len(pod.Status.PodIP) == 0 {
|
||||
glog.Errorf("Failed to find an IP for pod: %v", pod)
|
||||
continue
|
||||
}
|
||||
endpoints = append(endpoints, net.JoinHostPort(pod.CurrentState.PodIP, strconv.Itoa(port)))
|
||||
endpoints = append(endpoints, net.JoinHostPort(pod.Status.PodIP, strconv.Itoa(port)))
|
||||
}
|
||||
currentEndpoints, err := e.client.Endpoints(service.Namespace).Get(service.Name)
|
||||
if err != nil {
|
||||
@@ -137,10 +138,10 @@ func endpointsEqual(e *api.Endpoints, endpoints []string) bool {
|
||||
}
|
||||
|
||||
// findPort locates the container port for the given manifest and portName.
|
||||
func findPort(manifest *api.ContainerManifest, portName util.IntOrString) (int, error) {
|
||||
func findPort(pod *api.Pod, portName util.IntOrString) (int, error) {
|
||||
firstContainerPort := 0
|
||||
if len(manifest.Containers[0].Ports) > 0 {
|
||||
firstContainerPort = manifest.Containers[0].Ports[0].ContainerPort
|
||||
if len(pod.Spec.Containers) > 0 && len(pod.Spec.Containers[0].Ports) > 0 {
|
||||
firstContainerPort = pod.Spec.Containers[0].Ports[0].ContainerPort
|
||||
}
|
||||
|
||||
switch portName.Kind {
|
||||
@@ -152,7 +153,7 @@ func findPort(manifest *api.ContainerManifest, portName util.IntOrString) (int,
|
||||
break
|
||||
}
|
||||
name := portName.StrVal
|
||||
for _, container := range manifest.Containers {
|
||||
for _, container := range pod.Spec.Containers {
|
||||
for _, port := range container.Ports {
|
||||
if port.Name == name {
|
||||
return port.ContainerPort, nil
|
||||
@@ -169,5 +170,5 @@ func findPort(manifest *api.ContainerManifest, portName util.IntOrString) (int,
|
||||
return portName.IntVal, nil
|
||||
}
|
||||
|
||||
return 0, fmt.Errorf("no suitable port for manifest: %s", manifest.ID)
|
||||
return 0, fmt.Errorf("no suitable port for manifest: %s", pod.UID)
|
||||
}
|
||||
|
@@ -36,20 +36,18 @@ func newPodList(count int) *api.PodList {
|
||||
pods = append(pods, api.Pod{
|
||||
TypeMeta: api.TypeMeta{APIVersion: testapi.Version()},
|
||||
ObjectMeta: api.ObjectMeta{Name: fmt.Sprintf("pod%d", i)},
|
||||
DesiredState: api.PodState{
|
||||
Manifest: api.ContainerManifest{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Ports: []api.Port{
|
||||
{
|
||||
ContainerPort: 8080,
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Ports: []api.Port{
|
||||
{
|
||||
ContainerPort: 8080,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
CurrentState: api.PodState{
|
||||
Status: api.PodStatus{
|
||||
PodIP: "1.2.3.4",
|
||||
},
|
||||
})
|
||||
@@ -61,95 +59,101 @@ func newPodList(count int) *api.PodList {
|
||||
}
|
||||
|
||||
func TestFindPort(t *testing.T) {
|
||||
manifest := api.ContainerManifest{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Ports: []api.Port{
|
||||
{
|
||||
Name: "foo",
|
||||
ContainerPort: 8080,
|
||||
HostPort: 9090,
|
||||
},
|
||||
{
|
||||
Name: "bar",
|
||||
ContainerPort: 8000,
|
||||
HostPort: 9000,
|
||||
pod := api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Ports: []api.Port{
|
||||
{
|
||||
Name: "foo",
|
||||
ContainerPort: 8080,
|
||||
HostPort: 9090,
|
||||
},
|
||||
{
|
||||
Name: "bar",
|
||||
ContainerPort: 8000,
|
||||
HostPort: 9000,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
emptyPortsManifest := api.ContainerManifest{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Ports: []api.Port{},
|
||||
|
||||
emptyPortsPod := api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Ports: []api.Port{},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
manifest api.ContainerManifest
|
||||
pod api.Pod
|
||||
portName util.IntOrString
|
||||
|
||||
wport int
|
||||
werr bool
|
||||
}{
|
||||
{
|
||||
manifest,
|
||||
pod,
|
||||
util.IntOrString{Kind: util.IntstrString, StrVal: "foo"},
|
||||
8080,
|
||||
false,
|
||||
},
|
||||
{
|
||||
manifest,
|
||||
pod,
|
||||
util.IntOrString{Kind: util.IntstrString, StrVal: "bar"},
|
||||
8000,
|
||||
false,
|
||||
},
|
||||
{
|
||||
manifest,
|
||||
pod,
|
||||
util.IntOrString{Kind: util.IntstrInt, IntVal: 8000},
|
||||
8000,
|
||||
false,
|
||||
},
|
||||
{
|
||||
manifest,
|
||||
pod,
|
||||
util.IntOrString{Kind: util.IntstrInt, IntVal: 7000},
|
||||
7000,
|
||||
false,
|
||||
},
|
||||
{
|
||||
manifest,
|
||||
pod,
|
||||
util.IntOrString{Kind: util.IntstrString, StrVal: "baz"},
|
||||
0,
|
||||
true,
|
||||
},
|
||||
{
|
||||
manifest,
|
||||
pod,
|
||||
util.IntOrString{Kind: util.IntstrString, StrVal: ""},
|
||||
8080,
|
||||
false,
|
||||
},
|
||||
{
|
||||
manifest,
|
||||
pod,
|
||||
util.IntOrString{Kind: util.IntstrInt, IntVal: 0},
|
||||
8080,
|
||||
false,
|
||||
},
|
||||
{
|
||||
emptyPortsManifest,
|
||||
emptyPortsPod,
|
||||
util.IntOrString{Kind: util.IntstrString, StrVal: ""},
|
||||
0,
|
||||
true,
|
||||
},
|
||||
{
|
||||
emptyPortsManifest,
|
||||
emptyPortsPod,
|
||||
util.IntOrString{Kind: util.IntstrInt, IntVal: 0},
|
||||
0,
|
||||
true,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
port, err := findPort(&test.manifest, test.portName)
|
||||
port, err := findPort(&test.pod, test.portName)
|
||||
if port != test.wport {
|
||||
t.Errorf("Expected port %d, Got %d", test.wport, port)
|
||||
}
|
||||
|
Reference in New Issue
Block a user