Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0a1b6bbcb3
|
||
|
|
2cd78f4310
|
||
|
|
751a96b142
|
||
|
|
75411915a0
|
||
|
|
ea8a518921
|
||
|
|
e853273df0
|
||
|
|
f3531af469
|
||
|
|
1c60f1e0e5
|
||
|
|
9c946beefb
|
||
|
|
bcbbe000f3
|
||
|
|
56bd8445a4
|
||
|
|
ce2faa39eb
|
||
|
|
2bb100c87b
|
||
|
|
a4987f4f42
|
||
|
|
eee330aea5
|
||
|
|
ea33973994
|
||
|
|
a617999440
|
||
|
|
ce259555bc
|
||
|
|
2f382b02a2
|
||
|
|
e92f902930
|
||
|
|
73f15774d6
|
Generated
+1
-1
@@ -70,6 +70,6 @@
|
||||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "5dbb4b7db532dc553a8522b23f0913aea04bf84bf96ea48d5c1920637edb6af3"
|
||||
inputs-digest = "6ca9d144505195ce3da40f2598509d79c7205b05dddfa38c4a18acc7200a3755"
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
||||
|
||||
+1
-1
@@ -30,8 +30,8 @@
|
||||
version = "0.8.0"
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "mcquay.me/metrics"
|
||||
version = "1.0.0"
|
||||
|
||||
[prune]
|
||||
go-tests = true
|
||||
|
||||
@@ -1,5 +1,40 @@
|
||||
bin/hw: cmd/hw/main.go bin
|
||||
GOOS=linux go build -v -o bin/hw ./cmd/hw
|
||||
.PHONY: default
|
||||
default: bin/hw bin/hwc bin/hwl
|
||||
|
||||
VERSION := $(shell git describe --tags 2> /dev/null || echo "unreleased")
|
||||
V_DIRTY := $(shell git describe --exact-match HEAD 2> /dev/null > /dev/null || echo "-unreleased")
|
||||
GIT := $(shell git rev-parse --short HEAD)
|
||||
DIRTY := $(shell git diff-index --quiet HEAD 2> /dev/null > /dev/null || echo "-dirty")
|
||||
|
||||
bin/hw: $(shell ls *.go) cmd/hw/main.go bin
|
||||
@echo hw
|
||||
@go build -ldflags \
|
||||
"-X mcquay.me/hw.Version=$(VERSION)$(V_DIRTY) \
|
||||
-X mcquay.me/hw.Git=$(GIT)$(DIRTY)" \
|
||||
-v -o bin/hw ./cmd/hw
|
||||
|
||||
bin/hwc: $(shell ls *.go ) cmd/hwc/main.go bin
|
||||
@echo hwc
|
||||
@go build -ldflags \
|
||||
"-X mcquay.me/hw.Version=$(VERSION)$(V_DIRTY) \
|
||||
-X mcquay.me/hw.Git=$(GIT)$(DIRTY)" \
|
||||
-v -o bin/hwc ./cmd/hwc
|
||||
|
||||
bin/hwl: $(shell ls *.go) cmd/hwl/main.go bin
|
||||
@echo hwl
|
||||
@go build -ldflags \
|
||||
"-X mcquay.me/hw.Version=$(VERSION)$(V_DIRTY) \
|
||||
-X mcquay.me/hw.Git=$(GIT)$(DIRTY)" \
|
||||
-v -o bin/hwl ./cmd/hwl
|
||||
|
||||
bin:
|
||||
mkdir bin
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
@rm -fv bin/{hw,hwl,hwc}
|
||||
|
||||
.PHONY: lint
|
||||
lint:
|
||||
@golint $(shell go list mcquay.me/hw/...)
|
||||
@go vet $(shell go list mcquay.me/hw/...)
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
# hw
|
||||
|
||||
simple http server that responds with a version
|
||||
collection of tooling to validate kubernetes server behavior. It includes:
|
||||
|
||||
- `hw`: simple http server that responds with a version
|
||||
- `hwc`: client to `hw`, responds with information collected from various hw
|
||||
- `hwl`: periodically prints a log message
|
||||
|
||||
+6
-13
@@ -9,16 +9,10 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"mcquay.me/hw"
|
||||
"mcquay.me/metrics"
|
||||
)
|
||||
|
||||
const version = "v0.1.0"
|
||||
|
||||
type v struct {
|
||||
Hostname string `json:"hostname"`
|
||||
V string `json:"version"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
m, err := metrics.New("hw")
|
||||
if err != nil {
|
||||
@@ -30,9 +24,10 @@ func main() {
|
||||
}
|
||||
http.HandleFunc("/", m.WrapFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
r := v{
|
||||
r := hw.V{
|
||||
Hostname: hn,
|
||||
V: version,
|
||||
V: hw.Version,
|
||||
G: hw.Git,
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(r); err != nil {
|
||||
log.Printf("json: %+v", err)
|
||||
@@ -53,12 +48,10 @@ func main() {
|
||||
code := codes[rand.Intn(len(codes))]
|
||||
w.WriteHeader(code)
|
||||
}))
|
||||
http.HandleFunc("/live", m.WrapFunc("/live", ok))
|
||||
http.HandleFunc("/ready", m.WrapFunc("/ready", ok))
|
||||
http.HandleFunc("/live", m.WrapFunc("/live", hw.OK))
|
||||
http.HandleFunc("/ready", m.WrapFunc("/ready", hw.OK))
|
||||
http.Handle("/metrics", promhttp.Handler())
|
||||
if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||
log.Fatalf("listen and serve: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func ok(w http.ResponseWriter, req *http.Request) {}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
FROM scratch
|
||||
ADD etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
||||
ADD bin/hwc /bin/
|
||||
CMD ["/bin/hwc"]
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"mcquay.me/hw"
|
||||
"mcquay.me/metrics"
|
||||
)
|
||||
|
||||
var success *prometheus.CounterVec
|
||||
|
||||
type state struct {
|
||||
Hostname string `json:"hostname"`
|
||||
V string `json:"version"`
|
||||
G string `json:"git"`
|
||||
|
||||
sync.RWMutex
|
||||
Counts map[string]int `json:"counts"`
|
||||
}
|
||||
|
||||
func (s *state) update(target string) {
|
||||
u := fmt.Sprintf("http://%s:8080/", target)
|
||||
for {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
req, err := http.NewRequest("GET", u, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
req.Close = true
|
||||
httpResp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
success.WithLabelValues("false").Inc()
|
||||
continue
|
||||
}
|
||||
success.WithLabelValues("true").Inc()
|
||||
rv := hw.V{}
|
||||
if err := json.NewDecoder(httpResp.Body).Decode(&rv); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := httpResp.Body.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
s.Lock()
|
||||
s.Counts[rv.Hostname]++
|
||||
s.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
log.Fatal("usage: hwc <target hostname>")
|
||||
}
|
||||
target := os.Args[1]
|
||||
|
||||
m, err := metrics.New("hwc")
|
||||
if err != nil {
|
||||
log.Fatalf("metrics: %v", err)
|
||||
}
|
||||
|
||||
success = prometheus.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Name: "hwc_success_count",
|
||||
Help: "counts successes and failures of fetch attempts",
|
||||
},
|
||||
[]string{"ok"},
|
||||
)
|
||||
|
||||
if err := prometheus.Register(success); err != nil {
|
||||
log.Fatalf("registering success: %v", err)
|
||||
}
|
||||
|
||||
hn, err := os.Hostname()
|
||||
if err != nil {
|
||||
log.Fatalf("hostname: %+v", err)
|
||||
}
|
||||
|
||||
fetcher := state{
|
||||
Hostname: hn,
|
||||
V: hw.Version,
|
||||
G: hw.Git,
|
||||
Counts: map[string]int{},
|
||||
}
|
||||
go fetcher.update(target)
|
||||
|
||||
http.HandleFunc("/", m.WrapFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
fetcher.RLock()
|
||||
defer fetcher.RUnlock()
|
||||
if err := json.NewEncoder(w).Encode(&fetcher); err != nil {
|
||||
log.Printf("json: %+v", err)
|
||||
}
|
||||
}))
|
||||
|
||||
http.HandleFunc("/live", m.WrapFunc("/live", hw.OK))
|
||||
http.HandleFunc("/ready", m.WrapFunc("/ready", hw.OK))
|
||||
|
||||
http.Handle("/metrics", promhttp.Handler())
|
||||
|
||||
if err := http.ListenAndServe(":8081", nil); err != nil {
|
||||
log.Fatalf("listen and serve: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
FROM scratch
|
||||
ADD etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
||||
ADD bin/hwl /bin/
|
||||
CMD ["/bin/hwl"]
|
||||
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"mcquay.me/hw"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for {
|
||||
log.Printf("hwl version=%q, git=%q", hw.Version, hw.Git)
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package hw
|
||||
|
||||
import "net/http"
|
||||
|
||||
// Version is version.
|
||||
var Version = "unset"
|
||||
|
||||
// Git exists so we can override it
|
||||
var Git = "unset"
|
||||
|
||||
// OK simply return 200
|
||||
func OK(w http.ResponseWriter, req *http.Request) {}
|
||||
|
||||
// V houses version information
|
||||
type V struct {
|
||||
Hostname string `json:"hostname"`
|
||||
V string `json:"version"`
|
||||
G string `json:"git"`
|
||||
}
|
||||
Reference in New Issue
Block a user