-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
110 lines (91 loc) · 2.56 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"context"
"fmt"
"github.com/ccremer/kubernetes-zfs-provisioner/pkg/provisioner"
"k8s.io/klog/v2"
"net/http"
"os"
"strconv"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/sig-storage-lib-external-provisioner/v10/controller"
)
const (
metricsAddrKey = "METRICS_ADDR"
metricsPortKey = "METRICS_PORT"
kubeConfigPathKey = "KUBE_CONFIG_PATH"
provisionerInstanceKey = "PROVISIONER_INSTANCE"
)
type Settings struct {
MetricsAddr string
MetricsPort int
KubeConfigPath string
ProvisionerInstance string
}
var (
// These will be populated by Goreleaser at build time
version = "snapshot"
commit = "dirty"
settings Settings
)
func main() {
loadEnvironmentVariables()
log := klog.NewKlogr()
log.Info("Using configuration", "config", settings)
config, err := clientcmd.BuildConfigFromFlags("", settings.KubeConfigPath)
if err != nil {
klog.Fatalf("Couldn't get in-cluster or kubectl config: %v", err)
}
// Retrieve config
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
klog.Fatalf("Failed to create kubernetes client: %v", err)
}
log.Info("Connected to cluster", "host", config.Host)
p, err := provisioner.NewZFSProvisioner(settings.ProvisionerInstance, log)
if err != nil {
klog.Fatalf("Failed to create ZFS provisioner: %v", err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/metrics", http.StatusMovedPermanently)
})
pc := controller.NewProvisionController(
log,
clientset,
settings.ProvisionerInstance,
p,
controller.MetricsAddress(settings.MetricsAddr),
controller.MetricsPort(int32(settings.MetricsPort)),
)
log.Info("Starting provisioner", "version", version, "commit", commit)
pc.Run(context.Background())
}
func loadEnvironmentVariables() {
prefix := "ZFS_"
defaults := map[string]string{
metricsPortKey: "8080",
metricsAddrKey: "0.0.0.0",
kubeConfigPathKey: "",
provisionerInstanceKey: "pv.kubernetes.io/zfs",
}
for key, _ := range defaults {
value, found := os.LookupEnv(fmt.Sprintf("%s%s", prefix, key))
if found {
defaults[key] = value
}
}
settings = Settings{
MetricsAddr: defaults[metricsAddrKey],
MetricsPort: parseInt(defaults[metricsPortKey]),
KubeConfigPath: defaults[kubeConfigPathKey],
ProvisionerInstance: defaults[provisionerInstanceKey],
}
}
func parseInt(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
klog.Fatalf("Failed to convert metrics port to integer: %v", err)
}
return i
}