-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
115 lines (93 loc) · 4.02 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
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"flag"
"net/http"
"os"
"path/filepath"
"github.com/emicklei/go-restful"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/component-base/logs"
"k8s.io/klog"
"github.com/samuel-sujith/mwatch/pkg/types"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
kingpin "gopkg.in/alecthomas/kingpin.v2"
basecmd "github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/cmd"
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"
fakeprov "github.com/samuel-sujith/mwatch/pkg/provider"
)
//SampleAdapter encasulates the apiserver base
//Options are added to it to incorporate into metrics server
type SampleAdapter struct {
basecmd.AdapterBase
// Message is printed on succesful startup
Message string
}
func (a *SampleAdapter) makeProviderOrDie(intconf types.Interimconfig) (provider.MetricsProvider, *restful.WebService, int64) {
client, err := a.DynamicClient()
if err != nil {
klog.Fatalf("unable to construct dynamic client: %v", err)
}
mapper, err := a.RESTMapper()
if err != nil {
klog.Fatalf("unable to construct discovery REST mapper: %v", err)
}
return fakeprov.NewFakeProvider(client, mapper, intconf)
}
func main() {
logs.InitLogs()
defer logs.FlushLogs()
w := log.NewSyncWriter(os.Stderr)
logger := log.NewLogfmtLogger(w)
configuration := types.Cfg{Listenaddress: "a dummy address", DesiredMetric: "a dummy metric"}
var cert, key string
var skipServerCertCheck bool
a := kingpin.New(filepath.Base(os.Args[0]), "The Model metric watcher")
a.Flag("listenaddress", "Model watcher address to watch.").Default("http://localhost:8080/metrics").StringVar(&configuration.Listenaddress)
a.Flag("desired_metric", "Desired metric to watch out for").Default("process_open_fds").StringVar(&configuration.DesiredMetric)
a.Flag("cert", "certificate file for client.").Default("").StringVar(&cert)
a.Flag("key", "key for certificate file for client.").Default("").StringVar(&key)
a.Flag("accept-invalid-cert", "Skipping cert check").Default("true").BoolVar(&skipServerCertCheck)
cmd := &SampleAdapter{}
cmd.Flags().StringVar(&cmd.Message, "msg", "starting adapter...", "startup message")
cmd.Flags().AddGoFlagSet(flag.CommandLine) // make sure we get the klog flags
cmd.Flags().Parse(os.Args)
promscrapeaddress := os.Getenv("PROM_IP")
configurationactual := types.Cfg{Listenaddress: promscrapeaddress, DesiredMetric: "a dummy metric"}
interimconfig := types.Interimconfig{
Configuration: configurationactual,
Logger: logger,
Cert: cert,
Key: key,
SkipServerCertCheck: skipServerCertCheck,
}
testProvider, webService, noofmetricsloaded := cmd.makeProviderOrDie(interimconfig)
cmd.WithCustomMetrics(testProvider)
cmd.WithExternalMetrics(testProvider)
level.Info(logger).Log("msg", cmd.Message)
level.Info(logger).Log("msg", "starting to listen on ", "address", promscrapeaddress)
level.Info(logger).Log("msg", "No of metrics loaded is ", "number", noofmetricsloaded)
// Set up POST endpoint for writing fake metric values
restful.DefaultContainer.Add(webService)
go func() {
// Open port for POSTing fake metrics
//klog.Fatal(http.ListenAndServe(":8080", nil))
level.Error(logger).Log("msg", "unable to make transport", "error", http.ListenAndServe(":8080", nil))
}()
if err := cmd.Run(wait.NeverStop); err != nil {
//klog.Fatalf("unable to run custom metrics adapter: %v", err)
level.Error(logger).Log("msg", "unable to run custom metrics adapter", "error", err)
}
}