Building a Simple Network Event Logger

Let's combine the setup and event handling logic into a practical tool: a basic network event logger. This application will use shared informers to monitor Pods and Network Policies within specified namespaces and print concise log messages when network-relevant events occur, such as Pod IP assignments/changes or Network Policy creations/deletions.

This serves as a concrete example of how informers can be used for real-time cluster monitoring.

The Goal:

Create a command-line Go application that:

  1. Connects to a Kubernetes cluster using kubeconfig.

  2. Optionally targets a specific namespace or monitors all namespaces.

  3. Uses SharedInformerFactory to monitor v1.Pod and networkingv1.NetworkPolicy resources.

  4. Logs messages to the console for the following events:

    • Pod Added: Log Name, Namespace, Assigned Pod IP, Node Name.

    • Pod Updated: Log Name, Namespace if Pod IP or Phase changes.

    • Pod Deleted: Log Name, Namespace.

    • NetworkPolicy Added: Log Name, Namespace.

    • NetworkPolicy Updated: Log Name, Namespace (indicating a change).

    • NetworkPolicy Deleted: Log Name, Namespace.

  5. Handles graceful shutdown on SIGINT/SIGTERM.

The Code:

This code integrates the informer setup with more specific logging logic in the handlers.

Last updated

Was this helpful?