@@ -2,9 +2,11 @@ package controller
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "reflect"
78
9+ "github.com/api7/api7-ingress-controller/api/v1alpha1"
810 "github.com/api7/api7-ingress-controller/internal/controller/config"
911 "github.com/api7/api7-ingress-controller/internal/controller/indexer"
1012 "github.com/api7/api7-ingress-controller/internal/provider"
@@ -95,6 +97,12 @@ func (r *IngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
9597 // create a translate context
9698 tctx := provider .NewDefaultTranslateContext (ctx )
9799
100+ // process IngressClass parameters if they reference GatewayProxy
101+ if err := r .processIngressClassParameters (ctx , tctx , ingress ); err != nil {
102+ r .Log .Error (err , "failed to process IngressClass parameters" , "ingress" , ingress .Name )
103+ return ctrl.Result {}, err
104+ }
105+
98106 // process TLS configuration
99107 if err := r .processTLS (tctx , ingress ); err != nil {
100108 r .Log .Error (err , "failed to process TLS configuration" , "ingress" , ingress .Name )
@@ -122,6 +130,46 @@ func (r *IngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
122130 return ctrl.Result {}, nil
123131}
124132
133+ // getIngressClass get the ingress class for the ingress
134+ func (r * IngressReconciler ) getIngressClass (obj client.Object ) (* networkingv1.IngressClass , error ) {
135+ ingress := obj .(* networkingv1.Ingress )
136+
137+ if ingress .Spec .IngressClassName == nil {
138+ // handle the case where IngressClassName is not specified
139+ // find all ingress classes and check if any of them is marked as default
140+ ingressClassList := & networkingv1.IngressClassList {}
141+ if err := r .List (context .Background (), ingressClassList , client.MatchingFields {
142+ indexer .IngressClass : config .GetControllerName (),
143+ }); err != nil {
144+ r .Log .Error (err , "failed to list ingress classes" )
145+ return nil , err
146+ }
147+
148+ // find the ingress class that is marked as default
149+ for _ , ic := range ingressClassList .Items {
150+ if IsDefaultIngressClass (& ic ) && matchesController (ic .Spec .Controller ) {
151+ log .Debugw ("match the default ingress class" )
152+ return & ic , nil
153+ }
154+ }
155+
156+ log .Debugw ("no default ingress class found" )
157+ return nil , errors .New ("no default ingress class found" )
158+ }
159+
160+ // if it does not match, check if the ingress class is controlled by us
161+ ingressClass := networkingv1.IngressClass {}
162+ if err := r .Client .Get (context .Background (), client.ObjectKey {Name : * ingress .Spec .IngressClassName }, & ingressClass ); err != nil {
163+ return nil , err
164+ }
165+
166+ if matchesController (ingressClass .Spec .Controller ) {
167+ return & ingressClass , nil
168+ }
169+
170+ return nil , errors .New ("ingress class is not controlled by us" )
171+ }
172+
125173// checkIngressClass check if the ingress uses the ingress class that we control
126174func (r * IngressReconciler ) checkIngressClass (obj client.Object ) bool {
127175 ingress := obj .(* networkingv1.Ingress )
@@ -413,6 +461,8 @@ func (r *IngressReconciler) processBackendService(tctx *provider.TranslateContex
413461func (r * IngressReconciler ) updateStatus (ctx context.Context , ingress * networkingv1.Ingress ) error {
414462 var loadBalancerStatus networkingv1.IngressLoadBalancerStatus
415463
464+ // todo: remove using default config, use the StatusAddress And PublishService in the gateway proxy
465+
416466 // 1. use the IngressStatusAddress in the config
417467 statusAddresses := config .GetIngressStatusAddress ()
418468 if len (statusAddresses ) > 0 {
@@ -469,3 +519,84 @@ func (r *IngressReconciler) updateStatus(ctx context.Context, ingress *networkin
469519
470520 return nil
471521}
522+
523+ // processIngressClassParameters processes the IngressClass parameters that reference GatewayProxy
524+ func (r * IngressReconciler ) processIngressClassParameters (ctx context.Context , tctx * provider.TranslateContext , ingress * networkingv1.Ingress ) error {
525+ ingressClass , err := r .getIngressClass (ingress )
526+ if err != nil {
527+ r .Log .Error (err , "failed to get IngressClass" , "name" , ingress .Spec .IngressClassName )
528+ return err
529+ }
530+
531+ if ingressClass .Spec .Parameters == nil {
532+ return nil
533+ }
534+
535+ ingressClassKind := provider.ResourceKind {
536+ Kind : ingressClass .Kind ,
537+ Namespace : ingressClass .Namespace ,
538+ Name : ingressClass .Name ,
539+ }
540+
541+ ingressKind := provider.ResourceKind {
542+ Kind : ingress .Kind ,
543+ Namespace : ingress .Namespace ,
544+ Name : ingress .Name ,
545+ }
546+
547+ parameters := ingressClass .Spec .Parameters
548+ // check if the parameters reference GatewayProxy
549+ if parameters .APIGroup != nil && * parameters .APIGroup == v1alpha1 .GroupVersion .Group && parameters .Kind == "GatewayProxy" {
550+ ns := ingress .GetNamespace ()
551+ if parameters .Namespace != nil {
552+ ns = * parameters .Namespace
553+ }
554+
555+ gatewayProxy := & v1alpha1.GatewayProxy {}
556+ if err := r .Get (ctx , client.ObjectKey {
557+ Namespace : ns ,
558+ Name : parameters .Name ,
559+ }, gatewayProxy ); err != nil {
560+ r .Log .Error (err , "failed to get GatewayProxy" , "namespace" , ns , "name" , parameters .Name )
561+ return err
562+ }
563+
564+ r .Log .Info ("found GatewayProxy for IngressClass" , "ingressClass" , ingressClass .Name , "gatewayproxy" , gatewayProxy .Name )
565+ tctx .GatewayProxies [ingressClassKind ] = * gatewayProxy
566+ tctx .ResourceParentRefs [ingressKind ] = append (tctx .ResourceParentRefs [ingressKind ], ingressClassKind )
567+
568+ // check if the provider field references a secret
569+ if gatewayProxy .Spec .Provider != nil && gatewayProxy .Spec .Provider .Type == v1alpha1 .ProviderTypeControlPlane {
570+ if gatewayProxy .Spec .Provider .ControlPlane != nil &&
571+ gatewayProxy .Spec .Provider .ControlPlane .Auth .Type == v1alpha1 .AuthTypeAdminKey &&
572+ gatewayProxy .Spec .Provider .ControlPlane .Auth .AdminKey != nil &&
573+ gatewayProxy .Spec .Provider .ControlPlane .Auth .AdminKey .ValueFrom != nil &&
574+ gatewayProxy .Spec .Provider .ControlPlane .Auth .AdminKey .ValueFrom .SecretKeyRef != nil {
575+
576+ secretRef := gatewayProxy .Spec .Provider .ControlPlane .Auth .AdminKey .ValueFrom .SecretKeyRef
577+ secret := & corev1.Secret {}
578+ if err := r .Get (ctx , client.ObjectKey {
579+ Namespace : ns ,
580+ Name : secretRef .Name ,
581+ }, secret ); err != nil {
582+ r .Log .Error (err , "failed to get secret for GatewayProxy provider" ,
583+ "namespace" , ns ,
584+ "name" , secretRef .Name )
585+ return err
586+ }
587+
588+ r .Log .Info ("found secret for GatewayProxy provider" ,
589+ "ingressClass" , ingressClass .Name ,
590+ "gatewayproxy" , gatewayProxy .Name ,
591+ "secret" , secretRef .Name )
592+
593+ tctx .Secrets [types.NamespacedName {
594+ Namespace : ns ,
595+ Name : secretRef .Name ,
596+ }] = secret
597+ }
598+ }
599+ }
600+
601+ return nil
602+ }
0 commit comments