@@ -33,6 +33,7 @@ import (
3333
3434 adctypes "github.com/apache/apisix-ingress-controller/api/adc"
3535 "github.com/apache/apisix-ingress-controller/internal/adc/cache"
36+ "github.com/apache/apisix-ingress-controller/internal/provider/common"
3637 "github.com/apache/apisix-ingress-controller/internal/types"
3738 pkgmetrics "github.com/apache/apisix-ingress-controller/pkg/metrics"
3839)
@@ -43,20 +44,24 @@ type Client struct {
4344
4445 executor ADCExecutor
4546 BackendMode string
47+
48+ ConfigManager * common.ConfigManager [types.NamespacedNameKind , adctypes.Config ]
4649}
4750
4851func New (mode string ) (* Client , error ) {
4952 return & Client {
50- Store : cache .NewStore (),
51- executor : & DefaultADCExecutor {},
52- BackendMode : mode ,
53+ Store : cache .NewStore (),
54+ executor : & DefaultADCExecutor {},
55+ BackendMode : mode ,
56+ ConfigManager : common .NewConfigManager [types.NamespacedNameKind , adctypes.Config ](),
5357 }, nil
5458}
5559
5660type Task struct {
61+ Key types.NamespacedNameKind
5762 Name string
5863 Labels map [string ]string
59- Configs [ ]adctypes.Config
64+ Configs map [types. NamespacedNameKind ]adctypes.Config
6065 ResourceTypes []string
6166 Resources * adctypes.Resources
6267}
@@ -80,7 +85,7 @@ func (d *Client) Remove(ctx context.Context, args Task) error {
8085 d .mu .Lock ()
8186 defer d .mu .Unlock ()
8287 for _ , config := range args .Configs {
83- if err := d .Delete (config .Name , args .ResourceTypes , args .Labels ); err != nil {
88+ if err := d .Store . Delete (config .Name , args .ResourceTypes , args .Labels ); err != nil {
8489 log .Errorw ("failed to delete resources from store" ,
8590 zap .String ("name" , config .Name ),
8691 zap .Error (err ),
@@ -94,14 +99,134 @@ func (d *Client) Remove(ctx context.Context, args Task) error {
9499func (d * Client ) Update (ctx context.Context , args Task ) error {
95100 d .mu .Lock ()
96101 defer d .mu .Unlock ()
102+ deleteConfigs , err := d .ConfigManager .Update (args .Key , args .Configs )
103+ if err != nil {
104+ log .Errorw ("failed to update configs" , zap .Error (err ))
105+ return err
106+ }
107+
108+ for _ , config := range deleteConfigs {
109+ if err := d .Store .Delete (config .Name , args .ResourceTypes , args .Labels ); err != nil {
110+ log .Errorw ("failed to delete resources from store" ,
111+ zap .String ("name" , config .Name ),
112+ zap .Error (err ),
113+ )
114+ return err
115+ }
116+ }
117+
118+ for _ , config := range args .Configs {
119+ if err := d .Store .Insert (config .Name , args .ResourceTypes , args .Resources , args .Labels ); err != nil {
120+ log .Errorw ("failed to insert resources into store" ,
121+ zap .String ("name" , config .Name ),
122+ zap .Error (err ),
123+ )
124+ return err
125+ }
126+ }
127+
128+ if len (deleteConfigs ) > 0 {
129+ err := d .sync (ctx , Task {
130+ Name : args .Name ,
131+ Labels : args .Labels ,
132+ ResourceTypes : args .ResourceTypes ,
133+ Configs : deleteConfigs ,
134+ })
135+ if err != nil {
136+ log .Warnw ("failed to sync deleted configs" , zap .Error (err ))
137+ }
138+ }
97139 return d .sync (ctx , args )
98140}
99141
100- func (c * Client ) Sync (ctx context.Context , cfg map [string ]adctypes.Config ) (map [string ]types.ADCExecutionErrors , error ) {
142+ func (d * Client ) UpdateConfig (ctx context.Context , args Task ) error {
143+ d .mu .Lock ()
144+ defer d .mu .Unlock ()
145+ deleteConfigs , err := d .ConfigManager .Update (args .Key , args .Configs )
146+ if err != nil {
147+ log .Errorw ("failed to update configs" , zap .Error (err ))
148+
149+ return err
150+ }
151+
152+ for _ , config := range deleteConfigs {
153+ if err := d .Store .Delete (config .Name , args .ResourceTypes , args .Labels ); err != nil {
154+ log .Errorw ("failed to delete resources from store" ,
155+ zap .String ("name" , config .Name ),
156+ zap .Error (err ),
157+ )
158+ return err
159+ }
160+ }
161+
162+ for _ , config := range args .Configs {
163+ if err := d .Store .Insert (config .Name , args .ResourceTypes , args .Resources , args .Labels ); err != nil {
164+ log .Errorw ("failed to insert resources into store" ,
165+ zap .String ("name" , config .Name ),
166+ zap .Error (err ),
167+ )
168+ return err
169+ }
170+ }
171+ return nil
172+ }
173+
174+ func (d * Client ) Delete (ctx context.Context , args Task ) error {
175+ d .mu .Lock ()
176+ defer d .mu .Unlock ()
177+
178+ configs := d .ConfigManager .Get (args .Key )
179+
180+ for _ , config := range configs {
181+ if err := d .Store .Delete (config .Name , args .ResourceTypes , args .Labels ); err != nil {
182+ log .Errorw ("failed to delete resources from store" ,
183+ zap .String ("name" , config .Name ),
184+ zap .Error (err ),
185+ )
186+ return err
187+ }
188+ }
189+
190+ return d .sync (ctx , Task {
191+ Labels : args .Labels ,
192+ ResourceTypes : args .ResourceTypes ,
193+ })
194+ }
195+
196+ func (d * Client ) DeleteConfig (ctx context.Context , args Task ) error {
197+ d .mu .Lock ()
198+ defer d .mu .Unlock ()
199+
200+ for _ , config := range args .Configs {
201+ if err := d .Store .Delete (config .Name , args .ResourceTypes , args .Labels ); err != nil {
202+ log .Errorw ("failed to delete resources from store" ,
203+ zap .String ("name" , config .Name ),
204+ zap .Error (err ),
205+ )
206+ return err
207+ }
208+ }
209+
210+ return nil
211+ }
212+
213+ func (c * Client ) Sync (ctx context.Context ) (map [string ]types.ADCExecutionErrors , error ) {
101214 c .mu .Lock ()
102215 defer c .mu .Unlock ()
103216 log .Debug ("syncing all resources" )
104217
218+ configs := c .ConfigManager .List ()
219+
220+ if len (configs ) == 0 {
221+ log .Warn ("no GatewayProxy configs provided" )
222+ return nil , nil
223+ }
224+
225+ cfg := map [string ]adctypes.Config {}
226+ for _ , config := range configs {
227+ cfg [config .Name ] = config
228+ }
229+
105230 if len (cfg ) == 0 {
106231 log .Warn ("no adc configs provided" )
107232 return nil , nil
@@ -123,8 +248,10 @@ func (c *Client) Sync(ctx context.Context, cfg map[string]adctypes.Config) (map[
123248 }
124249
125250 if err := c .sync (ctx , Task {
126- Name : name + "-sync" ,
127- Configs : []adctypes.Config {config },
251+ Name : name + "-sync" ,
252+ Configs : map [types.NamespacedNameKind ]adctypes.Config {
253+ {}: config ,
254+ },
128255 Resources : resources ,
129256 }); err != nil {
130257 log .Errorw ("failed to sync resources" , zap .String ("name" , name ), zap .Error (err ))
@@ -157,7 +284,7 @@ func (c *Client) sync(ctx context.Context, task Task) error {
157284 // for global rules, we need to list all global rules and set it to the task resources
158285 if slices .Contains (task .ResourceTypes , "global_rule" ) {
159286 for _ , config := range task .Configs {
160- globalRules , err := c .Store . ListGlobalRules (config .Name )
287+ globalRules , err := c .ListGlobalRules (config .Name )
161288 if err != nil {
162289 return err
163290 }
@@ -171,6 +298,9 @@ func (c *Client) sync(ctx context.Context, task Task) error {
171298 }
172299 globalrule = adctypes .GlobalRule (merged )
173300 }
301+ if task .Resources == nil {
302+ task .Resources = & adctypes.Resources {}
303+ }
174304
175305 task .Resources .GlobalRules = globalrule
176306 log .Debugw ("syncing resources global rules" , zap .Any ("globalRules" , task .Resources .GlobalRules ))
0 commit comments