Skip to content

Commit

Permalink
Refactor Docker image (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
gab-arrobo authored Mar 5, 2024
1 parent 55995a5 commit ee02eac
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 18 deletions.
26 changes: 14 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,29 @@

FROM golang:1.22.0-bookworm AS builder

LABEL maintainer="ONF <omec-dev@opennetworking.org>"
LABEL maintainer="Aether SD-Core <dev@aetherproject.org>"

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
65 changes: 61 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion api/apiserver/api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/metricfunc/metricfunc.go → metricfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ee02eac

Please sign in to comment.