-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
213 lines (189 loc) · 5.77 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"context"
"encoding/json"
"net/http"
"os"
"path/filepath"
"time"
"github.com/TheMeier/k8sinfo/model"
"github.com/TheMeier/k8sinfo/stores"
"github.com/globalsign/mgo"
"github.com/jasonlvhit/gocron"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
kingpin "gopkg.in/alecthomas/kingpin.v2"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
networking "k8s.io/api/networking/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/util/homedir"
)
var k8sInfoData = NewK8sInfoHolder()
func getDefaultOverride() clientcmd.ConfigOverrides {
return clientcmd.ConfigOverrides{
ClusterInfo: api.Cluster{
Server: "",
},
}
}
func scrapeData(kubeconfigs []string, mongoSession *mgo.Session, mongoEnable *bool) {
newData := make(map[string]*model.K8sInfoElement)
for _, kubeconfig := range kubeconfigs {
cnf, err := clientcmd.LoadFromFile(kubeconfig)
if err != nil {
log.Errorf("Failed to parse config at %s", kubeconfig)
log.Errorf("%s", err)
return
}
for contextName := range cnf.Contexts {
log.Debugf("Context: %s", contextName)
override := getDefaultOverride()
config := clientcmd.NewNonInteractiveClientConfig(*cnf, contextName, &override, nil)
clientConfig, err := config.ClientConfig()
if err != nil {
log.Errorf("Failed to create clientConfig: %s", err)
continue
}
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
clientset, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
log.Errorf("Failed to create clientset: %s", err)
}
deployments, _ := clientset.AppsV1().Deployments("").List(ctx, v1.ListOptions{})
services, _ := clientset.CoreV1().Services("").List(ctx, v1.ListOptions{})
ingresses, _ := clientset.NetworkingV1().Ingresses("").List(ctx, v1.ListOptions{})
newData[contextName] = &model.K8sInfoElement{
Deployments: deployments,
Services: services,
Ingresses: ingresses,
}
}
}
k8sInfoData.Set(newData)
if *mongoEnable {
stores.UpdateMongoDB(k8sInfoData.Get(), mongoSession)
}
}
func k8sHTTPHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(k8sInfoData.Get())
if err != nil {
log.Error(err)
}
}
func k8sHTTPHandlerDeployments(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
data := k8sInfoData.Get()
ret := make(map[string]*apps.DeploymentList)
for context, value := range data {
ret[context] = value.Deployments
}
err := json.NewEncoder(w).Encode(ret)
if err != nil {
log.Errorf("Failed to encode json: %s", err)
}
}
func k8sHTTPHandlerServices(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
data := k8sInfoData.Get()
ret := make(map[string]*core.ServiceList)
for context, value := range data {
ret[context] = value.Services
}
err := json.NewEncoder(w).Encode(ret)
if err != nil {
log.Errorf("Failed to encode json: %s", err)
}
}
func k8sHTTPHandlerIngresses(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
data := k8sInfoData.Get()
ret := make(map[string]*networking.IngressList)
for context, value := range data {
ret[context] = value.Ingresses
}
err := json.NewEncoder(w).Encode(ret)
if err != nil {
log.Errorf("Failed to encode json: %s", err)
}
}
func main() {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
kubeconfigs := kingpin.Flag("kubeconfig",
"path to one or multiple kubeconfig files").
Default(filepath.Join(homedir.HomeDir(), ".kube", "config")).
Short('c').
ExistingFiles()
scrapeInterval := kingpin.Flag("scrapeInterval",
"Interval between data scraping").
Default("120").
Short('i').
Int()
host := kingpin.Flag("web.listen-address",
"Address to listen on for http requests").
Default(":2112").
Short('l').
String()
debug := kingpin.Flag("debug", "Set log level to debug").
Default("false").
Short('d').
Bool()
mongoEnable := kingpin.Flag("mongoEnable", "Enable exporter for mongodb").
Default("false").
Bool()
mongoAddress := kingpin.Flag("mongoAddress",
"address to mongo seed servers, can be specified multiple times").
Default("localhost:27017").
Short('m').
Strings()
kingpin.Parse()
if *debug {
log.SetLevel(log.DebugLevel)
}
log.Infof("Staring k8sinfo, listening on %s, scrape interval %d",
*host,
*scrapeInterval)
mongoSession := &mgo.Session{}
if *mongoEnable {
mongoDBDialInfo := &mgo.DialInfo{
Addrs: *mongoAddress,
Timeout: 60 * time.Second,
Database: "k8sinfo",
Username: os.Getenv("MONGO_USERNAME"),
Password: os.Getenv("MONGO_PASSWORD"),
}
var err error
mongoSession, err = mgo.DialWithInfo(mongoDBDialInfo)
if err != nil {
log.Fatalf("CreateSession: %s\n", err)
}
} else {
mongoSession = &mgo.Session{}
}
scrapeData(*kubeconfigs, mongoSession, mongoEnable)
go func() {
err := gocron.Every(uint64(*scrapeInterval)).Seconds().Do(scrapeData, *kubeconfigs, mongoSession, mongoEnable)
if err != nil {
log.Errorf("Failed to start gocron: %s", err)
}
<-gocron.Start()
}()
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", k8sHTTPHandler)
http.HandleFunc("/deployments", k8sHTTPHandlerDeployments)
http.HandleFunc("/services", k8sHTTPHandlerServices)
http.HandleFunc("/ingresses", k8sHTTPHandlerIngresses)
http.HandleFunc("/trigger", func(w http.ResponseWriter, r *http.Request) {
scrapeData(*kubeconfigs, mongoSession, mongoEnable)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(200)
})
log.Fatal(http.ListenAndServe(*host, nil))
}