Skip to content

Commit 5155c64

Browse files
committed
Initial commit
0 parents  commit 5155c64

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3640
-0
lines changed

.dockerignore

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

.gitignore

+26
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+
testbin/*
10+
Dockerfile.cross
11+
12+
# Test binary, build with `go test -c`
13+
*.test
14+
15+
# Output of the go coverage tool, specifically when used with LiteIDE
16+
*.out
17+
18+
# Kubernetes Generated files - skip generated files, except for vendored files
19+
20+
!vendor/**/zz_generated.*
21+
22+
# editor and IDE paraphernalia
23+
.idea
24+
*.swp
25+
*.swo
26+
*~

Dockerfile

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Build the manager binary
2+
FROM golang:1.19 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 main.go main.go
16+
COPY api/ api/
17+
COPY controllers/ controllers/
18+
COPY pkg/ pkg/
19+
20+
# Build
21+
# the GOARCH has not a default value to allow the binary be built according to the host where the command
22+
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
23+
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
24+
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
25+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go
26+
27+
# Use distroless as minimal base image to package the manager binary
28+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
29+
FROM gcr.io/distroless/static:nonroot
30+
WORKDIR /
31+
COPY --from=builder /workspace/manager .
32+
USER 65532:65532
33+
34+
ENTRYPOINT ["/manager"]

Makefile

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
2+
# Image URL to use all building/pushing image targets
3+
IMG ?= ghcr.io/nais/unleasherator/unleasherator:latest
4+
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
5+
ENVTEST_K8S_VERSION = 1.25.0
6+
7+
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
8+
ifeq (,$(shell go env GOBIN))
9+
GOBIN=$(shell go env GOPATH)/bin
10+
else
11+
GOBIN=$(shell go env GOBIN)
12+
endif
13+
14+
# Setting SHELL to bash allows bash commands to be executed by recipes.
15+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
16+
SHELL = /usr/bin/env bash -o pipefail
17+
.SHELLFLAGS = -ec
18+
19+
.PHONY: all
20+
all: build
21+
22+
##@ General
23+
24+
# The help target prints out all targets with their descriptions organized
25+
# beneath their categories. The categories are represented by '##@' and the
26+
# target descriptions by '##'. The awk commands is responsible for reading the
27+
# entire set of makefiles included in this invocation, looking for lines of the
28+
# file as xyz: ## something, and then pretty-format the target and help. Then,
29+
# if there's a line with ##@ something, that gets pretty-printed as a category.
30+
# More info on the usage of ANSI control characters for terminal formatting:
31+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
32+
# More info on the awk command:
33+
# http://linuxcommand.org/lc3_adv_awk.php
34+
35+
.PHONY: help
36+
help: ## Display this help.
37+
@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)
38+
39+
##@ Development
40+
41+
.PHONY: manifests
42+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
43+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
44+
45+
.PHONY: generate
46+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
47+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
48+
49+
.PHONY: fmt
50+
fmt: ## Run go fmt against code.
51+
go fmt ./...
52+
53+
.PHONY: vet
54+
vet: ## Run go vet against code.
55+
go vet ./...
56+
57+
.PHONY: test
58+
test: manifests generate fmt vet envtest ## Run tests.
59+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
60+
61+
##@ Build
62+
63+
.PHONY: build
64+
build: generate fmt vet ## Build manager binary.
65+
go build -o bin/manager main.go
66+
67+
.PHONY: run
68+
run: manifests generate fmt vet ## Run a controller from your host.
69+
go run ./main.go
70+
71+
# If you wish built the manager image targeting other platforms you can use the --platform flag.
72+
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
73+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
74+
.PHONY: docker-build
75+
docker-build: test ## Build docker image with the manager.
76+
docker build -t ${IMG} .
77+
78+
.PHONY: docker-push
79+
docker-push: ## Push docker image with the manager.
80+
docker push ${IMG}
81+
82+
# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
83+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
84+
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
85+
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
86+
# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> than the export will fail)
87+
# To properly provided solutions that supports more than one platform you should use this option.
88+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
89+
.PHONY: docker-buildx
90+
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
91+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
92+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
93+
- docker buildx create --name project-v3-builder
94+
docker buildx use project-v3-builder
95+
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross
96+
- docker buildx rm project-v3-builder
97+
rm Dockerfile.cross
98+
99+
##@ Deployment
100+
101+
ifndef ignore-not-found
102+
ignore-not-found = false
103+
endif
104+
105+
.PHONY: install
106+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
107+
$(KUSTOMIZE) build config/crd | kubectl apply -f -
108+
109+
.PHONY: uninstall
110+
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
111+
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
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: restart
119+
restart: manifests kustomize ## Restart controller in the K8s cluster specified in ~/.kube/config.
120+
kubectl rollout restart deployment/unleasherator-controller-manager -n unleasherator-system
121+
122+
.PHONY: logs
123+
logs: manifests kustomize ## Show logs for controller in the K8s cluster specified in ~/.kube/config.
124+
kubectl logs deployment/unleasherator-controller-manager -n unleasherator-system -f
125+
126+
.PHONY: undeploy
127+
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.
128+
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
129+
130+
##@ Build Dependencies
131+
132+
## Location to install dependencies to
133+
LOCALBIN ?= $(shell pwd)/bin
134+
$(LOCALBIN):
135+
mkdir -p $(LOCALBIN)
136+
137+
## Tool Binaries
138+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
139+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
140+
ENVTEST ?= $(LOCALBIN)/setup-envtest
141+
142+
## Tool Versions
143+
KUSTOMIZE_VERSION ?= v4.5.5
144+
CONTROLLER_TOOLS_VERSION ?= v0.9.2
145+
146+
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
147+
.PHONY: kustomize
148+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
149+
$(KUSTOMIZE): $(LOCALBIN)
150+
test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
151+
152+
.PHONY: controller-gen
153+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
154+
$(CONTROLLER_GEN): $(LOCALBIN)
155+
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
156+
157+
.PHONY: envtest
158+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
159+
$(ENVTEST): $(LOCALBIN)
160+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

PROJECT

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
componentConfig: true
2+
domain: nais.io
3+
layout:
4+
- go.kubebuilder.io/v4-alpha
5+
projectName: unleasherator
6+
repo: github.com/nais/unleasherator
7+
resources:
8+
- api:
9+
crdVersion: v1
10+
namespaced: true
11+
controller: true
12+
domain: nais.io
13+
group: featuretoggling
14+
kind: Unleash
15+
path: github.com/nais/unleasherator/api/v1
16+
version: v1
17+
version: "3"

README.md

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# unleasherator
2+
// TODO(user): Add simple overview of use/purpose
3+
4+
## Description
5+
// TODO(user): An in-depth paragraph about your project and overview of use
6+
7+
## Getting Started
8+
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.
9+
**Note:** Your controller will automatically use the current context in your kubeconfig file (i.e. whatever cluster `kubectl cluster-info` shows).
10+
11+
### Running on the cluster
12+
1. Install Instances of Custom Resources:
13+
14+
```sh
15+
kubectl apply -f config/samples/
16+
```
17+
18+
2. Build and push your image to the location specified by `IMG`:
19+
20+
```sh
21+
make docker-build docker-push IMG=<some-registry>/unleasherator:tag
22+
```
23+
24+
3. Deploy the controller to the cluster with the image specified by `IMG`:
25+
26+
```sh
27+
make deploy IMG=<some-registry>/unleasherator:tag
28+
```
29+
30+
### Uninstall CRDs
31+
To delete the CRDs from the cluster:
32+
33+
```sh
34+
make uninstall
35+
```
36+
37+
### Undeploy controller
38+
UnDeploy the controller to the cluster:
39+
40+
```sh
41+
make undeploy
42+
```
43+
44+
## Contributing
45+
// TODO(user): Add detailed information on how you would like others to contribute to this project
46+
47+
### How it works
48+
This project aims to follow the Kubernetes [Operator pattern](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/)
49+
50+
It uses [Controllers](https://kubernetes.io/docs/concepts/architecture/controller/)
51+
which provides a reconcile function responsible for synchronizing resources untile the desired state is reached on the cluster
52+
53+
### Test It Out
54+
1. Install the CRDs into the cluster:
55+
56+
```sh
57+
make install
58+
```
59+
60+
2. Run your controller (this will run in the foreground, so switch to a new terminal if you want to leave it running):
61+
62+
```sh
63+
make run
64+
```
65+
66+
**NOTE:** You can also run this in one step by running: `make install run`
67+
68+
### Modifying the API definitions
69+
If you are editing the API definitions, generate the manifests such as CRs or CRDs using:
70+
71+
```sh
72+
make manifests
73+
```
74+
75+
**NOTE:** Run `make --help` for more information on all potential `make` targets
76+
77+
More information can be found via the [Kubebuilder Documentation](https://book.kubebuilder.io/introduction.html)
78+
79+
## License
80+
81+
Copyright 2022.
82+
83+
Licensed under the Apache License, Version 2.0 (the "License");
84+
you may not use this file except in compliance with the License.
85+
You may obtain a copy of the License at
86+
87+
http://www.apache.org/licenses/LICENSE-2.0
88+
89+
Unless required by applicable law or agreed to in writing, software
90+
distributed under the License is distributed on an "AS IS" BASIS,
91+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
92+
See the License for the specific language governing permissions and
93+
limitations under the License.
94+

api/v1/groupversion_info.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2022.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package v1 contains API Schema definitions for the featuretoggling v1 API group
18+
// +kubebuilder:object:generate=true
19+
// +groupName=featuretoggling.nais.io
20+
package v1
21+
22+
import (
23+
"k8s.io/apimachinery/pkg/runtime/schema"
24+
"sigs.k8s.io/controller-runtime/pkg/scheme"
25+
)
26+
27+
var (
28+
// GroupVersion is group version used to register these objects
29+
GroupVersion = schema.GroupVersion{Group: "featuretoggling.nais.io", Version: "v1"}
30+
31+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
32+
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
33+
34+
// AddToScheme adds the types in this group-version to the given scheme.
35+
AddToScheme = SchemeBuilder.AddToScheme
36+
)

0 commit comments

Comments
 (0)