Skip to content

Commit 890640e

Browse files
ronethingAlinsRan
andauthored
feat: support event triggered synchronization (#2478) (#210)
Co-authored-by: AlinsRan <[email protected]>
1 parent 5b7fcf1 commit 890640e

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

internal/provider/adc/adc.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ type adcClient struct {
9494

9595
updater status.Updater
9696
statusUpdateMap map[types.NamespacedNameKind][]string
97+
98+
syncCh chan struct{}
9799
}
98100

99101
type Task struct {
@@ -116,6 +118,7 @@ func New(updater status.Updater, opts ...Option) (provider.Provider, error) {
116118
store: NewStore(),
117119
executor: &DefaultADCExecutor{},
118120
updater: updater,
121+
syncCh: make(chan struct{}, 1),
119122
}, nil
120123
}
121124

@@ -220,6 +223,7 @@ func (d *adcClient) Update(ctx context.Context, tctx *provider.TranslateContext,
220223
// which only needs to be saved in cache
221224
// and triggered by a timer for synchronization
222225
if d.BackendMode == BackendModeAPISIXStandalone || d.BackendMode == BackendModeAPISIX {
226+
d.syncNotify()
223227
return nil
224228
}
225229

@@ -289,6 +293,8 @@ func (d *adcClient) Delete(ctx context.Context, obj client.Object) error {
289293
Name: obj.GetName(),
290294
configs: configs,
291295
})
296+
} else {
297+
d.syncNotify()
292298
}
293299
return nil
294300
case BackendModeAPI7EE:
@@ -306,26 +312,34 @@ func (d *adcClient) Delete(ctx context.Context, obj client.Object) error {
306312

307313
func (d *adcClient) Start(ctx context.Context) error {
308314
initalSyncDelay := d.InitSyncDelay
309-
time.AfterFunc(initalSyncDelay, func() {
310-
if err := d.Sync(ctx); err != nil {
311-
log.Error(err)
312-
return
313-
}
314-
})
315+
if initalSyncDelay > 0 {
316+
time.AfterFunc(initalSyncDelay, func() {
317+
if err := d.Sync(ctx); err != nil {
318+
log.Error(err)
319+
return
320+
}
321+
})
322+
}
315323

316324
if d.SyncPeriod < 1 {
317325
return nil
318326
}
319327
ticker := time.NewTicker(d.SyncPeriod)
320328
defer ticker.Stop()
321329
for {
330+
synced := false
322331
select {
332+
case <-d.syncCh:
333+
synced = true
323334
case <-ticker.C:
335+
synced = true
336+
case <-ctx.Done():
337+
return nil
338+
}
339+
if synced {
324340
if err := d.Sync(ctx); err != nil {
325341
log.Error(err)
326342
}
327-
case <-ctx.Done():
328-
return nil
329343
}
330344
}
331345
}
@@ -506,6 +520,13 @@ func (d *adcClient) sync(ctx context.Context, task Task) error {
506520
return nil
507521
}
508522

523+
func (d *adcClient) syncNotify() {
524+
select {
525+
case d.syncCh <- struct{}{}:
526+
default:
527+
}
528+
}
529+
509530
func prepareSyncFile(resources any) (string, func(), error) {
510531
data, err := json.Marshal(resources)
511532
if err != nil {

internal/provider/adc/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ func (d *adcClient) updateConfigForGatewayProxy(tctx *provider.TranslateContext,
207207
for _, ref := range referrers {
208208
d.configs[ref] = *config
209209
}
210+
211+
d.syncNotify()
210212
return nil
211213
}
212214

test/conformance/apisix/suite_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ func TestMain(m *testing.M) {
165165
ControllerName: s.GetControllerName(),
166166
Namespace: namespace,
167167
StatusAddress: address,
168-
InitSyncDelay: 1 * time.Minute,
168+
InitSyncDelay: 20 * time.Minute,
169169
ProviderType: framework.ProviderType,
170-
ProviderSyncPeriod: 10 * time.Millisecond,
170+
ProviderSyncPeriod: 1 * time.Hour,
171171
})
172172

173173
adminEndpoint := fmt.Sprintf("http://%s.%s:9180", svc.Name, namespace)

test/e2e/scaffold/apisix_deployer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func (s *APISIXDeployer) DeployIngress() {
260260
s.Framework.DeployIngress(framework.IngressDeployOpts{
261261
ControllerName: s.opts.ControllerName,
262262
ProviderType: framework.ProviderType,
263-
ProviderSyncPeriod: 200 * time.Millisecond,
263+
ProviderSyncPeriod: 1 * time.Hour,
264264
Namespace: s.namespace,
265265
Replicas: 1,
266266
})
@@ -270,7 +270,7 @@ func (s *APISIXDeployer) ScaleIngress(replicas int) {
270270
s.Framework.DeployIngress(framework.IngressDeployOpts{
271271
ControllerName: s.opts.ControllerName,
272272
ProviderType: framework.ProviderType,
273-
ProviderSyncPeriod: 200 * time.Millisecond,
273+
ProviderSyncPeriod: 1 * time.Hour,
274274
Namespace: s.namespace,
275275
Replicas: replicas,
276276
})

0 commit comments

Comments
 (0)