-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
108 lines (94 loc) · 2.86 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/vshn/exporter-filterproxy/target"
)
var kubeSAPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
func main() {
mux := http.NewServeMux()
configPath := flag.String("config", "", "path to config file")
flag.Parse()
conf, err := readConfig(*configPath)
if err != nil {
log.Fatalf("Failed to open configuration file %q: %s", *configPath, err.Error())
return
}
targetDiscovery := multiTargetConfigFetcher{}
for name, endpoint := range conf.Endpoints {
authToken, err := getAuthToken(endpoint.Auth)
if err != nil {
log.Fatalf("Failed to get bearer token: %s", err.Error())
return
}
switch {
case endpoint.Target != "":
log.Printf("Registering static endpoint %q at %s", name, endpoint.Path)
sf := target.NewStaticFetcher(endpoint.Target, authToken, endpoint.RefreshInterval, endpoint.InsecureSkipVerify)
mux.HandleFunc(endpoint.Path,
handler(sf),
)
targetDiscovery[endpoint.Path] = sf
case endpoint.KubernetesTarget != nil:
log.Printf("Registering kube endpoint %q at %s", name, endpoint.Path)
kf, err := target.NewKubernetesEndpointFetcher(
target.KubernetesEndpointFetcherOpts{
Endpointname: endpoint.KubernetesTarget.Endpoint.Name,
Namespace: endpoint.KubernetesTarget.Endpoint.Namespace,
Port: endpoint.KubernetesTarget.Endpoint.Port,
Path: endpoint.KubernetesTarget.Endpoint.Path,
Scheme: endpoint.KubernetesTarget.Endpoint.Scheme,
AuthToken: authToken,
RefreshInterval: endpoint.RefreshInterval,
InsecureSkipVerify: endpoint.InsecureSkipVerify,
},
)
if err != nil {
log.Fatalf("Failed to initalize Kubernetes endpoint: %s", err.Error())
return
}
mux.HandleFunc(endpoint.Path+"/",
multiHandler(endpoint.Path, kf),
)
mux.HandleFunc(endpoint.Path,
serviceDiscoveryHandler(endpoint.Path, kf),
)
targetDiscovery[endpoint.Path] = kf
default:
log.Fatalf("No target set for endpoint %s", name)
return
}
}
mux.HandleFunc("/",
serviceDiscoveryHandler("", targetDiscovery),
)
srv := &http.Server{
Addr: conf.Addr,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
Handler: mux,
}
log.Printf("Listening on %s", conf.Addr)
log.Println(srv.ListenAndServe())
}
func getAuthToken(conf endpointAuth) (string, error) {
switch conf.Type {
case authModeBearer:
return fmt.Sprintf("Bearer %s", conf.Token), nil
case authModeKube:
saToken, err := os.ReadFile(kubeSAPath)
if err != nil {
return "", fmt.Errorf("failed to get kubernetes serviceaccount token: %w", err)
}
return fmt.Sprintf("Bearer %s", string(saToken)), nil
case authModeNone:
return "", nil
default:
return "", fmt.Errorf("unkown auth type: %q", conf.Type)
}
}