Skip to content

Commit b40efb3

Browse files
committed
additional logging in core reconciler
Our debug-level logging wasn't particularly verbose or informative. This patch adds a number of debug-level log messages into the core reconciler along with a new `ResourceLogger` struct in `pkg/runtime/log` that can be used to simplify resource-specific log messages. This new ResourceLogger implements a new AWSLogger interface, which itself implements a new Tracer interface, which means the ResourceLogger may be used to provide function or code block tracing capabilities, like so: ```go func (r *resourceReconciler) someMethod( ctx context.Context, ... ) error { var err error rlog := ackrtlog.FromContext(ctx) exit := rlog.Trace("someMethod") defer exit(err) // do some action... } ``` This allows for a clean mechanism for "wrapping" the tracing of a function or a code block. Example output of these new log messages along with similar debug-level messages added to the s3-controller's bucket resource manager. These resource manager-level log messages will need to be added in a separate patch to the code-generator once this patch merges. ``` [jaypipes@thelio community]$ k logs -n ack-system ack-s3-controller-55f4487599-f6x6w --follow 2021-06-18T01:30:35.970Z INFO controller-runtime.metrics metrics server is starting to listen {"addr": "0.0.0.0:8080"} 2021-06-18T01:30:35.971Z INFO setup initializing service controller {"aws.service": "s3"} 2021-06-18T01:30:35.973Z INFO setup starting manager {"aws.service": "s3"} 2021-06-18T01:30:35.973Z INFO controller-runtime.manager starting metrics server {"path": "/metrics"} 2021-06-18T01:30:35.973Z INFO controller-runtime.controller Starting EventSource {"controller": "adoptedresource", "source": "kind source: /, Kind="} 2021-06-18T01:30:35.973Z INFO controller-runtime.controller Starting EventSource {"controller": "bucket", "source": "kind source: /, Kind="} 2021-06-18T01:30:36.067Z DEBUG ackrt.cache.namespace created namespace {"name": "kube-node-lease"} 2021-06-18T01:30:36.067Z DEBUG ackrt.cache.namespace created namespace {"name": "default"} 2021-06-18T01:30:36.067Z DEBUG ackrt.cache.namespace created namespace {"name": "local-path-storage"} 2021-06-18T01:30:36.067Z DEBUG ackrt.cache.namespace created namespace {"name": "ack-system"} 2021-06-18T01:30:36.167Z DEBUG adopted-reconciler.cache.namespace created namespace {"name": "kube-node-lease"} 2021-06-18T01:30:36.167Z DEBUG adopted-reconciler.cache.namespace created namespace {"name": "default"} 2021-06-18T01:30:36.167Z DEBUG adopted-reconciler.cache.namespace created namespace {"name": "local-path-storage"} 2021-06-18T01:30:36.167Z DEBUG adopted-reconciler.cache.namespace created namespace {"name": "ack-system"} 2021-06-18T01:30:36.273Z INFO controller-runtime.controller Starting Controller {"controller": "adoptedresource"} 2021-06-18T01:30:36.273Z INFO controller-runtime.controller Starting workers {"controller": "adoptedresource", "worker count": 1} 2021-06-18T01:30:36.274Z INFO controller-runtime.controller Starting Controller {"controller": "bucket"} 2021-06-18T01:30:36.274Z INFO controller-runtime.controller Starting workers {"controller": "bucket", "worker count": 1} 2021-06-18T01:30:53.524Z INFO ackrt > r.Sync {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:53.524Z INFO ackrt >> rm.ReadOne {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:53.524Z DEBUG ackrt >>> rm.sdkFind {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "spec": {"name":"s3-bucket-cpstsqpcszsgd7"}, "status": {"ackResourceMetadata":null,"conditions":null}} 2021-06-18T01:30:54.431Z DEBUG ackrt <<< rm.sdkFind {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "error": "resource not found"} 2021-06-18T01:30:54.431Z INFO ackrt << rm.ReadOne {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "error": "resource not found"} 2021-06-18T01:30:54.431Z INFO ackrt >> r.setResourceManaged {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:54.431Z INFO ackrt >>> kc.Patch (all) {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:54.435Z INFO ackrt <<< kc.Patch (all) {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:54.435Z DEBUG ackrt marked resource as managed {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:54.435Z INFO ackrt << r.setResourceManaged {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:54.435Z INFO ackrt >> rm.Create {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:54.435Z DEBUG ackrt >>> rm.sdkCreate {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "spec": {"name":"s3-bucket-cpstsqpcszsgd7"}, "status": {"ackResourceMetadata":null,"conditions":null}} 2021-06-18T01:30:55.593Z DEBUG ackrt <<< rm.sdkCreate {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "spec": {"name":"s3-bucket-cpstsqpcszsgd7"}, "status": {"ackResourceMetadata":{"ownerAccountID":"750630568209"},"conditions":[],"location":"http://s3-bucket-cpstsqpcszsgd7.s3.amazonaws.com/"}} 2021-06-18T01:30:55.593Z INFO ackrt << rm.Create {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.593Z INFO ackrt created new resource {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.593Z INFO ackrt >> r.patchResource {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.593Z INFO ackrt >>> kc.Patch (status) {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.596Z INFO ackrt <<< kc.Patch (status) {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.596Z DEBUG ackrt patched resource status {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "latest": {}} 2021-06-18T01:30:55.596Z INFO ackrt << r.patchResource {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.596Z INFO ackrt < r.Sync {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.596Z DEBUG controller-runtime.controller Successfully Reconciled {"controller": "bucket", "request": "default/s3-bucket-cpstsqpcszsgd7"} 2021-06-18T01:30:55.596Z INFO ackrt > r.Sync {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.596Z INFO ackrt >> rm.ReadOne {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.596Z DEBUG ackrt >>> rm.sdkFind {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "spec": {"name":"s3-bucket-cpstsqpcszsgd7"}, "status": {"ackResourceMetadata":{"ownerAccountID":"750630568209"},"conditions":[],"location":"http://s3-bucket-cpstsqpcszsgd7.s3.amazonaws.com/"}} 2021-06-18T01:30:55.762Z DEBUG ackrt <<< rm.sdkFind {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "spec": {"name":"s3-bucket-cpstsqpcszsgd7"}, "status": {"ackResourceMetadata":{"ownerAccountID":"750630568209"},"conditions":[],"location":"http://s3-bucket-cpstsqpcszsgd7.s3.amazonaws.com/"}} 2021-06-18T01:30:55.762Z INFO ackrt << rm.ReadOne {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.762Z INFO ackrt >> r.setResourceManaged {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.762Z INFO ackrt << r.setResourceManaged {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.762Z INFO ackrt >> r.patchResource {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.762Z INFO ackrt >>> kc.Patch (status) {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.765Z INFO ackrt <<< kc.Patch (status) {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.765Z DEBUG ackrt patched resource status {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "latest": {}} 2021-06-18T01:30:55.765Z INFO ackrt << r.patchResource {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.765Z INFO ackrt < r.Sync {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1, "account": "750630568209", "role": "", "region": "us-west-2", "is_adopted": false, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 1} 2021-06-18T01:30:55.765Z DEBUG controller-runtime.controller Successfully Reconciled {"controller": "bucket", "request": "default/s3-bucket-cpstsqpcszsgd7"} 2021-06-18T01:31:14.200Z INFO ackrt > r.cleanup {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "account": "750630568209", "role": "", "region": "us-west-2", "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2} 2021-06-18T01:31:14.200Z INFO ackrt >> rm.ReadOne {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "account": "750630568209", "role": "", "region": "us-west-2", "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2} 2021-06-18T01:31:14.200Z DEBUG ackrt >>> rm.sdkFind {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "spec": {"name":"s3-bucket-cpstsqpcszsgd7"}, "status": {"ackResourceMetadata":{"ownerAccountID":"750630568209"},"conditions":[],"location":"http://s3-bucket-cpstsqpcszsgd7.s3.amazonaws.com/"}} 2021-06-18T01:31:14.911Z DEBUG ackrt <<< rm.sdkFind {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "spec": {"name":"s3-bucket-cpstsqpcszsgd7"}, "status": {"ackResourceMetadata":{"ownerAccountID":"750630568209"},"conditions":[],"location":"http://s3-bucket-cpstsqpcszsgd7.s3.amazonaws.com/"}} 2021-06-18T01:31:14.911Z INFO ackrt << rm.ReadOne {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "account": "750630568209", "role": "", "region": "us-west-2", "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2} 2021-06-18T01:31:14.911Z INFO ackrt >> rm.Delete {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "account": "750630568209", "role": "", "region": "us-west-2", "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2} 2021-06-18T01:31:14.911Z DEBUG ackrt >>> rm.sdkDelete {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "spec": {"name":"s3-bucket-cpstsqpcszsgd7"}, "status": {"ackResourceMetadata":{"ownerAccountID":"750630568209"},"conditions":[],"location":"http://s3-bucket-cpstsqpcszsgd7.s3.amazonaws.com/"}} 2021-06-18T01:31:15.629Z DEBUG ackrt <<< rm.sdkDelete {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2} 2021-06-18T01:31:15.629Z INFO ackrt << rm.Delete {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "account": "750630568209", "role": "", "region": "us-west-2", "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2} 2021-06-18T01:31:15.629Z INFO ackrt >> r.setResourceUnmanaged {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "account": "750630568209", "role": "", "region": "us-west-2", "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2} 2021-06-18T01:31:15.629Z INFO ackrt >>> kc.Patch (all) {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "account": "750630568209", "role": "", "region": "us-west-2", "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2} 2021-06-18T01:31:15.634Z INFO ackrt <<< kc.Patch (all) {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "account": "750630568209", "role": "", "region": "us-west-2", "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2} 2021-06-18T01:31:15.634Z DEBUG ackrt removed resource from management {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "account": "750630568209", "role": "", "region": "us-west-2", "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2} 2021-06-18T01:31:15.634Z INFO ackrt << r.setResourceUnmanaged {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "account": "750630568209", "role": "", "region": "us-west-2", "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2} 2021-06-18T01:31:15.634Z INFO ackrt deleted resource {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "account": "750630568209", "role": "", "region": "us-west-2", "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2} 2021-06-18T01:31:15.634Z INFO ackrt < r.cleanup {"kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2, "account": "750630568209", "role": "", "region": "us-west-2", "kind": "Bucket", "namespace": "default", "name": "s3-bucket-cpstsqpcszsgd7", "generation": 2} 2021-06-18T01:31:15.634Z DEBUG controller-runtime.controller Successfully Reconciled {"controller": "bucket", "request": "default/s3-bucket-cpstsqpcszsgd7"} 2021-06-18T01:31:15.634Z DEBUG controller-runtime.controller Successfully Reconciled {"controller": "bucket", "request": "default/s3-bucket-cpstsqpcszsgd7"} ```
1 parent 0ff9ffa commit b40efb3

File tree

9 files changed

+431
-36
lines changed

9 files changed

+431
-36
lines changed

mocks/pkg/types/logger.go

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/pkg/types/trace_exiter.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/pkg/types/tracer.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/runtime/log/context.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package log
15+
16+
import (
17+
"context"
18+
19+
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
20+
)
21+
22+
const (
23+
// ContextKey is the string key used to store a logger in a Context
24+
ContextKey = "ack.logger"
25+
)
26+
27+
// FromContext returns a `pkg/types.Logger` from a saved key in the request
28+
// context
29+
func FromContext(ctx context.Context) acktypes.Logger {
30+
if v := ctx.Value(ContextKey); v != nil {
31+
return v.(*ResourceLogger)
32+
}
33+
return NoopLogger
34+
}

pkg/runtime/log/noop.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package log
15+
16+
import (
17+
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
18+
)
19+
20+
var (
21+
// NoopLogger is useful for testing/mocking
22+
NoopLogger acktypes.Logger = &voidLogger{}
23+
)
24+
25+
// voidLogger implements Logger but does nothing. Useful for
26+
// testing and mocking...
27+
type voidLogger struct{}
28+
29+
func (l *voidLogger) WithValues(...interface{}) {}
30+
func (l *voidLogger) Info(string, ...interface{}) {}
31+
func (l *voidLogger) Debug(string, ...interface{}) {}
32+
func (l *voidLogger) Trace(name string, additionalValues ...interface{}) acktypes.TraceExiter {
33+
f := func(err error, args ...interface{}) {
34+
l.Exit(name, err, args...)
35+
}
36+
return f
37+
}
38+
func (l *voidLogger) Enter(string, ...interface{}) {}
39+
func (l *voidLogger) Exit(string, error, ...interface{}) {}

pkg/runtime/log/resource.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,106 @@
1414
package log
1515

1616
import (
17+
"strings"
18+
1719
"github.com/go-logr/logr"
1820

1921
"github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
2022
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
2123
)
2224

25+
// ResourceLogger is a wrapper around a logr.Logger that writes log messages
26+
// about resources involved in a controller loop. It implements
27+
// `pkg/types.Logger`
28+
type ResourceLogger struct {
29+
log logr.Logger
30+
res acktypes.AWSResource
31+
blockDepth int
32+
}
33+
34+
// WithValues adapts the internal logger with a set of additional values
35+
func (rl *ResourceLogger) WithValues(
36+
values ...interface{},
37+
) {
38+
rl.log = rl.log.WithValues(values...)
39+
}
40+
41+
// Debug writes a supplied log message about a resource that includes a set of
42+
// standard log values for the resource's kind, namespace, name, etc
43+
func (rl *ResourceLogger) Debug(
44+
msg string,
45+
additionalValues ...interface{},
46+
) {
47+
AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg)
48+
}
49+
50+
// Info writes a supplied log message about a resource that includes a
51+
// set of standard log values for the resource's kind, namespace, name, etc
52+
func (rl *ResourceLogger) Info(
53+
msg string,
54+
additionalValues ...interface{},
55+
) {
56+
AdaptResource(rl.log, rl.res, additionalValues...).V(0).Info(msg)
57+
}
58+
59+
// Enter logs an entry to a function or code block
60+
func (rl *ResourceLogger) Enter(
61+
name string, // name of the function or code block we're entering
62+
additionalValues ...interface{},
63+
) {
64+
if rl.log.V(1).Enabled() {
65+
rl.blockDepth++
66+
depth := strings.Repeat(">", rl.blockDepth)
67+
msg := depth + " " + name
68+
AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg)
69+
}
70+
}
71+
72+
// Exit logs an exit from a function or code block
73+
func (rl *ResourceLogger) Exit(
74+
name string, // name of the function or code block we're exiting
75+
err error,
76+
additionalValues ...interface{},
77+
) {
78+
if rl.log.V(1).Enabled() {
79+
depth := strings.Repeat("<", rl.blockDepth)
80+
msg := depth + " " + name
81+
if err != nil {
82+
additionalValues = append(additionalValues, "error")
83+
additionalValues = append(additionalValues, err)
84+
}
85+
AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg)
86+
rl.blockDepth--
87+
}
88+
}
89+
90+
// Trace logs an entry to a function or code block and returns a functor
91+
// that can be called to log the exit of the function or code block
92+
func (rl *ResourceLogger) Trace(
93+
name string,
94+
additionalValues ...interface{},
95+
) acktypes.TraceExiter {
96+
rl.Enter(name, additionalValues...)
97+
f := func(err error, args ...interface{}) {
98+
rl.Exit(name, err, args...)
99+
}
100+
return f
101+
}
102+
103+
// NewResourceLogger returns a resourceLogger that can write log messages about
104+
// a resource.
105+
func NewResourceLogger(
106+
log logr.Logger,
107+
res acktypes.AWSResource,
108+
additionalValues ...interface{},
109+
) *ResourceLogger {
110+
return &ResourceLogger{
111+
log: AdaptResource(log, res, additionalValues...),
112+
res: res,
113+
blockDepth: 0,
114+
}
115+
}
116+
23117
// AdaptResource returns a logger with log values set for the resource's kind,
24118
// namespace, name, etc
25119
func AdaptResource(

0 commit comments

Comments
 (0)