Skip to content
This repository was archived by the owner on Dec 1, 2018. It is now read-only.

Commit 76c7d3b

Browse files
committed
add healthz to eventer
1 parent 8c6c154 commit 76c7d3b

File tree

5 files changed

+113
-8
lines changed

5 files changed

+113
-8
lines changed

events/api/api.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2017 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package api
16+
17+
import (
18+
"net/http"
19+
20+
"github.com/prometheus/client_golang/prometheus"
21+
"k8s.io/apiserver/pkg/server/healthz"
22+
)
23+
24+
func init() {
25+
healthz.InstallHandler(http.DefaultServeMux, healthzChecker())
26+
27+
http.Handle("/metrics", prometheus.UninstrumentedHandler())
28+
}

events/api/handler.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2017 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package api
16+
17+
import (
18+
"errors"
19+
"fmt"
20+
"net/http"
21+
"time"
22+
23+
"github.com/golang/glog"
24+
"k8s.io/apiserver/pkg/server/healthz"
25+
"k8s.io/heapster/events/manager"
26+
)
27+
28+
const (
29+
// MaxEventsScrapeDelay should be larger than `frequency` command line argument.
30+
MaxEventsScrapeDelay = 3 * time.Minute
31+
)
32+
33+
func healthzChecker() healthz.HealthzChecker {
34+
return healthz.NamedCheck("healthz", func(r *http.Request) error {
35+
if time.Since(manager.LatestScrapeTime) > MaxEventsScrapeDelay {
36+
msg := fmt.Sprintf(
37+
"No event batch within %s (latest: %s)",
38+
MaxEventsScrapeDelay,
39+
manager.LatestScrapeTime,
40+
)
41+
glog.Warning(msg)
42+
return errors.New(msg)
43+
}
44+
45+
return nil
46+
})
47+
}

events/eventer.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,32 @@ package main
1919
import (
2020
"flag"
2121
"fmt"
22+
"net"
23+
"net/http"
2224
"os"
2325
"runtime"
26+
"strconv"
2427
"strings"
2528
"time"
2629

2730
"github.com/golang/glog"
2831
"k8s.io/apiserver/pkg/util/logs"
2932
"k8s.io/heapster/common/flags"
33+
"k8s.io/heapster/events/api"
3034
"k8s.io/heapster/events/manager"
3135
"k8s.io/heapster/events/sinks"
3236
"k8s.io/heapster/events/sources"
3337
"k8s.io/heapster/version"
3438
)
3539

3640
var (
37-
argFrequency = flag.Duration("frequency", 30*time.Second, "The frequency at which Eventer pushes events to sinks")
38-
argMaxProcs = flag.Int("max_procs", 0, "Max number of logical CPUs that can be used simultaneously, default to number of logical CPUs available on the machine when not specified or less than 1")
39-
argSources flags.Uris
40-
argSinks flags.Uris
41-
argVersion bool
41+
argFrequency = flag.Duration("frequency", 30*time.Second, "The resolution at which Eventer pushes events to sinks")
42+
argMaxProcs = flag.Int("max_procs", 0, "max number of CPUs that can be used simultaneously. Less than 1 for default (number of cores)")
43+
argSources flags.Uris
44+
argSinks flags.Uris
45+
argVersion bool
46+
argHealthzIP = flag.String("healthz-ip", "0.0.0.0", "ip eventer health check service uses")
47+
argHealthzPort = flag.Uint("healthz-port", 8084, "port eventer health check listens on")
4248
)
4349

4450
func main() {
@@ -98,16 +104,34 @@ func main() {
98104
if err != nil {
99105
glog.Fatalf("Failed to create main manager: %v", err)
100106
}
101-
manager.Start()
102107

108+
manager.Start()
103109
glog.Infof("Starting eventer")
110+
111+
go startHTTPServer()
112+
104113
<-quitChannel
105114
}
106115

116+
func startHTTPServer() {
117+
glog.Info("Starting eventer http service")
118+
119+
glog.Fatal(http.ListenAndServe(net.JoinHostPort(*argHealthzIP, strconv.Itoa(int(*argHealthzPort))), nil))
120+
}
121+
107122
func validateFlags() error {
108-
if *argFrequency < 5*time.Second {
109-
return fmt.Errorf("frequency needs to be greater than 5 seconds - %d", *argFrequency)
123+
var minFrequency = 5 * time.Second
124+
125+
if *argFrequency < minFrequency {
126+
return fmt.Errorf("frequency needs to be greater than %s, supplied %s", minFrequency,
127+
*argFrequency)
110128
}
129+
130+
if *argFrequency > api.MaxEventsScrapeDelay {
131+
return fmt.Errorf("frequency needs to be smaller than %s, supplied %s",
132+
api.MaxEventsScrapeDelay, *argFrequency)
133+
}
134+
111135
return nil
112136
}
113137

events/manager/manager.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ var (
3131
Name: "last_time_seconds",
3232
Help: "Last time of eventer housekeep since unix epoch in seconds.",
3333
})
34+
35+
// Time of latest scrape operation
36+
LatestScrapeTime = time.Now()
3437
)
3538

3639
func init() {
@@ -89,6 +92,8 @@ func (rm *realManager) Housekeep() {
8992
func (rm *realManager) housekeep() {
9093
defer lastHousekeepTimestamp.Set(float64(time.Now().Unix()))
9194

95+
LatestScrapeTime = time.Now()
96+
9297
// No parallelism. Assumes that the events are pushed to Heapster. Add parallelism
9398
// when this stops to be true.
9499
events := rm.source.GetNewEvents()

events/sources/kubernetes/kubernetes_source.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ event_loop:
9696
}
9797

9898
totalEventsNum.Add(float64(len(result.Events)))
99+
99100
return &result
100101
}
101102

0 commit comments

Comments
 (0)