@@ -27,9 +27,8 @@ import (
2727 "sync"
2828 "time"
2929
30- "github.com/api7/gopkg/pkg/log "
30+ "github.com/go-logr/logr "
3131 "github.com/pkg/errors"
32- "go.uber.org/zap"
3332
3433 adctypes "github.com/apache/apisix-ingress-controller/api/adc"
3534 "github.com/apache/apisix-ingress-controller/internal/adc/cache"
@@ -49,23 +48,29 @@ type Client struct {
4948
5049 ConfigManager * common.ConfigManager [types.NamespacedNameKind , adctypes.Config ]
5150 ADCDebugProvider * common.ADCDebugProvider
51+
52+ log logr.Logger
5253}
5354
54- func New (mode string , timeout time.Duration ) (* Client , error ) {
55+ func New (log logr. Logger , mode string , timeout time.Duration ) (* Client , error ) {
5556 serverURL := os .Getenv ("ADC_SERVER_URL" )
5657 if serverURL == "" {
5758 serverURL = defaultHTTPADCExecutorAddr
5859 }
5960
60- store := cache .NewStore ()
61+ store := cache .NewStore (log )
6162 configManager := common .NewConfigManager [types.NamespacedNameKind , adctypes.Config ]()
62- log .Infow ("using HTTP ADC Executor" , zap .String ("server_url" , serverURL ))
63+
64+ logger := log .WithName ("client" )
65+ logger .Info ("ADC client initialized" , "mode" , mode )
66+
6367 return & Client {
6468 Store : store ,
65- executor : NewHTTPADCExecutor (serverURL , timeout ),
69+ executor : NewHTTPADCExecutor (log , serverURL , timeout ),
6670 BackendMode : mode ,
6771 ConfigManager : configManager ,
6872 ADCDebugProvider : common .NewADCDebugProvider (store , configManager ),
73+ log : logger ,
6974 }, nil
7075}
7176
@@ -83,55 +88,55 @@ type StoreDelta struct {
8388 Applied map [types.NamespacedNameKind ]adctypes.Config
8489}
8590
86- func (d * Client ) applyStoreChanges (args Task , isDelete bool ) (StoreDelta , error ) {
87- d .mu .Lock ()
88- defer d .mu .Unlock ()
91+ func (c * Client ) applyStoreChanges (args Task , isDelete bool ) (StoreDelta , error ) {
92+ c .mu .Lock ()
93+ defer c .mu .Unlock ()
8994
9095 var delta StoreDelta
9196
9297 if isDelete {
93- delta .Deleted = d .ConfigManager .Get (args .Key )
94- d .ConfigManager .Delete (args .Key )
98+ delta .Deleted = c .ConfigManager .Get (args .Key )
99+ c .ConfigManager .Delete (args .Key )
95100 } else {
96- deleted := d .ConfigManager .Update (args .Key , args .Configs )
101+ deleted := c .ConfigManager .Update (args .Key , args .Configs )
97102 delta .Deleted = deleted
98103 delta .Applied = args .Configs
99104 }
100105
101106 for _ , cfg := range delta .Deleted {
102- if err := d .Store .Delete (cfg .Name , args .ResourceTypes , args .Labels ); err != nil {
103- log .Errorw ( "store delete failed" , zap . Error ( err ), zap . Any ( "cfg" , cfg ), zap . Any ( "args" , args ) )
107+ if err := c .Store .Delete (cfg .Name , args .ResourceTypes , args .Labels ); err != nil {
108+ c . log .Error ( err , "store delete failed" , "cfg" , cfg , "args" , args )
104109 return StoreDelta {}, errors .Wrap (err , fmt .Sprintf ("store delete failed for config %s" , cfg .Name ))
105110 }
106111 }
107112
108113 for _ , cfg := range delta .Applied {
109- if err := d .Insert (cfg .Name , args .ResourceTypes , args .Resources , args .Labels ); err != nil {
110- log .Errorw ( "store insert failed" , zap . Error ( err ), zap . Any ( "cfg" , cfg ), zap . Any ( "args" , args ) )
114+ if err := c .Insert (cfg .Name , args .ResourceTypes , args .Resources , args .Labels ); err != nil {
115+ c . log .Error ( err , "store insert failed" , "cfg" , cfg , "args" , args )
111116 return StoreDelta {}, errors .Wrap (err , fmt .Sprintf ("store insert failed for config %s" , cfg .Name ))
112117 }
113118 }
114119
115120 return delta , nil
116121}
117122
118- func (d * Client ) applySync (ctx context.Context , args Task , delta StoreDelta ) error {
119- d .syncMu .RLock ()
120- defer d .syncMu .RUnlock ()
123+ func (c * Client ) applySync (ctx context.Context , args Task , delta StoreDelta ) error {
124+ c .syncMu .RLock ()
125+ defer c .syncMu .RUnlock ()
121126
122127 if len (delta .Deleted ) > 0 {
123- if err := d .sync (ctx , Task {
128+ if err := c .sync (ctx , Task {
124129 Name : args .Name ,
125130 Labels : args .Labels ,
126131 ResourceTypes : args .ResourceTypes ,
127132 Configs : delta .Deleted ,
128133 }); err != nil {
129- log .Warnw ( "failed to sync deleted configs" , zap . Error ( err ) )
134+ c . log .Error ( err , "failed to sync deleted configs" , "args" , args , "delta" , delta )
130135 }
131136 }
132137
133138 if len (delta .Applied ) > 0 {
134- return d .sync (ctx , Task {
139+ return c .sync (ctx , Task {
135140 Name : args .Name ,
136141 Labels : args .Labels ,
137142 ResourceTypes : args .ResourceTypes ,
@@ -142,53 +147,53 @@ func (d *Client) applySync(ctx context.Context, args Task, delta StoreDelta) err
142147 return nil
143148}
144149
145- func (d * Client ) Update (ctx context.Context , args Task ) error {
146- delta , err := d .applyStoreChanges (args , false )
150+ func (c * Client ) Update (ctx context.Context , args Task ) error {
151+ delta , err := c .applyStoreChanges (args , false )
147152 if err != nil {
148153 return err
149154 }
150- return d .applySync (ctx , args , delta )
155+ return c .applySync (ctx , args , delta )
151156}
152157
153- func (d * Client ) UpdateConfig (ctx context.Context , args Task ) error {
154- _ , err := d .applyStoreChanges (args , false )
158+ func (c * Client ) UpdateConfig (ctx context.Context , args Task ) error {
159+ _ , err := c .applyStoreChanges (args , false )
155160 return err
156161}
157162
158- func (d * Client ) Delete (ctx context.Context , args Task ) error {
159- delta , err := d .applyStoreChanges (args , true )
163+ func (c * Client ) Delete (ctx context.Context , args Task ) error {
164+ delta , err := c .applyStoreChanges (args , true )
160165 if err != nil {
161166 return err
162167 }
163- return d .applySync (ctx , args , delta )
168+ return c .applySync (ctx , args , delta )
164169}
165170
166- func (d * Client ) DeleteConfig (ctx context.Context , args Task ) error {
167- _ , err := d .applyStoreChanges (args , true )
171+ func (c * Client ) DeleteConfig (ctx context.Context , args Task ) error {
172+ _ , err := c .applyStoreChanges (args , true )
168173 return err
169174}
170175
171176func (c * Client ) Sync (ctx context.Context ) (map [string ]types.ADCExecutionErrors , error ) {
172177 c .syncMu .Lock ()
173178 defer c .syncMu .Unlock ()
174- log .Debug ("syncing all resources" )
179+ c . log .Info ("syncing all resources" )
175180
176181 configs := c .ConfigManager .List ()
177182
178183 if len (configs ) == 0 {
179- log .Warn ("no GatewayProxy configs provided" )
184+ c . log .Info ("no GatewayProxy configs provided" )
180185 return nil , nil
181186 }
182187
183- log .Debugw ( "syncing resources with multiple configs" , zap . Any ( "configs" , configs ) )
188+ c . log .V ( 1 ). Info ( "syncing resources with multiple configs" , "configs" , configs )
184189
185190 failedMap := map [string ]types.ADCExecutionErrors {}
186191 var failedConfigs []string
187192 for _ , config := range configs {
188193 name := config .Name
189194 resources , err := c .GetResources (name )
190195 if err != nil {
191- log .Errorw ( "failed to get resources from store" , zap . String ( "name" , name ), zap . Error ( err ) )
196+ c . log .Error ( err , "failed to get resources from store" , "name" , name )
192197 failedConfigs = append (failedConfigs , name )
193198 continue
194199 }
@@ -203,7 +208,7 @@ func (c *Client) Sync(ctx context.Context) (map[string]types.ADCExecutionErrors,
203208 },
204209 Resources : resources ,
205210 }); err != nil {
206- log .Errorw ( "failed to sync resources" , zap . String ( "name" , name ), zap . Error ( err ) )
211+ c . log .Error ( err , "failed to sync resources" , "name" , name )
207212 failedConfigs = append (failedConfigs , name )
208213 var execErrs types.ADCExecutionErrors
209214 if errors .As (err , & execErrs ) {
@@ -222,15 +227,15 @@ func (c *Client) Sync(ctx context.Context) (map[string]types.ADCExecutionErrors,
222227}
223228
224229func (c * Client ) sync (ctx context.Context , task Task ) error {
225- log .Debugw ( "syncing resources" , zap . Any ( "task" , task ) )
230+ c . log .V ( 1 ). Info ( "syncing resources" , "task" , task )
226231
227232 if len (task .Labels ) > 0 {
228233 // only keep the resource key label for filtering resources
229234 task .Labels = map [string ]string {label .LabelResourceKey : task .Labels [label .LabelResourceKey ]}
230235 }
231236
232237 if len (task .Configs ) == 0 {
233- log .Warnw ("no adc configs provided" , zap . Any ( "task" , task ) )
238+ c . log .Info ("no adc configs provided" )
234239 return nil
235240 }
236241
@@ -258,7 +263,7 @@ func (c *Client) sync(ctx context.Context, task Task) error {
258263 }
259264
260265 task .Resources .GlobalRules = globalrule
261- log .Debugw ( "syncing resources global rules" , zap . Any ( "globalRules" , task .Resources .GlobalRules ) )
266+ c . log .V ( 1 ). Info ( "syncing resources global rules" , "globalRules" , task .Resources .GlobalRules )
262267
263268 fileIOStart := time .Now ()
264269 syncFilePath , cleanup , err := prepareSyncFile (task .Resources )
@@ -284,7 +289,7 @@ func (c *Client) sync(ctx context.Context, task Task) error {
284289 status := "success"
285290 if err != nil {
286291 status = "failure"
287- log .Errorw ( "failed to execute adc command" , zap . Error ( err ), zap . Any ( "config" , config ) )
292+ c . log .Error ( err , "failed to execute adc command" , "config" , config )
288293
289294 var execErr types.ADCExecutionError
290295 if errors .As (err , & execErr ) {
@@ -315,6 +320,7 @@ func (c *Client) sync(ctx context.Context, task Task) error {
315320 }
316321 pkgmetrics .RecordFileIODuration ("prepare_sync_file" , adctypes .StatusSuccess , time .Since (fileIOStart ).Seconds ())
317322 defer cleanup ()
323+ c .log .V (1 ).Info ("prepared sync file" , "path" , syncFilePath )
318324
319325 args := BuildADCExecuteArgs (syncFilePath , task .Labels , task .ResourceTypes )
320326
@@ -332,7 +338,7 @@ func (c *Client) sync(ctx context.Context, task Task) error {
332338 status := adctypes .StatusSuccess
333339 if err != nil {
334340 status = "failure"
335- log .Errorw ( "failed to execute adc command" , zap . Error ( err ), zap . Any ( "config" , config ) )
341+ c . log .Error ( err , "failed to execute adc command" , "config" , config )
336342
337343 var execErr types.ADCExecutionError
338344 if errors .As (err , & execErr ) {
@@ -372,7 +378,5 @@ func prepareSyncFile(resources any) (string, func(), error) {
372378 return "" , nil , err
373379 }
374380
375- log .Debugw ("generated adc file" , zap .String ("filename" , tmpFile .Name ()), zap .String ("json" , string (data )))
376-
377381 return tmpFile .Name (), cleanup , nil
378382}
0 commit comments