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

Sort ingresses by creation timestamp #3582

Merged
merged 1 commit into from
Jan 2, 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
6 changes: 0 additions & 6 deletions internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,7 @@ func (n *NGINXController) syncIngress(interface{}) error {
return nil
}

// sort Ingresses using the ResourceVersion field
ings := n.store.ListIngresses()
sort.SliceStable(ings, func(i, j int) bool {
ir := ings[i].ResourceVersion
jr := ings[j].ResourceVersion
return ir < jr
})

upstreams, servers := n.getBackendServers(ings)
var passUpstreams []*ingress.SSLPassthroughBackend
Expand Down
8 changes: 8 additions & 0 deletions internal/ingress/controller/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io/ioutil"
"reflect"
"sort"
"sync"
"time"

Expand Down Expand Up @@ -736,6 +737,13 @@ func (s *k8sStore) ListIngresses() []*ingress.Ingress {
ingresses = append(ingresses, ing)
}

// sort Ingresses using the CreationTimestamp field
sort.SliceStable(ingresses, func(i, j int) bool {
ir := ingresses[i].CreationTimestamp
jr := ingresses[j].CreationTimestamp
return ir.Before(&jr)
})

return ingresses
}

Expand Down
24 changes: 21 additions & 3 deletions internal/ingress/controller/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"encoding/base64"
"io/ioutil"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/ingress-nginx/internal/file"
Expand Down Expand Up @@ -939,6 +939,8 @@ func TestUpdateSecretIngressMap(t *testing.T) {
func TestListIngresses(t *testing.T) {
s := newStore(t)

sameTime := metav1.NewTime(time.Now())

ingressToIgnore := &ingress.Ingress{
Ingress: extensions.Ingress{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -947,6 +949,7 @@ func TestListIngresses(t *testing.T) {
Annotations: map[string]string{
"kubernetes.io/ingress.class": "something",
},
CreationTimestamp: sameTime,
},
Spec: extensions.IngressSpec{
Backend: &extensions.IngressBackend{
Expand All @@ -961,8 +964,9 @@ func TestListIngresses(t *testing.T) {
ingressWithoutPath := &ingress.Ingress{
Ingress: extensions.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "test-3",
Namespace: "testns",
Name: "test-3",
Namespace: "testns",
CreationTimestamp: sameTime,
},
Spec: extensions.IngressSpec{
Rules: []extensions.IngressRule{
Expand Down Expand Up @@ -995,6 +999,7 @@ func TestListIngresses(t *testing.T) {
Annotations: map[string]string{
"kubernetes.io/ingress.class": "nginx",
},
CreationTimestamp: metav1.NewTime(time.Now()),
},
Spec: extensions.IngressSpec{
Rules: []extensions.IngressRule{
Expand All @@ -1021,9 +1026,22 @@ func TestListIngresses(t *testing.T) {
s.listers.IngressWithAnnotation.Add(ingressWithNginxClass)

ingresses := s.ListIngresses()

if s := len(ingresses); s != 3 {
t.Errorf("Expected 3 Ingresses but got %v", s)
}

if ingresses[0].Name != "test-2" {
t.Errorf("Expected Ingress test-2 but got %v", ingresses[0].Name)
}

if ingresses[1].Name != "test-3" {
t.Errorf("Expected Ingress test-3 but got %v", ingresses[1].Name)
}

if ingresses[2].Name != "test-4" {
t.Errorf("Expected Ingress test-4 but got %v", ingresses[2].Name)
}
}

func TestWriteSSLSessionTicketKey(t *testing.T) {
Expand Down