Skip to content

Instantly share code, notes, and snippets.

@mohatb
Last active April 29, 2023 11:46
Show Gist options
  • Select an option

  • Save mohatb/5398c475ca43932e79349978ab3e30ea to your computer and use it in GitHub Desktop.

Select an option

Save mohatb/5398c475ca43932e79349978ab3e30ea to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
)
// getContainersPrometheusLabels takes containerdSocket and namespace as input and returns a map containing the Prometheus labels for all containers.
func getContainersPrometheusLabels(containerdSocket, namespace string) (map[string]map[string]string, error) {
// Connect to the containerd daemon using the provided socket
client, err := containerd.New(containerdSocket)
if err != nil {
return nil, fmt.Errorf("failed to connect to containerd: %v", err)
}
defer client.Close()
// Set the namespace for the context
ctx := namespaces.WithNamespace(context.Background(), namespace)
// Get a reference to the container service
containerService := client.ContainerService()
// List all the containers in the specified namespace
containers, err := containerService.List(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list containers: %v", err)
}
// Create a map to store container IDs and their corresponding Prometheus labels
containerLabels := make(map[string]map[string]string)
// Iterate through the containers
for _, container := range containers {
containerID := container.ID
labels := make(map[string]string)
ids := make(map[string]string)
// Store container ID and Kubernetes pod UID in ids map
ids["containerd_namespace"] = containerID
ids["containerd_pod_sandbox_id"] = container.Labels["io.kubernetes.pod.uid"]
containerName := container.Labels["io.kubernetes.container.name"]
// Define a function to get Prometheus labels for a container
containerPrometheusLabelsFunc := func(labels, ids map[string]string, name string) map[string]string {
labels["id"] = name
labels["containerd_namespace"] = ids["containerd_namespace"]
labels["containerd_pod_sandbox_id"] = ids["containerd_pod_sandbox_id"]
labels["container_name"] = name
// Add all the container's labels to the Prometheus labels
for k, v := range container.Labels {
labels[k] = v
}
return labels
}
// Get the Prometheus labels for this container and add them to the containerLabels map
containerLabels[containerID] = containerPrometheusLabelsFunc(labels, ids, containerName)
// Print the labels for this container
fmt.Printf("Labels for container %s:\n", containerID)
for k, v := range container.Labels {
fmt.Printf("%s=%s\n", k, v)
}
fmt.Println()
}
return containerLabels, nil
}
func main() {
// Define the containerd socket and namespace
containerdSocket := "/run/containerd/containerd.sock"
namespace := "k8s.io"
// Call the getContainersPrometheusLabels function with the socket and namespace
getContainersPrometheusLabels(containerdSocket, namespace)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment