Skip to content

Commit

Permalink
backend skeleteon (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
fishkerez committed Nov 21, 2022
1 parent cc41f0d commit 03c2625
Show file tree
Hide file tree
Showing 14 changed files with 1,112 additions and 81 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:
with:
go-version: ${{ env.GO_VERSION }}

- name: Check licenses
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: make license-check
# - name: Check licenses
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# run: make license-check

- name: Run verification
run: make check
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable-build-and-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
with:
context: .
tags: ghcr.io/openclarity/vmclarity:${{ inputs.image_tag }}
file: Dockerfile
file: Dockerfile.backend
push: ${{ inputs.push }}
outputs: "${{ env.OUTPUTS }}"
cache-from: type=local,src=/tmp/.buildx-cache
Expand Down
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ linters-settings:
errorlint:
# Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats
errorf: false
gomoddirectives:
# Allow local `replace` directives. Default is false.
replace-local: true
# List of allowed `replace` directives. Default is empty.
replace-allow-list:

gosec:
# To specify a set of rules to explicitly exclude.
Expand Down
15 changes: 14 additions & 1 deletion .licensei.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ approved = [
]

ignored = [

"github.com/russross/blackfriday/v2", # Simplified BSD License - https://github.com/russross/blackfriday/blob/master/LICENSE.txt
"github.com/jmespath/go-jmespath", # Apache 2.0 - https://github.com/jmespath/go-jmespath/blob/master/LICENSE
"github.com/aws/aws-sdk-go-v2", # Apache 2.0 - https://github.com/aws/aws-sdk-go-v2/blob/main/LICENSE.txt
"github.com/aws/aws-sdk-go-v2/credentials", # Apache 2.0 - https://github.com/aws/aws-sdk-go-v2/blob/main/credentials/LICENSE.txt
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds", # Apache 2.0 - https://github.com/aws/aws-sdk-go-v2/blob/main/feature/ec2/imds/LICENSE.txt
"github.com/aws/aws-sdk-go-v2/internal/configsources", # Apache 2.0 - https://github.com/aws/aws-sdk-go-v2/blob/main/internal/configsources/LICENSE.txt
"github.com/aws/aws-sdk-go-v2/internal/endpoints/v2:", # Apache 2.0 - https://github.com/aws/aws-sdk-go-v2/blob/main/internal/endpoints/v2/LICENSE.txt
"github.com/aws/aws-sdk-go-v2/service/sso", # Apache 2.0 - https://github.com/aws/aws-sdk-go-v2/blob/main/service/sso/LICENSE.txt
"github.com/aws/aws-sdk-go-v2/service/ssooidc", # Apache 2.0 - https://github.com/aws/aws-sdk-go-v2/blob/main/service/ssooidc/LICENSE.txt
"github.com/aws/aws-sdk-go-v2/service/internal/presigned-url", # Apache 2.0 - https://github.com/aws/aws-sdk-go-v2/blob/main/service/internal/presigned-url/LICENSE.txt
"github.com/aws/aws-sdk-go-v2/service/sts", # Apache 2.0 - https://github.com/aws/aws-sdk-go-v2/blob/main/service/sts/LICENSE.txt
"github.com/aws/aws-sdk-go-v2/internal/ini", # Apache 2.0 - https://github.com/aws/aws-sdk-go-v2/blob/main/internal/ini/LICENSE.txt
"github.com/aws/aws-sdk-go-v2/config", # Apache 2.0 - https://github.com/aws/aws-sdk-go-v2/blob/main/config/LICENSE.txt
"github.com/aws/aws-sdk-go-v2/service/ec2", # Apache 2.0 - https://github.com/aws/aws-sdk-go-v2/blob/main/service/ec2/LICENSE.txt
]

[header]
Expand Down
33 changes: 0 additions & 33 deletions Dockerfile

This file was deleted.

37 changes: 37 additions & 0 deletions Dockerfile.backend
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM golang:1.19.3-alpine AS builder

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

ARG VERSION
ARG BUILD_TIMESTAMP
ARG COMMIT_HASH

# 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 \
-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

FROM alpine:3.16

WORKDIR /app

COPY --from=builder ["/build/backend/backend", "./backend"]

ENTRYPOINT ["/app/backend"]
51 changes: 35 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,46 @@ help: ## This help.
.DEFAULT_GOAL := help

.PHONY: build
build: ## Build VMClarity
@(echo "Building VMClarity" )
@(go build -ldflags="-s -w \
-X 'github.com/openclarity/vmclarity/pkg/version.Version=${VERSION}'" -o bin/${BINARY_NAME} ./main.go \
&& ls -lh bin/${BINARY_NAME})
build: build-all-go ## Build All

.PHONY: build-all-go
build-all-go: backend ## Build All GO executables

.PHONY: backend
backend: ## Build Backend
@(echo "Building Backend ..." )
@(cd backend && go build -o bin/backend cmd/backend/main.go && ls -l bin/)

.PHONY: docker
docker: ## Build VMClarity docker image
@(echo "Building VMClarity docker image ..." )
docker build --file ./Dockerfile --build-arg VERSION=${VERSION} \
docker: docker-backend ## Build All Docker images

.PHONY: push-docker
push-docker: push-docker-backend ## Build and Push All Docker images

.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} .

.PHONY: push-docker
push-docker: docker ## Build and Push VMClarity docker image
@echo "Publishing VMClarity Docker image ..."
.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}

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

.PHONY: clean-backend
clean-backend:
@(rm -rf backend/bin ; echo "Backend cleanup done" )

.PHONY: clean
clean: ## Clean all build artifacts
clean: clean-backend ## Clean all build artifacts

bin/golangci-lint: bin/golangci-lint-${GOLANGCI_VERSION}
@ln -sf golangci-lint-${GOLANGCI_VERSION} bin/golangci-lint
Expand All @@ -57,12 +71,13 @@ bin/golangci-lint-${GOLANGCI_VERSION}:

.PHONY: lint
lint: bin/golangci-lint ## Run linter
./bin/golangci-lint run
cd backend && ../bin/golangci-lint run
cd runtime_scan && ../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

bin/licensei: bin/licensei-${LICENSEI_VERSION}
Expand All @@ -75,14 +90,18 @@ bin/licensei-${LICENSEI_VERSION}:
.PHONY: license-check
license-check: bin/licensei ## Run license check
./bin/licensei header
cd backend/pkg && ../../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
./bin/licensei cache
cd backend/pkg && ../../bin/licensei cache --config=../../.licensei.toml
cd runtime_scan && ../bin/licensei cache --config=../.licensei.toml

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

.PHONY: gomod-tidy
gomod-tidy:
cd runtime_scan && go mod tidy
cd backend && go mod tidy
cd runtime_scan && go mod tidy
80 changes: 80 additions & 0 deletions backend/cmd/backend/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright © 2022 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"os"

logutils "github.com/Portshift/go-utils/log"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/urfave/cli"

"github.com/openclarity/vmclarity/backend/pkg/backend"
"github.com/openclarity/vmclarity/backend/pkg/config"
"github.com/openclarity/vmclarity/backend/pkg/version"
)

func run(c *cli.Context) {
logutils.InitLogs(c, os.Stdout)
backend.Run()
}

func versionCommand(_ *cli.Context) {
fmt.Printf("Version: %s \nCommit: %s\nBuild Time: %s",
version.Version, version.CommitHash, version.BuildTimestamp)
}

func main() {
viper.SetDefault(config.HealthCheckAddress, ":8081")
viper.SetDefault(config.BackendRestPort, "8080")
viper.AutomaticEnv()
app := cli.NewApp()
app.Usage = ""
app.Name = "VMClarity"
app.Version = version.Version

runCommand := cli.Command{
Name: "run",
Usage: "Starts VMClarity",
Action: run,
Flags: []cli.Flag{
cli.StringFlag{
Name: logutils.LogLevelFlag,
Value: logutils.LogLevelDefaultValue,
Usage: logutils.LogLevelFlagUsage,
},
},
}
runCommand.UsageText = runCommand.Name

versionCommand := cli.Command{
Name: "version",
Usage: "VMClarity Version Details",
Action: versionCommand,
}
versionCommand.UsageText = versionCommand.Name

app.Commands = []cli.Command{
runCommand,
versionCommand,
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
52 changes: 52 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module github.com/openclarity/vmclarity/backend

go 1.19

require (
github.com/Portshift/go-utils v0.0.0-20220421083203-89265d8a6487
github.com/openclarity/vmclarity/runtime_scan v0.0.0
github.com/sirupsen/logrus v1.9.0
github.com/spf13/viper v1.14.0
github.com/urfave/cli v1.22.10
)

require (
github.com/aws/aws-sdk-go-v2 v1.17.1 // indirect
github.com/aws/aws-sdk-go-v2/config v1.17.10 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.12.23 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.19 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.25 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.19 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.26 // indirect
github.com/aws/aws-sdk-go-v2/service/ec2 v1.66.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.19 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.11.25 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.13.8 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.17.1 // indirect
github.com/aws/smithy-go v1.13.4 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/grpc v1.50.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

// nolint:gomoddirectives
replace github.com/openclarity/vmclarity/runtime_scan v0.0.0 => ./../runtime_scan
Loading

0 comments on commit 03c2625

Please sign in to comment.