Skip to content
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

Fix a bug in NEG controller namedport handling #932

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
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
44 changes: 12 additions & 32 deletions pkg/neg/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package neg

import (
"fmt"
"strconv"
"time"

istioV1alpha3 "istio.io/api/networking/v1alpha3"
Expand All @@ -39,6 +38,7 @@ import (
"k8s.io/client-go/util/workqueue"
"k8s.io/ingress-gce/pkg/annotations"
"k8s.io/ingress-gce/pkg/context"
"k8s.io/ingress-gce/pkg/controller/translator"
"k8s.io/ingress-gce/pkg/flags"
"k8s.io/ingress-gce/pkg/neg/metrics"
"k8s.io/ingress-gce/pkg/neg/readiness"
Expand Down Expand Up @@ -647,46 +647,26 @@ func (c *Controller) gc() {
// gatherPortMappingUsedByIngress returns a map containing port:targetport
// of all service ports of the service that are referenced by ingresses
func gatherPortMappingUsedByIngress(ings []v1beta1.Ingress, svc *apiv1.Service) negtypes.SvcPortTupleSet {
servicePorts := sets.NewString()
ingressSvcPortTuples := make(negtypes.SvcPortTupleSet)
for _, ing := range ings {
if utils.IsGLBCIngress(&ing) {
utils.TraverseIngressBackends(&ing, func(id utils.ServicePortID) bool {
if id.Service.Name == svc.Name {
servicePorts.Insert(id.Port.String())
if id.Service.Name == svc.Name && id.Service.Namespace == svc.Namespace {
servicePort := translator.ServicePort(*svc, id.Port)
if servicePort == nil {
klog.Warningf("Port %+v in Service %q not found", id.Port, id.Service.String())
return false
}
ingressSvcPortTuples.Insert(negtypes.SvcPortTuple{
Port: servicePort.Port,
Name: servicePort.Name,
TargetPort: servicePort.TargetPort.String(),
})
}
return false
})
}
}

for _, svcPort := range svc.Spec.Ports {
found := false
for _, refSvcPort := range servicePorts.List() {
port, err := strconv.Atoi(refSvcPort)
if err != nil {
// port name matches
if refSvcPort == svcPort.Name {
found = true
break
}
} else {
// service port matches
if port == int(svcPort.Port) {
found = true
break
}
}
}
if found {
ingressSvcPortTuples.Insert(negtypes.SvcPortTuple{
Port: svcPort.Port,
Name: svcPort.Name,
TargetPort: svcPort.TargetPort.String(),
})
}
}

return ingressSvcPortTuples
}

Expand Down
42 changes: 38 additions & 4 deletions pkg/neg/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ import (
)

const (
testServiceNamespace = "test-ns"
testServiceName = "test-Name"
testNamedPort = "named-Port"
testNamePort2 = "http"
testServiceNamespace = "test-ns"
testServiceName = "test-Name"
testNamedPort = "named-Port"
testNamedPortWithNumber = "80"
)

var (
Expand Down Expand Up @@ -401,6 +401,35 @@ func TestGatherPortMappingUsedByIngress(t *testing.T) {
[]int32{80, 443, 8081},
"one ingress with multiple different references to service",
},
{
[]v1beta1.Ingress{{
ObjectMeta: metav1.ObjectMeta{
Name: testServiceName,
Namespace: testServiceNamespace,
},
Spec: v1beta1.IngressSpec{
Rules: []v1beta1.IngressRule{
{
IngressRuleValue: v1beta1.IngressRuleValue{
HTTP: &v1beta1.HTTPIngressRuleValue{
Paths: []v1beta1.HTTPIngressPath{
{
Path: "/path1",
Backend: v1beta1.IngressBackend{
ServiceName: testServiceName,
ServicePort: intstr.FromString(testNamedPortWithNumber),
},
},
},
},
},
},
},
},
}},
[]int32{8881},
"ingress reference service with service port name with number",
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -999,6 +1028,11 @@ var ports = []apiv1.ServicePort{
Port: 8888,
TargetPort: intstr.FromInt(8888),
},
{
Name: testNamedPortWithNumber,
Port: 8881,
TargetPort: intstr.FromInt(8882),
},
}

func getTestSvcPortTuple(svcPort int32) negtypes.SvcPortTuple {
Expand Down