From ee02eacc5bd4c24a20b522be9f4e7a222981c61d Mon Sep 17 00:00:00 2001 From: gab-arrobo Date: Tue, 5 Mar 2024 14:57:48 -0800 Subject: [PATCH] Refactor Docker image (#35) --- Dockerfile | 26 ++++---- Makefile | 65 +++++++++++++++++-- api/apiserver/api_handler.go | 2 +- {cmd/controller => controller}/controller.go | 0 cmd/metricfunc/metricfunc.go => metricfunc.go | 2 +- 5 files changed, 77 insertions(+), 18 deletions(-) rename {cmd/controller => controller}/controller.go (100%) rename cmd/metricfunc/metricfunc.go => metricfunc.go (98%) diff --git a/Dockerfile b/Dockerfile index 1c064db..d9edf1a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,27 +5,29 @@ FROM golang:1.22.0-bookworm AS builder -LABEL maintainer="ONF " +LABEL maintainer="Aether SD-Core " -RUN apt-get update && apt-get -y install vim - - -RUN cd $GOPATH/src && mkdir -p metricfunc -COPY . $GOPATH/src/metricfunc -RUN cd $GOPATH/src/metricfunc/cmd/metricfunc && CGO_ENABLED=0 go build -mod=mod +WORKDIR $GOPATH/src/metricfunc +COPY . . +RUN make all FROM alpine:3.19 as metricfunc +LABEL description="Aether open source 5G Core Network" \ + version="Stage 3" + ARG DEBUG_TOOLS -# Install debug tools ~ 100MB (if DEBUG_TOOLS is set to true) -RUN apk update && apk add -U vim strace net-tools curl netcat-openbsd bind-tools bash tcpdump + +# Install debug tools ~ 50MB (if DEBUG_TOOLS is set to true) +RUN if [ "$DEBUG_TOOLS" = "true" ]; then \ + apk update && apk add --no-cache -U vim strace net-tools curl netcat-openbsd bind-tools tcpdump; \ + fi # Set working dir -WORKDIR /metricfunc -RUN mkdir -p /metricfunc/bin +WORKDIR /metricfunc/bin # Copy executable -COPY --from=builder /go/src/metricfunc/cmd/metricfunc/metricfunc /metricfunc/bin/ +COPY --from=builder /go/src/metricfunc/bin/* . #Image default directory WORKDIR /metricfunc diff --git a/Makefile b/Makefile index 9501eb6..c3f6787 100644 --- a/Makefile +++ b/Makefile @@ -4,21 +4,60 @@ # PROJECT_NAME := metricfunc -VERSION ?= $(shell cat ./VERSION) +DOCKER_VERSION ?= $(shell cat ./VERSION) ## Docker related DOCKER_REGISTRY ?= DOCKER_REPOSITORY ?= -DOCKER_TAG ?= ${VERSION} +DOCKER_TAG ?= ${DOCKER_VERSION} DOCKER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}${PROJECT_NAME}:${DOCKER_TAG} DOCKER_BUILDKIT ?= 1 DOCKER_BUILD_ARGS ?= +## Docker labels. Only set ref and commit date if committed +DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) +DOCKER_LABEL_VCS_REF ?= $(shell git diff-index --quiet HEAD -- && git rev-parse HEAD || echo "unknown") +DOCKER_LABEL_COMMIT_DATE ?= $(shell git diff-index --quiet HEAD -- && git show -s --format=%cd --date=iso-strict HEAD || echo "unknown" ) DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") DOCKER_TARGETS ?= metricfunc -# https://docs.docker.com/engine/reference/commandline/build/#specifying-target-build-stage---target +GO_BIN_PATH = bin +GO_SRC_PATH = ./ +C_BUILD_PATH = build +ROOT_PATH = $(shell pwd) + +NF = $(GO_NF) +GO_NF = metricfunc + +NF_GO_FILES = $(shell find $(GO_SRC_PATH)/$(%) -name "*.go" ! -name "*_test.go") + +VERSION = $(shell git describe --tags) +BUILD_TIME = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") +COMMIT_HASH = $(shell git submodule status | grep $(GO_SRC_PATH)/$(@F) | awk '{print $$(1)}' | cut -c1-8) +COMMIT_TIME = $(shell cd $(GO_SRC_PATH) && git log --pretty="%ai" -1 | awk '{time=$$(1)"T"$$(2)"Z"; print time}') + +.PHONY: $(NF) clean docker-build docker-push + +.DEFAULT_GOAL: nfs + +nfs: $(NF) + +all: $(NF) + +$(GO_NF): % : $(GO_BIN_PATH)/% + +$(GO_BIN_PATH)/%: %.go $(NF_GO_FILES) +# $(@F): The file-within-directory part of the file name of the target. + @echo "Start building $(@F)...." + cd $(GO_SRC_PATH)/ && \ + CGO_ENABLED=0 go build -o $(ROOT_PATH)/$@ $(@F).go + +vpath %.go $(addprefix $(GO_SRC_PATH)/, $(GO_NF)) + +clean: + rm -rf $(addprefix $(GO_BIN_PATH)/, $(GO_NF)) + rm -rf $(addprefix $(GO_SRC_PATH)/, $(addsuffix /$(C_BUILD_PATH), $(C_NF))) docker-build: @go mod vendor @@ -41,7 +80,25 @@ docker-push: docker push ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}$$target:${DOCKER_TAG}; \ done +.coverage: + rm -rf $(CURDIR)/.coverage + mkdir -p $(CURDIR)/.coverage + +test: .coverage + docker run --rm -v $(CURDIR):/metricfunc -w /metricfunc golang:latest \ + go test \ + -race \ + -failfast \ + -coverprofile=.coverage/coverage-unit.txt \ + -covermode=atomic \ + -v \ + ./ ./... -.PHONY: docker-build docker-push +fmt: + @go fmt ./... +golint: + @docker run --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:latest golangci-lint run -v --config /app/.golangci.yml +check-reuse: + @docker run --rm -v $(CURDIR):/metricfunc -w /metricfunc omecproject/reuse-verify:latest reuse lint diff --git a/api/apiserver/api_handler.go b/api/apiserver/api_handler.go index 8c0dfa1..23a7555 100644 --- a/api/apiserver/api_handler.go +++ b/api/apiserver/api_handler.go @@ -9,7 +9,7 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/omec-project/metricfunc/cmd/controller" + "github.com/omec-project/metricfunc/controller" "github.com/omec-project/metricfunc/internal/metricdata" "github.com/omec-project/metricfunc/logger" "github.com/omec-project/openapi" diff --git a/cmd/controller/controller.go b/controller/controller.go similarity index 100% rename from cmd/controller/controller.go rename to controller/controller.go diff --git a/cmd/metricfunc/metricfunc.go b/metricfunc.go similarity index 98% rename from cmd/metricfunc/metricfunc.go rename to metricfunc.go index a0788f4..c562bf4 100644 --- a/cmd/metricfunc/metricfunc.go +++ b/metricfunc.go @@ -19,8 +19,8 @@ import ( "gopkg.in/yaml.v2" "github.com/omec-project/metricfunc/api/apiserver" - "github.com/omec-project/metricfunc/cmd/controller" "github.com/omec-project/metricfunc/config" + "github.com/omec-project/metricfunc/controller" "github.com/omec-project/metricfunc/internal/promclient" "github.com/omec-project/metricfunc/internal/reader" "github.com/omec-project/metricfunc/logger"