23 Commits
Author SHA1 Message Date
sm 7a7dfb139d Adds docker push 2018-03-09 10:15:19 -08:00
sm 636231a7f3 Adds docker building 2018-03-09 10:12:51 -08:00
sm 0a1b6bbcb3 React to lint errors 2018-03-08 16:31:06 -08:00
sm 2cd78f4310 Adds linting 2018-03-08 16:30:54 -08:00
sm 751a96b142 cmds depend on hw package 2018-03-08 16:30:41 -08:00
sm 75411915a0 some docs 2018-03-08 16:28:56 -08:00
sm ea8a518921 hwl: print git 2018-03-08 16:25:02 -08:00
sm e853273df0 Give some build feedback 2018-03-08 16:24:00 -08:00
sm f3531af469 Adds make clean 2018-03-08 16:23:50 -08:00
sm 1c60f1e0e5 Build for local GOOS
If you want to build for linux, set GOOS
2018-03-08 16:22:19 -08:00
sm 9c946beefb Add git info to build 2018-03-08 16:17:54 -08:00
sm bcbbe000f3 store and report git info 2018-03-08 16:17:11 -08:00
sm 56bd8445a4 Consolidates the V struct 2018-03-08 16:15:44 -08:00
sm ce2faa39eb Add automatic version to build output 2018-03-08 16:09:51 -08:00
sm 2bb100c87b set Version to "unset" so that it's overwritten in make 2018-03-08 15:55:17 -08:00
sm a4987f4f42 Consolidate version location 2018-03-08 14:48:25 -08:00
sm eee330aea5 move version to top of file 2018-03-08 14:45:18 -08:00
sm ea33973994 track success/fails 2018-03-08 14:33:24 -08:00
sm a617999440 hwc should use the hwc prefix in prometheus 2018-03-08 14:33:04 -08:00
sm ce259555bc Update readme 2018-03-08 14:12:08 -08:00
sm 2f382b02a2 Lock metrics at 1.0.0 2018-03-08 14:09:37 -08:00
sm e92f902930 Consolidate OK handler 2018-03-08 14:08:37 -08:00
sm 73f15774d6 move hwc and hwl into this project 2018-03-08 14:07:14 -08:00
11 changed files with 215 additions and 18 deletions
Generated
+1 -1
View File
@@ -70,6 +70,6 @@
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "5dbb4b7db532dc553a8522b23f0913aea04bf84bf96ea48d5c1920637edb6af3" inputs-digest = "6ca9d144505195ce3da40f2598509d79c7205b05dddfa38c4a18acc7200a3755"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1
+1 -1
View File
@@ -30,8 +30,8 @@
version = "0.8.0" version = "0.8.0"
[[constraint]] [[constraint]]
branch = "master"
name = "mcquay.me/metrics" name = "mcquay.me/metrics"
version = "1.0.0"
[prune] [prune]
go-tests = true go-tests = true
+49 -2
View File
@@ -1,5 +1,52 @@
bin/hw: cmd/hw/main.go bin .PHONY: default
GOOS=linux go build -v -o bin/hw ./cmd/hw 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: bin:
mkdir 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/...)
.PHONY: docker-build
docker-build: bin/hw bin/hwc bin/hwl
docker build -f cmd/hw/Dockerfile . -t smcquay/hw:$(VERSION)$(V_DIRTY)
docker build -f cmd/hwc/Dockerfile . -t smcquay/hwc:$(VERSION)$(V_DIRTY)
docker build -f cmd/hwl/Dockerfile . -t smcquay/hwl:$(VERSION)$(V_DIRTY)
.PHONY: docker-push
docker-push:
docker push smcquay/hw:$(VERSION)$(V_DIRTY)
docker push smcquay/hwc:$(VERSION)$(V_DIRTY)
docker push smcquay/hwl:$(VERSION)$(V_DIRTY)
+5 -1
View File
@@ -1,3 +1,7 @@
# hw # 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
View File
+6 -13
View File
@@ -9,16 +9,10 @@ import (
"os" "os"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"mcquay.me/hw"
"mcquay.me/metrics" "mcquay.me/metrics"
) )
const version = "v0.1.0"
type v struct {
Hostname string `json:"hostname"`
V string `json:"version"`
}
func main() { func main() {
m, err := metrics.New("hw") m, err := metrics.New("hw")
if err != nil { if err != nil {
@@ -30,9 +24,10 @@ func main() {
} }
http.HandleFunc("/", m.WrapFunc("/", func(w http.ResponseWriter, req *http.Request) { http.HandleFunc("/", m.WrapFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
r := v{ r := hw.V{
Hostname: hn, Hostname: hn,
V: version, V: hw.Version,
G: hw.Git,
} }
if err := json.NewEncoder(w).Encode(r); err != nil { if err := json.NewEncoder(w).Encode(r); err != nil {
log.Printf("json: %+v", err) log.Printf("json: %+v", err)
@@ -53,12 +48,10 @@ func main() {
code := codes[rand.Intn(len(codes))] code := codes[rand.Intn(len(codes))]
w.WriteHeader(code) w.WriteHeader(code)
})) }))
http.HandleFunc("/live", m.WrapFunc("/live", ok)) http.HandleFunc("/live", m.WrapFunc("/live", hw.OK))
http.HandleFunc("/ready", m.WrapFunc("/ready", ok)) http.HandleFunc("/ready", m.WrapFunc("/ready", hw.OK))
http.Handle("/metrics", promhttp.Handler()) http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(":8080", nil); err != nil { if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("listen and serve: %v", err) log.Fatalf("listen and serve: %v", err)
} }
} }
func ok(w http.ResponseWriter, req *http.Request) {}
+4
View File
@@ -0,0 +1,4 @@
FROM scratch
ADD etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
ADD bin/hwc /bin/
CMD ["/bin/hwc"]
+111
View File
@@ -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)
}
}
+4
View File
@@ -0,0 +1,4 @@
FROM scratch
ADD etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
ADD bin/hwl /bin/
CMD ["/bin/hwl"]
+15
View File
@@ -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)
}
}
+19
View File
@@ -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"`
}