Skip to content

Commit d5d228a

Browse files
committed
feat: add support for apisix backend mode
1 parent 4ee2596 commit d5d228a

File tree

13 files changed

+127
-15
lines changed

13 files changed

+127
-15
lines changed

.github/workflows/apisix-conformance-test.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ jobs:
3636
timeout-minutes: 60
3737
needs:
3838
- prepare
39+
strategy:
40+
matrix:
41+
provider_type:
42+
- apisix-standalone
43+
- apisix
3944
runs-on: buildjet-2vcpu-ubuntu-2204
4045
steps:
4146
- name: Checkout
@@ -79,6 +84,8 @@ jobs:
7984
- name: Run Conformance Test
8085
shell: bash
8186
continue-on-error: true
87+
env:
88+
PROVIDER_TYPE: ${{ matrix.provider_type }}
8289
run: |
8390
make conformance-test-standalone
8491

.github/workflows/apisix-e2e-test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ jobs:
3535
e2e-test:
3636
needs:
3737
- prepare
38+
strategy:
39+
matrix:
40+
provider_type:
41+
- apisix-standalone
42+
- apisix
3843
runs-on: buildjet-2vcpu-ubuntu-2204
3944
steps:
4045
- name: Checkout
@@ -83,5 +88,6 @@ jobs:
8388
shell: bash
8489
env:
8590
TEST_DIR: "./test/e2e/apisix/"
91+
PROVIDER_TYPE: ${{ matrix.provider_type }}
8692
run: |
8793
make e2e-test

internal/controller/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func validateProvider(config ProviderConfig) error {
118118
return fmt.Errorf("sync_period must be greater than 0 for standalone provider")
119119
}
120120
return nil
121-
case ProviderTypeAPI7EE:
121+
case ProviderTypeAPI7EE, ProviderTypeAPISIX:
122122
return nil
123123
default:
124124
return fmt.Errorf("unsupported provider type: %s", config.Type)

internal/controller/config/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type ProviderType string
2121
const (
2222
ProviderTypeStandalone ProviderType = "apisix-standalone"
2323
ProviderTypeAPI7EE ProviderType = "api7ee"
24+
ProviderTypeAPISIX ProviderType = "apisix"
2425
)
2526

2627
const (

internal/provider/adc/adc.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ package adc
1515
import (
1616
"context"
1717
"encoding/json"
18-
"errors"
1918
"os"
2019
"sync"
2120
"time"
2221

2322
"github.com/api7/gopkg/pkg/log"
23+
"github.com/pkg/errors"
2424
"go.uber.org/zap"
2525
networkingv1 "k8s.io/api/networking/v1"
2626
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -48,6 +48,7 @@ type BackendMode string
4848
const (
4949
BackendModeAPISIXStandalone string = "apisix-standalone"
5050
BackendModeAPI7EE string = "api7ee"
51+
BackendModeAPISIX string = "apisix"
5152
)
5253

5354
type adcClient struct {
@@ -185,11 +186,21 @@ func (d *adcClient) Update(ctx context.Context, tctx *provider.TranslateContext,
185186
}
186187
}
187188

188-
// This mode is full synchronization,
189-
// which only needs to be saved in cache
190-
// and triggered by a timer for synchronization
191-
if d.BackendMode == BackendModeAPISIXStandalone || apiv2.Is(obj) {
189+
switch d.BackendMode {
190+
case BackendModeAPI7EE:
191+
if apiv2.Is(obj) {
192+
return nil
193+
}
194+
case BackendModeAPISIX:
195+
// do nothing
196+
case BackendModeAPISIXStandalone:
197+
// This mode is full synchronization,
198+
// which only needs to be saved in cache
199+
// and triggered by a timer for synchronization
192200
return nil
201+
default:
202+
return errors.Errorf("unknown backend mode: %s", d.BackendMode)
203+
193204
}
194205

195206
return d.sync(ctx, Task{

internal/provider/adc/executor.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,19 @@ func (e *DefaultADCExecutor) Execute(ctx context.Context, mode string, config ad
4646

4747
func (e *DefaultADCExecutor) runADC(ctx context.Context, mode string, config adcConfig, args []string) error {
4848
for _, addr := range config.ServerAddrs {
49-
ctxWithTimeout, cancel := context.WithTimeout(ctx, 15*time.Second)
50-
defer cancel()
51-
if err := e.runForSingleServer(ctxWithTimeout, addr, mode, config, args); err != nil {
49+
if err := e.runForSingleServerWithTimeout(ctx, addr, mode, config, args); err != nil {
5250
return err
5351
}
5452
}
5553
return nil
5654
}
5755

56+
func (e *DefaultADCExecutor) runForSingleServerWithTimeout(ctx context.Context, serverAddr, mode string, config adcConfig, args []string) error {
57+
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
58+
defer cancel()
59+
return e.runForSingleServer(ctx, serverAddr, mode, config, args)
60+
}
61+
5862
func (e *DefaultADCExecutor) runForSingleServer(ctx context.Context, serverAddr, mode string, config adcConfig, args []string) error {
5963
cmdArgs := append([]string{}, args...)
6064
if !config.TlsVerify {
@@ -108,6 +112,10 @@ func (e *DefaultADCExecutor) buildCmdError(runErr error, stdout, stderr []byte)
108112

109113
func (e *DefaultADCExecutor) handleOutput(output []byte) error {
110114
var result adctypes.SyncResult
115+
if index := strings.IndexByte(string(output), '{'); index > 0 {
116+
log.Warnf("extra output: %s", string(output[:index]))
117+
output = output[index:]
118+
}
111119
if err := json.Unmarshal(output, &result); err != nil {
112120
log.Errorw("failed to unmarshal adc output",
113121
zap.Error(err),

test/conformance/apisix/suite_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
package conformance
1313

1414
import (
15+
"cmp"
1516
"context"
1617
"fmt"
1718
"os"
@@ -160,7 +161,7 @@ func TestMain(m *testing.M) {
160161
Namespace: namespace,
161162
StatusAddress: address,
162163
InitSyncDelay: 1 * time.Minute,
163-
ProviderType: "apisix-standalone",
164+
ProviderType: cmp.Or(os.Getenv(framework.EnvKeyProviderType), "apisix-standalone"),
164165
ProviderSyncPeriod: 10 * time.Millisecond,
165166
})
166167

test/e2e/framework/apisix_consts.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,17 @@ import (
1919
"github.com/Masterminds/sprig/v3"
2020
)
2121

22+
const (
23+
EnvKeyProviderType = "PROVIDER_TYPE"
24+
)
25+
2226
var (
2327
//go:embed manifests/apisix-standalone.yaml
2428
apisixStandaloneTemplate string
2529
APISIXStandaloneTpl *template.Template
30+
31+
//go:embed manifests/etcd.yaml
32+
EtcdSpec string
2633
)
2734

2835
var (

test/e2e/framework/assertion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func APIv2MustHaveCondition(t testing.TestingT, cli client.Client, timeout time.
111111
}
112112
err := PollUntilAPIv2MustHaveStatus(cli, timeout, nn, obj, f)
113113

114-
require.NoError(t, err, "error waiting status to have a Condition matching %+v", nn, cond)
114+
require.NoError(t, err, "error waiting %s status to have a Condition matching %+v", nn, cond)
115115
}
116116

117117
func PollUntilAPIv2MustHaveStatus(cli client.Client, timeout time.Duration, nn types.NamespacedName, obj client.Object, f func(client.Object) bool) error {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@ data:
77
deployment:
88
role: traditional
99
role_traditional:
10-
config_provider: yaml
10+
# on backend mode apisix-standalone, config_provider is "yaml"
11+
# on backend mode apisix, config_provider is "etcd"
12+
config_provider: {{ .ConfigProvider | default "yaml" }}
1113
admin:
1214
allow_admin:
1315
- 0.0.0.0/0
1416
admin_key:
1517
- key: {{ .AdminKey }}
1618
name: admin
1719
role: admin
20+
{{- if eq .ConfigProvider "etcd" }}
21+
etcd:
22+
host:
23+
- "http://etcd:2379"
24+
{{- end }}
1825
nginx_config:
1926
worker_processes: 2
2027
error_log_level: info

0 commit comments

Comments
 (0)