From 6ab49a7188dcf140872bafeb9366e8b411586b57 Mon Sep 17 00:00:00 2001 From: Balazs Nadasdi Date: Tue, 1 Feb 2022 10:31:13 +0100 Subject: [PATCH] Refactor serve and add metrics binary to build and release --- .github/workflows/release.yml | 2 + Makefile | 26 +++- internal/command/metrics/serve.go | 191 +++++++++++++++--------------- 3 files changed, 120 insertions(+), 99 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 55a5b960..2ead75f8 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -76,3 +76,5 @@ jobs: files: | bin/flintlockd_amd64 bin/flintlockd_arm64 + bin/flintlock-metrics_amd64 + bin/flintlock-metrics_arm64 diff --git a/Makefile b/Makefile index de6b2ded..b7bdce32 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ REPO_ROOT := $(shell git rev-parse --show-toplevel) BIN_DIR := bin OUT_DIR := out FLINTLOCKD_CMD := cmd/flintlockd +FLINTLOCK_METRICS_CMD := cmd/flintlock-metrics TOOLS_DIR := hack/tools TOOLS_BIN_DIR := $(TOOLS_DIR)/bin TOOLS_SHARE_DIR := $(TOOLS_DIR)/share @@ -54,13 +55,28 @@ test_image = weaveworks/flintlock-e2e ##@ Build .PHONY: build -build: $(BIN_DIR) ## Build the binaries - go build -o $(BIN_DIR)/flintlockd ./cmd/flintlockd +build: build-flintlockd build-flintlock-metrics ## Build the binaries + +.PHONY: build-flintlockd +build-flintlockd: $(BIN_DIR) ## Build flintlockd binary + go build -o $(BIN_DIR)/flintlockd ./$(FLINTLOCKD_CMD) + +.PHONY: build-flintlock-metrics ## Build flintlock-metrics binary +build-flintlock-metrics: $(BIN_DIR) + go build -o $(BIN_DIR)/flintlock-metrics ./$(FLINTLOCK_METRICS_CMD) .PHONY: build-release -build-release: $(BIN_DIR) ## Build the release binaries - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -o $(BIN_DIR)/flintlockd_amd64 -ldflags "-X $(VERSION_PKG).Version=$(VERSION) -X $(VERSION_PKG).BuildDate=$(BUILD_DATE) -X $(VERSION_PKG).CommitHash=$(GIT_COMMIT)" ./cmd/flintlockd - CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o $(BIN_DIR)/flintlockd_arm64 -ldflags "-X $(VERSION_PKG).Version=$(VERSION) -X $(VERSION_PKG).BuildDate=$(BUILD_DATE) -X $(VERSION_PKG).CommitHash=$(GIT_COMMIT)" ./cmd/flintlockd +build-release: build-release-flintlockd build-release-flintlock-metrics ## Build the release binaries + +.PHONY: build-release-flintlockd +build-release-flintlockd: $(BIN_DIR) ## Build flintlockd release binaries + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -o $(BIN_DIR)/flintlockd_amd64 -ldflags "-X $(VERSION_PKG).Version=$(VERSION) -X $(VERSION_PKG).BuildDate=$(BUILD_DATE) -X $(VERSION_PKG).CommitHash=$(GIT_COMMIT)" ./$(FLINTLOCKD_CMD) + CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o $(BIN_DIR)/flintlockd_arm64 -ldflags "-X $(VERSION_PKG).Version=$(VERSION) -X $(VERSION_PKG).BuildDate=$(BUILD_DATE) -X $(VERSION_PKG).CommitHash=$(GIT_COMMIT)" ./$(FLINTLOCKD_CMD) + +.PHONY: build-release-flintlock-metrics +build-release-flintlock-metrics: $(BIN_DIR) ## Build flintlock-metrics release binaries + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -o $(BIN_DIR)/flintlock-metrics_amd64 -ldflags "-X $(VERSION_PKG).Version=$(VERSION) -X $(VERSION_PKG).BuildDate=$(BUILD_DATE) -X $(VERSION_PKG).CommitHash=$(GIT_COMMIT)" ./$(FLINTLOCK_METRICS_CMD) + CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o $(BIN_DIR)/flintlock-metrics_arm64 -ldflags "-X $(VERSION_PKG).Version=$(VERSION) -X $(VERSION_PKG).BuildDate=$(BUILD_DATE) -X $(VERSION_PKG).CommitHash=$(GIT_COMMIT)" ./$(FLINTLOCK_METRICS_CMD) ##@ Generate diff --git a/internal/command/metrics/serve.go b/internal/command/metrics/serve.go index 84d2f2aa..5c81166d 100644 --- a/internal/command/metrics/serve.go +++ b/internal/command/metrics/serve.go @@ -16,6 +16,8 @@ import ( "github.com/weaveworks/flintlock/internal/inject" ) +type serveFunc func(http.ResponseWriter, *http.Request) + func serveCommand() *cli.Command { cfg := &config.Config{} @@ -43,124 +45,125 @@ func serve(cfg *config.Config) error { router := mux.NewRouter() - router.HandleFunc( - "/machine/uid/{uid}", - func(response http.ResponseWriter, request *http.Request) { - vars := mux.Vars(request) + router.HandleFunc("/machine/uid/{uid}", serveMachineByUID(aports)) + router.HandleFunc("/machine/{namespace}/{name}", serveMachinesByName(aports)) + router.HandleFunc("/machine/{namespace}", serveMachinesByNamespace(aports)) + router.HandleFunc("/machine", serveAllMachines(aports)) - vm, err := aports.Repo.Get(context.Background(), ports.RepositoryGetOptions{ - UID: vars["uid"], - }) - if err != nil { - logrus.Error(err.Error()) - response.WriteHeader(http.StatusInternalServerError) + logrus.Infof("Start listening on %s", cfg.HTTPAPIEndpoint) - return - } + return http.ListenAndServe(cfg.HTTPAPIEndpoint, router) +} - metrics, err := aports.Provider.Metrics(context.Background(), vm.ID) - if err != nil { - logrus.Error(err.Error()) - response.WriteHeader(http.StatusInternalServerError) +func getAllMachineMetrics(ctx context.Context, aports *ports.Collection, query models.ListMicroVMQuery) ([]ports.MachineMetrics, error) { + mms := []ports.MachineMetrics{} - return - } + machines, err := aports.Repo.GetAll(ctx, query) + if err != nil { + return mms, err + } - response.WriteHeader(http.StatusOK) + for _, machine := range machines { + metrics, err := aports.Provider.Metrics(ctx, machine.ID) + if err != nil { + return mms, err + } - _, _ = response.Write(metrics.ToPrometheus()) - }, - ) - - router.HandleFunc( - "/machine/{namespace}/{name}", - func(response http.ResponseWriter, request *http.Request) { - vars := mux.Vars(request) - - mms, err := getAllMachineMetrics( - context.Background(), - aports, - models.ListMicroVMQuery{ - "namespace": vars["namespace"], - "name": vars["name"], - }, - ) - if err != nil { - response.WriteHeader(http.StatusInternalServerError) - _, _ = response.Write([]byte(err.Error())) - - return - } - - response.WriteHeader(http.StatusOK) - - for _, mm := range mms { - _, _ = response.Write(append(mm.ToPrometheus(), '\n')) - } - }, - ) + mms = append(mms, metrics) + } - router.HandleFunc( - "/machine/{namespace}", - func(response http.ResponseWriter, request *http.Request) { - vars := mux.Vars(request) + return mms, nil +} - mms, err := getAllMachineMetrics(context.Background(), aports, models.ListMicroVMQuery{"namespace": vars["namespace"]}) - if err != nil { - response.WriteHeader(http.StatusInternalServerError) - _, _ = response.Write([]byte(err.Error())) +func serveMachineByUID(aports *ports.Collection) serveFunc { + return func(response http.ResponseWriter, request *http.Request) { + vars := mux.Vars(request) - return - } + vm, err := aports.Repo.Get(context.Background(), ports.RepositoryGetOptions{ + UID: vars["uid"], + }) + if err != nil { + logrus.Error(err.Error()) + response.WriteHeader(http.StatusInternalServerError) - response.WriteHeader(http.StatusOK) + return + } - for _, mm := range mms { - _, _ = response.Write(append(mm.ToPrometheus(), '\n')) - } - }, - ) + metrics, err := aports.Provider.Metrics(context.Background(), vm.ID) + if err != nil { + logrus.Error(err.Error()) + response.WriteHeader(http.StatusInternalServerError) - router.HandleFunc( - "/machine", - func(response http.ResponseWriter, request *http.Request) { - mms, err := getAllMachineMetrics(context.Background(), aports, models.ListMicroVMQuery{}) - if err != nil { - response.WriteHeader(http.StatusInternalServerError) - _, _ = response.Write([]byte(err.Error())) + return + } - return - } + response.WriteHeader(http.StatusOK) - response.WriteHeader(http.StatusOK) + _, _ = response.Write(metrics.ToPrometheus()) + } +} - for _, mm := range mms { - _, _ = response.Write(append(mm.ToPrometheus(), '\n')) - } - }, - ) +func serveMachinesByName(aports *ports.Collection) serveFunc { + return func(response http.ResponseWriter, request *http.Request) { + vars := mux.Vars(request) + + mms, err := getAllMachineMetrics( + context.Background(), + aports, + models.ListMicroVMQuery{ + "namespace": vars["namespace"], + "name": vars["name"], + }, + ) + if err != nil { + response.WriteHeader(http.StatusInternalServerError) + _, _ = response.Write([]byte(err.Error())) - logrus.Infof("Start listening on %s", cfg.HTTPAPIEndpoint) + return + } - return http.ListenAndServe(cfg.HTTPAPIEndpoint, router) + response.WriteHeader(http.StatusOK) + + for _, mm := range mms { + _, _ = response.Write(append(mm.ToPrometheus(), '\n')) + } + } } -func getAllMachineMetrics(ctx context.Context, aports *ports.Collection, query models.ListMicroVMQuery) ([]ports.MachineMetrics, error) { - mms := []ports.MachineMetrics{} +func serveMachinesByNamespace(aports *ports.Collection) serveFunc { + return func(response http.ResponseWriter, request *http.Request) { + vars := mux.Vars(request) - machines, err := aports.Repo.GetAll(ctx, query) - if err != nil { - return mms, err + mms, err := getAllMachineMetrics(context.Background(), aports, models.ListMicroVMQuery{"namespace": vars["namespace"]}) + if err != nil { + response.WriteHeader(http.StatusInternalServerError) + _, _ = response.Write([]byte(err.Error())) + + return + } + + response.WriteHeader(http.StatusOK) + + for _, mm := range mms { + _, _ = response.Write(append(mm.ToPrometheus(), '\n')) + } } +} - for _, machine := range machines { - metrics, err := aports.Provider.Metrics(ctx, machine.ID) +func serveAllMachines(aports *ports.Collection) serveFunc { + return func(response http.ResponseWriter, request *http.Request) { + mms, err := getAllMachineMetrics(context.Background(), aports, models.ListMicroVMQuery{}) if err != nil { - return mms, err + response.WriteHeader(http.StatusInternalServerError) + _, _ = response.Write([]byte(err.Error())) + + return } - mms = append(mms, metrics) - } + response.WriteHeader(http.StatusOK) - return mms, nil + for _, mm := range mms { + _, _ = response.Write(append(mm.ToPrometheus(), '\n')) + } + } }