Skip to content

Commit 094a551

Browse files
committed
chore: add adc dump
Signed-off-by: ashing <[email protected]>
1 parent 824382b commit 094a551

File tree

9 files changed

+244
-27
lines changed

9 files changed

+244
-27
lines changed

test/e2e/framework/manifests/apisix-standalone.yaml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ metadata:
44
name: apisix-conf
55
data:
66
config.yaml: |
7-
apisix:
8-
enable_admin: true
9-
admin_key:
10-
- name: admin
11-
key: {{ .AdminKey }}
12-
role: admin
13-
ssl:
14-
enabled: true
15-
nginx_config:
16-
worker_processes: 2
17-
error_log_level: info
187
deployment:
198
role: traditional
209
role_traditional:
2110
config_provider: yaml
11+
admin:
12+
allow_admin:
13+
- 0.0.0.0/0
14+
admin_key:
15+
- key: {{ .AdminKey }}
16+
name: admin
17+
role: admin
18+
nginx_config:
19+
worker_processes: 2
20+
error_log_level: info
2221
---
2322
apiVersion: apps/v1
2423
kind: Deployment

test/e2e/gatewayapi/controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package gatewayapi
1414

1515
import (
1616
"fmt"
17+
"log"
1718
"time"
1819

1920
. "github.com/onsi/ginkgo/v2"
@@ -166,9 +167,10 @@ spec:
166167
BeforeEach(func() {
167168
beforeEach(s1, "gateway1")
168169
})
169-
It("Apply resource ", func() {
170+
FIt("Apply resource ", func() {
170171
ResourceApplied(s1, "HTTPRoute", "httpbin", "gateway1", route1, 1)
171172
routes, err := s1.DefaultDataplaneResource().Route().List(s1.Context)
173+
log.Println("routes", routes)
172174
Expect(err).NotTo(HaveOccurred())
173175
Expect(routes).To(HaveLen(1))
174176
assert.Equal(GinkgoT(), routes[0].Labels["k8s/controller-name"], "apisix.apache.org/apisix-ingress-controller-1")

test/e2e/gatewayapi/gateway.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package gatewayapi
1515
import (
1616
"context"
1717
"fmt"
18+
"log"
1819
"strings"
1920
"time"
2021

@@ -204,7 +205,9 @@ spec:
204205
tls, err := s.DefaultDataplaneResource().SSL().List(context.Background())
205206
assert.Nil(GinkgoT(), err, "list tls error")
206207
assert.Len(GinkgoT(), tls, 1, "tls number not expect")
207-
assert.Equal(GinkgoT(), Cert, tls[0].Cert, "tls cert not expect")
208+
log.Println("tls", tls)
209+
assert.Len(GinkgoT(), tls[0].Certificates, 1, "length of certificates not expect")
210+
assert.Equal(GinkgoT(), Cert, tls[0].Certificates[0].Certificate, "tls cert not expect")
208211
assert.ElementsMatch(GinkgoT(), []string{host, "*.api6.com"}, tls[0].Snis)
209212
})
210213

@@ -277,7 +280,8 @@ spec:
277280
tls, err := s.DefaultDataplaneResource().SSL().List(context.Background())
278281
assert.Nil(GinkgoT(), err, "list tls error")
279282
assert.Len(GinkgoT(), tls, 1, "tls number not expect")
280-
assert.Equal(GinkgoT(), Cert, tls[0].Cert, "tls cert not expect")
283+
assert.Len(GinkgoT(), tls[0].Certificates, 1, "length of certificates not expect")
284+
assert.Equal(GinkgoT(), Cert, tls[0].Certificates[0].Certificate, "tls cert not expect")
281285
assert.Equal(GinkgoT(), tls[0].Labels["k8s/controller-name"], "apisix.apache.org/apisix-ingress-controller")
282286

283287
By("update secret")
@@ -289,7 +293,10 @@ spec:
289293
if len(tls) < 1 {
290294
return ""
291295
}
292-
return tls[0].Cert
296+
if len(tls[0].Certificates) < 1 {
297+
return ""
298+
}
299+
return tls[0].Certificates[0].Certificate
293300
}).WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(framework.TestCert))
294301
})
295302
})

test/e2e/ingress/ingress.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ spec:
131131
tls, err := s.DefaultDataplaneResource().SSL().List(context.Background())
132132
assert.Nil(GinkgoT(), err, "list tls error")
133133
assert.Len(GinkgoT(), tls, 1, "tls number not expect")
134-
assert.Equal(GinkgoT(), Cert, tls[0].Cert, "tls cert not expect")
134+
assert.Len(GinkgoT(), tls[0].Certificates, 1, "length of certificates not expect")
135+
assert.Equal(GinkgoT(), Cert, tls[0].Certificates[0].Certificate, "tls cert not expect")
135136
assert.ElementsMatch(GinkgoT(), []string{host}, tls[0].Snis)
136137
})
137138
})

test/e2e/scaffold/adc.go

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
13+
package scaffold
14+
15+
import (
16+
"bytes"
17+
"context"
18+
"encoding/json"
19+
"os"
20+
"os/exec"
21+
"time"
22+
23+
adctypes "github.com/apache/apisix-ingress-controller/api/adc"
24+
"github.com/apache/apisix-ingress-controller/internal/provider/adc/translator"
25+
)
26+
27+
// DataplaneResource defines the interface for accessing dataplane resources
28+
type DataplaneResource interface {
29+
Route() RouteResource
30+
Service() ServiceResource
31+
SSL() SSLResource
32+
Consumer() ConsumerResource
33+
}
34+
35+
// RouteResource defines the interface for route resources
36+
type RouteResource interface {
37+
List(ctx context.Context) ([]*adctypes.Route, error)
38+
}
39+
40+
// ServiceResource defines the interface for service resources
41+
type ServiceResource interface {
42+
List(ctx context.Context) ([]*adctypes.Service, error)
43+
}
44+
45+
// SSLResource defines the interface for SSL resources
46+
type SSLResource interface {
47+
List(ctx context.Context) ([]*adctypes.SSL, error)
48+
}
49+
50+
// ConsumerResource defines the interface for consumer resources
51+
type ConsumerResource interface {
52+
List(ctx context.Context) ([]*adctypes.Consumer, error)
53+
}
54+
55+
// adcDataplaneResource implements DataplaneResource using ADC command
56+
type adcDataplaneResource struct {
57+
backend string
58+
serverAddr string
59+
token string
60+
tlsVerify bool
61+
syncTimeout time.Duration
62+
}
63+
64+
// newADCDataplaneResource creates a new ADC-based dataplane resource
65+
func newADCDataplaneResource(backend, serverAddr, token string, tlsVerify bool) DataplaneResource {
66+
return &adcDataplaneResource{
67+
backend: backend,
68+
serverAddr: serverAddr,
69+
token: token,
70+
tlsVerify: tlsVerify,
71+
syncTimeout: 30 * time.Second,
72+
}
73+
}
74+
75+
func (a *adcDataplaneResource) Route() RouteResource {
76+
return &adcRouteResource{a}
77+
}
78+
79+
func (a *adcDataplaneResource) Service() ServiceResource {
80+
return &adcServiceResource{a}
81+
}
82+
83+
func (a *adcDataplaneResource) SSL() SSLResource {
84+
return &adcSSLResource{a}
85+
}
86+
87+
func (a *adcDataplaneResource) Consumer() ConsumerResource {
88+
return &adcConsumerResource{a}
89+
}
90+
91+
// dumpResources executes adc dump command and returns the resources
92+
func (a *adcDataplaneResource) dumpResources(ctx context.Context) (*translator.TranslateResult, error) {
93+
ctxWithTimeout, cancel := context.WithTimeout(ctx, a.syncTimeout)
94+
defer cancel()
95+
96+
args := []string{"dump", "-o", "/tmp/dump.yaml"}
97+
if !a.tlsVerify {
98+
args = append(args, "--tls-skip-verify")
99+
}
100+
101+
adcEnv := []string{
102+
"ADC_BACKEND=" + a.backend,
103+
"ADC_SERVER=" + a.serverAddr,
104+
"ADC_TOKEN=" + a.token,
105+
}
106+
107+
var stdout, stderr bytes.Buffer
108+
cmd := exec.CommandContext(ctxWithTimeout, "adc", args...)
109+
cmd.Stdout = &stdout
110+
cmd.Stderr = &stderr
111+
cmd.Env = append(cmd.Env, os.Environ()...)
112+
cmd.Env = append(cmd.Env, adcEnv...)
113+
114+
if err := cmd.Run(); err != nil {
115+
return nil, err
116+
}
117+
118+
var resources adctypes.Resources
119+
if err := json.Unmarshal(stdout.Bytes(), &resources); err != nil {
120+
return nil, err
121+
}
122+
123+
// Extract routes from services
124+
var routes []*adctypes.Route
125+
for _, service := range resources.Services {
126+
routes = append(routes, service.Routes...)
127+
}
128+
129+
return &translator.TranslateResult{
130+
Routes: routes,
131+
Services: resources.Services,
132+
SSL: resources.SSLs,
133+
GlobalRules: resources.GlobalRules,
134+
PluginMetadata: resources.PluginMetadata,
135+
Consumers: resources.Consumers,
136+
}, nil
137+
}
138+
139+
// adcRouteResource implements RouteResource
140+
type adcRouteResource struct {
141+
*adcDataplaneResource
142+
}
143+
144+
func (r *adcRouteResource) List(ctx context.Context) ([]*adctypes.Route, error) {
145+
result, err := r.dumpResources(ctx)
146+
if err != nil {
147+
return nil, err
148+
}
149+
return result.Routes, nil
150+
}
151+
152+
// adcServiceResource implements ServiceResource
153+
type adcServiceResource struct {
154+
*adcDataplaneResource
155+
}
156+
157+
func (s *adcServiceResource) List(ctx context.Context) ([]*adctypes.Service, error) {
158+
result, err := s.dumpResources(ctx)
159+
if err != nil {
160+
return nil, err
161+
}
162+
return result.Services, nil
163+
}
164+
165+
// adcSSLResource implements SSLResource
166+
type adcSSLResource struct {
167+
*adcDataplaneResource
168+
}
169+
170+
func (s *adcSSLResource) List(ctx context.Context) ([]*adctypes.SSL, error) {
171+
result, err := s.dumpResources(ctx)
172+
if err != nil {
173+
return nil, err
174+
}
175+
return result.SSL, nil
176+
}
177+
178+
// adcConsumerResource implements ConsumerResource
179+
type adcConsumerResource struct {
180+
*adcDataplaneResource
181+
}
182+
183+
func (c *adcConsumerResource) List(ctx context.Context) ([]*adctypes.Consumer, error) {
184+
result, err := c.dumpResources(ctx)
185+
if err != nil {
186+
return nil, err
187+
}
188+
return result.Consumers, nil
189+
}

test/e2e/scaffold/api7_deployer.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,12 @@ func (s *API7Deployer) GetAdminEndpoint(_ ...*corev1.Service) string {
272272
// always return the default dashboard endpoint
273273
return framework.DashboardTLSEndpoint
274274
}
275+
276+
func (s *API7Deployer) DefaultDataplaneResource() DataplaneResource {
277+
return newADCDataplaneResource(
278+
"api7ee",
279+
s.Framework.GetDashboardEndpoint(),
280+
s.AdminKey(),
281+
false,
282+
)
283+
}

test/e2e/scaffold/apisix_deployer.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type APISIXDeployOptions struct {
4040

4141
type APISIXDeployer struct {
4242
*Scaffold
43+
adminTunnel *k8s.Tunnel
4344
}
4445

4546
func NewAPISIXDeployer(s *Scaffold) *APISIXDeployer {
@@ -91,6 +92,7 @@ func (s *APISIXDeployer) BeforeEach() {
9192
e.Add(func() {
9293
s.DeployDataplane()
9394
s.DeployIngress()
95+
s.createAdminTunnel(s.dataplaneService)
9496
})
9597
e.Add(s.DeployTestService)
9698
e.Wait()
@@ -220,12 +222,7 @@ func getEnvOrDefault(key, defaultValue string) string {
220222
return defaultValue
221223
}
222224

223-
//nolint:unused
224-
func (s *APISIXDeployer) createAdminTunnel(
225-
svc *corev1.Service,
226-
kubectlOpts *k8s.KubectlOptions,
227-
serviceName string,
228-
) (*k8s.Tunnel, error) {
225+
func (s *APISIXDeployer) createAdminTunnel(svc *corev1.Service) (*k8s.Tunnel, error) {
229226
var (
230227
adminNodePort int
231228
adminPort int
@@ -239,7 +236,9 @@ func (s *APISIXDeployer) createAdminTunnel(
239236
}
240237
}
241238

242-
adminTunnel := k8s.NewTunnel(kubectlOpts, k8s.ResourceTypeService, serviceName,
239+
kubectlOpts := k8s.NewKubectlOptions("", "", svc.Namespace)
240+
241+
adminTunnel := k8s.NewTunnel(kubectlOpts, k8s.ResourceTypeService, svc.Name,
243242
adminNodePort, adminPort)
244243

245244
if err := adminTunnel.ForwardPortE(s.t); err != nil {
@@ -343,3 +342,12 @@ func (s *APISIXDeployer) GetAdminEndpoint(svc ...*corev1.Service) string {
343342
}
344343
return fmt.Sprintf("http://%s.%s:9180", svc[0].Name, svc[0].Namespace)
345344
}
345+
346+
func (s *APISIXDeployer) DefaultDataplaneResource() DataplaneResource {
347+
return newADCDataplaneResource(
348+
"apisix-standalone",
349+
s.adminTunnel.Endpoint(),
350+
s.AdminKey(),
351+
false, // tlsVerify
352+
)
353+
}

test/e2e/scaffold/deployer.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
package scaffold
1414

15-
import corev1 "k8s.io/api/core/v1"
15+
import (
16+
corev1 "k8s.io/api/core/v1"
17+
)
1618

1719
// Deployer defines the interface for deploying data plane components
1820
type Deployer interface {
@@ -25,6 +27,7 @@ type Deployer interface {
2527
CreateAdditionalGateway(namePrefix string) (string, string, error)
2628
CleanupAdditionalGateway(identifier string) error
2729
GetAdminEndpoint(...*corev1.Service) string
30+
DefaultDataplaneResource() DataplaneResource
2831
}
2932

3033
var NewDeployer func(*Scaffold) Deployer

test/e2e/scaffold/scaffold.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
corev1 "k8s.io/api/core/v1"
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131

32-
"github.com/apache/apisix-ingress-controller/pkg/dashboard"
3332
"github.com/apache/apisix-ingress-controller/test/e2e/framework"
3433
)
3534

@@ -188,8 +187,8 @@ func (s *Scaffold) NewAPISIXHttpsClient(host string) *httpexpect.Expect {
188187
})
189188
}
190189

191-
func (s *Scaffold) DefaultDataplaneResource() dashboard.Cluster {
192-
return nil
190+
func (s *Scaffold) DefaultDataplaneResource() DataplaneResource {
191+
return s.Deployer.DefaultDataplaneResource()
193192
}
194193

195194
func (s *Scaffold) DeployTestService() {

0 commit comments

Comments
 (0)