Skip to content

Commit

Permalink
cmd/kubernetes-static: use go:embed for serving static data
Browse files Browse the repository at this point in the history
This way, binary is independent from host file system and it's only
important during build.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
  • Loading branch information
invidian committed Jul 9, 2021
1 parent 8030043 commit 6968c22
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
Binary file removed cmd/kubernetes-static/config_example.png
Binary file not shown.
21 changes: 12 additions & 9 deletions cmd/kubernetes-static/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"errors"
"embed"
"fmt"
"io/fs"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -37,15 +38,19 @@ type argumentList struct {

var args argumentList

func main() {
// Embed static metrics into binary.
//go:embed data
var content embed.FS

func main() {
// Determines which subdirectory of cmd/kubernetes-static/ to use
// for serving the static metrics
k8sMetricsVersion := os.Getenv("K8S_METRICS_VERSION")
if k8sMetricsVersion == "" {
k8sMetricsVersion = "1_18"
}
endpoint := startStaticMetricsServer(k8sMetricsVersion)

endpoint := startStaticMetricsServer(content, k8sMetricsVersion)

// let the http server start...
time.Sleep(time.Millisecond * 100)
Expand Down Expand Up @@ -149,7 +154,7 @@ func main() {
fmt.Println()
}

func startStaticMetricsServer(k8sMetricsVersion string) string {
func startStaticMetricsServer(content embed.FS, k8sMetricsVersion string) string {
// This will allocate a random port
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
Expand All @@ -161,14 +166,12 @@ func startStaticMetricsServer(k8sMetricsVersion string) string {

mux := http.NewServeMux()

dataDir := fmt.Sprintf("./cmd/kubernetes-static/data/%s", k8sMetricsVersion)

path, err := filepath.Abs(dataDir)
k8sContent, err := fs.Sub(content, filepath.Join("data", k8sMetricsVersion))
if err != nil {
log.Fatal(errors.New("cannot start server"))
panic(err)
}

mux.Handle("/", http.FileServer(http.Dir(path)))
mux.Handle("/", http.FileServer(http.FS(k8sContent)))
go func() {
logrus.Fatal(http.Serve(listener, mux))
}()
Expand Down
11 changes: 0 additions & 11 deletions cmd/kubernetes-static/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,3 @@ go run cmd/kubernetes-static/main.go cmd/kubernetes-static/basic_http_client.go
```

This is not sending any data to an agent, but outputs the JSON to stdout.

## Configuring your IDE

It's import that the working directory is set to root of the repository, because it expects the `data` folder to be
in the `cmd/kubernetes-static` directory relative to the working directory.

Some IDE's/editor use a temporary folder as the working directory.

Example configuration for GoLand:
![Goland configuration example](./config_example.png)

0 comments on commit 6968c22

Please sign in to comment.