Skip to content

Commit 5d4ee3f

Browse files
authored
Merge pull request #1 from Kristian-ZH/main
Initial version of the operator
2 parents b21a4b7 + a08ce8a commit 5d4ee3f

26 files changed

+1338
-0
lines changed

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
2+
# Ignore build and test binaries.
3+
bin/

.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# Binaries for programs and plugins
3+
*.exe
4+
*.exe~
5+
*.dll
6+
*.so
7+
*.dylib
8+
bin/*
9+
Dockerfile.cross
10+
11+
# Test binary, build with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Kubernetes Generated files - skip generated files, except for vendored files
18+
19+
!vendor/**/zz_generated.*
20+
21+
# editor and IDE paraphernalia
22+
.idea
23+
.vscode
24+
*.swp
25+
*.swo
26+
*~

Dockerfile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Build the manager binary
2+
FROM golang:1.20 as builder
3+
ARG TARGETOS
4+
ARG TARGETARCH
5+
6+
WORKDIR /workspace
7+
# Copy the Go Modules manifests
8+
COPY go.mod go.mod
9+
COPY go.sum go.sum
10+
# cache deps before building and copying source so that we don't need to re-download as much
11+
# and so that source changes don't invalidate our downloaded layer
12+
RUN go mod download
13+
14+
# Copy the go source
15+
COPY cmd/main.go cmd/main.go
16+
COPY internal/controller/ internal/controller/
17+
18+
# Copy the manifests
19+
COPY config config
20+
21+
# Copy version related files
22+
COPY hack hack
23+
COPY VERSION VERSION
24+
COPY .git .git
25+
26+
# Build
27+
# the GOARCH has not a default value to allow the binary be built according to the host where the command
28+
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
29+
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
30+
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
31+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -ldflags "$(./hack/get-build-ld-flags.sh)" -a -o manager cmd/main.go
32+
33+
# Use distroless as minimal base image to package the manager binary
34+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
35+
FROM gcr.io/distroless/static:nonroot
36+
WORKDIR /
37+
COPY --from=builder /workspace/manager .
38+
COPY --from=builder /workspace/config /config
39+
USER 65532:65532
40+
41+
ENTRYPOINT ["/manager"]

Makefile

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
2+
# VERSION comes from the VERSION file
3+
VERSION := $(shell cat VERSION)
4+
# REGISTRY is the container registry where the operator images are stored
5+
REGISTRY ?= kristianzhe/endpoint-copier-operator
6+
# Image URL to use all building/pushing image targets
7+
IMG ?= $(REGISTRY):$(VERSION)
8+
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
9+
ENVTEST_K8S_VERSION = 1.27.1
10+
# LD_FLAGS are the flags which will be used by the operator in order to print the correct version
11+
LD_FLAGS :=$(shell hack/get-build-ld-flags.sh)
12+
13+
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
14+
ifeq (,$(shell go env GOBIN))
15+
GOBIN=$(shell go env GOPATH)/bin
16+
else
17+
GOBIN=$(shell go env GOBIN)
18+
endif
19+
20+
# CONTAINER_TOOL defines the container tool to be used for building images.
21+
# Be aware that the target commands are only tested with Docker which is
22+
# scaffolded by default. However, you might want to replace it to use other
23+
# tools. (i.e. podman)
24+
CONTAINER_TOOL ?= docker
25+
26+
# Setting SHELL to bash allows bash commands to be executed by recipes.
27+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
28+
SHELL = /usr/bin/env bash -o pipefail
29+
.SHELLFLAGS = -ec
30+
31+
.PHONY: all
32+
all: build
33+
34+
##@ General
35+
36+
# The help target prints out all targets with their descriptions organized
37+
# beneath their categories. The categories are represented by '##@' and the
38+
# target descriptions by '##'. The awk commands is responsible for reading the
39+
# entire set of makefiles included in this invocation, looking for lines of the
40+
# file as xyz: ## something, and then pretty-format the target and help. Then,
41+
# if there's a line with ##@ something, that gets pretty-printed as a category.
42+
# More info on the usage of ANSI control characters for terminal formatting:
43+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
44+
# More info on the awk command:
45+
# http://linuxcommand.org/lc3_adv_awk.php
46+
47+
.PHONY: help
48+
help: ## Display this help.
49+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
50+
51+
##@ Development
52+
53+
.PHONY: manifests
54+
manifests: controller-gen ## Generate ClusterRole objects.
55+
$(CONTROLLER_GEN) rbac:roleName=manager-role paths="./..."
56+
57+
.PHONY: fmt
58+
fmt: ## Run go fmt against code.
59+
go fmt ./...
60+
61+
.PHONY: vet
62+
vet: ## Run go vet against code.
63+
go vet ./...
64+
65+
.PHONY: test
66+
test: manifests fmt vet envtest ## Run tests.
67+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
68+
69+
##@ Build
70+
71+
.PHONY: build
72+
build: manifests fmt vet ## Build manager binary.
73+
go build -ldflags="$(LD_FLAGS)" -o bin/manager cmd/main.go
74+
75+
.PHONY: run
76+
run: manifests fmt vet ## Run a controller from your host.
77+
go run -ldflags="$(LD_FLAGS)" ./cmd/main.go
78+
79+
# If you wish built the manager image targeting other platforms you can use the --platform flag.
80+
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
81+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
82+
.PHONY: docker-build
83+
docker-build: test ## Build docker image with the manager.
84+
$(CONTAINER_TOOL) build -t ${IMG} .
85+
86+
.PHONY: docker-push
87+
docker-push: ## Push docker image with the manager.
88+
$(CONTAINER_TOOL) push ${IMG}
89+
90+
# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
91+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
92+
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
93+
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
94+
# - 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)
95+
# To properly provided solutions that supports more than one platform you should use this option.
96+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
97+
.PHONY: docker-buildx
98+
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
99+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
100+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
101+
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
102+
$(CONTAINER_TOOL) buildx use project-v3-builder
103+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
104+
- $(CONTAINER_TOOL) buildx rm project-v3-builder
105+
rm Dockerfile.cross
106+
107+
##@ Deployment
108+
109+
ifndef ignore-not-found
110+
ignore-not-found = false
111+
endif
112+
113+
.PHONY: deploy
114+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
115+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
116+
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
117+
118+
.PHONY: undeploy
119+
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.
120+
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
121+
122+
##@ Build Dependencies
123+
124+
## Location to install dependencies to
125+
LOCALBIN ?= $(shell pwd)/bin
126+
$(LOCALBIN):
127+
mkdir -p $(LOCALBIN)
128+
129+
## Tool Binaries
130+
KUBECTL ?= kubectl
131+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
132+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
133+
ENVTEST ?= $(LOCALBIN)/setup-envtest
134+
135+
## Tool Versions
136+
KUSTOMIZE_VERSION ?= v5.0.1
137+
CONTROLLER_TOOLS_VERSION ?= v0.12.0
138+
139+
.PHONY: kustomize
140+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
141+
$(KUSTOMIZE): $(LOCALBIN)
142+
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
143+
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
144+
rm -rf $(LOCALBIN)/kustomize; \
145+
fi
146+
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
147+
148+
.PHONY: controller-gen
149+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
150+
$(CONTROLLER_GEN): $(LOCALBIN)
151+
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
152+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
153+
154+
.PHONY: envtest
155+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
156+
$(ENVTEST): $(LOCALBIN)
157+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

PROJECT

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Code generated by tool. DO NOT EDIT.
2+
# This file is used to track the info used to scaffold your project
3+
# and allow the plugins properly work.
4+
# More info: https://book.kubebuilder.io/reference/project-config.html
5+
domain: endpoint-copier-operator
6+
layout:
7+
- go.kubebuilder.io/v4
8+
projectName: endpoint-copier-operator
9+
repo: github/endpoint-copier-operator
10+
resources:
11+
- controller: true
12+
group: core
13+
kind: Endpoints
14+
path: k8s.io/api/core/v1
15+
version: v1
16+
version: "3"

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# endpoint-copier-operator
2+
This is an Kubernetes operator whose purpose is to create a copy of the default Kubernetes Service (as LoadBalancer type) and Endpoint and to keep them synced.
3+
4+
## Getting Started
5+
You’ll need a Kubernetes cluster to run against. You can use [KIND](https://sigs.k8s.io/kind) to get a local cluster for testing, or run against a remote cluster.
6+
**Note:** Your controller will automatically use the current context in your kubeconfig file (i.e. whatever cluster `kubectl cluster-info` shows).
7+
8+
### Running on the cluster
9+
Deploy the controller to the cluster
10+
11+
```sh
12+
make deploy
13+
```
14+
15+
### Undeploy controller
16+
UnDeploy the controller from the cluster:
17+
18+
```sh
19+
make undeploy
20+
```
21+
22+
### How it works
23+
This project aims to follow the Kubernetes [Operator pattern](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/).
24+
25+
It uses [Controllers](https://kubernetes.io/docs/concepts/architecture/controller/),
26+
which provide a reconcile function responsible for synchronizing resources until the desired state is reached on the cluster.
27+
28+
### Test It Out
29+
1. Run your controller (this will run in the foreground, so switch to a new terminal if you want to leave it running):
30+
31+
```sh
32+
make run
33+
```
34+
35+
**NOTE:** Run `make help` for more information on all potential `make` targets
36+
37+
More information can be found via the [Kubebuilder Documentation](https://book.kubebuilder.io/introduction.html)
38+
39+
## License
40+
41+
Copyright 2023.
42+
43+
Licensed under the Apache License, Version 2.0 (the "License");
44+
you may not use this file except in compliance with the License.
45+
You may obtain a copy of the License at
46+
47+
http://www.apache.org/licenses/LICENSE-2.0
48+
49+
Unless required by applicable law or agreed to in writing, software
50+
distributed under the License is distributed on an "AS IS" BASIS,
51+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
52+
See the License for the specific language governing permissions and
53+
limitations under the License.

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v0.43

0 commit comments

Comments
 (0)