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

VirtualServer respecting kubernetes.io/ingress.class #937

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 10 additions & 10 deletions internal/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func (lbc *LoadBalancerController) syncEndpoint(task task) {
var mergableIngressesSlice []*configs.MergeableIngresses

for i := range ings {
if !lbc.IsNginxIngress(&ings[i]) {
if !lbc.CheckIngressClassAnnotation(ings[i].Annotations) {
continue
}
if isMinion(&ings[i]) {
Expand Down Expand Up @@ -597,7 +597,7 @@ func (lbc *LoadBalancerController) GetManagedIngresses() ([]extensions.Ingress,
ings, _ := lbc.ingressLister.List()
for i := range ings.Items {
ing := ings.Items[i]
if !lbc.IsNginxIngress(&ing) {
if !lbc.CheckIngressClassAnnotation(ing.Annotations) {
continue
}
if isMinion(&ing) {
Expand Down Expand Up @@ -1324,7 +1324,7 @@ items:
continue
}

if !lbc.IsNginxIngress(&ing) {
if !lbc.CheckIngressClassAnnotation(ing.Annotations) {
continue
}

Expand Down Expand Up @@ -1376,7 +1376,7 @@ items:
func (lbc *LoadBalancerController) EnqueueIngressForService(svc *api_v1.Service) {
ings := lbc.getIngressesForService(svc)
for _, ing := range ings {
if !lbc.IsNginxIngress(&ing) {
if !lbc.CheckIngressClassAnnotation(ing.Annotations) {
continue
}
if isMinion(&ing) {
Expand Down Expand Up @@ -2235,10 +2235,10 @@ func (lbc *LoadBalancerController) getServiceForIngressBackend(backend *extensio
return nil, fmt.Errorf("service %s doesn't exist", svcKey)
}

// IsNginxIngress checks if resource ingress class annotation (if exists) is matching with ingress controller class
// If annotation is absent and use-ingress-class-only enabled - ingress resource would ignore
func (lbc *LoadBalancerController) IsNginxIngress(ing *extensions.Ingress) bool {
if class, exists := ing.Annotations[ingressClassKey]; exists {
// CheckIngressClassAnnotation checks if the map of annotations contains the ingress class key, and if the value matches
// the configured ingress class name.
func (lbc *LoadBalancerController) CheckIngressClassAnnotation(annotations map[string]string) bool {
if class, exists := annotations[ingressClassKey]; exists {
if lbc.useIngressClassOnly {
return class == lbc.ingressClass
}
Expand Down Expand Up @@ -2291,7 +2291,7 @@ func (lbc *LoadBalancerController) getMinionsForMaster(master *configs.IngressEx
var minionPaths = make(map[string]*extensions.Ingress)

for i := range ings.Items {
if !lbc.IsNginxIngress(&ings.Items[i]) {
if !lbc.CheckIngressClassAnnotation(ings.Items[i].Annotations) {
continue
}
if !isMinion(&ings.Items[i]) {
Expand Down Expand Up @@ -2345,7 +2345,7 @@ func (lbc *LoadBalancerController) FindMasterForMinion(minion *extensions.Ingres
}

for i := range ings.Items {
if !lbc.IsNginxIngress(&ings.Items[i]) {
if !lbc.CheckIngressClassAnnotation(ings.Items[i].Annotations) {
continue
}
if !lbc.configurator.HasIngress(&ings.Items[i]) {
Expand Down
130 changes: 39 additions & 91 deletions internal/k8s/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,131 +23,79 @@ import (
"k8s.io/client-go/tools/cache"
)

func TestIsNginxIngress(t *testing.T) {
func TestCheckIngressClassAnnotation(t *testing.T) {
ingressClass := "ing-ctrl"

lbcWithoutIngressClassOnly := &LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: false,
metricsCollector: collectors.NewControllerFakeCollector(),
}

lbcWithIngressClassOnly := &LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: true,
metricsCollector: collectors.NewControllerFakeCollector(),
}

var testsWithoutIngressClassOnly = []struct {
lbc *LoadBalancerController
ing *extensions.Ingress
expected bool
lbc *LoadBalancerController
annotations map[string]string
expected bool
}{
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: false,
metricsCollector: collectors.NewControllerFakeCollector(),
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{ingressClassKey: ""},
},
},
lbcWithoutIngressClassOnly,
map[string]string{ingressClassKey: ""},
true,
},
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: false,
metricsCollector: collectors.NewControllerFakeCollector(),
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{ingressClassKey: "gce"},
},
},
lbcWithoutIngressClassOnly,
map[string]string{ingressClassKey: "gce"},
false,
},
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: false,
metricsCollector: collectors.NewControllerFakeCollector(),
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{ingressClassKey: ingressClass},
},
},
lbcWithoutIngressClassOnly,
map[string]string{ingressClassKey: ingressClass},
true,
},
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: false,
metricsCollector: collectors.NewControllerFakeCollector(),
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{},
},
},
lbcWithoutIngressClassOnly,
map[string]string{},
true,
},
}

var testsWithIngressClassOnly = []struct {
lbc *LoadBalancerController
ing *extensions.Ingress
expected bool
lbc *LoadBalancerController
annotations map[string]string
expected bool
}{
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: true,
metricsCollector: collectors.NewControllerFakeCollector(),
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{ingressClassKey: ""},
},
},
lbcWithIngressClassOnly,
map[string]string{ingressClassKey: ""},
false,
},
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: true,
metricsCollector: collectors.NewControllerFakeCollector(),
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{ingressClassKey: "gce"},
},
},
lbcWithIngressClassOnly,
map[string]string{ingressClassKey: "gce"},
false,
},
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: true,
metricsCollector: collectors.NewControllerFakeCollector(),
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{ingressClassKey: ingressClass},
},
},
lbcWithIngressClassOnly,
map[string]string{ingressClassKey: ingressClass},
true,
},
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: true,
metricsCollector: collectors.NewControllerFakeCollector(),
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{},
},
},
lbcWithIngressClassOnly,
map[string]string{},
false,
},
}

for _, test := range testsWithoutIngressClassOnly {
if result := test.lbc.IsNginxIngress(test.ing); result != test.expected {
if result := test.lbc.CheckIngressClassAnnotation(test.annotations); result != test.expected {
classAnnotation := "N/A"
if class, exists := test.ing.Annotations[ingressClassKey]; exists {
if class, exists := test.annotations[ingressClassKey]; exists {
classAnnotation = class
}
t.Errorf("lbc.IsNginxIngress(ing), lbc.ingressClass=%v, lbc.useIngressClassOnly=%v, ing.Annotations['%v']=%v; got %v, expected %v",
Expand All @@ -156,9 +104,9 @@ func TestIsNginxIngress(t *testing.T) {
}

for _, test := range testsWithIngressClassOnly {
if result := test.lbc.IsNginxIngress(test.ing); result != test.expected {
if result := test.lbc.CheckIngressClassAnnotation(test.annotations); result != test.expected {
classAnnotation := "N/A"
if class, exists := test.ing.Annotations[ingressClassKey]; exists {
if class, exists := test.annotations[ingressClassKey]; exists {
classAnnotation = class
}
t.Errorf("lbc.IsNginxIngress(ing), lbc.ingressClass=%v, lbc.useIngressClassOnly=%v, ing.Annotations['%v']=%v; got %v, expected %v",
Expand Down
26 changes: 23 additions & 3 deletions internal/k8s/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func createIngressHandlers(lbc *LoadBalancerController) cache.ResourceEventHandl
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
ingress := obj.(*v1beta1.Ingress)
if !lbc.IsNginxIngress(ingress) {
if !lbc.CheckIngressClassAnnotation(ingress.Annotations) {
glog.Infof("Ignoring Ingress %v based on Annotation %v", ingress.Name, ingressClassKey)
return
}
Expand All @@ -114,7 +114,7 @@ func createIngressHandlers(lbc *LoadBalancerController) cache.ResourceEventHandl
return
}
}
if !lbc.IsNginxIngress(ingress) {
if !lbc.CheckIngressClassAnnotation(ingress.Annotations) {
return
}
if isMinion(ingress) {
Expand All @@ -133,7 +133,7 @@ func createIngressHandlers(lbc *LoadBalancerController) cache.ResourceEventHandl
UpdateFunc: func(old, current interface{}) {
c := current.(*v1beta1.Ingress)
o := old.(*v1beta1.Ingress)
if !lbc.IsNginxIngress(c) {
if !lbc.CheckIngressClassAnnotation(c.Annotations) {
return
}
if hasChanges(o, c) {
Expand Down Expand Up @@ -316,6 +316,10 @@ func createVirtualServerHandlers(lbc *LoadBalancerController) cache.ResourceEven
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
vs := obj.(*conf_v1.VirtualServer)
if !lbc.CheckIngressClassAnnotation(vs.Annotations) {
glog.Infof("Ignoring VirtualServer %v based on Annotation %v", vs.Name, ingressClassKey)
return
}
glog.V(3).Infof("Adding VirtualServer: %v", vs.Name)
lbc.AddSyncQueue(vs)
},
Expand All @@ -333,11 +337,17 @@ func createVirtualServerHandlers(lbc *LoadBalancerController) cache.ResourceEven
return
}
}
if !lbc.CheckIngressClassAnnotation(vs.Annotations) {
return
}
glog.V(3).Infof("Removing VirtualServer: %v", vs.Name)
lbc.AddSyncQueue(vs)
},
UpdateFunc: func(old, cur interface{}) {
curVs := cur.(*conf_v1.VirtualServer)
if !lbc.CheckIngressClassAnnotation(curVs.Annotations) {
return
}
if !reflect.DeepEqual(old, cur) {
glog.V(3).Infof("VirtualServer %v changed, syncing", curVs.Name)
lbc.AddSyncQueue(curVs)
Expand All @@ -350,6 +360,10 @@ func createVirtualServerRouteHandlers(lbc *LoadBalancerController) cache.Resourc
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
vsr := obj.(*conf_v1.VirtualServerRoute)
if !lbc.CheckIngressClassAnnotation(vsr.Annotations) {
glog.Infof("Ignoring VirtualServerRoute %v based on Annotation %v", vsr.Name, ingressClassKey)
return
}
glog.V(3).Infof("Adding VirtualServerRoute: %v", vsr.Name)
lbc.AddSyncQueue(vsr)
},
Expand All @@ -367,11 +381,17 @@ func createVirtualServerRouteHandlers(lbc *LoadBalancerController) cache.Resourc
return
}
}
if !lbc.CheckIngressClassAnnotation(vsr.Annotations) {
return
}
glog.V(3).Infof("Removing VirtualServerRoute: %v", vsr.Name)
lbc.AddSyncQueue(vsr)
},
UpdateFunc: func(old, cur interface{}) {
curVsr := cur.(*conf_v1.VirtualServerRoute)
if !lbc.CheckIngressClassAnnotation(curVsr.Annotations) {
return
}
if !reflect.DeepEqual(old, cur) {
glog.V(3).Infof("VirtualServerRoute %v changed, syncing", curVsr.Name)
lbc.AddSyncQueue(curVsr)
Expand Down