Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Build the manager binary
FROM registry.hub.docker.com/library/golang:1.14 AS builder

WORKDIR /workspace

# Bring in the go dependencies before anything else so we can take
# advantage of caching these layers in future builds.
COPY go.mod go.mod
COPY go.sum go.sum
RUN go mod download
Comment thread
dhellmann marked this conversation as resolved.

COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o baremetal-operator main.go

# Copy the controller-manager into a thin image
# BMO has a dependency preventing us to use the static one,
# using the base one instead
FROM gcr.io/distroless/base:latest
Comment thread
zaneb marked this conversation as resolved.
WORKDIR /
COPY --from=builder /workspace/baremetal-operator .
Comment thread
dhellmann marked this conversation as resolved.
USER nonroot:nonroot
ENTRYPOINT ["/baremetal-operator"]

LABEL io.k8s.display-name="Metal3 BareMetal Operator" \
io.k8s.description="This is the image for the Metal3 BareMetal Operator."
259 changes: 153 additions & 106 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
TEST_NAMESPACE = operator-test
RUN_NAMESPACE = metal3
GO_TEST_FLAGS = $(VERBOSE)
DEBUG = --debug
SETUP = --no-setup
CODE_DIRS = ./cmd ./pkg ./version
PACKAGES = $(foreach dir,$(CODE_DIRS),$(dir)/...)
COVER_PROFILE = cover.out

# CRD Generation Options
#
# trivialVersions=false means generate the CRD with multiple versions,
# eventually allowing conversion webhooks
# allowDangerousTypes=true lets use the float64 field for clock speeds
# crdVersions=v1 generates the v1 version of the CRD type
#
# NOTE: Assumes the default is preserveUnknownFields=false with
# crdVersions=v1, so that the API server discards "extra" data
# instead of storing it
#
CRD_OPTIONS ?= "crd:trivialVersions=false,allowDangerousTypes=true,crdVersions=v1"
CONTROLLER_TOOLS_VERSION=v0.4.0

# Directories.
TOOLS_DIR := tools
TOOLS_BIN_DIR := $(TOOLS_DIR)/bin
Expand Down Expand Up @@ -38,153 +48,181 @@ help: ## Display this help
@echo " GO_TEST_FLAGS -- flags to pass to --go-test-flags ($(GO_TEST_FLAGS))"
@echo " DEBUG -- debug flag, if any ($(DEBUG))"

.PHONY: $(KUSTOMIZE)
$(KUSTOMIZE):
cd $(TOOLS_DIR); ./install_kustomize.sh
# Image URL to use all building/pushing image targets
IMG ?= baremetal-operator:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"

.PHONY: test
test: fmt generate lint vet unit ## Run common developer tests
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

.PHONY: show_packages show_dirs
show_packages:
@echo $(PACKAGES)
show_dirs:
@echo $(CODE_DIRS)
## --------------------------------------
## Test Targets
## --------------------------------------

.PHONY: generate
generate: bin/operator-sdk ## Run the operator-sdk code generator
./bin/operator-sdk generate $(VERBOSE) k8s
./bin/operator-sdk generate $(VERBOSE) crds --crd-version=v1
openapi-gen \
--input-dirs ./pkg/apis/metal3/v1alpha1 \
--output-package ./pkg/apis/metal3/v1alpha1 \
--output-base "" \
--output-file-base zz_generated.openapi \
--report-filename "-" \
--go-header-file /dev/null

bin/operator-sdk: bin
make -C tools/operator-sdk install

bin:
mkdir -p bin

.PHONY: travis
travis: unit-verbose lint
# Run tests
.PHONY: test
test: generate fmt lint vet manifests unit ## Run common developer tests

.PHONY: unit
unit: ## Run unit tests
go test $(GO_TEST_FLAGS) $(PACKAGES)
go test ./... $(VERBOSE) -coverprofile $(COVER_PROFILE)

.PHONY: unit-cover
unit-cover: ## Run unit tests with code coverage
go test -coverprofile=$(COVER_PROFILE) $(GO_TEST_FLAGS) $(PACKAGES)
go test -coverprofile=$(COVER_PROFILE) $(GO_TEST_FLAGS) ./...
go tool cover -func=$(COVER_PROFILE)

.PHONY: unit-cover-html
unit-cover-html:
go test -coverprofile=cover.out $(GO_TEST_FLAGS) $(PACKAGES)
go tool cover -html=cover.out

.PHONY: unit-verbose
unit-verbose: ## Run unit tests with verbose output
VERBOSE=-v make unit

## --------------------------------------
## Linter Targets
## --------------------------------------

.PHONY: linters
linters: sec lint generate-check fmt-check vet ## Run all linters

.PHONY: vet
vet: ## Run go vet
go vet $(PACKAGES)

.PHONY: lint
lint: golint-binary ## Run golint
find $(CODE_DIRS) -type f -name \*.go |grep -v zz_ | xargs -L1 golint -set_exit_status

.PHONY: generate-check
generate-check:
./hack/generate.sh

.PHONY: generate-check-local
generate-check-local:
IS_CONTAINER=local ./hack/generate.sh

.PHONY: sec
sec: $(GOPATH)/bin/gosec ## Run gosec
gosec -severity medium --confidence medium -quiet $(PACKAGES)
gosec -severity medium --confidence medium -quiet ./...

$(GOPATH)/bin/gosec:
go get -u github.com/securego/gosec/cmd/gosec

.PHONY: fmt
fmt: ## Run go fmt against code
Comment thread
dhellmann marked this conversation as resolved.
go fmt ./...

.PHONY: vet
vet: ## Run go vet against code
go vet ./...

.PHONY: lint
lint: golint-binary ## Run golint
find $(CODE_DIRS) -type f -name \*.go |grep -v zz_ | xargs -L1 golint -set_exit_status

.PHONY: golint-binary
golint-binary:
which golint 2>&1 >/dev/null || $(MAKE) $(GOPATH)/bin/golint
$(GOPATH)/bin/golint:
go get -u golang.org/x/lint/golint

.PHONY: fmt
fmt: ## Run gofmt and write changes to each file
gofmt -l -w $(CODE_DIRS)

.PHONY: fmt-check
fmt-check: ## Run gofmt and report an error if any changes are made
./hack/gofmt.sh

.PHONY: docs
docs: $(patsubst %.dot,%.png,$(wildcard docs/*.dot))
## --------------------------------------
## Build/Run Targets
## --------------------------------------

%.png: %.dot
dot -Tpng $< >$@
.PHONY: build
build: generate manifests manager tools ## Build everything

.PHONY: e2e-local
e2e-local:
operator-sdk test local ./test/e2e \
--namespace $(TEST_NAMESPACE) \
--up-local $(SETUP) \
$(DEBUG) --go-test-flags "$(GO_TEST_FLAGS)"
.PHONY: manager
manager: generate fmt vet ## Build manager binary
go build -ldflags $(LDFLAGS) -o bin/manager main.go

.PHONY: run
run: ## Run the operator outside of a cluster in development mode
operator-sdk run --local \
--go-ldflags=$(LDFLAGS) \
--watch-namespace=$(RUN_NAMESPACE) \
--operator-flags="-dev"
run: generate fmt vet manifests ## Run against the configured Kubernetes cluster in ~/.kube/config
go run -ldflags $(LDFLAGS) ./main.go -namespace=$(RUN_NAMESPACE) -dev

.PHONY: demo
demo: ## Run the operator outside of a cluster using the demo driver
operator-sdk run --local \
--go-ldflags=$(LDFLAGS) \
--watch-namespace=$(RUN_NAMESPACE) \
--operator-flags="-dev -demo-mode"
demo: generate fmt vet manifests ## Run in demo mode
go run -ldflags $(LDFLAGS) ./main.go -namespace=$(RUN_NAMESPACE) -dev -demo-mode

.PHONY: install
install: $(KUSTOMIZE) manifests ## Install CRDs into a cluster
$(KUSTOMIZE) build config/crd | kubectl apply -f -

.PHONY: uninstall
uninstall: $(KUSTOMIZE) manifests ## Uninstall CRDs from a cluster
kustomize build config/crd | kubectl delete -f -

.PHONY: deploy
deploy: $(KUSTOMIZE) manifests ## Deploy controller in the configured Kubernetes cluster in ~/.kube/config
cd config/manager && kustomize edit set image controller=${IMG}
kustomize build config/default | kubectl apply -f -

.PHONY: manifests
manifests: controller-gen ## Generate manifests e.g. CRD, RBAC etc.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases

.PHONY: generate
generate: controller-gen ## Generate code
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

.PHONY: $(KUSTOMIZE)
$(KUSTOMIZE):
cd $(TOOLS_DIR); ./install_kustomize.sh

# find or download controller-gen
# download controller-gen if necessary
.PHONY: controller-gen
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
Comment on lines +164 to +176

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not ensure that we are running with a specific version of controller-gen. If the user has an outdated controller-gen installed on his machine, it might result in confusing behaviour. Should the executable be placed in a dedicated location, and referred as such, like Kustomize ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed this today in the community meeting and agreed that I will make this change as a follow-up PR.


## --------------------------------------
## Docker Targets
## --------------------------------------

.PHONY: docker
docker: docker-operator docker-sdk docker-golint ## Build docker images
docker: test ## Build the docker image
docker build . -t ${IMG}

.PHONY: docker-operator
docker-operator:
docker build . -f build/Dockerfile
# Push the docker image
.PHONY: docker-push
docker-push:
docker push ${IMG}

.PHONY: docker-sdk
docker-sdk:
docker build . -f hack/Dockerfile.operator-sdk
## --------------------------------------
## CI Targets
## --------------------------------------

.PHONY: docker-golint
docker-golint:
docker build . -f hack/Dockerfile.golint
.PHONY: generate-check
generate-check:
./hack/generate.sh

.PHONY: build
build: ## Build the operator binary
@echo LDFLAGS=$(LDFLAGS)
go build -ldflags $(LDFLAGS) -o build/_output/bin/baremetal-operator cmd/manager/main.go
.PHONY: generate-check-local
generate-check-local:
IS_CONTAINER=local ./hack/generate.sh

.PHONY: fmt-check
fmt-check: ## Run gofmt and report an error if any changes are made
./hack/gofmt.sh

## --------------------------------------
## Documentation
## --------------------------------------

.PHONY: docs
docs: $(patsubst %.dot,%.png,$(wildcard docs/*.dot))

%.png: %.dot
dot -Tpng $< >$@

## --------------------------------------
## Tool apps
## --------------------------------------

.PHONY: tools
tools:
go build -o build/_output/bin/get-hardware-details cmd/get-hardware-details/main.go

.PHONY: deploy
deploy:
cd deploy && kustomize edit set namespace $(RUN_NAMESPACE) && cd ..
kustomize build deploy | kubectl apply -f -
go build -o bin/get-hardware-details cmd/get-hardware-details/main.go
go build -o bin/make-bm-worker cmd/make-bm-worker/main.go
go build -o bin/make-virt-host cmd/make-virt-host/main.go

## --------------------------------------
## Tilt / Kind
Expand All @@ -201,3 +239,12 @@ tilt-up: $(KUSTOMIZE) kind-create ## start tilt and build kind cluster if needed
.PHONY: kind-reset
kind-reset: ## Destroys the "bmo" kind cluster.
kind delete cluster --name=bmo || true

## --------------------------------------
## Go module Targets
## --------------------------------------

.PHONY:
mod: ## Clean up go module settings
go mod tidy
go mod verify
7 changes: 7 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
multigroup: true
repo: github.com/metal3-io/baremetal-operator
resources:
- group: metal3.io
kind: BareMetalHost
version: v1alpha1
version: "2"
2 changes: 1 addition & 1 deletion Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def validate_auth():

tilt_helper_dockerfile_header = """
# Tilt image
FROM golang:1.13.15 as tilt-helper
FROM golang:1.14 as tilt-helper
# Support live reloading with Tilt
RUN wget --output-document /restart.sh --quiet https://raw.githubusercontent.com/windmilleng/rerun-process-wrapper/master/restart.sh && \
wget --output-document /start.sh --quiet https://raw.githubusercontent.com/windmilleng/rerun-process-wrapper/master/start.sh && \
Expand Down
Loading