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

cherrypick #402 to release-1.2 branch #407

Merged
merged 1 commit into from
Jul 18, 2018
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
14 changes: 7 additions & 7 deletions pkg/controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,16 @@ func setInstanceGroupsAnnotation(existing map[string]string, igs []*compute.Inst
}

// uniq returns an array of unique service ports from the given array.
func uniq(nodePorts []utils.ServicePort) []utils.ServicePort {
portMap := map[int64]utils.ServicePort{}
for _, p := range nodePorts {
portMap[p.NodePort] = p
func uniq(svcPorts []utils.ServicePort) []utils.ServicePort {
portMap := map[string]utils.ServicePort{}
for _, p := range svcPorts {
portMap[fmt.Sprintf("%q-%d", p.ID.Service.String(), p.Port)] = p
}
nodePorts = make([]utils.ServicePort, 0, len(portMap))
svcPorts = make([]utils.ServicePort, 0, len(portMap))
for _, sp := range portMap {
nodePorts = append(nodePorts, sp)
svcPorts = append(svcPorts, sp)
}
return nodePorts
return svcPorts
}

// getReadyNodeNames returns names of schedulable, ready nodes from the node lister.
Expand Down
117 changes: 117 additions & 0 deletions pkg/controller/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ import (

api_v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"

"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/ingress-gce/pkg/annotations"
"k8s.io/ingress-gce/pkg/flags"
"k8s.io/ingress-gce/pkg/utils"
)

// Pods created in loops start from this time, for routines that
Expand Down Expand Up @@ -199,6 +202,106 @@ func TestNodeStatusChanged(t *testing.T) {
}
}

func TestUniq(t *testing.T) {
testCases := []struct {
desc string
input []utils.ServicePort
expect []utils.ServicePort
}{
{
"Empty",
[]utils.ServicePort{},
[]utils.ServicePort{},
},
{
"Two service ports",
[]utils.ServicePort{
testServicePort("ns", "name", "80", 80, 30080),
testServicePort("ns", "name", "443", 443, 30443),
},
[]utils.ServicePort{
testServicePort("ns", "name", "80", 80, 30080),
testServicePort("ns", "name", "443", 443, 30443),
},
},
{
"Two service ports with different names",
[]utils.ServicePort{
testServicePort("ns", "name1", "80", 80, 30080),
testServicePort("ns", "name2", "80", 80, 30880),
},
[]utils.ServicePort{
testServicePort("ns", "name1", "80", 80, 30080),
testServicePort("ns", "name2", "80", 80, 30880),
},
},
{
"Two duplicate service ports",
[]utils.ServicePort{
testServicePort("ns", "name", "80", 80, 30080),
testServicePort("ns", "name", "80", 80, 30080),
},
[]utils.ServicePort{
testServicePort("ns", "name", "80", 80, 30080),
},
},
{
"Two services without nodeports",
[]utils.ServicePort{
testServicePort("ns", "name", "80", 80, 0),
testServicePort("ns", "name", "443", 443, 0),
},
[]utils.ServicePort{
testServicePort("ns", "name", "80", 80, 0),
testServicePort("ns", "name", "443", 443, 0),
},
},
{
"2 out of 3 are duplicates",
[]utils.ServicePort{
testServicePort("ns", "name", "80", 80, 0),
testServicePort("ns", "name", "443", 443, 0),
testServicePort("ns", "name", "443", 443, 0),
},
[]utils.ServicePort{
testServicePort("ns", "name", "80", 80, 0),
testServicePort("ns", "name", "443", 443, 0),
},
},
{
"mix of named port and port number references",
[]utils.ServicePort{
testServicePort("ns", "name", "http", 80, 0),
testServicePort("ns", "name", "https", 443, 0),
testServicePort("ns", "name", "443", 443, 0),
},
[]utils.ServicePort{
testServicePort("ns", "name", "http", 80, 0),
testServicePort("ns", "name", "443", 443, 0),
},
},
}

for _, tc := range testCases {
res := uniq(tc.input)
if len(res) != len(tc.expect) {
t.Errorf("Test case %q: Expect %d, got %d", tc.desc, len(tc.expect), len(res))
}
for _, svcPort := range tc.expect {
found := false
for _, sp := range res {
if svcPort == sp {
found = true
}
}
if !found {
t.Errorf("Test case %q: Expect service port %v to be present. But not found", tc.desc, svcPort)
}
}
}

}

func testNode() *api_v1.Node {
return &api_v1.Node{
ObjectMeta: meta_v1.ObjectMeta{
Expand All @@ -223,3 +326,17 @@ func testNode() *api_v1.Node {
},
}
}

func testServicePort(namespace, name, port string, servicePort, nodePort int) utils.ServicePort {
return utils.ServicePort{
ID: utils.ServicePortID{
Service: types.NamespacedName{
Namespace: namespace,
Name: name,
},
Port: intstr.FromString(port),
},
Port: int32(servicePort),
NodePort: int64(nodePort),
}
}
5 changes: 5 additions & 0 deletions pkg/utils/serviceport.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"

"fmt"
"k8s.io/ingress-gce/pkg/annotations"
backendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1"
)
Expand All @@ -31,6 +32,10 @@ type ServicePortID struct {
Port intstr.IntOrString
}

func (id ServicePortID) String() string {
return fmt.Sprintf("%v/%v", id.Service.String(), id.Port.String())
}

// ServicePort maintains configuration for a single backend.
type ServicePort struct {
// Ingress backend-specified service name and port
Expand Down