Skip to content

Commit

Permalink
refactor: Make more consistent with other kuadrant operators
Browse files Browse the repository at this point in the history
* Keep Makefile consistent with other operators
* Remove gateway prefix from commands and make targets
  • Loading branch information
mikenairn committed Feb 22, 2024
1 parent b4f0aa1 commit 0e0d23c
Show file tree
Hide file tree
Showing 21 changed files with 228 additions and 249 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- main
- "release-*"
- remove_gateway_prefix
tags:
- "v[0-9]+.[0-9]+.[0-9]+"
paths-ignore:
Expand Down Expand Up @@ -68,7 +69,7 @@ jobs:
make local-setup
- name: Deploy
run: |
make docker-build-gateway-controller kind-load-gateway-controller deploy-gateway-controller
make local-deploy
kubectl --context kind-mgc-control-plane -n multicluster-gateway-controller-system get deployments/mgc-controller-manager -o yaml | yq .spec.template.spec.containers[0].image
kubectl --context kind-mgc-control-plane -n multicluster-gateway-controller-system wait --timeout=300s --for=condition=Available deployment/mgc-controller-manager
kubectl --context kind-mgc-control-plane logs --all-containers --ignore-errors deployment/mgc-controller-manager -n multicluster-gateway-controller-system
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/controller-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- main
- "release-*"
- remove_gateway_prefix
tags:
- "v[0-9]+.[0-9]+.[0-9]+"

Expand Down
11 changes: 5 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@ COPY go.sum go.sum
RUN go mod download

# Copy the go source
COPY cmd/ cmd/
COPY cmd/main.go cmd/main.go
COPY pkg/ pkg/

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
FROM builder as controller_builder
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o controller cmd/gateway_controller/main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot as controller
FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=controller_builder /workspace/controller .
COPY --from=builder /workspace/manager .
USER 65532:65532

ENTRYPOINT ["/controller"]
ENTRYPOINT ["/manager"]
219 changes: 205 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
# 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 ?= 0.0.0

ifeq (0.0.0,$(VERSION))
IMAGE_TAG ?= latest
else
IMAGE_TAG ?= v$(VERSION)
endif

# Image URL to use all building/pushing image targets
TAG ?= latest
# CHANNELS define the bundle channels used in the bundle.
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "candidate,fast,stable")
# To re-generate a bundle for other specific channels without changing the standard setup, you can:
# - use the CHANNELS as arg of the bundle target (e.g make bundle CHANNELS=candidate,fast,stable)
# - use environment variables to overwrite this value (e.g export CHANNELS="candidate,fast,stable")
ifneq ($(origin CHANNELS), undefined)
BUNDLE_CHANNELS := --channels=$(CHANNELS)
endif

# DEFAULT_CHANNEL defines the default channel used in the bundle.
# Add a new line here if you would like to change its default config. (E.g DEFAULT_CHANNEL = "stable")
# To re-generate a bundle for any other default channel without changing the default setup, you can:
# - use the DEFAULT_CHANNEL as arg of the bundle target (e.g make bundle DEFAULT_CHANNEL=stable)
# - use environment variables to overwrite this value (e.g export DEFAULT_CHANNEL="stable")
ifneq ($(origin DEFAULT_CHANNEL), undefined)
BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
endif
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)

# IMAGE_TAG_BASE defines the docker.io namespace and part of the image name for remote images.
# This variable is used to construct full image tags for bundle and catalog images.
#
# For example, running 'make bundle-build bundle-push catalog-build catalog-push' will build and push both
# quay.io/kuadrant/multicluster-gateway-controller-bundle:$VERSION and quay.io/kuadrant/multicluster-gateway-controller-catalog:$VERSION.
IMAGE_TAG_BASE ?= quay.io/kuadrant/multicluster-gateway-controller

# BUNDLE_IMG defines the image:tag used for the bundle.
# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:$(IMAGE_TAG)

# BUNDLE_GEN_FLAGS are the flags passed to the operator-sdk generate bundle command
BUNDLE_GEN_FLAGS ?= -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)

# USE_IMAGE_DIGESTS defines if images are resolved via tags or digests
# You can enable this value if you would like to use SHA Based Digests
# To enable set flag to true
USE_IMAGE_DIGESTS ?= false
ifeq ($(USE_IMAGE_DIGESTS), true)
BUNDLE_GEN_FLAGS += --use-image-digests
endif

# Image URL to use all building/pushing image targets
DEFAULT_IMG ?= $(IMAGE_TAG_BASE):$(IMAGE_TAG)
IMG ?= $(DEFAULT_IMG)
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.25.0
ENVTEST_K8S_VERSION = 1.27.1

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand All @@ -12,13 +65,17 @@ else
GOBIN=$(shell go env GOBIN)
endif

# CONTAINER_TOOL defines the container tool to be used for building images.
# Be aware that the target commands are only tested with Docker which is
# scaffolded by default. However, you might want to replace it to use other
# tools. (i.e. podman)
CONTAINER_TOOL ?= docker

# Setting SHELL to bash allows bash commands to be executed by recipes.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec

include ./hack/make/*.make

.PHONY: all
all: build

Expand Down Expand Up @@ -47,13 +104,10 @@ clean: ## Clean up temporary files.
-rm -rf ./tmp
-rm -rf ./config/**/charts

.PHONY: gateway-manifests
.PHONY: manifests
gateway-manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role paths="./pkg/controllers/gateway" output:rbac:artifacts:config=config/rbac

.PHONY: manifests
manifests: gateway-manifests

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
Expand All @@ -74,6 +128,9 @@ lint: ## Run golangci-lint against code.
imports: openshift-goimports ## Run openshift goimports against code.
$(OPENSHIFT_GOIMPORTS) -m github.com/Kuadrant/multicluster-gateway-controller -i github.com/kuadrant

.PHONY: test
test: test-unit test-integration ## Run tests.

.PHONY: test-unit
test-unit: manifests generate fmt vet envtest ## Run unit tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $(shell find ./pkg/_internal -mindepth 1 -type d) ./... -tags=unit -coverprofile cover-unit.out
Expand All @@ -82,9 +139,6 @@ test-unit: manifests generate fmt vet envtest ## Run unit tests.
test-integration: ginkgo manifests generate fmt vet envtest ## Run integration tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" $(GINKGO) -tags=integration -v --focus "${FOCUS}" ./test/gateway_integration

.PHONY: test
test: test-unit test-integration ## Run tests.

.PHONY: test-e2e
test-e2e: ginkgo
$(GINKGO) -tags=e2e -v ./test/e2e
Expand All @@ -111,10 +165,50 @@ local-cleanup: kind ## Cleanup kind clusters created by local-setup
local-cleanup-mgc: ## Cleanup MGC from kind clusters
./hack/local-cleanup-mgc.sh

.PHONY: local-deploy
local-deploy: IMG := $(IMAGE_TAG_BASE):dev
local-deploy: docker-build kind-load-image deploy ## Deploy the operator into local kind cluster from the current code

##@ Build

.PHONY: build
build: build-gateway-controller ## Build all binaries.
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/manager cmd/main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./cmd/main.go

# If you wish built the manager image targeting other platforms you can use the --platform flag.
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build
docker-build: ## Build docker image with the manager.
$(CONTAINER_TOOL) build -t ${IMG} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
$(CONTAINER_TOOL) push ${IMG}

# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
# To properly provided solutions that supports more than one platform you should use this option.
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
.PHONY: docker-buildx
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
$(CONTAINER_TOOL) buildx use project-v3-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
- $(CONTAINER_TOOL) buildx rm project-v3-builder
rm Dockerfile.cross

##@ Deployment

ifndef ignore-not-found
ignore-not-found = false
endif
Expand All @@ -124,6 +218,95 @@ endif
deploy-sample-applicationset:
kubectl apply -f ./samples/argocd-applicationset/echo-applicationset.yaml

.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/deploy/local | kubectl apply -f -
@if [ "$(METRICS)" = "true" ]; then\
$(KUSTOMIZE) build config/prometheus | kubectl apply -f -;\
fi

.PHONY: undeploy
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
@if [ "$(METRICS)" = "true" ]; then\
$(KUSTOMIZE) build config/prometheus | kubectl delete --ignore-not-found=$(ignore-not-found) -f -;\
fi
$(KUSTOMIZE) build config/deploy/local | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

##@ OLM

.PHONY: bundle
bundle: manifests kustomize operator-sdk ## Generate bundle manifests and metadata, then validate generated files.
$(OPERATOR_SDK) generate kustomize manifests -q
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
$(KUSTOMIZE) build config/manifests | $(OPERATOR_SDK) generate bundle $(BUNDLE_GEN_FLAGS)
$(OPERATOR_SDK) bundle validate ./bundle
$(MAKE) bundle-ignore-createdAt

# Since operator-sdk 1.26.0, `make bundle` changes the `createdAt` field from the bundle
# even if it is patched:
# https://github.com/operator-framework/operator-sdk/pull/6136
# This code checks if only the createdAt field. If is the only change, it is ignored.
# Else, it will do nothing.
# https://github.com/operator-framework/operator-sdk/issues/6285#issuecomment-1415350333
# https://github.com/operator-framework/operator-sdk/issues/6285#issuecomment-1532150678
.PHONY: bundle-ignore-createdAt
bundle-ignore-createdAt:
git diff --quiet -I'^ createdAt: ' ./bundle && git checkout ./bundle || true

.PHONY: bundle-build
bundle-build: ## Build the bundle image.
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .

.PHONY: bundle-push
bundle-push: ## Push the bundle image.
$(MAKE) docker-push IMG=$(BUNDLE_IMG)

.PHONY: opm
OPM = $(LOCALBIN)/opm
opm: ## Download opm locally if necessary.
ifeq (,$(wildcard $(OPM)))
ifeq (,$(shell which opm 2>/dev/null))
@{ \
set -e ;\
mkdir -p $(dir $(OPM)) ;\
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.23.0/$${OS}-$${ARCH}-opm ;\
chmod +x $(OPM) ;\
}
else
OPM = $(shell which opm)
endif
endif

# A comma-separated list of bundle images (e.g. make catalog-build BUNDLE_IMGS=example.com/operator-bundle:v0.1.0,example.com/operator-bundle:v0.2.0).
# These images MUST exist in a registry and be pull-able.
BUNDLE_IMGS ?= $(BUNDLE_IMG)

# The image tag given to the resulting catalog image (e.g. make catalog-build CATALOG_IMG=example.com/operator-catalog:v0.2.0).
CATALOG_IMG ?= $(IMAGE_TAG_BASE)-catalog:$(IMAGE_TAG)

# Set CATALOG_BASE_IMG to an existing catalog image tag to add $BUNDLE_IMGS to that image.
ifneq ($(origin CATALOG_BASE_IMG), undefined)
FROM_INDEX_OPT := --from-index $(CATALOG_BASE_IMG)
endif

# Build a catalog image by adding bundle images to an empty catalog using the operator package manager tool, 'opm'.
# This recipe invokes 'opm' in 'semver' bundle add mode. For more information on add modes, see:
# https://github.com/operator-framework/community-operators/blob/7f1438c/docs/packaging-operator.md#updating-your-existing-operator
.PHONY: catalog-build
catalog-build: opm ## Build a catalog image.
mkdir -p tmp/catalog
cd tmp/catalog && $(OPM) index add --container-tool docker --mode semver --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT) --generate
cd tmp/catalog && docker build -t $(CATALOG_IMG) -f index.Dockerfile .

# Push the catalog image.
.PHONY: catalog-push
catalog-push: ## Push a catalog image.
$(MAKE) docker-push IMG=$(CATALOG_IMG)

##@ Misc

.PHONY: thanos-manifests
thanos-manifests: ./hack/thanos/thanos_build.sh ./hack/thanos/thanos.jsonnet
./hack/thanos/thanos_build.sh
Expand All @@ -144,4 +327,12 @@ clear-dev-tls:

.PHONY: skupper-setup
skupper-setup:
./hack/skupper/skupper-setup.sh
./hack/skupper/skupper-setup.sh

KIND_CLUSTER_NAME ?= mgc-control-plane
.PHONY: kind-load-image
kind-load-image: kind ## Load image to kind cluster.
$(KIND) load docker-image $(IMG) --name $(KIND_CLUSTER_NAME)

# Include last to avoid changing MAKEFILE_LIST used above
include ./hack/make/*.make
34 changes: 4 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,15 @@ When deploying the multicluster gateway controller using the make targets, the f
```sh
make local-setup MGC_WORKLOAD_CLUSTERS_COUNT=<NUMBER_WORKLOAD_CLUSTER>
```
1. Build the controller image and load it into the control plane
1. Build the controller image, load it into the control plane and deploy
```sh
kubectl config use-context kind-mgc-control-plane
make kind-load-gateway-controller
```

1. Deploy the controller(s) to the control plane cluster
```sh
make deploy-gateway-controller
make local-deploy
```

1. (Optional) View the logs of the deployed controller
```sh
kubectl logs -f $(kubectl get pods -n multi-cluster-gateways | grep "mgc-" | awk '{print $1}') -n multi-cluster-gateways
kubectl logs -f deployment/mgc-controller-manager -n multicluster-gateway-controller-system
```

## 2. Running the controller locally:
Expand All @@ -68,30 +63,9 @@ When deploying the multicluster gateway controller using the make targets, the f
1. Run the controller locally:
```sh
kubectl config use-context kind-mgc-control-plane
make build-gateway-controller run-gateway-controller
```

## 3. Running the agent in the cluster:
1. Build the agent image and load it into the workload cluster
```sh
kubectl config use-context kind-mgc-workload-1
make kind-load-agent
make build run
```

1. Deploy the agent to the workload cluster
```sh
make deploy-agent
```

## 4. Running the agent locally
1. Target the workload cluster you wish to run on:
```sh
export KUBECONFIG=./tmp/kubeconfigs/mgc-workload-1.kubeconfig
```
1. Run the agent locally:
```sh
make build-agent run-agent
```
## 5. Clean up local environment
In any terminal window target control plane cluster by:
```bash
Expand Down
Loading

0 comments on commit 0e0d23c

Please sign in to comment.