Skip to content

Commit

Permalink
Refactor serve and add metrics binary to build and release
Browse files Browse the repository at this point in the history
  • Loading branch information
yitsushi committed Feb 1, 2022
1 parent 5f6e6e8 commit 6ab49a7
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 99 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ jobs:
files: |
bin/flintlockd_amd64
bin/flintlockd_arm64
bin/flintlock-metrics_amd64
bin/flintlock-metrics_arm64
26 changes: 21 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
191 changes: 97 additions & 94 deletions internal/command/metrics/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -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'))
}
}
}

0 comments on commit 6ab49a7

Please sign in to comment.