From 1b7699e10cb17f40b5da18b3272d021384ddeaee Mon Sep 17 00:00:00 2001
From: Mario Macias <mmaciasl@redhat.com>
Date: Mon, 7 Mar 2022 11:49:34 +0100
Subject: [PATCH] initial version of the build scripts

---
 .gitignore             | 15 +---------
 .golangci.yml          | 34 +++++++++++++++++++++
 Dockerfile             | 30 +++++++++++++++++++
 Makefile               | 68 ++++++++++++++++++++++++++++++++++++++++++
 README.md              |  3 +-
 cmd/netobserv-agent.go |  7 +++++
 go.mod                 |  3 ++
 7 files changed, 145 insertions(+), 15 deletions(-)
 create mode 100644 .golangci.yml
 create mode 100644 Dockerfile
 create mode 100644 Makefile
 create mode 100644 cmd/netobserv-agent.go
 create mode 100644 go.mod

diff --git a/.gitignore b/.gitignore
index 66fd13c90..a1eb1811f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,15 +1,2 @@
-# Binaries for programs and plugins
-*.exe
-*.exe~
-*.dll
-*.so
-*.dylib
-
-# Test binary, built with `go test -c`
-*.test
-
-# Output of the go coverage tool, specifically when used with LiteIDE
 *.out
-
-# Dependency directories (remove the comment below to include it)
-# vendor/
+bin/
diff --git a/.golangci.yml b/.golangci.yml
new file mode 100644
index 000000000..dd337cd20
--- /dev/null
+++ b/.golangci.yml
@@ -0,0 +1,34 @@
+linters:
+  enable:
+    - deadcode
+    - errcheck
+    - errorlint
+    - cyclop
+    - errname
+    - exhaustive
+    - exportloopref
+    - gocritic
+    - goimports
+    - gosimple
+    - govet
+    - ineffassign
+    - revive
+    - staticcheck
+    - structcheck
+    - stylecheck
+    - typecheck
+    - unused
+    - varcheck
+linters-settings:
+  stylecheck:
+    go: "1.17"
+  gocritic:
+    enabled-checks:
+      - hugeParam
+      - rangeExprCopy
+      - rangeValCopy
+      - indexAlloc
+      - deprecatedComment
+  cyclop:
+    max-complexity: 20
+
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 000000000..ee3cc4d89
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,30 @@
+# Build the manager binary
+FROM registry.access.redhat.com/ubi8/go-toolset:1.16.7-5 as builder
+ARG VERSION="unknown"
+
+WORKDIR /opt/app-root
+
+# TEMPORARY STEPS UNTIL ubi8 releases a go1.17 image
+RUN wget -q https://go.dev/dl/go1.17.8.linux-amd64.tar.gz && tar -xzf go1.17.8.linux-amd64.tar.gz
+ENV GOROOT /opt/app-root/go
+ENV PATH $GOROOT/bin:$PATH
+# END OF LINES TO REMOVE
+
+# Copy the go manifests and source
+COPY go.mod go.mod
+COPY go.sum go.sum
+COPY vendor/ vendor/
+COPY main.go main.go
+COPY cmd/ cmd/
+COPY pkg/ pkg/
+
+# Build
+RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -ldflags "-X main.version=$OPVERSION" -mod vendor -a -o manager main.go
+
+# Create final image from minimal + built binary
+FROM registry.access.redhat.com/ubi8/ubi-minimal:8.5-204
+WORKDIR /
+COPY --from=builder /opt/app-root/netobserv-agent .
+USER 65532:65532
+
+ENTRYPOINT ["/netobserv-agent"]
diff --git a/Makefile b/Makefile
new file mode 100644
index 000000000..39a000b12
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,68 @@
+# VERSION defines the project version for the bundle.
+# Update this value when you upgrade the version of your project.
+# To re-generate a bundle for another specific version without changing the standard setup, you can:
+# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2)
+# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
+VERSION ?= main
+
+# IMAGE_TAG_BASE defines the namespace and part of the image name for remote images.
+# This variable is used to construct full image tags for bundle and catalog images.
+IMAGE_TAG_BASE ?= quay.io/netobserv/netobserv-agent
+
+# Image URL to use all building/pushing image targets
+IMG ?= $(IMAGE_TAG_BASE):$(VERSION)
+
+GOLANGCI_LINT_VERSION = v1.42.1
+
+# Image building tool (docker / podman)
+ifeq (,$(shell which podman 2>/dev/null))
+OCI_BIN=docker
+else
+OCI_BIN=podman
+endif
+
+.PHONY: vendors
+vendors:
+	@echo "### Checking vendors"
+	go mod tidy && go mod vendor
+
+.PHONY: prereqs
+prereqs:
+	@echo "### Check if prerequisites are met, and installing missing dependencies"
+	test -f $(go env GOPATH)/bin/golangci-lint || GOFLAGS="" go install github.com/golangci/golangci-lint/cmd/golangci-lint@${GOLANGCI_LINT_VERSION}
+
+.PHONY: fmt
+fmt: ## Run go fmt against code.
+	@echo "### Formatting code"
+	go fmt ./...
+
+.PHONY: lint
+lint: prereqs
+	@echo "### Linting code"
+	golangci-lint run ./...
+
+.PHONY: build
+build: fmt vendors lint
+	@echo "### Building project"
+	go build -ldflags "-X main.version=${VERSION}" -mod vendor -o bin/netobserv-agent cmd/netobserv-agent.go
+
+.PHONY: test
+test:
+	@echo "### Testing code"
+	go test ./... -coverpkg=./... -coverprofile cover.out
+
+.PHONY: coverage-report
+coverage-report:
+	@echo "### Generating coverage report"
+	go tool cover --func=./cover.out
+
+.PHONY: coverage-report-html
+coverage-report-html:
+	@echo "### Generating HTML coverage report"
+	go tool cover --html=./cover.out
+
+image-build: test ## Build OCI image with the manager.
+	$(OCI_BIN) build --build-arg VERSION="$(VERSION)" -t ${IMG} .
+
+image-push: ## Push OCI image with the manager.
+	$(OCI_BIN) push ${IMG}
\ No newline at end of file
diff --git a/README.md b/README.md
index f4e98d1f0..1b9d663b7 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,3 @@
-# netobserv-agent
+# Network Observability Agent
+
 Network Observability Agent
diff --git a/cmd/netobserv-agent.go b/cmd/netobserv-agent.go
new file mode 100644
index 000000000..8a8c31d34
--- /dev/null
+++ b/cmd/netobserv-agent.go
@@ -0,0 +1,7 @@
+package main
+
+import "fmt"
+
+func main() {
+	fmt.Printf("hello")
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 000000000..d83e2b17e
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module github.com/netobserv/netobserv-agent
+
+go 1.17
-- 
GitLab