Add new flag to watch ingressclass by name instead of spec (#7609)
This commit is contained in:
parent
89eee0deba
commit
cda59ccc9c
8 changed files with 201 additions and 5 deletions
|
|
@ -42,4 +42,8 @@ type IngressClassConfiguration struct {
|
|||
// WatchWithoutClass defines if Controller should watch to Ingress Objects that does
|
||||
// not contain an IngressClass configuration
|
||||
WatchWithoutClass bool
|
||||
|
||||
//IngressClassByName defines if the Controller should watch for Ingress Classes by
|
||||
// .metadata.name together with .spec.Controller
|
||||
IngressClassByName bool
|
||||
}
|
||||
|
|
|
|||
|
|
@ -426,7 +426,12 @@ func New(
|
|||
ingressClassEventHandler := cache.ResourceEventHandlerFuncs{
|
||||
AddFunc: func(obj interface{}) {
|
||||
ingressclass := obj.(*networkingv1.IngressClass)
|
||||
if ingressclass.Spec.Controller != icConfig.Controller {
|
||||
foundClassByName := false
|
||||
if icConfig.IngressClassByName && ingressclass.Name == icConfig.AnnotationValue {
|
||||
klog.InfoS("adding ingressclass as ingress-class-by-name is configured", "ingressclass", klog.KObj(ingressclass))
|
||||
foundClassByName = true
|
||||
}
|
||||
if !foundClassByName && ingressclass.Spec.Controller != icConfig.Controller {
|
||||
klog.InfoS("ignoring ingressclass as the spec.controller is not the same of this ingress", "ingressclass", klog.KObj(ingressclass))
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -316,7 +316,7 @@ func TestStore(t *testing.T) {
|
|||
|
||||
err := framework.WaitForIngressInNamespace(clientSet, ns, ing.Name)
|
||||
if err != nil {
|
||||
t.Errorf("error waiting for secret: %v", err)
|
||||
t.Errorf("error waiting for ingress: %v", err)
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
|
|
@ -486,6 +486,112 @@ func TestStore(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
t.Run("should return two events for add and delete and one for update of ingress and watch-ingress-by-name", func(t *testing.T) {
|
||||
ns := createNamespace(clientSet, t)
|
||||
defer deleteNamespace(ns, clientSet, t)
|
||||
ic := createIngressClass(clientSet, t, "not-k8s.io/by-name")
|
||||
defer deleteIngressClass(ic, clientSet, t)
|
||||
|
||||
createConfigMap(clientSet, ns, t)
|
||||
|
||||
stopCh := make(chan struct{})
|
||||
updateCh := channels.NewRingChannel(1024)
|
||||
|
||||
var add uint64
|
||||
var upd uint64
|
||||
var del uint64
|
||||
|
||||
go func(ch *channels.RingChannel) {
|
||||
for {
|
||||
evt, ok := <-ch.Out()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
e := evt.(Event)
|
||||
if e.Obj == nil {
|
||||
continue
|
||||
}
|
||||
if _, ok := e.Obj.(*networking.Ingress); !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
switch e.Type {
|
||||
case CreateEvent:
|
||||
atomic.AddUint64(&add, 1)
|
||||
case UpdateEvent:
|
||||
atomic.AddUint64(&upd, 1)
|
||||
case DeleteEvent:
|
||||
atomic.AddUint64(&del, 1)
|
||||
}
|
||||
}
|
||||
}(updateCh)
|
||||
|
||||
ingressClassconfig := &ingressclass.IngressClassConfiguration{
|
||||
Controller: ingressclass.DefaultControllerName,
|
||||
AnnotationValue: ic,
|
||||
IngressClassByName: true,
|
||||
}
|
||||
|
||||
storer := New(
|
||||
ns,
|
||||
fmt.Sprintf("%v/config", ns),
|
||||
fmt.Sprintf("%v/tcp", ns),
|
||||
fmt.Sprintf("%v/udp", ns),
|
||||
"",
|
||||
10*time.Minute,
|
||||
clientSet,
|
||||
updateCh,
|
||||
false,
|
||||
ingressClassconfig)
|
||||
|
||||
storer.Run(stopCh)
|
||||
validSpec := commonIngressSpec
|
||||
validSpec.IngressClassName = &ic
|
||||
ing := ensureIngress(&networking.Ingress{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "ingclass-by-name",
|
||||
Namespace: ns,
|
||||
},
|
||||
Spec: validSpec,
|
||||
}, clientSet, t)
|
||||
|
||||
err := framework.WaitForIngressInNamespace(clientSet, ns, ing.Name)
|
||||
if err != nil {
|
||||
t.Errorf("error waiting for ingress: %v", err)
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
ingressUpdated := ing.DeepCopy()
|
||||
ingressUpdated.Spec.Rules[0].Host = "update-dummy"
|
||||
_ = ensureIngress(ingressUpdated, clientSet, t)
|
||||
if err != nil {
|
||||
t.Errorf("error updating ingress: %v", err)
|
||||
}
|
||||
// Secret takes a bit to update
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
err = clientSet.NetworkingV1().Ingresses(ingressUpdated.Namespace).Delete(context.TODO(), ingressUpdated.Name, metav1.DeleteOptions{})
|
||||
if err != nil {
|
||||
t.Errorf("error deleting ingress: %v", err)
|
||||
}
|
||||
|
||||
err = framework.WaitForNoIngressInNamespace(clientSet, ingressUpdated.Namespace, ingressUpdated.Name)
|
||||
if err != nil {
|
||||
t.Errorf("error waiting for ingress deletion: %v", err)
|
||||
}
|
||||
|
||||
if atomic.LoadUint64(&add) != 1 {
|
||||
t.Errorf("expected 1 event of type Create but %v occurred", add)
|
||||
}
|
||||
if atomic.LoadUint64(&upd) != 1 {
|
||||
t.Errorf("expected 1 event of type Update but %v occurred", upd)
|
||||
}
|
||||
if atomic.LoadUint64(&del) != 1 {
|
||||
t.Errorf("expected 1 event of type Delete but %v occurred", del)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should not receive updates for ingress with invalid class annotation", func(t *testing.T) {
|
||||
ns := createNamespace(clientSet, t)
|
||||
defer deleteNamespace(ns, clientSet, t)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue