-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathmain.go
75 lines (60 loc) · 1.83 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"time"
"github.com/supporttools/push-to-k8s/pkg/config"
"github.com/supporttools/push-to-k8s/pkg/k8s"
"github.com/supporttools/push-to-k8s/pkg/logging"
"github.com/supporttools/push-to-k8s/pkg/metrics"
"k8s.io/client-go/kubernetes"
)
var log = logging.SetupLogging()
func main() {
// Load configuration from environment
cfg := config.LoadConfigFromEnv()
logConfigStatus(cfg)
// Initialize Kubernetes client
clientset := initializeK8sClient()
// Start Prometheus metrics server
startMetricsServer(cfg)
// Start periodic secret sync and namespace watcher
startPeriodicSync(clientset, cfg)
startNamespaceWatcher(clientset, cfg)
// Block forever
select {}
}
func logConfigStatus(cfg config.Config) {
if cfg.Debug {
log.Debug("Debug mode enabled")
} else {
log.Info("Debug mode disabled")
}
if cfg.Namespace == "" {
log.Fatalf("Source namespace is not specified. Set the NAMESPACE environment variable.")
}
}
func initializeK8sClient() *kubernetes.Clientset {
clientset, err := k8s.CreateClusterConnection(log)
if err != nil {
log.Fatalf("Failed to connect to Kubernetes cluster: %v", err)
}
return clientset
}
func startMetricsServer(cfg config.Config) {
metricsPort := fmt.Sprintf(":%d", cfg.MetricsPort)
go metrics.StartMetricsServer(metricsPort, log)
}
func startPeriodicSync(clientset *kubernetes.Clientset, cfg config.Config) {
go func() {
ticker := time.NewTicker(time.Duration(cfg.SyncInterval) * time.Minute)
defer ticker.Stop()
for range ticker.C {
if err := k8s.SyncSecrets(clientset, cfg.Namespace, cfg.ExcludeNamespaceLabel, log); err != nil {
log.Errorf("Error syncing secrets: %v", err)
}
}
}()
}
func startNamespaceWatcher(clientset *kubernetes.Clientset, cfg config.Config) {
go k8s.WatchNamespaces(clientset, cfg.Namespace, cfg.ExcludeNamespaceLabel, log)
}