Skip to content

Commit

Permalink
feat(ingressclass) expand default behavior to all
Browse files Browse the repository at this point in the history
Use default IngressClass behavior for any resource that supports ingress
class. This re-integrates the Ingress V1 controller into the generated
controllers, as all controllers that support class now include the
additional IngressClass watch.

Condense the store class annotation validation functions into a single
function for either Ingress or Knative annotations.

Add a logger to the fake store for tests.
  • Loading branch information
rainest committed Mar 10, 2022
1 parent ec83f1f commit 53496fd
Show file tree
Hide file tree
Showing 9 changed files with 561 additions and 238 deletions.
51 changes: 48 additions & 3 deletions hack/generators/controllers/networking/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ var inputControllersNeeded = &typesNeeded{
AcceptsIngressClassNameSpec: false,
RBACVerbs: []string{"list", "watch"},
},
typeNeeded{
Group: "networking.k8s.io",
Version: "v1",
Kind: "Ingress",
PackageImportAlias: "netv1",
PackageAlias: "NetV1",
Package: netv1,
Plural: "ingresses",
CacheType: "IngressV1",
NeedsStatusPermissions: true,
CapableOfStatusUpdates: true,
AcceptsIngressClassNameAnnotation: true,
AcceptsIngressClassNameSpec: true,
RBACVerbs: []string{"get", "list", "watch"},
},
typeNeeded{
Group: "networking.k8s.io",
Version: "v1",
Expand All @@ -87,7 +102,6 @@ var inputControllersNeeded = &typesNeeded{
AcceptsIngressClassNameSpec: false,
RBACVerbs: []string{"get", "list", "watch"},
},

typeNeeded{
Group: "networking.k8s.io",
Version: "v1beta1",
Expand Down Expand Up @@ -396,6 +410,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
ctrlutils "github.com/kong/kubernetes-ingress-controller/v2/internal/controllers/utils"
Expand Down Expand Up @@ -464,6 +480,14 @@ func (r *{{.PackageAlias}}{{.Kind}}Reconciler) SetupWithManager(mgr ctrl.Manager
}
{{- end}}
{{- if .AcceptsIngressClassNameAnnotation}}
err = c.Watch(
&source.Kind{Type: &netv1.IngressClass{}},
handler.EnqueueRequestsFromMapFunc(r.listClassless),
predicate.NewPredicateFuncs(ctrlutils.IsDefaultIngressClass),
)
if err != nil {
return err
}
preds := ctrlutils.GeneratePredicateFuncsForIngressClassFilter(r.IngressClassName, {{.AcceptsIngressClassNameSpec}}, true)
{{- end}}
return c.Watch(
Expand All @@ -475,6 +499,29 @@ func (r *{{.PackageAlias}}{{.Kind}}Reconciler) SetupWithManager(mgr ctrl.Manager
)
}
{{- if .AcceptsIngressClassNameAnnotation}}
// listClassless finds and reconciles all objects without ingress class information
func (r *{{.PackageAlias}}{{.Kind}}Reconciler) listClassless(obj client.Object) []reconcile.Request {
resourceList := &{{.PackageImportAlias}}.{{.Kind}}List{}
if err := r.Client.List(context.Background(), resourceList); err != nil {
r.Log.Error(err, "failed to list classless {{.Plural}}")
return nil
}
var recs []reconcile.Request
for _, resource := range resourceList.Items {
if ctrlutils.IsIngressClassEmpty(&resource) {
recs = append(recs, reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: resource.Namespace,
Name: resource.Name,
},
})
}
}
return recs
}
{{- end}}
//+kubebuilder:rbac:groups={{.Group}},resources={{.Plural}},verbs={{ .RBACVerbs | join ";" }}
{{- if .NeedsStatusPermissions}}
//+kubebuilder:rbac:groups={{.Group}},resources={{.Plural}}/status,verbs=get;update;patch
Expand Down Expand Up @@ -512,7 +559,6 @@ func (r *{{.PackageAlias}}{{.Kind}}Reconciler) Reconcile(ctx context.Context, re
return ctrl.Result{}, nil
}
{{if .AcceptsIngressClassNameAnnotation}}
// retrieve the configured IngressClass, to check if it's the default IngressClass
class := new(netv1.IngressClass)
if err := r.Get(ctx, types.NamespacedName{Name: r.IngressClassName}, class); err != nil {
// we log this without taking action to support legacy configurations that only set ingressClassName or
Expand All @@ -521,7 +567,6 @@ func (r *{{.PackageAlias}}{{.Kind}}Reconciler) Reconcile(ctx context.Context, re
// if none exists.
log.V(util.DebugLevel).Info("could not retrieve IngressClass", "ingressclass", r.IngressClassName)
}
// if the object is not configured with our ingress.class, then we need to ensure it's removed from the cache
if !ctrlutils.MatchesIngressClassName(obj, r.IngressClassName, ctrlutils.IsDefaultIngressClass(class)) {
log.V(util.DebugLevel).Info("object missing ingress class, ensuring it's removed from configuration", "namespace", req.Namespace, "name", req.Name)
Expand Down
6 changes: 4 additions & 2 deletions internal/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ func IngressClassValidatorFunc(

return func(obj metav1.Object, handling ClassMatching) bool {
ingress := obj.GetAnnotations()[IngressClassKey]
return validIngress(ingress, ingressClass, handling)
knative := obj.GetAnnotations()[KnativeIngressClassKey]
return validIngress(ingress, ingressClass, handling) || validIngress(knative, ingressClass, handling)
}
}

Expand All @@ -102,7 +103,8 @@ func IngressClassValidatorFuncFromObjectMeta(

return func(obj *metav1.ObjectMeta, handling ClassMatching) bool {
ingress := obj.GetAnnotations()[IngressClassKey]
return validIngress(ingress, ingressClass, handling)
knative := obj.GetAnnotations()[KnativeIngressClassKey]
return validIngress(ingress, ingressClass, handling) || validIngress(knative, ingressClass, handling)
}
}

Expand Down
184 changes: 0 additions & 184 deletions internal/controllers/configuration/ingressv1.go

This file was deleted.

Loading

0 comments on commit 53496fd

Please sign in to comment.