|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "flag" |
| 21 | + "net/http" |
| 22 | + "net/url" |
| 23 | + "time" |
| 24 | + |
| 25 | + "k8s.io/perf-tests/slo-monitor/src/monitors" |
| 26 | + |
| 27 | + clientset "k8s.io/client-go/kubernetes" |
| 28 | + |
| 29 | + "k8s.io/contrib/cluster-autoscaler/config" |
| 30 | + |
| 31 | + "github.com/golang/glog" |
| 32 | + "github.com/prometheus/client_golang/prometheus" |
| 33 | + "github.com/spf13/pflag" |
| 34 | +) |
| 35 | + |
| 36 | +var ( |
| 37 | + kubernetesURL string |
| 38 | + listenURL string |
| 39 | + purgeAfterSeconds int32 |
| 40 | +) |
| 41 | + |
| 42 | +func registerFlags(fs *pflag.FlagSet) { |
| 43 | + fs.StringVar(&kubernetesURL, "kubernetes-url", "", "Kubernetes master location. Leave blank for default") |
| 44 | + fs.StringVar(&listenURL, "listen-url", ":8080", "URL on which monitor should serve metrics") |
| 45 | + fs.Int32Var(&purgeAfterSeconds, "purge-after-seconds", 120, "Time after which deleted entries are purged.") |
| 46 | +} |
| 47 | + |
| 48 | +func createKubeClient() clientset.Interface { |
| 49 | + url, err := url.Parse(kubernetesURL) |
| 50 | + if err != nil { |
| 51 | + glog.Fatalf("Failed to parse Kuberentes url: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + kubeConfig, err := config.GetKubeClientConfig(url) |
| 55 | + if err != nil { |
| 56 | + glog.Fatalf("Failed to build Kuberentes client configuration: %v", err) |
| 57 | + } |
| 58 | + kubeConfig.ContentType = "application/vnd.kubernetes.protobuf" |
| 59 | + |
| 60 | + return clientset.NewForConfigOrDie(kubeConfig) |
| 61 | +} |
| 62 | + |
| 63 | +func main() { |
| 64 | + registerFlags(pflag.CommandLine) |
| 65 | + pflag.CommandLine.AddGoFlagSet(flag.CommandLine) |
| 66 | + pflag.Parse() |
| 67 | + |
| 68 | + glog.Infof("Starting Performance SLO monitor on port %v", listenURL) |
| 69 | + |
| 70 | + monitors.Register() |
| 71 | + http.Handle("/metrics", prometheus.Handler()) |
| 72 | + |
| 73 | + kubeClient := createKubeClient() |
| 74 | + |
| 75 | + stopCh := make(chan struct{}) |
| 76 | + defer close(stopCh) |
| 77 | + |
| 78 | + monitor := monitors.NewPodStartupLatencyDataMonitor(kubeClient, time.Duration(purgeAfterSeconds)*time.Second) |
| 79 | + go func() { |
| 80 | + if err := monitor.Run(stopCh); err != nil { |
| 81 | + panic(err) |
| 82 | + } |
| 83 | + }() |
| 84 | + |
| 85 | + glog.Fatal(http.ListenAndServe(listenURL, nil)) |
| 86 | +} |
0 commit comments