azure-container-networking
azure-container-networking copied to clipboard
Fix raw byte array formatting in logs for json.RawMessage fields
Problem
When CNS logs structures containing json.RawMessage fields (such as OrchestratorContext), it uses Go's %+v format specifier which outputs raw bytes as integer arrays. This makes the logs very difficult to read and understand, as shown in this example:
OrchestratorContext:[123 34 80 111 100 78 97 109 101 34 58 34 122 116 117 110 110 101 108 45 57 122 54 55 100 34 44 34 80 111 100 78 97 109 101 115 112 97 99 101 34 58 34 105 115 116 105 111 45 115 121 115 116 101 109 34 125]
These bytes actually represent the JSON string:
{"PodName":"ztunnel-9z67d","PodNamespace":"istio-system"}
Solution
This PR introduces a new toJSONString helper function in both the base logger (log/logger.go) and CNS logger (cns/logger/cnslogger.go) that properly formats objects containing json.RawMessage fields.
The approach:
- Use
json.Marshalinstead of%+vto convert objects to strings for logging - This ensures
json.RawMessagefields are properly formatted as JSON - Fall back to the original
%+vformatting if JSON marshaling fails - Update all the structured logging methods to use this new helper function
Example
Before:
Code:FailedToAllocateIpConfig, {DesiredIPAddresses:[] PodInterfaceID:c34a4c61-eth0 InfraContainerID:c34a4c61b6b2173f7cb62945a3e9f00ea33b99e4aa6b283e714b095b7875a87b OrchestratorContext:[123 34 80 111 100 78 97 109 101 34 58 34 122 116 117 110 110 101 108 45 57 122 54 55 100 34 44 34 80 111 100 78 97 109 101 115 112 97 99 101 34 58 34 105 115 116 105 111 45 115 121 115 116 101 109 34 125] ...}
After:
Code:FailedToAllocateIpConfig, {"DesiredIPAddresses":[],"PodInterfaceID":"c34a4c61-eth0","InfraContainerID":"c34a4c61b6b2173f7cb62945a3e9f00ea33b99e4aa6b283e714b095b7875a87b","OrchestratorContext":{"PodName":"ztunnel-9z67d","PodNamespace":"istio-system"} ...}
Fixes #3673.
[!WARNING]
Firewall rules blocked me from connecting to one or more addresses
I tried to connect to the following addresses, but was blocked by firewall rules:
cdn.fwupd.org
- Triggering command:
/usr/bin/fwupdmgr refresh(dns block)If you need me to access, download, or install something from one of these locations, you can either:
- Configure Actions setup steps to set up my environment, which run before the firewall is enabled
- Add the appropriate URLs or hosts to my firewall allow list
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.