@@ -17,6 +17,7 @@ import (
1717 "encoding/json"
1818 "fmt"
1919
20+ "github.com/pkg/errors"
2021 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122 "k8s.io/apimachinery/pkg/types"
2223 "k8s.io/utils/ptr"
@@ -26,8 +27,9 @@ import (
2627 apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
2728 "github.com/apache/apisix-ingress-controller/internal/controller/label"
2829 "github.com/apache/apisix-ingress-controller/internal/provider"
30+ "github.com/apache/apisix-ingress-controller/internal/utils"
2931 "github.com/apache/apisix-ingress-controller/pkg/id"
30- "github.com/apache/apisix-ingress-controller/pkg/utils"
32+ pkgutils "github.com/apache/apisix-ingress-controller/pkg/utils"
3133)
3234
3335func (t * Translator ) TranslateApisixRoute (tctx * provider.TranslateContext , ar * apiv2.ApisixRoute ) (result * TranslateResult , err error ) {
@@ -58,7 +60,7 @@ func (t *Translator) TranslateApisixRoute(tctx *provider.TranslateContext, ar *a
5860 if plugin .SecretRef != "" {
5961 if secret , ok := tctx .Secrets [types.NamespacedName {Namespace : ar .Namespace , Name : plugin .SecretRef }]; ok {
6062 for key , value := range secret .Data {
61- utils .InsertKeyInMap (key , string (value ), config )
63+ pkgutils .InsertKeyInMap (key , string (value ), config )
6264 }
6365 }
6466 }
@@ -115,25 +117,26 @@ func (t *Translator) TranslateApisixRoute(tctx *provider.TranslateContext, ar *a
115117 // translate to adc.Upstream
116118 var backendErr error
117119 for _ , backend := range rule .Backends {
118- weight := int32 ( * cmp . Or ( backend . Weight , ptr . To ( apiv2 . DefaultWeight )))
119- backendRef := gatewayv1. BackendRef {
120- BackendObjectReference : gatewayv1. BackendObjectReference {
121- Group : ( * gatewayv1 . Group )( & apiv2 . GroupVersion . Group ),
122- Kind : ( * gatewayv1 . Kind )( ptr . To ( "Service" )),
123- Name : gatewayv1 . ObjectName ( backend . ServiceName ),
124- Namespace : ( * gatewayv1 . Namespace )( & ar . Namespace ),
125- Port : ( * gatewayv1 . PortNumber )( & backend . ServicePort . IntVal ),
126- },
127- Weight : & weight ,
128- }
129- upNodes , err := t . translateBackendRef ( tctx , backendRef )
130- if err != nil {
131- backendErr = err
132- continue
120+ var (
121+ upNodes adc. UpstreamNodes
122+ )
123+ if backend . ResolveGranularity == "service" {
124+ upNodes , backendErr = t . translateApisixRouteBackendResolveGranularityService ( tctx , utils . NamespacedName ( ar ), backend )
125+ if backendErr != nil {
126+ t . Log . Error ( backendErr , "failed to translate ApisixRoute backend with ResolveGranularity Service" )
127+ continue
128+ }
129+ } else {
130+ upNodes , backendErr = t . translateApisixRouteBackendResolveGranularityEndpoint ( tctx , utils . NamespacedName ( ar ), backend )
131+ if backendErr != nil {
132+ t . Log . Error ( backendErr , "failed to translate ApisixRoute backend with ResolveGranularity Endpoint" )
133+ continue
134+ }
133135 }
134- t . AttachBackendTrafficPolicyToUpstream ( backendRef , tctx . BackendTrafficPolicies , upstream )
136+
135137 upstream .Nodes = append (upstream .Nodes , upNodes ... )
136138 }
139+
137140 //nolint:staticcheck
138141 if len (rule .Backends ) == 0 && len (rule .Upstreams ) > 0 {
139142 // FIXME: when the API ApisixUpstream is supported
@@ -164,3 +167,39 @@ func (t *Translator) TranslateApisixRoute(tctx *provider.TranslateContext, ar *a
164167
165168 return result , nil
166169}
170+
171+ func (t * Translator ) translateApisixRouteBackendResolveGranularityService (tctx * provider.TranslateContext , arNN types.NamespacedName , backend apiv2.ApisixRouteHTTPBackend ) (adc.UpstreamNodes , error ) {
172+ serviceNN := types.NamespacedName {
173+ Namespace : arNN .Namespace ,
174+ Name : backend .ServiceName ,
175+ }
176+ svc , ok := tctx .Services [serviceNN ]
177+ if ! ok {
178+ return nil , errors .Errorf ("service not found, ApisixRoute: %s, Service: %s" , arNN , serviceNN )
179+ }
180+ if svc .Spec .ClusterIP == "" {
181+ return nil , errors .Errorf ("conflict headless service and backend resolve granularity, ApisixRoute: %s, Service: %s" , arNN , serviceNN )
182+ }
183+ return adc.UpstreamNodes {
184+ {
185+ Host : svc .Spec .ClusterIP ,
186+ Port : backend .ServicePort .IntValue (),
187+ Weight : * cmp .Or (backend .Weight , ptr .To (apiv2 .DefaultWeight )),
188+ },
189+ }, nil
190+ }
191+
192+ func (t * Translator ) translateApisixRouteBackendResolveGranularityEndpoint (tctx * provider.TranslateContext , arNN types.NamespacedName , backend apiv2.ApisixRouteHTTPBackend ) (adc.UpstreamNodes , error ) {
193+ weight := int32 (* cmp .Or (backend .Weight , ptr .To (apiv2 .DefaultWeight )))
194+ backendRef := gatewayv1.BackendRef {
195+ BackendObjectReference : gatewayv1.BackendObjectReference {
196+ Group : (* gatewayv1 .Group )(& apiv2 .GroupVersion .Group ),
197+ Kind : (* gatewayv1 .Kind )(ptr .To ("Service" )),
198+ Name : gatewayv1 .ObjectName (backend .ServiceName ),
199+ Namespace : (* gatewayv1 .Namespace )(& arNN .Namespace ),
200+ Port : (* gatewayv1 .PortNumber )(& backend .ServicePort .IntVal ),
201+ },
202+ Weight : & weight ,
203+ }
204+ return t .translateBackendRef (tctx , backendRef )
205+ }
0 commit comments