9
9
"sync"
10
10
"time"
11
11
12
- log "github.com/sirupsen/logrus "
12
+ "github.com/go-logr/logr "
13
13
"golang.org/x/sync/semaphore"
14
14
"k8s.io/apimachinery/pkg/api/errors"
15
15
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -23,8 +23,10 @@ import (
23
23
"k8s.io/client-go/tools/cache"
24
24
"k8s.io/client-go/tools/pager"
25
25
watchutil "k8s.io/client-go/tools/watch"
26
+ "k8s.io/klog/v2/klogr"
26
27
27
28
"github.com/argoproj/gitops-engine/pkg/utils/kube"
29
+ "github.com/argoproj/gitops-engine/pkg/utils/tracing"
28
30
)
29
31
30
32
const (
@@ -110,21 +112,25 @@ type WeightedSemaphore interface {
110
112
111
113
// NewClusterCache creates new instance of cluster cache
112
114
func NewClusterCache (config * rest.Config , opts ... UpdateSettingsFunc ) * clusterCache {
115
+ log := klogr .New ()
113
116
cache := & clusterCache {
114
- resyncTimeout : clusterResyncTimeout ,
115
- settings : Settings {ResourceHealthOverride : & noopSettings {}, ResourcesFilter : & noopSettings {}},
116
- apisMeta : make (map [schema.GroupKind ]* apiMeta ),
117
- listPageSize : defaultListPageSize ,
118
- listPageBufferSize : defaultListPageBufferSize ,
119
- listSemaphore : semaphore .NewWeighted (defaultListSemaphoreWeight ),
120
- resources : make (map [kube.ResourceKey ]* Resource ),
121
- nsIndex : make (map [string ]map [kube.ResourceKey ]* Resource ),
122
- config : config ,
123
- kubectl : & kube.KubectlCmd {},
117
+ resyncTimeout : clusterResyncTimeout ,
118
+ settings : Settings {ResourceHealthOverride : & noopSettings {}, ResourcesFilter : & noopSettings {}},
119
+ apisMeta : make (map [schema.GroupKind ]* apiMeta ),
120
+ listPageSize : defaultListPageSize ,
121
+ listPageBufferSize : defaultListPageBufferSize ,
122
+ listSemaphore : semaphore .NewWeighted (defaultListSemaphoreWeight ),
123
+ resources : make (map [kube.ResourceKey ]* Resource ),
124
+ nsIndex : make (map [string ]map [kube.ResourceKey ]* Resource ),
125
+ config : config ,
126
+ kubectl : & kube.KubectlCmd {
127
+ Log : log ,
128
+ Tracer : tracing.NopTracer {},
129
+ },
124
130
syncTime : nil ,
125
131
resourceUpdatedHandlers : map [uint64 ]OnResourceUpdatedHandler {},
126
132
eventHandlers : map [uint64 ]OnEventHandler {},
127
- log : log . WithField ( "server" , config . Host ) ,
133
+ log : log ,
128
134
}
129
135
for i := range opts {
130
136
opts [i ](cache )
@@ -154,7 +160,7 @@ type clusterCache struct {
154
160
nsIndex map [string ]map [kube.ResourceKey ]* Resource
155
161
156
162
kubectl kube.Kubectl
157
- log * log. Entry
163
+ log logr. Logger
158
164
config * rest.Config
159
165
namespaces []string
160
166
settings Settings
@@ -315,7 +321,7 @@ func (c *clusterCache) Invalidate(opts ...UpdateSettingsFunc) {
315
321
}
316
322
c .apisMeta = nil
317
323
c .namespacedResources = nil
318
- c .log .Warnf ( "invalidated cluster" )
324
+ c .log .Info ( "Invalidated cluster" )
319
325
}
320
326
321
327
func (c * clusterCache ) synced () bool {
@@ -336,7 +342,7 @@ func (c *clusterCache) stopWatching(gk schema.GroupKind, ns string) {
336
342
info .watchCancel ()
337
343
delete (c .apisMeta , gk )
338
344
c .replaceResourceCache (gk , nil , ns )
339
- c .log .Warnf ( "Stop watching: %s not found" , gk )
345
+ c .log .Info ( fmt . Sprintf ( "Stop watching: %s not found" , gk ) )
340
346
}
341
347
}
342
348
@@ -398,7 +404,7 @@ func (c *clusterCache) listResources(ctx context.Context, resClient dynamic.Reso
398
404
}
399
405
400
406
func (c * clusterCache ) watchEvents (ctx context.Context , api kube.APIResourceInfo , resClient dynamic.ResourceInterface , ns string , resourceVersion string ) {
401
- kube .RetryUntilSucceed (ctx , watchResourcesRetryTimeout , fmt .Sprintf ("watch %s on %s" , api .GroupKind , c .config .Host ), func () (err error ) {
407
+ kube .RetryUntilSucceed (ctx , watchResourcesRetryTimeout , fmt .Sprintf ("watch %s on %s" , api .GroupKind , c .config .Host ), c . log , func () (err error ) {
402
408
defer func () {
403
409
if r := recover (); r != nil {
404
410
err = fmt .Errorf ("Recovered from panic: %+v\n %s" , r , debug .Stack ())
@@ -493,7 +499,7 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo
493
499
return c .startMissingWatches ()
494
500
})
495
501
if err != nil {
496
- c .log .Warnf ( "Failed to start missing watch: %v" , err )
502
+ c .log .Error ( err , "Failed to start missing watch" )
497
503
}
498
504
}
499
505
}
@@ -586,8 +592,7 @@ func (c *clusterCache) sync() error {
586
592
})
587
593
588
594
if err != nil {
589
- log .Errorf ("Failed to sync cluster %s: %v" , c .config .Host , err )
590
- return err
595
+ return fmt .Errorf ("failed to sync cluster %s: %v" , c .config .Host , err )
591
596
}
592
597
593
598
c .log .Info ("Cluster successfully synced" )
@@ -653,7 +658,13 @@ func (c *clusterCache) IterateHierarchy(key kube.ResourceKey, action func(resour
653
658
})
654
659
child := children [0 ]
655
660
action (child , nsNodes )
656
- child .iterateChildren (nsNodes , map [kube.ResourceKey ]bool {res .ResourceKey (): true }, action )
661
+ child .iterateChildren (nsNodes , map [kube.ResourceKey ]bool {res .ResourceKey (): true }, func (err error , child * Resource , namespaceResources map [kube.ResourceKey ]* Resource ) {
662
+ if err != nil {
663
+ c .log .V (2 ).Info (err .Error ())
664
+ return
665
+ }
666
+ action (child , namespaceResources )
667
+ })
657
668
}
658
669
}
659
670
}
@@ -744,7 +755,7 @@ func (c *clusterCache) GetManagedLiveObjs(targetObjs []*unstructured.Unstructure
744
755
converted , err := c .kubectl .ConvertToVersion (managedObj , targetObj .GroupVersionKind ().Group , targetObj .GroupVersionKind ().Version )
745
756
if err != nil {
746
757
// fallback to loading resource from kubernetes if conversion fails
747
- log .Debugf ( "Failed to convert resource: %v" , err )
758
+ c . log .V ( 1 ). Info ( fmt . Sprintf ( "Failed to convert resource: %v" , err ) )
748
759
managedObj , err = c .kubectl .GetResource (context .TODO (), c .config , targetObj .GroupVersionKind (), managedObj .GetName (), managedObj .GetNamespace ())
749
760
if err != nil {
750
761
if errors .IsNotFound (err ) {
0 commit comments