@@ -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
2527type 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 {
3334func 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