Skip to content

Commit 3131194

Browse files
authored
refactor: use github.com/go-logr/logr for logging (argoproj#162)
1 parent 4eb3ca3 commit 3131194

29 files changed

+723
-554
lines changed

Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ agent-image:
2525
.PHONY: agent-manifests
2626
agent-manifests:
2727
kustomize build ./agent/manifests/cluster-install > ./agent/manifests/install.yaml
28-
kustomize build ./agent/manifests/namespace-install > ./agent/manifests/install-namespaced.yaml
28+
kustomize build ./agent/manifests/namespace-install > ./agent/manifests/install-namespaced.yaml
29+
30+
.PHONY: generate-mocks
31+
generate-mocks:
32+
go generate -x -v "github.com/argoproj/gitops-engine/pkg/utils/tracing/tracer_testing"

agent/main.go

+28-22
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ import (
1414
"text/tabwriter"
1515
"time"
1616

17-
log "github.com/sirupsen/logrus"
17+
"github.com/go-logr/logr"
1818
"github.com/spf13/cobra"
1919
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2020
"k8s.io/client-go/tools/clientcmd"
21+
"k8s.io/klog/v2/klogr"
2122

2223
"github.com/argoproj/gitops-engine/pkg/cache"
2324
"github.com/argoproj/gitops-engine/pkg/engine"
2425
"github.com/argoproj/gitops-engine/pkg/sync"
25-
"github.com/argoproj/gitops-engine/pkg/utils/errors"
26-
"github.com/argoproj/gitops-engine/pkg/utils/io"
2726
"github.com/argoproj/gitops-engine/pkg/utils/kube"
2827
)
2928

@@ -32,10 +31,9 @@ const (
3231
)
3332

3433
func main() {
35-
if err := newCmd().Execute(); err != nil {
36-
fmt.Fprintln(os.Stderr, err)
37-
os.Exit(1)
38-
}
34+
log := klogr.New() // Delegates to klog
35+
err := newCmd(log).Execute()
36+
checkError(err, log)
3937
}
4038

4139
type resourceInfo struct {
@@ -98,7 +96,7 @@ func (s *settings) parseManifests() ([]*unstructured.Unstructured, string, error
9896
return res, string(revision), nil
9997
}
10098

101-
func newCmd() *cobra.Command {
99+
func newCmd(log logr.Logger) *cobra.Command {
102100
var (
103101
clientConfig clientcmd.ClientConfig
104102
paths []string
@@ -117,10 +115,10 @@ func newCmd() *cobra.Command {
117115
}
118116
s := settings{args[0], paths}
119117
config, err := clientConfig.ClientConfig()
120-
errors.CheckErrorWithCode(err, errors.ErrorCommandSpecific)
118+
checkError(err, log)
121119
if namespace == "" {
122120
namespace, _, err = clientConfig.Namespace()
123-
errors.CheckErrorWithCode(err, errors.ErrorCommandSpecific)
121+
checkError(err, log)
124122
}
125123

126124
var namespaces []string
@@ -129,6 +127,7 @@ func newCmd() *cobra.Command {
129127
}
130128
clusterCache := cache.NewClusterCache(config,
131129
cache.SetNamespaces(namespaces),
130+
cache.SetLogr(log),
132131
cache.SetPopulateResourceInfoHandler(func(un *unstructured.Unstructured, isRoot bool) (info interface{}, cacheManifest bool) {
133132
// store gc mark of every resource
134133
gcMark := un.GetAnnotations()[annotationGCMark]
@@ -138,43 +137,42 @@ func newCmd() *cobra.Command {
138137
return
139138
}),
140139
)
141-
gitOpsEngine := engine.NewEngine(config, clusterCache)
142-
errors.CheckErrorWithCode(err, errors.ErrorCommandSpecific)
140+
gitOpsEngine := engine.NewEngine(config, clusterCache, engine.WithLogr(log))
141+
checkError(err, log)
143142

144-
closer, err := gitOpsEngine.Run()
145-
errors.CheckErrorWithCode(err, errors.ErrorCommandSpecific)
146-
147-
defer io.Close(closer)
143+
cleanup, err := gitOpsEngine.Run()
144+
checkError(err, log)
145+
defer cleanup()
148146

149147
resync := make(chan bool)
150148
go func() {
151149
ticker := time.NewTicker(time.Second * time.Duration(resyncSeconds))
152150
for {
153151
<-ticker.C
154-
log.Infof("Synchronization triggered by timer")
152+
log.Info("Synchronization triggered by timer")
155153
resync <- true
156154
}
157155
}()
158156
http.HandleFunc("/api/v1/sync", func(writer http.ResponseWriter, request *http.Request) {
159-
log.Infof("Synchronization triggered by API call")
157+
log.Info("Synchronization triggered by API call")
160158
resync <- true
161159
})
162160
go func() {
163-
errors.CheckErrorWithCode(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil), errors.ErrorCommandSpecific)
161+
checkError(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil), log)
164162
}()
165163

166164
for ; true; <-resync {
167165
target, revision, err := s.parseManifests()
168166
if err != nil {
169-
log.Warnf("failed to parse target state: %v", err)
167+
log.Error(err, "Failed to parse target state")
170168
continue
171169
}
172170

173171
result, err := gitOpsEngine.Sync(context.Background(), target, func(r *cache.Resource) bool {
174172
return r.Info.(*resourceInfo).gcMark == s.getGCMark(r.ResourceKey())
175-
}, revision, namespace, sync.WithPrune(prune))
173+
}, revision, namespace, sync.WithPrune(prune), sync.WithLogr(log))
176174
if err != nil {
177-
log.Warnf("failed to synchronize cluster state: %v", err)
175+
log.Error(err, "Failed to synchronize cluster state")
178176
continue
179177
}
180178
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
@@ -209,3 +207,11 @@ func addKubectlFlagsToCmd(cmd *cobra.Command) clientcmd.ClientConfig {
209207
clientcmd.BindOverrideFlags(&overrides, cmd.PersistentFlags(), kflags)
210208
return clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, &overrides, os.Stdin)
211209
}
210+
211+
// checkError is a convenience function to check if an error is non-nil and exit if it was
212+
func checkError(err error, log logr.Logger) {
213+
if err != nil {
214+
log.Error(err, "Fatal error")
215+
os.Exit(1)
216+
}
217+
}

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ go 1.14
44

55
require (
66
github.com/evanphx/json-patch v4.9.0+incompatible
7-
github.com/sirupsen/logrus v1.6.0
7+
github.com/go-logr/logr v0.2.0
8+
github.com/golang/mock v1.4.4
89
github.com/spf13/cobra v1.0.0
910
github.com/stretchr/testify v1.6.1
1011
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
@@ -13,6 +14,7 @@ require (
1314
k8s.io/apimachinery v0.19.2
1415
k8s.io/cli-runtime v0.19.2
1516
k8s.io/client-go v0.19.2
17+
k8s.io/klog/v2 v2.2.0
1618
k8s.io/kube-aggregator v0.19.2
1719
k8s.io/kubectl v0.19.2
1820
k8s.io/kubernetes v1.19.2

go.sum

+5
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,10 @@ github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 h1:5ZkaAPbicIKTF
236236
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
237237
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
238238
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
239+
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
239240
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
241+
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
242+
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
240243
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
241244
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
242245
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -562,6 +565,7 @@ golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKG
562565
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
563566
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
564567
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
568+
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
565569
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
566570
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
567571
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -681,6 +685,7 @@ golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtn
681685
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
682686
golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
683687
golang.org/x/tools v0.0.0-20191227053925-7b8e75db28f4/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
688+
golang.org/x/tools v0.0.0-20200616133436-c1934b75d054 h1:HHeAlu5H9b71C+Fx0K+1dGgVFN1DM1/wz4aoGOA5qS8=
684689
golang.org/x/tools v0.0.0-20200616133436-c1934b75d054/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
685690
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
686691
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

pkg/cache/cluster.go

+32-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"sync"
1010
"time"
1111

12-
log "github.com/sirupsen/logrus"
12+
"github.com/go-logr/logr"
1313
"golang.org/x/sync/semaphore"
1414
"k8s.io/apimachinery/pkg/api/errors"
1515
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -23,8 +23,10 @@ import (
2323
"k8s.io/client-go/tools/cache"
2424
"k8s.io/client-go/tools/pager"
2525
watchutil "k8s.io/client-go/tools/watch"
26+
"k8s.io/klog/v2/klogr"
2627

2728
"github.com/argoproj/gitops-engine/pkg/utils/kube"
29+
"github.com/argoproj/gitops-engine/pkg/utils/tracing"
2830
)
2931

3032
const (
@@ -110,21 +112,25 @@ type WeightedSemaphore interface {
110112

111113
// NewClusterCache creates new instance of cluster cache
112114
func NewClusterCache(config *rest.Config, opts ...UpdateSettingsFunc) *clusterCache {
115+
log := klogr.New()
113116
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+
},
124130
syncTime: nil,
125131
resourceUpdatedHandlers: map[uint64]OnResourceUpdatedHandler{},
126132
eventHandlers: map[uint64]OnEventHandler{},
127-
log: log.WithField("server", config.Host),
133+
log: log,
128134
}
129135
for i := range opts {
130136
opts[i](cache)
@@ -154,7 +160,7 @@ type clusterCache struct {
154160
nsIndex map[string]map[kube.ResourceKey]*Resource
155161

156162
kubectl kube.Kubectl
157-
log *log.Entry
163+
log logr.Logger
158164
config *rest.Config
159165
namespaces []string
160166
settings Settings
@@ -315,7 +321,7 @@ func (c *clusterCache) Invalidate(opts ...UpdateSettingsFunc) {
315321
}
316322
c.apisMeta = nil
317323
c.namespacedResources = nil
318-
c.log.Warnf("invalidated cluster")
324+
c.log.Info("Invalidated cluster")
319325
}
320326

321327
func (c *clusterCache) synced() bool {
@@ -336,7 +342,7 @@ func (c *clusterCache) stopWatching(gk schema.GroupKind, ns string) {
336342
info.watchCancel()
337343
delete(c.apisMeta, gk)
338344
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))
340346
}
341347
}
342348

@@ -398,7 +404,7 @@ func (c *clusterCache) listResources(ctx context.Context, resClient dynamic.Reso
398404
}
399405

400406
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) {
402408
defer func() {
403409
if r := recover(); r != nil {
404410
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
493499
return c.startMissingWatches()
494500
})
495501
if err != nil {
496-
c.log.Warnf("Failed to start missing watch: %v", err)
502+
c.log.Error(err, "Failed to start missing watch")
497503
}
498504
}
499505
}
@@ -586,8 +592,7 @@ func (c *clusterCache) sync() error {
586592
})
587593

588594
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)
591596
}
592597

593598
c.log.Info("Cluster successfully synced")
@@ -653,7 +658,13 @@ func (c *clusterCache) IterateHierarchy(key kube.ResourceKey, action func(resour
653658
})
654659
child := children[0]
655660
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+
})
657668
}
658669
}
659670
}
@@ -744,7 +755,7 @@ func (c *clusterCache) GetManagedLiveObjs(targetObjs []*unstructured.Unstructure
744755
converted, err := c.kubectl.ConvertToVersion(managedObj, targetObj.GroupVersionKind().Group, targetObj.GroupVersionKind().Version)
745756
if err != nil {
746757
// 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))
748759
managedObj, err = c.kubectl.GetResource(context.TODO(), c.config, targetObj.GroupVersionKind(), managedObj.GetName(), managedObj.GetNamespace())
749760
if err != nil {
750761
if errors.IsNotFound(err) {

pkg/cache/references.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"strings"
77

8-
log "github.com/sirupsen/logrus"
98
v1 "k8s.io/api/apps/v1"
109
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1110
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -52,7 +51,7 @@ func (c *clusterCache) resolveResourceReferences(un *unstructured.Unstructured)
5251

5352
case (un.GroupVersionKind().Group == "apps" || un.GroupVersionKind().Group == "extensions") && un.GetKind() == kube.StatefulSetKind:
5453
if refs, err := isStatefulSetChild(un); err != nil {
55-
log.Errorf("Failed to extract StatefulSet %s/%s PVC references: %v", un.GetNamespace(), un.GetName(), err)
54+
c.log.Error(err, fmt.Sprintf("Failed to extract StatefulSet %s/%s PVC references", un.GetNamespace(), un.GetName()))
5655
} else {
5756
isInferredParentOf = refs
5857
}

pkg/cache/resource.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package cache
22

33
import (
4-
log "github.com/sirupsen/logrus"
4+
"fmt"
5+
56
v1 "k8s.io/api/core/v1"
67
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
78
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -84,14 +85,14 @@ func newResourceKeySet(set map[kube.ResourceKey]bool, keys ...kube.ResourceKey)
8485
return newSet
8586
}
8687

87-
func (r *Resource) iterateChildren(ns map[kube.ResourceKey]*Resource, parents map[kube.ResourceKey]bool, action func(child *Resource, namespaceResources map[kube.ResourceKey]*Resource)) {
88+
func (r *Resource) iterateChildren(ns map[kube.ResourceKey]*Resource, parents map[kube.ResourceKey]bool, action func(err error, child *Resource, namespaceResources map[kube.ResourceKey]*Resource)) {
8889
for childKey, child := range ns {
8990
if r.isParentOf(ns[childKey]) {
9091
if parents[childKey] {
9192
key := r.ResourceKey()
92-
log.Warnf("Circular dependency detected. %s is child and parent of %s", childKey.String(), key.String())
93+
action(fmt.Errorf("circular dependency detected. %s is child and parent of %s", childKey.String(), key.String()), child, ns)
9394
} else {
94-
action(child, ns)
95+
action(nil, child, ns)
9596
child.iterateChildren(ns, newResourceKeySet(parents, r.ResourceKey()), action)
9697
}
9798
}

0 commit comments

Comments
 (0)