How enix/x509-certificate-exporter works
Project Type and Purpose
• This is a backend monitoring tool written in Go: a Prometheus exporter focused on X.509 certificate expiration.
• It runs as a standalone service or inside Kubernetes (via Helm chart), scans PEM files, directories, kubeconfigs and Kubernetes Secrets, then exposes metrics on an HTTP endpoint.
File Structure Analysis
• Top-level:
• cmd/x509-certificate-exporter: entry point (main.go, utility.go)
• internal/: core logic (certificate parsing, collectors, Kubernetes client, utility and version)
• deploy/: deployment manifests and Helm chart for Kubernetes, systemd unit for Linux, OpenBSD daemon script
• docker-compose.yml, Dockerfile: containerized execution
• hack/: development scripts (kind cluster setup, build server)
• test/: fixture files and automated tests for certificates and YAML configs
• docs/: Grafana dashboard screenshot
• Configuration files: .env, .air.toml (live-reload), go.mod/sum, CI pipelines under .github/workflows
README Insights
• Describes metrics, usage modes (CLI flags, Docker image, Helm chart), Prometheus alert rules.
Main Components
a. Exporter Service (Go binary)
• CLI / configuration loader
• File watcher (scan files & directories)
• Kubernetes watcher (client-go API, list/watch secrets)
• Certificate Collector (parses PEM data, computes not-before/after)
• Metrics Registry (Prometheus client library)
• HTTP Server or systemd socket listener (exposes /metrics)
b. Configuration & Deployment
• CLI flags or environment variables
• Docker container or systemd service unit
• Helm chart layering: Deployment, DaemonSet, ServiceMonitor, Grafana Dashboard
c. External Services
• Prometheus server scrapes the /metrics endpoint
• (Optional) Grafana dashboard visualizes metrics
• Kubernetes API (when --watch-kube-secrets enabled)
Interactions and Data Flow
• On startup, the Exporter reads configuration flags (CLI, kubeconfig path, watch dirs/files, include/exclude filters).
• File watcher emits file paths → Collector parses certificates → Metrics Registry updates gauges.
• Kubernetes watcher uses client-go → streams Secrets → Collector extracts certificates → Metrics Registry updates.
• HTTP server serves /metrics with current metric values.
• Prometheus polls /metrics → stores time series → Grafana queries Prometheus for dashboards/alerts.
Architectural Patterns and Principles
• Modular layering: cmd (orchestration) → internal packages (domain logic) → Prometheus client
• Dependency injection for Kubernetes client and file system interfaces (testable via fake clientset).
• Separation of concerns: watching vs parsing vs metrics vs serving.
• Use of standard exporter pattern (single‐responsibility metrics server).
Technologies and Frameworks
• Go modules, Go standard library (net/http, time, io/fs)
• client-go/kubernetes/fake for Kubernetes API interactions
• Prometheus Go client library
• Helm for Kubernetes chart
• Docker, containerd (via Dockerfile, docker-compose)
• systemd socket activation (optional)
• Kind for local Kubernetes in hack scripts