@@ -25,6 +25,7 @@ import (
2525 "github.com/api7/gopkg/pkg/log"
2626 "github.com/pkg/errors"
2727 "go.uber.org/zap"
28+ corev1 "k8s.io/api/core/v1"
2829 discoveryv1 "k8s.io/api/discovery/v1"
2930 k8stypes "k8s.io/apimachinery/pkg/types"
3031 "k8s.io/utils/ptr"
@@ -36,7 +37,7 @@ import (
3637 "github.com/apache/apisix-ingress-controller/internal/utils"
3738)
3839
39- func (t * Translator ) TranslateGatewayProxyToConfig (tctx * provider.TranslateContext , gatewayProxy * v1alpha1.GatewayProxy , mode string ) (* types.Config , error ) {
40+ func (t * Translator ) TranslateGatewayProxyToConfig (tctx * provider.TranslateContext , gatewayProxy * v1alpha1.GatewayProxy , resolveEndpoints bool ) (* types.Config , error ) {
4041 if gatewayProxy == nil || gatewayProxy .Spec .Provider == nil {
4142 return nil , nil
4243 }
@@ -87,14 +88,14 @@ func (t *Translator) TranslateGatewayProxyToConfig(tctx *provider.TranslateConte
8788 Namespace : gatewayProxy .Namespace ,
8889 Name : provider .ControlPlane .Service .Name ,
8990 }
90- _ , ok := tctx .Services [namespacedName ]
91+ svc , ok := tctx .Services [namespacedName ]
9192 if ! ok {
9293 return nil , fmt .Errorf ("no service found for service reference: %s" , namespacedName )
9394 }
9495
9596 // APISIXStandalone, configurations need to be sent to each data plane instance;
9697 // In other cases, the service is directly accessed as the adc backend server address.
97- if mode == "endpoints" {
98+ if resolveEndpoints {
9899 endpoint := tctx .EndpointSlices [namespacedName ]
99100 if endpoint == nil {
100101 return nil , nil
@@ -119,9 +120,13 @@ func (t *Translator) TranslateGatewayProxyToConfig(tctx *provider.TranslateConte
119120 config .ServerAddrs = append (config .ServerAddrs , "http://" + net .JoinHostPort (node .Host , strconv .Itoa (node .Port )))
120121 }
121122 } else {
122- config .ServerAddrs = []string {
123- fmt .Sprintf ("http://%s.%s:%d" , provider .ControlPlane .Service .Name , gatewayProxy .Namespace , provider .ControlPlane .Service .Port ),
123+ var serverAddr string
124+ if svc .Spec .Type == corev1 .ServiceTypeExternalName {
125+ serverAddr = fmt .Sprintf ("http://%s:%d" , svc .Spec .ExternalName , provider .ControlPlane .Service .Port )
126+ } else {
127+ serverAddr = fmt .Sprintf ("http://%s.%s.svc:%d" , provider .ControlPlane .Service .Name , gatewayProxy .Namespace , provider .ControlPlane .Service .Port )
124128 }
129+ config .ServerAddrs = []string {serverAddr }
125130 }
126131
127132 log .Debugw ("add server address to config.ServiceAddrs" , zap .Strings ("config.ServerAddrs" , config .ServerAddrs ))
0 commit comments