Skip to content

Commit

Permalink
Remove go.mod from subpackages except API and fix Makefile
Browse files Browse the repository at this point in the history
This commit removes the go.mod from the subpackages runtime_scan and
backend as they import each other in multiple places resulting in
managing the same set of dependecies in three different places.

Once these go.mods are removed then the Makefile targets for linting,
fixing and testing can be cleaned up significantly and all packages
treated as one module.

This commit also fixes an issue in the docker targets which results in
the cli and backend containers getting tagged with the same image name
"vmclarity" when they should be "vmclarity-cli" and "vmclarity-backend".

One final change is that the Dockerfiles have been updated to use the
buildkit caches for the go compilations which speed us local docker
build times significantly.
  • Loading branch information
Tehsmash authored and Sam Betts committed Jan 18, 2023
1 parent 76eeeb3 commit 715e4dc
Show file tree
Hide file tree
Showing 29 changed files with 169 additions and 1,556 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
installation
bin
LICENSE
Makefile
README.md
Dockerfile*
31 changes: 9 additions & 22 deletions Dockerfile.backend
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,19 @@ FROM golang:1.19.5-alpine AS builder

RUN apk add --update --no-cache gcc g++ git

# Copy vmclarity code to /build
COPY . /build

# Build backend code
WORKDIR /build/backend

ARG VERSION
ARG BUILD_TIMESTAMP
ARG COMMIT_HASH

# Copy api code
WORKDIR /build
COPY api ./api

# Copy runtime_scan go.mod & go.sum
WORKDIR /build/runtime_scan
COPY runtime_scan/go.* ./
RUN go mod download

# Copy runtime_scan code
WORKDIR /build
COPY runtime_scan ./runtime_scan

# Copy backend go.mod & go.sum
WORKDIR /build/backend
COPY backend/go.* ./
RUN go mod download

# Copy and build backend code
WORKDIR /build/backend
COPY backend .
RUN go build -ldflags="-s -w \
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go build -ldflags="-s -w \
-X 'github.com/openclarity/vmclarity/backend/pkg/version.Version=${VERSION}' \
-X 'github.com/openclarity/vmclarity/backend/pkg/version.CommitHash=${COMMIT_HASH}' \
-X 'github.com/openclarity/vmclarity/backend/pkg/version.BuildTimestamp=${BUILD_TIMESTAMP}'" -o backend ./cmd/backend/main.go
Expand Down
20 changes: 5 additions & 15 deletions Dockerfile.cli
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,11 @@ FROM golang:1.19.5-alpine AS builder
RUN apk add --update --no-cache ca-certificates git
RUN apk add build-base

# Copy go.mod and go.sum
WORKDIR /build
COPY go.* .
# Copy vmclarity code to /build
COPY . /build

# Copy api code
WORKDIR /build/api
COPY api .

# Copy shared code
WORKDIR /build/shared
COPY shared .

# Copy and build cli code
# Build cli code
WORKDIR /build/cli
COPY cli .

ARG COMMIT_HASH

Expand All @@ -31,7 +21,7 @@ FROM ${VMCLARITY_TOOLS_BASE}

WORKDIR /app

COPY --from=builder /build/cli/cli ./cli
COPY --from=builder /build/cli/cli ./vmclarity-cli
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

ENTRYPOINT ["/app/cli"]
ENTRYPOINT ["/app/vmclarity-cli"]
28 changes: 8 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,29 @@ docker-cli: ## Build CLI Docker image
docker build --file ./Dockerfile.cli --build-arg VERSION=${VERSION} \
--build-arg BUILD_TIMESTAMP=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") \
--build-arg COMMIT_HASH=$(shell git rev-parse HEAD) ${VMCLARITY_TOOLS_CLI_DOCKER_ARG} \
-t ${DOCKER_IMAGE}:${DOCKER_TAG} .
-t ${DOCKER_IMAGE}-cli:${DOCKER_TAG} .

.PHONY: push-docker-cli
push-docker-cli: docker-cli ## Build and Push CLI Docker image
@echo "Publishing cli docker image ..."
docker push ${DOCKER_IMAGE}:${DOCKER_TAG}
docker push ${DOCKER_IMAGE}-cli:${DOCKER_TAG}

.PHONY: docker-backend
docker-backend: ## Build Backend Docker image
@(echo "Building backend docker image ..." )
docker build --file ./Dockerfile.backend --build-arg VERSION=${VERSION} \
--build-arg BUILD_TIMESTAMP=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") \
--build-arg COMMIT_HASH=$(shell git rev-parse HEAD) \
-t ${DOCKER_IMAGE}:${DOCKER_TAG} .
-t ${DOCKER_IMAGE}-backend:${DOCKER_TAG} .

.PHONY: push-docker-backend
push-docker-backend: docker-backend ## Build and Push Backend Docker image
@echo "Publishing backend docker image ..."
docker push ${DOCKER_IMAGE}:${DOCKER_TAG}
docker push ${DOCKER_IMAGE}-backend:${DOCKER_TAG}

.PHONY: test
test: ## Run Unit Tests
@(cd backend && go test ./...)
@(cd runtime_scan && go test ./...)
@(cd cli && go test ./...)
@go test ./...

.PHONY: clean-backend
clean-backend:
Expand All @@ -95,16 +93,11 @@ bin/golangci-lint-${GOLANGCI_VERSION}:

.PHONY: lint
lint: bin/golangci-lint ## Run linter
cd backend && ../bin/golangci-lint run
cd runtime_scan && ../bin/golangci-lint run
cd cli && ../bin/golangci-lint run
./bin/golangci-lint run

.PHONY: fix
fix: bin/golangci-lint ## Fix lint violations
./bin/golangci-lint run --fix
cd backend && ../bin/golangci-lint run --fix
cd runtime_scan && ../bin/golangci-lint run --fix
cd cli && ../bin/golangci-lint run --fix

bin/licensei: bin/licensei-${LICENSEI_VERSION}
@ln -sf licensei-${LICENSEI_VERSION} bin/licensei
Expand All @@ -116,22 +109,17 @@ bin/licensei-${LICENSEI_VERSION}:
.PHONY: license-check
license-check: bin/licensei ## Run license check
./bin/licensei header
cd backend && ../bin/licensei check --config=../.licensei.toml
cd runtime_scan && ../bin/licensei check --config=../.licensei.toml

.PHONY: license-cache
license-cache: bin/licensei ## Generate license cache
cd backend && ../bin/licensei cache --config=../.licensei.toml
cd runtime_scan && ../bin/licensei cache --config=../.licensei.toml
./bin/licensei cache --config=../.licensei.toml

.PHONY: check
check: lint test ## Run tests and linters

.PHONY: gomod-tidy
gomod-tidy:
cd backend && go mod tidy
cd runtime_scan && go mod tidy
cd shared && go mod tidy
go mod tidy

.PHONY: api
api: ## Generating API code
Expand Down
83 changes: 0 additions & 83 deletions backend/go.mod

This file was deleted.

Loading

0 comments on commit 715e4dc

Please sign in to comment.