Skip to content

Commit

Permalink
k8s: Add test webserver (#908)
Browse files Browse the repository at this point in the history
This change adds a test webserver that helps validate:
* external access works as expected
* metrics can be collected

It may be extended in the future to allow for testing gRPC, etc.

Jira: INFRA-5878
  • Loading branch information
minor-fixes authored May 5, 2023
1 parent e58ae16 commit f79ad6e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
1 change: 0 additions & 1 deletion bb_reporter/reporter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ go_library(
"@com_github_buildbarn_bb_remote_execution//pkg/proto/completedactionlogger",
"@com_github_buildbarn_bb_remote_execution//pkg/proto/resourceusage",
"@com_github_golang_glog//:go_default_library",
"@com_github_kylelemons_godebug//pretty:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
"@com_github_prometheus_client_golang//prometheus/promauto:go_default_library",
"@com_google_cloud_go_bigquery//:go_default_library",
Expand Down
49 changes: 49 additions & 0 deletions infra/k8s_dummy/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_push")
load("@rules_pkg//:pkg.bzl", "pkg_tar")

go_library(
name = "go_default_library",
srcs = ["main.go"],
importpath = "github.com/enfabrica/enkit/infra/k8s_dummy",
visibility = ["//visibility:private"],
deps = [
"//lib/server:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
"@com_github_prometheus_client_golang//prometheus/promauto:go_default_library",
"@com_github_prometheus_client_golang//prometheus/promhttp:go_default_library",
],
)

go_binary(
name = "k8s_dummy",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)

pkg_tar(
name = "k8s_dummy_tar",
srcs = [":k8s_dummy"],
package_dir = "/enfabrica/bin",
)

container_image(
name = "k8s_dummy_image",
base = "@golang_base//image",
cmd = [
"/enfabrica/bin/k8s_dummy",
],
tars = [
":k8s_dummy_tar",
],
)

container_push(
name = "k8s_dummy_image_push",
format = "Docker",
image = ":k8s_dummy_image",
registry = "gcr.io",
repository = "devops-284019/infra/k8s_dummy",
# TODO: Change this tag to "live"
tag = "testing",
)
40 changes: 40 additions & 0 deletions infra/k8s_dummy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Webserver that exposes metrics for testing k8s setups
package main

import (
"flag"
"fmt"
"net/http"

"github.com/enfabrica/enkit/lib/server"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
metricPathVisits = promauto.NewCounterVec(prometheus.CounterOpts{
Subsystem: "k8s_dummy",
Name: "path_visit_count",
},
[]string{
"path",
},
)
)

func printPath(w http.ResponseWriter, r *http.Request) {
metricPathVisits.WithLabelValues(r.URL.Path).Inc()
w.WriteHeader(200)
fmt.Fprintf(w, "You reached page: %s\n", r.URL.Path)
}

func main() {
flag.Parse()
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/printpath/", printPath)

server.Run(mux, nil, nil)
}

0 comments on commit f79ad6e

Please sign in to comment.