Skip to content

Commit 0c0118d

Browse files
committed
fix: adc backend server on different mode (#2455)
(cherry picked from commit 59af65e)
1 parent 5e06659 commit 0c0118d

File tree

6 files changed

+87
-72
lines changed

6 files changed

+87
-72
lines changed

internal/provider/adc/config.go

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,31 +92,40 @@ func (d *adcClient) getConfigsForGatewayProxy(tctx *provider.TranslateContext, g
9292
if !ok {
9393
return nil, fmt.Errorf("no service found for service reference: %s", namespacedName)
9494
}
95-
endpoint := tctx.EndpointSlices[namespacedName]
96-
if endpoint == nil {
97-
return nil, nil
98-
}
99-
upstreamNodes, err := d.translator.TranslateBackendRefWithFilter(tctx, gatewayv1.BackendRef{
100-
BackendObjectReference: gatewayv1.BackendObjectReference{
101-
Name: gatewayv1.ObjectName(provider.ControlPlane.Service.Name),
102-
Namespace: (*gatewayv1.Namespace)(&gatewayProxy.Namespace),
103-
Port: ptr.To(gatewayv1.PortNumber(provider.ControlPlane.Service.Port)),
104-
},
105-
}, func(endpoint *discoveryv1.Endpoint) bool {
106-
if endpoint.Conditions.Terminating != nil && *endpoint.Conditions.Terminating {
107-
log.Debugw("skip terminating endpoint", zap.Any("endpoint", endpoint))
108-
return false
95+
96+
// APISIXStandalone, configurations need to be sent to each data plane instance;
97+
// In other cases, the service is directly accessed as the adc backend server address.
98+
if d.BackendMode == BackendModeAPISIXStandalone {
99+
endpoint := tctx.EndpointSlices[namespacedName]
100+
if endpoint == nil {
101+
return nil, nil
102+
}
103+
upstreamNodes, err := d.translator.TranslateBackendRefWithFilter(tctx, gatewayv1.BackendRef{
104+
BackendObjectReference: gatewayv1.BackendObjectReference{
105+
Name: gatewayv1.ObjectName(provider.ControlPlane.Service.Name),
106+
Namespace: (*gatewayv1.Namespace)(&gatewayProxy.Namespace),
107+
Port: ptr.To(gatewayv1.PortNumber(provider.ControlPlane.Service.Port)),
108+
},
109+
}, func(endpoint *discoveryv1.Endpoint) bool {
110+
if endpoint.Conditions.Terminating != nil && *endpoint.Conditions.Terminating {
111+
log.Debugw("skip terminating endpoint", zap.Any("endpoint", endpoint))
112+
return false
113+
}
114+
return true
115+
})
116+
if err != nil {
117+
return nil, err
118+
}
119+
for _, node := range upstreamNodes {
120+
config.ServerAddrs = append(config.ServerAddrs, "http://"+net.JoinHostPort(node.Host, strconv.Itoa(node.Port)))
121+
}
122+
} else {
123+
config.ServerAddrs = []string{
124+
fmt.Sprintf("http://%s.%s:%d", provider.ControlPlane.Service.Name, gatewayProxy.Namespace, provider.ControlPlane.Service.Port),
109125
}
110-
return true
111-
})
112-
if err != nil {
113-
return nil, err
114-
}
115-
for _, node := range upstreamNodes {
116-
config.ServerAddrs = append(config.ServerAddrs, "http://"+net.JoinHostPort(node.Host, strconv.Itoa(node.Port)))
117126
}
118127

119-
log.Debugf("add server address to config.ServiceAddrs: %v", config.ServerAddrs)
128+
log.Debugw("add server address to config.ServiceAddrs", zap.Strings("config.ServerAddrs", config.ServerAddrs))
120129
}
121130

122131
return &config, nil

test/e2e/crds/gatewayproxy.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"k8s.io/apimachinery/pkg/util/wait"
3131
"k8s.io/utils/ptr"
3232

33+
"github.com/apache/apisix-ingress-controller/internal/provider/adc"
3334
"github.com/apache/apisix-ingress-controller/test/e2e/framework"
3435
"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
3536
)
@@ -173,4 +174,21 @@ spec:
173174
}
174175
})
175176
})
177+
178+
Context("Backend server", func() {
179+
It("backend server on apisix/apisix-standalone mode", func() {
180+
var (
181+
keyword string
182+
)
183+
184+
if framework.ProviderType == adc.BackendModeAPISIX {
185+
keyword = fmt.Sprintf(`{"config.ServerAddrs": ["%s"]}`, s.Deployer.GetAdminEndpoint())
186+
} else {
187+
keyword = fmt.Sprintf(`{"config.ServerAddrs": ["http://%s:9180"]}`, s.GetPodIP(s.Namespace(), "app.kubernetes.io/name=apisix"))
188+
}
189+
190+
By(fmt.Sprintf("wait for keyword: %s", keyword))
191+
s.WaitControllerManagerLog(keyword, 60, time.Minute)
192+
})
193+
})
176194
})

test/e2e/framework/k8s.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,9 @@ func (f *Framework) Scale(name string, replicas int32) {
201201
fmt.Sprintf("ensure service %s/%s has %v endpoints failed", _namespace, name, replicas))
202202
}
203203

204-
func (f *Framework) GetPodIP(selector string) string {
205-
pods := f.GetPods("", selector)
204+
func (f *Framework) GetPodIP(namespace, selector string) string {
205+
pods := f.GetPods(namespace, selector)
206+
f.GomegaT.Expect(pods).ShouldNot(BeEmpty())
206207
return pods[0].Status.PodIP
207208
}
208209

@@ -211,7 +212,6 @@ func (f *Framework) GetPods(namespace, selector string) []corev1.Pod {
211212
LabelSelector: selector,
212213
})
213214
f.GomegaT.Expect(err).ShouldNot(HaveOccurred())
214-
f.GomegaT.Expect(podList.Items).ShouldNot(BeEmpty())
215215
return podList.Items
216216
}
217217

test/e2e/gatewayapi/httproute.go

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,14 @@ import (
3434
"sigs.k8s.io/gateway-api/apis/v1alpha2"
3535

3636
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
37-
"github.com/apache/apisix-ingress-controller/internal/provider/adc"
3837
"github.com/apache/apisix-ingress-controller/test/e2e/framework"
3938
"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
4039
)
4140

4241
var _ = Describe("Test HTTPRoute", Label("networking.k8s.io", "httproute"), func() {
4342
s := scaffold.NewDefaultScaffold()
4443

45-
getGatewayProxySpec := func(endpoint, adminKey string) string {
46-
if framework.ProviderType == adc.BackendModeAPISIXStandalone {
47-
var gatewayProxyYaml = `
44+
var gatewayProxyYaml = `
4845
apiVersion: apisix.apache.org/v1alpha1
4946
kind: GatewayProxy
5047
metadata:
@@ -54,32 +51,15 @@ spec:
5451
type: ControlPlane
5552
controlPlane:
5653
service:
57-
name: apisix-standalone
54+
name: %s
5855
port: 9180
5956
auth:
6057
type: AdminKey
6158
adminKey:
6259
value: "%s"
6360
`
64-
return fmt.Sprintf(gatewayProxyYaml, adminKey)
65-
}
66-
var gatewayProxyYaml = `
67-
apiVersion: apisix.apache.org/v1alpha1
68-
kind: GatewayProxy
69-
metadata:
70-
name: apisix-proxy-config
71-
spec:
72-
provider:
73-
type: ControlPlane
74-
controlPlane:
75-
endpoints:
76-
- %s
77-
auth:
78-
type: AdminKey
79-
adminKey:
80-
value: "%s"
81-
`
82-
return fmt.Sprintf(gatewayProxyYaml, endpoint, adminKey)
61+
getGatewayProxySpec := func() string {
62+
return fmt.Sprintf(gatewayProxyYaml, framework.ProviderType, s.AdminKey())
8363
}
8464

8565
var gatewayClassYaml = `
@@ -153,8 +133,7 @@ spec:
153133

154134
var beforeEachHTTP = func() {
155135
By("create GatewayProxy")
156-
gatewayProxy := getGatewayProxySpec(s.Deployer.GetAdminEndpoint(), s.AdminKey())
157-
err := s.CreateResourceFromString(gatewayProxy)
136+
err := s.CreateResourceFromString(getGatewayProxySpec())
158137
Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy")
159138
time.Sleep(5 * time.Second)
160139

@@ -184,8 +163,7 @@ spec:
184163

185164
var beforeEachHTTPS = func() {
186165
By("create GatewayProxy")
187-
gatewayProxy := getGatewayProxySpec(s.Deployer.GetAdminEndpoint(), s.AdminKey())
188-
err := s.CreateResourceFromString(gatewayProxy)
166+
err := s.CreateResourceFromString(getGatewayProxySpec())
189167
Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy")
190168
time.Sleep(5 * time.Second)
191169

test/e2e/scaffold/apisix_deployer.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,12 @@ func (s *APISIXDeployer) DeployDataplane(deployOpts DeployDataplaneOptions) {
168168
opts.Replicas = deployOpts.Replicas
169169
}
170170

171-
for _, tunnel := range []*k8s.Tunnel{
172-
s.adminTunnel,
173-
s.apisixHttpTunnel,
174-
s.apisixHttpsTunnel,
171+
for _, close := range []func(){
172+
s.closeAdminTunnel,
173+
s.closeApisixHttpTunnel,
174+
s.closeApisixHttpsTunnel,
175175
} {
176-
if tunnel != nil {
177-
tunnel.Close()
178-
}
176+
close()
179177
}
180178

181179
svc := s.deployDataplane(&opts)
@@ -302,14 +300,18 @@ func (s *APISIXDeployer) createAdminTunnel(svc *corev1.Service) (*k8s.Tunnel, er
302300
if err := adminTunnel.ForwardPortE(s.t); err != nil {
303301
return nil, err
304302
}
305-
s.addFinalizers(func() {
306-
adminTunnel.Close()
307-
s.adminTunnel = nil
308-
})
303+
s.addFinalizers(s.closeAdminTunnel)
309304

310305
return adminTunnel, nil
311306
}
312307

308+
func (s *APISIXDeployer) closeAdminTunnel() {
309+
if s.adminTunnel != nil {
310+
s.adminTunnel.Close()
311+
s.adminTunnel = nil
312+
}
313+
}
314+
313315
func (s *APISIXDeployer) CreateAdditionalGateway(namePrefix string) (string, *corev1.Service, error) {
314316
// Create a new namespace for this additional gateway
315317
additionalNS := fmt.Sprintf("%s-%d", namePrefix, time.Now().Unix())

test/e2e/scaffold/scaffold.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,23 +314,31 @@ func (s *Scaffold) createDataplaneTunnels(
314314
if err := httpTunnel.ForwardPortE(s.t); err != nil {
315315
return nil, nil, err
316316
}
317-
s.addFinalizers(func() {
318-
httpTunnel.Close()
319-
s.apisixHttpTunnel = nil
320-
})
317+
s.addFinalizers(s.closeApisixHttpTunnel)
321318

322319
if err := httpsTunnel.ForwardPortE(s.t); err != nil {
323320
httpTunnel.Close()
324321
return nil, nil, err
325322
}
326-
s.addFinalizers(func() {
327-
httpsTunnel.Close()
328-
s.apisixHttpsTunnel = nil
329-
})
323+
s.addFinalizers(s.closeApisixHttpsTunnel)
330324

331325
return httpTunnel, httpsTunnel, nil
332326
}
333327

328+
func (s *Scaffold) closeApisixHttpTunnel() {
329+
if s.apisixHttpTunnel != nil {
330+
s.apisixHttpTunnel.Close()
331+
s.apisixHttpTunnel = nil
332+
}
333+
}
334+
335+
func (s *Scaffold) closeApisixHttpsTunnel() {
336+
if s.apisixHttpsTunnel != nil {
337+
s.apisixHttpsTunnel.Close()
338+
s.apisixHttpsTunnel = nil
339+
}
340+
}
341+
334342
// GetAdditionalGateway returns resources associated with a specific gateway
335343
func (s *Scaffold) GetAdditionalGateway(identifier string) (*GatewayResources, bool) {
336344
resources, exists := s.additionalGateways[identifier]

0 commit comments

Comments
 (0)