forked from openshift/compliance-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
258 lines (213 loc) · 9.53 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Operator variables
# ==================
export APP_NAME=compliance-operator
RESULTSCOLLECTORBIN=resultscollector
OPENSCAP_IMAGE_NAME=openscap-ocp
RESULTSCOLLECTOR_IMAGE_NAME=$(RESULTSCOLLECTORBIN)
# Container image variables
# =========================
IMAGE_REPO?=quay.io/jhrozek
RUNTIME?=podman
# Image path to use. Set this if you want to use a specific path for building
# or your e2e tests. This is overwritten if we bulid the image and push it to
# the cluster or if we're on CI.
IMAGE_PATH?=$(IMAGE_REPO)/$(APP_NAME)
OPENSCAP_IMAGE_PATH=$(IMAGE_REPO)/$(OPENSCAP_IMAGE_NAME)
OPENSCAP_DOCKERFILE_PATH=./images/openscap/Dockerfile
RESULTSCOLLECTOR_IMAGE_PATH=$(IMAGE_REPO)/$(RESULTSCOLLECTOR_IMAGE_NAME)
RESULTSCOLLECTOR_DOCKERFILE_PATH=./images/openscap/Dockerfile
# Image tag to use. Set this if you want to use a specific tag for building
# or your e2e tests.
TAG?=latest
# Build variables
# ===============
CURPATH=$(PWD)
TARGET_DIR=$(CURPATH)/build/_output
GO=GOFLAGS=-mod=vendor GO111MODULE=auto go
GOBUILD=$(GO) build
BUILD_GOPATH=$(TARGET_DIR):$(CURPATH)/cmd
TARGET=$(TARGET_DIR)/bin/$(APP_NAME)
RESULTSCOLLECTOR_TARGET=$(TARGET_DIR)/bin/$(RESULTSCOLLECTORBIN)
MAIN_PKG=cmd/manager/main.go
PKGS=$(shell go list ./... | grep -v -E '/vendor/|/test|/examples')
# This is currently hardcoded to our most performance sensitive package
BENCHMARK_PKG?=github.com/openshift/compliance-operator/pkg/utils
# go source files, ignore vendor directory
SRC = $(shell find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./_output/*")
# Kubernetes variables
# ====================
KUBECONFIG?=$(HOME)/.kube/config
export NAMESPACE?=openshift-compliance
# Operator-sdk variables
# ======================
SDK_VERSION?=v0.13.0
OPERATOR_SDK_URL=https://github.com/operator-framework/operator-sdk/releases/download/$(SDK_VERSION)/operator-sdk-$(SDK_VERSION)-x86_64-linux-gnu
# Test variables
# ==============
TEST_OPTIONS?=
# Skip pushing the container to your cluster
E2E_SKIP_CONTAINER_PUSH?=false
# Pass extra flags to the e2e test run.
# e.g. to run a specific test in the e2e test suite, do:
# make e2e E2E_GO_TEST_FLAGS="-v -run TestE2E/TestScanWithNodeSelectorFiltersCorrectly"
E2E_GO_TEST_FLAGS?=-v -timeout 120m
.PHONY: all
all: build ## Test and Build the compliance-operator
.PHONY: help
help: ## Show this help screen
@echo 'Usage: make <OPTIONS> ... <TARGETS>'
@echo ''
@echo 'Available targets are:'
@echo ''
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
.PHONY: image
image: fmt operator-sdk operator-image resultscollector-image openscap-image ## Build the compliance-operator container image
.PHONY: operator-image
operator-image:
$(GOPATH)/bin/operator-sdk build $(IMAGE_PATH) --image-builder $(RUNTIME)
.PHONY: openscap-image
openscap-image:
$(RUNTIME) build -f $(OPENSCAP_DOCKERFILE_PATH) -t $(OPENSCAP_IMAGE_PATH):$(TAG)
.PHONY: resultscollector-image
resultscollector-image:
$(RUNTIME) build -f $(RESULTSCOLLECTOR_DOCKERFILE_PATH) -t $(RESULTSCOLLECTOR_IMAGE_PATH):$(TAG)
.PHONY: build
build: manager resultscollector ## Build the compliance-operator binary
manager:
$(GO) build -o $(TARGET) github.com/openshift/compliance-operator/cmd/manager
resultscollector:
$(GO) build -o $(RESULTSCOLLECTOR_TARGET) github.com/openshift/compliance-operator/cmd/resultscollector
.PHONY: operator-sdk
operator-sdk:
ifeq ("$(wildcard $(GOPATH)/bin/operator-sdk)","")
wget -nv $(OPERATOR_SDK_URL) -O $(GOPATH)/bin/operator-sdk || (echo "wget returned $$? trying to fetch operator-sdk. please install operator-sdk and try again"; exit 1)
chmod +x $(GOPATH)/bin/operator-sdk
endif
.PHONY: run
run: operator-sdk ## Run the compliance-operator locally
WATCH_NAMESPACE=$(NAMESPACE) \
KUBERNETES_CONFIG=$(KUBECONFIG) \
OPERATOR_NAME=compliance-operator \
$(GOPATH)/bin/operator-sdk up local --namespace $(NAMESPACE)
.PHONY: clean
clean: clean-modcache clean-cache clean-output ## Clean the golang environment
.PHONY: clean-output
clean-output:
rm -rf $(TARGET_DIR)
.PHONY: clean-cache
clean-cache:
$(GO) clean -cache -testcache $(PKGS)
.PHONY: clean-modcache
clean-modcache:
$(GO) clean -modcache $(PKGS)
.PHONY: fmt
fmt: ## Run the `go fmt` tool
@$(GO) fmt $(PKGS)
.PHONY: simplify
simplify:
@gofmt -s -l -w $(SRC)
.PHONY: verify
verify: vet mod-verify gosec ## Run code lint checks
.PHONY: vet
vet:
@$(GO) vet $(PKGS)
.PHONY: mod-verify
mod-verify:
@$(GO) mod verify
.PHONY: gosec
gosec:
@$(GO) run github.com/securego/gosec/cmd/gosec -severity medium -confidence medium -quiet ./...
.PHONY: generate
generate: operator-sdk ## Run operator-sdk's code generation (k8s and openapi)
$(GOPATH)/bin/operator-sdk generate k8s
$(GOPATH)/bin/operator-sdk generate openapi
.PHONY: test-unit
test-unit: fmt ## Run the unit tests
@$(GO) test $(TEST_OPTIONS) $(PKGS)
.PHONY: test-benchmark
test-benchmark: ## Run the benchmark tests -- Note that this can only be ran for one package. You can set $BENCHMARK_PKG for this. cpu.prof and mem.prof will be generated
@$(GO) test -cpuprofile cpu.prof -memprofile mem.prof -bench . $(TEST_OPTIONS) $(BENCHMARK_PKG)
@echo "The pprof files generated are: cpu.prof and mem.prof"
# This runs the end-to-end tests. If not running this on CI, it'll try to
# push the operator image to the cluster's registry. This behavior can be
# avoided with the E2E_SKIP_CONTAINER_PUSH environment variable.
.PHONY: e2e
ifeq ($(E2E_SKIP_CONTAINER_PUSH), false)
e2e: namespace operator-sdk check-if-ci image-to-cluster ## Run the end-to-end tests
else
e2e: namespace operator-sdk check-if-ci
endif
@echo "Running e2e tests"
unset GOFLAGS && $(GOPATH)/bin/operator-sdk test local ./tests/e2e --image "$(IMAGE_PATH)" --namespace "$(NAMESPACE)" --go-test-flags "$(E2E_GO_TEST_FLAGS)"
e2e-local: operator-sdk ## Run the end-to-end tests on a locally running operator (e.g. using make run)
unset GOFLAGS && $(GOPATH)/bin/operator-sdk test local ./tests/e2e --up-local --image "$(IMAGE_PATH)" --namespace "$(NAMESPACE)" --go-test-flags "$(E2E_GO_TEST_FLAGS)"
# This checks if we're in a CI environment by checking the IMAGE_FORMAT
# environmnet variable. if we are, lets ues the image from CI and use this
# operator as the component.
#
# The IMAGE_FORMAT variable comes from CI. It is of the format:
# <image path in CI registry>:${component}
# Here define the `component` variable, so, when we overwrite the
# IMAGE_PATH variable, it'll expand to the component we need.
.PHONY: check-if-ci
check-if-ci:
ifdef IMAGE_FORMAT
@echo "IMAGE_FORMAT variable detected. We're in a CI enviornment."
$(eval component = $(APP_NAME))
$(eval IMAGE_PATH = $(IMAGE_FORMAT))
else
@echo "IMAGE_FORMAT variable missing. We're in local enviornment."
endif
# If IMAGE_FORMAT is not defined, it means that we're not running on CI, so we
# probably want to push the compliance-operator image to the cluster we're
# developing on. This target exposes temporarily the image registry, pushes the
# image, and remove the route in the end.
.PHONY: image-to-cluster
ifdef IMAGE_FORMAT
image-to-cluster:
@echo "We're in a CI environment, skipping image-to-cluster target."
else
image-to-cluster: namespace openshift-user image
@echo "Temporarily exposing the default route to the image registry"
@oc patch configs.imageregistry.operator.openshift.io/cluster --patch '{"spec":{"defaultRoute":true}}' --type=merge
@echo "Pushing image $(IMAGE_PATH):$(TAG) to the image registry"
IMAGE_REGISTRY_HOST=$$(oc get route default-route -n openshift-image-registry --template='{{ .spec.host }}'); \
$(RUNTIME) login --tls-verify=false -u $(OPENSHIFT_USER) -p $(shell oc whoami -t) $${IMAGE_REGISTRY_HOST}; \
$(RUNTIME) push --tls-verify=false $(IMAGE_PATH):$(TAG) $${IMAGE_REGISTRY_HOST}/$(NAMESPACE)/$(APP_NAME):$(TAG); \
$(RUNTIME) push --tls-verify=false $(OPENSCAP_IMAGE_PATH):$(TAG) $${IMAGE_REGISTRY_HOST}/$(NAMESPACE)/$(OPENSCAP_IMAGE_NAME):$(TAG); \
$(RUNTIME) push --tls-verify=false $(RESULTSCOLLECTOR_IMAGE_PATH):$(TAG) $${IMAGE_REGISTRY_HOST}/$(NAMESPACE)/$(RESULTSCOLLECTOR_IMAGE_NAME):$(TAG)
@echo "Removing the route from the image registry"
@oc patch configs.imageregistry.operator.openshift.io/cluster --patch '{"spec":{"defaultRoute":false}}' --type=merge
$(eval IMAGE_PATH = image-registry.openshift-image-registry.svc:5000/$(NAMESPACE)/$(APP_NAME):$(TAG))
endif
.PHONY: namespace
namespace:
@echo "Creating '$(NAMESPACE)' namespace/project"
@oc create -f deploy/ns.yaml || true
.PHONY: openshift-user
openshift-user:
ifeq ($(shell oc whoami 2> /dev/null),kube:admin)
$(eval OPENSHIFT_USER = kubeadmin)
else
$(eval OPENSHIFT_USER = $(oc whoami))
endif
.PHONY: push
push: image
$(RUNTIME) tag $(IMAGE_PATH) $(IMAGE_PATH):$(TAG)
$(RUNTIME) push $(IMAGE_PATH):$(TAG)
versionPath=$(shell GO111MODULE=on go list -f {{.Dir}} k8s.io/code-generator/cmd/client-gen)
codegeneratorRoot=$(versionPath:/cmd/client-gen=)
codegeneratorTarget:=./vendor/k8s.io/code-generator
# go mod doesn't mark scripts as executable, so we need to do that ourselves
.PHONY: code-generator
code-generator:
@chmod +x $(codegeneratorTarget)/generate-groups.sh
@chmod +x $(codegeneratorTarget)/generate-internal-groups.sh
.PHONY: gen-mcfg-client
gen-mcfg-client: code-generator
$(codegeneratorTarget)/generate-groups.sh client \
github.com/openshift/compliance-operator/pkg/generated \
github.com/openshift/compliance-operator/pkg/apis \
"machineconfiguration:v1" \
--go-header-file=./custom-boilerplate.go.txt
cp -r $(GOPATH)/src/github.com/openshift/compliance-operator/pkg/generated pkg/