Skip to content

Commit

Permalink
Add a seperate Makefile for operator
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-demicev committed Mar 10, 2021
1 parent 3dbd349 commit a7cd7eb
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 39 deletions.
50 changes: 11 additions & 39 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ TOOLS_BIN_DIR := $(TOOLS_DIR)/bin
BIN_DIR := bin
E2E_FRAMEWORK_DIR := test/framework
CAPD_DIR := test/infrastructure/docker
OPERATOR_DIR := exp/operator
RELEASE_NOTES_BIN := bin/release-notes
RELEASE_NOTES := $(TOOLS_DIR)/$(RELEASE_NOTES_BIN)
GO_APIDIFF_BIN := bin/go-apidiff
Expand Down Expand Up @@ -87,10 +88,6 @@ KUBEADM_BOOTSTRAP_CONTROLLER_IMG ?= $(REGISTRY)/$(KUBEADM_BOOTSTRAP_IMAGE_NAME)
KUBEADM_CONTROL_PLANE_IMAGE_NAME ?= kubeadm-control-plane-controller
KUBEADM_CONTROL_PLANE_CONTROLLER_IMG ?= $(REGISTRY)/$(KUBEADM_CONTROL_PLANE_IMAGE_NAME)

# operator
OPERATOR_IMAGE_NAME ?= cluster-api-operator
OPERATOR_CONTROLLER_IMG ?= $(REGISTRY)/$(OPERATOR_IMAGE_NAME)

# It is set by Prow GIT_TAG, a git-based tag of the form vYYYYMMDD-hash, e.g., v20210120-v0.3.10-308-gc61521971
TAG ?= dev
ARCH ?= amd64
Expand Down Expand Up @@ -145,10 +142,6 @@ test-e2e: ## Run the e2e tests
## Binaries
## --------------------------------------

.PHONY: operator
operator: ## Build operator binary
go build -ldflags "$(LDFLAGS)" -o $(BIN_DIR)/operator sigs.k8s.io/cluster-api/exp/operator

.PHONY: manager-core
manager-core: ## Build core manager binary
go build -ldflags "$(LDFLAGS)" -o $(BIN_DIR)/manager sigs.k8s.io/cluster-api
Expand Down Expand Up @@ -237,7 +230,7 @@ generate-go: $(GOBINDATA) ## Runs Go related generate targets
$(MAKE) generate-go-core
$(MAKE) generate-go-kubeadm-bootstrap
$(MAKE) generate-go-kubeadm-control-plane
$(MAKE) generate-go-operator
$(MAKE) -C $(OPERATOR_DIR) generate-go-operator

.PHONY: generate-go-core
generate-go-core: $(CONTROLLER_GEN) $(CONVERSION_GEN)
Expand Down Expand Up @@ -287,12 +280,6 @@ generate-go-kubeadm-control-plane: $(CONTROLLER_GEN) $(CONVERSION_GEN) ## Runs G
--output-file-base=zz_generated.conversion \
--go-header-file=./hack/boilerplate/boilerplate.generatego.txt

.PHONY: generate-go-operator
generate-go-operator: $(CONTROLLER_GEN) ## Runs Go related generate targets for the operator
$(CONTROLLER_GEN) \
object:headerFile=./hack/boilerplate/boilerplate.generatego.txt \
paths=./exp/operator/api/...

.PHONY: generate-bindata
generate-bindata: $(KUSTOMIZE) $(GOBINDATA) clean-bindata $(CLOUDINIT_GENERATED) ## Generate code for embedding the clusterctl api manifest
# Package manifest YAML into a single file.
Expand All @@ -315,7 +302,7 @@ generate-manifests: ## Generate manifests e.g. CRD, RBAC etc.
$(MAKE) generate-core-manifests
$(MAKE) generate-kubeadm-bootstrap-manifests
$(MAKE) generate-kubeadm-control-plane-manifests
$(MAKE) generate-operator-manifests
$(MAKE) -C $(OPERATOR_DIR) generate-operator-manifests

.PHONY: generate-core-manifests
generate-core-manifests: $(CONTROLLER_GEN) ## Generate manifests for the core provider e.g. CRD, RBAC etc.
Expand Down Expand Up @@ -360,18 +347,6 @@ generate-kubeadm-control-plane-manifests: $(CONTROLLER_GEN) ## Generate manifest
output:webhook:dir=./controlplane/kubeadm/config/webhook \
webhook

.PHONY: generate-operator-manifests
generate-operator-manifests: $(CONTROLLER_GEN) ## Generate manifests for the operator e.g. CRD, RBAC etc.
$(CONTROLLER_GEN) \
paths=./exp/operator/api/... \
paths=./exp/operator/controllers/... \
crd:crdVersions=v1 \
rbac:roleName=manager-role \
output:crd:dir=./exp/operator/config/crd/bases \
output:rbac:dir=./exp/operator/config/rbac \
output:webhook:dir=./exp/operator/config/webhook \
webhook

.PHONY: modules
modules: ## Runs go mod to ensure modules are up to date.
go mod tidy
Expand Down Expand Up @@ -415,9 +390,9 @@ docker-build-kubeadm-control-plane: ## Build the docker image for kubeadm contro

.PHONY: docker-build-operator
docker-build-operator: ## Build the docker image for management cluster operator
DOCKER_BUILDKIT=1 docker build --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg package=./exp/operator --build-arg ldflags="$(LDFLAGS)" . -t $(OPERATOR_CONTROLLER_IMG)-$(ARCH):$(TAG)
$(MAKE) set-manifest-image MANIFEST_IMG=$(OPERATOR_CONTROLLER_IMG)-$(ARCH) MANIFEST_TAG=$(TAG) TARGET_RESOURCE="./exp/operator/config/default/manager_image_patch.yaml"
$(MAKE) set-manifest-pull-policy TARGET_RESOURCE="./exp/operator/config/default/manager_pull_policy.yaml"
$(MAKE) -C $(OPERATOR_DIR) docker-build-operator
$(MAKE) set-manifest-image MANIFEST_IMG=$(OPERATOR_CONTROLLER_IMG)-$(ARCH) MANIFEST_TAG=$(TAG) TARGET_RESOURCE="./$(OPERATOR_DIR)/config/default/manager_image_patch.yaml"
$(MAKE) set-manifest-pull-policy TARGET_RESOURCE="$(OPERATOR_DIR)/config/default/manager_pull_policy.yaml"

.PHONY: docker-push
docker-push: ## Push the docker images
Expand Down Expand Up @@ -474,11 +449,8 @@ docker-push-kubeadm-control-plane-manifest: ## Push the fat manifest docker imag

.PHONY: docker-push-operator-manifest
docker-push-operator-manifest: ## Push the fat manifest docker image for the operator image.
## Minimum docker version 18.06.0 is required for creating and pushing manifest images.
docker manifest create --amend $(OPERATOR_CONTROLLER_IMG):$(TAG) $(shell echo $(ALL_ARCH) | sed -e "s~[^ ]*~$(OPERATOR_CONTROLLER_IMG)\-&:$(TAG)~g")
@for arch in $(ALL_ARCH); do docker manifest annotate --arch $${arch} ${OPERATOR_CONTROLLER_IMG}:${TAG} ${OPERATOR_CONTROLLER_IMG}-$${arch}:${TAG}; done
docker manifest push --purge $(OPERATOR_CONTROLLER_IMG):$(TAG)
$(MAKE) set-manifest-image MANIFEST_IMG=$(OPERATOR_CONTROLLER_IMG) MANIFEST_TAG=$(TAG) TARGET_RESOURCE="./exp/operator/config/default/manager_image_patch.yaml"
$(MAKE) -C "./$(OPERATOR_DIR)/" docker-push-operator-manifest
$(MAKE) set-manifest-image MANIFEST_IMG=$(OPERATOR_CONTROLLER_IMG) MANIFEST_TAG=$(TAG) TARGET_RESOURCE="./$(OPERATOR_DIR)/config/default/manager_image_patch.yaml"
$(MAKE) set-manifest-pull-policy TARGET_RESOURCE="./exp/goperator/config/default/manager_pull_policy.yaml"

.PHONY: set-manifest-pull-policy
Expand Down Expand Up @@ -531,11 +503,11 @@ manifest-modification: # Set the manifest images to the staging/production bucke
TARGET_RESOURCE="./controlplane/kubeadm/config/default/manager_image_patch.yaml"
$(MAKE) set-manifest-image \
MANIFEST_IMG=$(PROD_REGISTRY)/$(OPERATOR_IMAGE_NAME) MANIFEST_TAG=$(RELEASE_TAG) \
TARGET_RESOURCE="./exp/operator/config/default/manager_image_patch.yaml"
TARGET_RESOURCE="./$(OPERATOR_DIR)/config/default/manager_image_patch.yaml"
$(MAKE) set-manifest-pull-policy PULL_POLICY=IfNotPresent TARGET_RESOURCE="./config/default/manager_pull_policy.yaml"
$(MAKE) set-manifest-pull-policy PULL_POLICY=IfNotPresent TARGET_RESOURCE="./bootstrap/kubeadm/config/default/manager_pull_policy.yaml"
$(MAKE) set-manifest-pull-policy PULL_POLICY=IfNotPresent TARGET_RESOURCE="./controlplane/kubeadm/config/default/manager_pull_policy.yaml"
$(MAKE) set-manifest-pull-policy PULL_POLICY=IfNotPresent TARGET_RESOURCE="./exp/operator/config/default/manager_pull_policy.yaml"
$(MAKE) set-manifest-pull-policy PULL_POLICY=IfNotPresent TARGET_RESOURCE="./$(OPERATOR_DIR)/config/default/manager_pull_policy.yaml"

.PHONY: release-manifests
release-manifests: $(RELEASE_DIR) $(KUSTOMIZE) ## Builds the manifests to publish with a release
Expand All @@ -546,7 +518,7 @@ release-manifests: $(RELEASE_DIR) $(KUSTOMIZE) ## Builds the manifests to publis
# Build control-plane-components.
$(KUSTOMIZE) build controlplane/kubeadm/config/default > $(RELEASE_DIR)/control-plane-components.yaml
# Build operator components.
$(KUSTOMIZE) build exp/operator/config/default > $(RELEASE_DIR)/operator-components.yaml
$(KUSTOMIZE) build $(OPERATOR_DIR)/config/default > $(RELEASE_DIR)/operator-components.yaml

## Build cluster-api-components (aggregate of all of the above).
cat $(RELEASE_DIR)/core-components.yaml > $(RELEASE_DIR)/cluster-api-components.yaml
Expand Down
115 changes: 115 additions & 0 deletions exp/operator/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Copyright 2021 The Kubernetes Authors.
#
# 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.

# If you update this file, please follow
# https://suva.sh/posts/well-documented-makefiles

# Ensure Make is run with bash shell as some syntax below is bash-specific
SHELL:=/usr/bin/env bash

# Path to main repo
ROOT = ../../

.DEFAULT_GOAL:=help

# Use GOPROXY environment variable if set
GOPROXY := $(shell go env GOPROXY)
ifeq ($(GOPROXY),)
GOPROXY := https://proxy.golang.org
endif
export GOPROXY

# Active module mode, as we use go modules to manage dependencies
export GO111MODULE=on

# This option is for running docker manifest command
export DOCKER_CLI_EXPERIMENTAL := enabled

# Directories
TOOLS_DIR := $(ROOT)/hack/tools
TOOLS_BIN_DIR := $(TOOLS_DIR)/bin

export PATH := $(abspath $(TOOLS_BIN_DIR)):$(PATH)

# Binaries.
# Need to use abspath so we can invoke these from subdirectories
CONTROLLER_GEN := $(abspath $(TOOLS_BIN_DIR)/controller-gen)

# Define Docker related variables. Releases should modify and double check these vars.
REGISTRY ?= gcr.io/$(shell gcloud config get-value project)

# Image name
OPERATOR_IMAGE_NAME ?= cluster-api-operator
OPERATOR_CONTROLLER_IMG ?= $(REGISTRY)/$(OPERATOR_IMAGE_NAME)

# It is set by Prow GIT_TAG, a git-based tag of the form vYYYYMMDD-hash, e.g., v20210120-v0.3.10-308-gc61521971
TAG ?= dev
ARCH ?= amd64
ALL_ARCH = amd64 arm arm64 ppc64le s390x

# Set build time variables including version details
LDFLAGS := $(shell $(ROOT)/hack/version.sh)

all: generate-go-operator generate-operator-manifests operator

help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[0-9A-Za-z_-]+:.*?##/ { printf " \033[36m%-45s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

## --------------------------------------
## Binaries
## --------------------------------------

.PHONY: operator
operator: ## Build operator binary
go build -ldflags "$(LDFLAGS)" -o $(BIN_DIR)/operator sigs.k8s.io/cluster-api/exp/operator

$(CONTROLLER_GEN): $(TOOLS_DIR)/go.mod # Build controller-gen from tools folder.
cd $(TOOLS_DIR); go build -tags=tools -o $(BIN_DIR)/controller-gen sigs.k8s.io/controller-tools/cmd/controller-gen

## --------------------------------------
## Generate / Manifests
## --------------------------------------

.PHONY: generate-go-operator
generate-go-operator: $(CONTROLLER_GEN) ## Runs Go related generate targets for the operator
$(CONTROLLER_GEN) \
object:headerFile=$(ROOT)/hack/boilerplate/boilerplate.generatego.txt \
paths=./api/...

.PHONY: generate-operator-manifests
generate-operator-manifests: $(CONTROLLER_GEN) ## Generate manifests for the operator e.g. CRD, RBAC etc.
$(CONTROLLER_GEN) \
paths=./api/... \
paths=./controllers/... \
crd:crdVersions=v1 \
rbac:roleName=manager-role \
output:crd:dir=./config/crd/bases \
output:rbac:dir=./config/rbac \
output:webhook:dir=./config/webhook \
webhook

## --------------------------------------
## Docker
## --------------------------------------

.PHONY: docker-build-operator
docker-build-operator: ## Build the docker image for management cluster operator
DOCKER_BUILDKIT=1 docker build --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg ldflags="$(LDFLAGS)" $(ROOT) -t $(OPERATOR_CONTROLLER_IMG)-$(ARCH):$(TAG)

.PHONY: docker-push-operator-manifest
docker-push-operator-manifest: ## Push the fat manifest docker image for the operator image.
## Minimum docker version 18.06.0 is required for creating and pushing manifest images.
docker manifest create --amend $(OPERATOR_CONTROLLER_IMG):$(TAG) $(shell echo $(ALL_ARCH) | sed -e "s~[^ ]*~$(OPERATOR_CONTROLLER_IMG)\-&:$(TAG)~g")
@for arch in $(ALL_ARCH); do docker manifest annotate --arch $${arch} ${OPERATOR_CONTROLLER_IMG}:${TAG} ${OPERATOR_CONTROLLER_IMG}-$${arch}:${TAG}; done
docker manifest push --purge $(OPERATOR_CONTROLLER_IMG):$(TAG)

0 comments on commit a7cd7eb

Please sign in to comment.