Skip to content

Commit 442d7e9

Browse files
authored
fix: support sync global rule in api7ee mode (#191)
Signed-off-by: ashing <[email protected]>
1 parent afae2b6 commit 442d7e9

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

internal/provider/adc/adc.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"encoding/json"
2323
"os"
24+
"slices"
2425
"sync"
2526
"time"
2627

@@ -353,6 +354,34 @@ func (d *adcClient) sync(ctx context.Context, task Task) error {
353354
return errors.New("no adc configs provided")
354355
}
355356

357+
// for global rules, we need to list all global rules and set it to the task resources
358+
if slices.Contains(task.ResourceTypes, "global_rule") {
359+
for _, config := range task.configs {
360+
globalRules, err := d.store.ListGlobalRules(config.Name)
361+
if err != nil {
362+
return err
363+
}
364+
task.Resources.GlobalRules = *globalRules
365+
log.Debugw("syncing resources global rules", zap.Any("globalRules", task.Resources.GlobalRules))
366+
367+
syncFilePath, cleanup, err := prepareSyncFile(task.Resources)
368+
if err != nil {
369+
return err
370+
}
371+
defer cleanup()
372+
373+
args := BuildADCExecuteArgs(syncFilePath, task.Labels, task.ResourceTypes)
374+
375+
if err := d.executor.Execute(ctx, d.BackendMode, config, args); err != nil {
376+
log.Errorw("failed to execute adc command", zap.Error(err), zap.Any("config", config))
377+
return err
378+
}
379+
}
380+
381+
return nil
382+
}
383+
384+
// every task resources is the same, so we can use the first config to prepare the sync file
356385
syncFilePath, cleanup, err := prepareSyncFile(task.Resources)
357386
if err != nil {
358387
return err

internal/provider/adc/store.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,28 @@ func (s *Store) GetResources(name string) (*adctypes.Resources, error) {
244244
PluginMetadata: metadata,
245245
}, nil
246246
}
247+
248+
func (s *Store) ListGlobalRules(name string) (*adctypes.GlobalRule, error) {
249+
s.Lock()
250+
defer s.Unlock()
251+
252+
var globalrule adctypes.GlobalRule
253+
targetCache, ok := s.cacheMap[name]
254+
if !ok {
255+
return &globalrule, nil
256+
}
257+
// Get all global rules from cache and merge them
258+
globalRuleItems, _ := targetCache.ListGlobalRules()
259+
if len(globalRuleItems) > 0 {
260+
merged := make(adctypes.Plugins)
261+
for _, item := range globalRuleItems {
262+
for k, v := range item.Plugins {
263+
merged[k] = v
264+
}
265+
}
266+
globalrule = adctypes.GlobalRule(merged)
267+
}
268+
log.Debugw("get resources global rule items", zap.Any("globalRuleItems", globalRuleItems))
269+
270+
return &globalrule, nil
271+
}

test/e2e/crds/v2/globalrule.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ spec:
8888

8989
Context("ApisixGlobalRule Basic Operations", func() {
9090
BeforeEach(func() {
91-
if s.Deployer.Name() == "api7ee" {
92-
Skip("GlobalRule is not supported in api7ee")
93-
}
9491
By("create GatewayProxy")
9592
gatewayProxy := fmt.Sprintf(gatewayProxyYaml, s.Deployer.GetAdminEndpoint(), s.AdminKey())
9693
err := s.CreateResourceFromStringWithNamespace(gatewayProxy, "default")

0 commit comments

Comments
 (0)