Skip to content

Commit 1d3f633

Browse files
authored
feat: add support for apisix backend mode (#181)
1 parent 24bb5b5 commit 1d3f633

File tree

14 files changed

+124
-22
lines changed

14 files changed

+124
-22
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
@@ -113,7 +113,7 @@ func (c *Config) Validate() error {
113113

114114
func validateProvider(config ProviderConfig) error {
115115
switch config.Type {
116-
case ProviderTypeStandalone:
116+
case ProviderTypeStandalone, ProviderTypeAPISIX:
117117
if config.SyncPeriod.Duration <= 0 {
118118
return fmt.Errorf("sync_period must be greater than 0 for standalone provider")
119119
}

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: 4 additions & 3 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 {
@@ -188,7 +189,7 @@ func (d *adcClient) Update(ctx context.Context, tctx *provider.TranslateContext,
188189
// This mode is full synchronization,
189190
// which only needs to be saved in cache
190191
// and triggered by a timer for synchronization
191-
if d.BackendMode == BackendModeAPISIXStandalone || apiv2.Is(obj) {
192+
if d.BackendMode == BackendModeAPISIXStandalone || d.BackendMode == BackendModeAPISIX || apiv2.Is(obj) {
192193
return nil
193194
}
194195

@@ -249,7 +250,7 @@ func (d *adcClient) Delete(ctx context.Context, obj client.Object) error {
249250
log.Debugw("successfully deleted resources from store", zap.Any("object", obj))
250251

251252
switch d.BackendMode {
252-
case BackendModeAPISIXStandalone:
253+
case BackendModeAPISIXStandalone, BackendModeAPISIX:
253254
// Full synchronization is performed on a gateway by gateway basis
254255
// and it is not possible to perform scheduled synchronization
255256
// on deleted gateway level resources

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func TestMain(m *testing.M) {
160160
Namespace: namespace,
161161
StatusAddress: address,
162162
InitSyncDelay: 1 * time.Minute,
163-
ProviderType: "apisix-standalone",
163+
ProviderType: framework.ProviderType,
164164
ProviderSyncPeriod: 10 * time.Millisecond,
165165
})
166166

test/e2e/framework/apisix_consts.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,25 @@
1313
package framework
1414

1515
import (
16+
"cmp"
1617
_ "embed"
18+
"os"
1719
"text/template"
1820

1921
"github.com/Masterminds/sprig/v3"
2022
)
2123

2224
var (
23-
//go:embed manifests/apisix-standalone.yaml
25+
ProviderType = cmp.Or(os.Getenv("PROVIDER_TYPE"), "apisix-standalone")
26+
)
27+
28+
var (
29+
//go:embed manifests/apisix.yaml
2430
apisixStandaloneTemplate string
2531
APISIXStandaloneTpl *template.Template
32+
33+
//go:embed manifests/etcd.yaml
34+
EtcdSpec string
2635
)
2736

2837
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 renamed to test/e2e/framework/manifests/apisix.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)