Skip to content

Commit 54dab10

Browse files
committed
fix: r
Signed-off-by: ashing <[email protected]>
1 parent 8b7a88e commit 54dab10

File tree

9 files changed

+143
-14
lines changed

9 files changed

+143
-14
lines changed

api/adc/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ func (g *GlobalRule) DeepCopy() GlobalRule {
103103
return GlobalRule(copied)
104104
}
105105

106+
// +k8s:deepcopy-gen=true
107+
type GlobalRuleItem struct {
108+
Metadata `json:",inline" yaml:",inline"`
109+
110+
Plugins Plugins `json:"plugins" yaml:"plugins"`
111+
}
112+
106113
type PluginMetadata Plugins
107114

108115
func (p *PluginMetadata) DeepCopy() PluginMetadata {

api/adc/zz_generated.deepcopy.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/provider/adc/adc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ func (d *adcClient) Update(ctx context.Context, tctx *provider.TranslateContext,
181181
// and triggered by a timer for synchronization
182182
return nil
183183
case BackendModeAPI7EE:
184+
// TODO: sync global rule in api7ee mode if we need
185+
if _, ok := obj.(*apiv2.ApisixGlobalRule); ok {
186+
log.Debugw("apisix global rule, skip sync", zap.Any("obj", obj))
187+
return nil
188+
}
189+
184190
return d.sync(ctx, Task{
185191
Name: obj.GetName(),
186192
Labels: label.GenLabel(obj),

internal/provider/adc/cache/cache.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,35 @@ type Cache interface {
2626
InsertService(*types.Service) error
2727
// InsertConsumer adds or updates consumer to cache.
2828
InsertConsumer(*types.Consumer) error
29+
// InsertGlobalRule adds or updates global rule to cache.
30+
InsertGlobalRule(*types.GlobalRuleItem) error
2931

3032
// GetSSL finds the ssl from cache according to the primary index (id).
3133
GetSSL(string) (*types.SSL, error)
3234
// GetUpstream finds the upstream from cache according to the primary index (id).
3335
GetService(string) (*types.Service, error)
3436
// GetConsumer finds the consumer from cache according to the primary index (username).
3537
GetConsumer(string) (*types.Consumer, error)
38+
// GetGlobalRule finds the global rule from cache according to the primary index (id).
39+
GetGlobalRule(string) (*types.GlobalRuleItem, error)
3640

3741
// DeleteSSL deletes the specified ssl in cache.
3842
DeleteSSL(*types.SSL) error
3943
// DeleteUpstream deletes the specified upstream in cache.
4044
DeleteService(*types.Service) error
41-
// DeleteGlobalRule deletes the specified stream_route in cache.
45+
// DeleteConsumer deletes the specified consumer in cache.
4246
DeleteConsumer(*types.Consumer) error
47+
// DeleteGlobalRule deletes the specified global rule in cache.
48+
DeleteGlobalRule(*types.GlobalRuleItem) error
4349

4450
// ListSSL lists all ssl objects in cache.
4551
ListSSL(...ListOption) ([]*types.SSL, error)
4652
// ListUpstreams lists all upstreams in cache.
4753
ListServices(...ListOption) ([]*types.Service, error)
4854
// ListConsumers lists all consumer objects in cache.
4955
ListConsumers(...ListOption) ([]*types.Consumer, error)
56+
// ListGlobalRules lists all global rule objects in cache.
57+
ListGlobalRules(...ListOption) ([]*types.GlobalRuleItem, error)
5058
}
5159

5260
type ListOption interface {

internal/provider/adc/cache/memdb.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func (c *dbCache) Insert(obj any) error {
5050
return c.InsertService(t)
5151
case *types.Consumer:
5252
return c.InsertConsumer(t)
53+
case *types.GlobalRuleItem:
54+
return c.InsertGlobalRule(t)
5355
default:
5456
return errors.New("unsupported type")
5557
}
@@ -65,6 +67,8 @@ func (c *dbCache) Delete(obj any) error {
6567
return c.DeleteService(t)
6668
case *types.Consumer:
6769
return c.DeleteConsumer(t)
70+
case *types.GlobalRuleItem:
71+
return c.DeleteGlobalRule(t)
6872
default:
6973
return errors.New("unsupported type")
7074
}
@@ -87,6 +91,10 @@ func (c *dbCache) InsertConsumer(consumer *types.Consumer) error {
8791
return c.insert("consumer", consumer.DeepCopy())
8892
}
8993

94+
func (c *dbCache) InsertGlobalRule(globalRule *types.GlobalRuleItem) error {
95+
return c.insert("global_rule", globalRule.DeepCopy())
96+
}
97+
9098
func (c *dbCache) insert(table string, obj any) error {
9199
txn := c.db.Txn(true)
92100
defer txn.Abort()
@@ -129,6 +137,14 @@ func (c *dbCache) GetConsumer(username string) (*types.Consumer, error) {
129137
return obj.(*types.Consumer).DeepCopy(), nil
130138
}
131139

140+
func (c *dbCache) GetGlobalRule(id string) (*types.GlobalRuleItem, error) {
141+
obj, err := c.get("global_rule", id)
142+
if err != nil {
143+
return nil, err
144+
}
145+
return obj.(*types.GlobalRuleItem).DeepCopy(), nil
146+
}
147+
132148
func (c *dbCache) GetStreamRoute(id string) (*types.StreamRoute, error) {
133149
obj, err := c.get("stream_route", id)
134150
if err != nil {
@@ -201,6 +217,18 @@ func (c *dbCache) ListConsumers(opts ...ListOption) ([]*types.Consumer, error) {
201217
return consumers, nil
202218
}
203219

220+
func (c *dbCache) ListGlobalRules(opts ...ListOption) ([]*types.GlobalRuleItem, error) {
221+
raws, err := c.list("global_rule", opts...)
222+
if err != nil {
223+
return nil, err
224+
}
225+
globalRules := make([]*types.GlobalRuleItem, 0, len(raws))
226+
for _, raw := range raws {
227+
globalRules = append(globalRules, raw.(*types.GlobalRuleItem).DeepCopy())
228+
}
229+
return globalRules, nil
230+
}
231+
204232
func (c *dbCache) list(table string, opts ...ListOption) ([]any, error) {
205233
txn := c.db.Txn(false)
206234
defer txn.Abort()
@@ -239,6 +267,10 @@ func (c *dbCache) DeleteConsumer(consumer *types.Consumer) error {
239267
return c.delete("consumer", consumer)
240268
}
241269

270+
func (c *dbCache) DeleteGlobalRule(globalRule *types.GlobalRuleItem) error {
271+
return c.delete("global_rule", globalRule)
272+
}
273+
242274
func (c *dbCache) delete(table string, obj any) error {
243275
txn := c.db.Txn(true)
244276
defer txn.Abort()

internal/provider/adc/cache/noop_db.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (c *noopCache) InsertService(u *types.Service) error {
4040
return nil
4141
}
4242

43-
func (c *noopCache) InsertGlobalRule(gr *types.GlobalRule) error {
43+
func (c *noopCache) InsertGlobalRule(gr *types.GlobalRuleItem) error {
4444
return nil
4545
}
4646

@@ -56,7 +56,7 @@ func (c *noopCache) GetService(id string) (*types.Service, error) {
5656
return nil, nil
5757
}
5858

59-
func (c *noopCache) GetGlobalRule(id string) (*types.GlobalRule, error) {
59+
func (c *noopCache) GetGlobalRule(id string) (*types.GlobalRuleItem, error) {
6060
return nil, nil
6161
}
6262

@@ -76,7 +76,7 @@ func (c *noopCache) ListStreamRoutes(...ListOption) ([]*types.StreamRoute, error
7676
return nil, nil
7777
}
7878

79-
func (c *noopCache) ListGlobalRules(...ListOption) ([]*types.GlobalRule, error) {
79+
func (c *noopCache) ListGlobalRules(...ListOption) ([]*types.GlobalRuleItem, error) {
8080
return nil, nil
8181
}
8282

@@ -92,7 +92,7 @@ func (c *noopCache) DeleteService(u *types.Service) error {
9292
return nil
9393
}
9494

95-
func (c *noopCache) DeleteGlobalRule(gr *types.GlobalRule) error {
95+
func (c *noopCache) DeleteGlobalRule(gr *types.GlobalRuleItem) error {
9696
return nil
9797
}
9898

internal/provider/adc/cache/schema.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ var (
7373
},
7474
},
7575
},
76+
"global_rule": {
77+
Name: "global_rule",
78+
Indexes: map[string]*memdb.IndexSchema{
79+
"id": {
80+
Name: "id",
81+
Unique: true,
82+
Indexer: &memdb.StringFieldIndex{Field: "ID"},
83+
},
84+
KindLabelIndex: {
85+
Name: KindLabelIndex,
86+
Unique: false,
87+
AllowMissing: true,
88+
Indexer: &KindLabelIndexer,
89+
},
90+
},
91+
},
7692
},
7793
}
7894
)

internal/provider/adc/executor.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ func (e *DefaultADCExecutor) Execute(ctx context.Context, mode string, config ad
4545
}
4646

4747
func (e *DefaultADCExecutor) runADC(ctx context.Context, mode string, config adcConfig, args []string) error {
48-
ctxWithTimeout, cancel := context.WithTimeout(ctx, 10*time.Second)
49-
defer cancel()
50-
5148
for _, addr := range config.ServerAddrs {
49+
ctxWithTimeout, cancel := context.WithTimeout(ctx, 15*time.Second)
50+
defer cancel()
5251
if err := e.runForSingleServer(ctxWithTimeout, addr, mode, config, args); err != nil {
5352
return err
5453
}
@@ -62,6 +61,8 @@ func (e *DefaultADCExecutor) runForSingleServer(ctx context.Context, serverAddr,
6261
cmdArgs = append(cmdArgs, "--tls-skip-verify")
6362
}
6463

64+
cmdArgs = append(cmdArgs, "--timeout", "15s")
65+
6566
env := e.prepareEnv(serverAddr, mode, config.Token)
6667

6768
var stdout, stderr bytes.Buffer

internal/provider/adc/store.go

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"sync"
1717

1818
"github.com/api7/gopkg/pkg/log"
19+
"github.com/google/uuid"
20+
"go.uber.org/zap"
1921

2022
adctypes "github.com/apache/apisix-ingress-controller/api/adc"
2123
"github.com/apache/apisix-ingress-controller/internal/controller/label"
@@ -24,7 +26,6 @@ import (
2426

2527
type Store struct {
2628
cacheMap map[string]cache.Cache
27-
globalruleMap map[string]adctypes.GlobalRule
2829
pluginMetadataMap map[string]adctypes.PluginMetadata
2930

3031
sync.Mutex
@@ -33,7 +34,6 @@ type Store struct {
3334
func NewStore() *Store {
3435
return &Store{
3536
cacheMap: make(map[string]cache.Cache),
36-
globalruleMap: make(map[string]adctypes.GlobalRule),
3737
pluginMetadataMap: make(map[string]adctypes.PluginMetadata),
3838
}
3939
}
@@ -105,7 +105,32 @@ func (s *Store) Insert(name string, resourceTypes []string, resources adctypes.R
105105
}
106106
}
107107
case "global_rule":
108-
s.globalruleMap[name] = resources.GlobalRules
108+
// List existing global rules that match the selector
109+
globalRules, err := targetCache.ListGlobalRules(selector)
110+
if err != nil {
111+
return err
112+
}
113+
// Delete existing matching global rules
114+
for _, globalRule := range globalRules {
115+
if err := targetCache.DeleteGlobalRule(globalRule); err != nil {
116+
return err
117+
}
118+
}
119+
// Convert GlobalRule (Plugins) to GlobalRuleItem and insert
120+
if len(resources.GlobalRules) > 0 {
121+
id := name + "-" + uuid.NewString()
122+
globalRuleItem := &adctypes.GlobalRuleItem{
123+
Metadata: adctypes.Metadata{
124+
ID: id,
125+
Name: id,
126+
Labels: Labels,
127+
},
128+
Plugins: adctypes.Plugins(resources.GlobalRules),
129+
}
130+
if err := targetCache.InsertGlobalRule(globalRuleItem); err != nil {
131+
return err
132+
}
133+
}
109134
case "plugin_metadata":
110135
s.pluginMetadataMap[name] = resources.PluginMetadata
111136
default:
@@ -160,7 +185,15 @@ func (s *Store) Delete(name string, resourceTypes []string, Labels map[string]st
160185
}
161186
}
162187
case "global_rule":
163-
delete(s.globalruleMap, name)
188+
globalRules, err := targetCache.ListGlobalRules(selector)
189+
if err != nil {
190+
log.Errorf("failed to list global rules: %v", err)
191+
}
192+
for _, globalRule := range globalRules {
193+
if err := targetCache.DeleteGlobalRule(globalRule); err != nil {
194+
log.Errorf("failed to delete global rule %s: %v", globalRule.ID, err)
195+
}
196+
}
164197
case "plugin_metadata":
165198
delete(s.pluginMetadataMap, name)
166199
}
@@ -180,9 +213,18 @@ func (s *Store) GetResources(name string) (*adctypes.Resources, error) {
180213
}
181214
var globalrule adctypes.GlobalRule
182215
var metadata adctypes.PluginMetadata
183-
if global, ok := s.globalruleMap[name]; ok {
184-
globalrule = global.DeepCopy()
216+
// Get all global rules from cache and merge them
217+
globalRuleItems, _ := targetCache.ListGlobalRules()
218+
if len(globalRuleItems) > 0 {
219+
merged := make(adctypes.Plugins)
220+
for _, item := range globalRuleItems {
221+
for k, v := range item.Plugins {
222+
merged[k] = v
223+
}
224+
}
225+
globalrule = adctypes.GlobalRule(merged)
185226
}
227+
log.Debugw("get resources global rule items", zap.Any("globalRuleItems", globalRuleItems))
186228
if meta, ok := s.pluginMetadataMap[name]; ok {
187229
metadata = meta.DeepCopy()
188230
}

0 commit comments

Comments
 (0)