Skip to content
This repository was archived by the owner on Apr 24, 2024. It is now read-only.

Commit 60bcfbe

Browse files
committed
Initial widget example
Signed-off-by: Andy Goldstein <andy.goldstein@redhat.com>
1 parent 8e36286 commit 60bcfbe

39 files changed

+1346
-15
lines changed

.dockerignore

Lines changed: 4 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ vendor/
1818
.idea
1919

2020
hack/tools
21+
22+
bin/

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Build the manager binary
2+
FROM golang:1.17 as builder
3+
4+
WORKDIR /workspace
5+
# Copy the Go Modules manifests
6+
COPY go.mod go.mod
7+
COPY go.sum go.sum
8+
# cache deps before building and copying source so that we don't need to re-download as much
9+
# and so that source changes don't invalidate our downloaded layer
10+
RUN go mod download
11+
12+
# Copy the go source
13+
COPY main.go main.go
14+
COPY api/ api/
15+
COPY controllers/ controllers/
16+
17+
# Build
18+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
19+
20+
# Use distroless as minimal base image to package the manager binary
21+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
22+
FROM gcr.io/distroless/static:nonroot
23+
WORKDIR /
24+
COPY --from=builder /workspace/manager .
25+
USER 65532:65532
26+
27+
ENTRYPOINT ["/manager"]

Makefile

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
2+
# Image URL to use all building/pushing image targets
3+
IMG ?= controller:latest
4+
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
5+
ENVTEST_K8S_VERSION = 1.23
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+
# This is a requirement for 'setup-envtest.sh' in the test target.
16+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
17+
SHELL = /usr/bin/env bash -o pipefail
18+
.SHELLFLAGS = -ec
19+
20+
.PHONY: all
21+
all: build
22+
23+
##@ General
24+
25+
# The help target prints out all targets with their descriptions organized
26+
# beneath their categories. The categories are represented by '##@' and the
27+
# target descriptions by '##'. The awk commands is responsible for reading the
28+
# entire set of makefiles included in this invocation, looking for lines of the
29+
# file as xyz: ## something, and then pretty-format the target and help. Then,
30+
# if there's a line with ##@ something, that gets pretty-printed as a category.
31+
# More info on the usage of ANSI control characters for terminal formatting:
32+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
33+
# More info on the awk command:
34+
# http://linuxcommand.org/lc3_adv_awk.php
35+
36+
.PHONY: help
37+
help: ## Display this help.
38+
@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)
39+
40+
##@ Development
41+
42+
.PHONY: manifests
43+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
44+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
45+
46+
.PHONY: apiexport
47+
apiexport: PREFIX ?= today
48+
apiexport: $(KUSTOMIZE)
49+
#$(KUSTOMIZE) build config/crd | kubectl kcp crd sync -f - --prefix $(PREFIX) >
50+
51+
.PHONY: generate
52+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
53+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
54+
55+
.PHONY: fmt
56+
fmt: ## Run go fmt against code.
57+
go fmt ./...
58+
59+
.PHONY: vet
60+
vet: ## Run go vet against code.
61+
go vet ./...
62+
63+
.PHONY: test
64+
test: manifests generate fmt vet envtest ## Run tests.
65+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
66+
67+
##@ Build
68+
69+
.PHONY: build
70+
build: generate fmt vet ## Build manager binary.
71+
go build -o bin/manager main.go
72+
73+
.PHONY: run
74+
run: manifests generate fmt vet ## Run a controller from your host.
75+
go run ./main.go
76+
77+
.PHONY: docker-build
78+
docker-build: test ## Build docker image with the manager.
79+
docker build -t ${IMG} .
80+
81+
.PHONY: docker-push
82+
docker-push: ## Push docker image with the manager.
83+
docker push ${IMG}
84+
85+
##@ Deployment
86+
87+
ifndef ignore-not-found
88+
ignore-not-found = false
89+
endif
90+
91+
.PHONY: install
92+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
93+
$(KUSTOMIZE) build config/crd | kubectl apply -f -
94+
95+
.PHONY: uninstall
96+
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.
97+
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
98+
99+
.PHONY: deploy
100+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
101+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
102+
$(KUSTOMIZE) build config/default | kubectl apply -f -
103+
104+
.PHONY: undeploy
105+
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.
106+
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
107+
108+
##@ Build Dependencies
109+
110+
## Location to install dependencies to
111+
LOCALBIN ?= $(shell pwd)/bin
112+
$(LOCALBIN):
113+
mkdir -p $(LOCALBIN)
114+
115+
## Tool Binaries
116+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
117+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
118+
ENVTEST ?= $(LOCALBIN)/setup-envtest
119+
120+
## Tool Versions
121+
KUSTOMIZE_VERSION ?= v3.8.7
122+
CONTROLLER_TOOLS_VERSION ?= v0.8.0
123+
124+
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
125+
.PHONY: kustomize
126+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
127+
$(KUSTOMIZE): $(LOCALBIN)
128+
curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN)
129+
130+
.PHONY: controller-gen
131+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
132+
$(CONTROLLER_GEN): $(LOCALBIN)
133+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
134+
135+
.PHONY: envtest
136+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
137+
$(ENVTEST): $(LOCALBIN)
138+
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+
domain: my.domain
2+
layout:
3+
- go.kubebuilder.io/v3
4+
projectName: controller-runtime-example
5+
repo: github.com/kcp-dev/controller-runtime-example
6+
resources:
7+
- api:
8+
crdVersion: v1
9+
namespaced: true
10+
controller: true
11+
domain: my.domain
12+
group: data
13+
kind: Widget
14+
path: github.com/kcp-dev/controller-runtime-example/api/v1alpha1
15+
version: v1alpha1
16+
version: "3"

README.md

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

api/v1alpha1/groupversion_info.go

Lines changed: 36 additions & 0 deletions
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 v1alpha1 contains API Schema definitions for the data v1alpha1 API group
18+
//+kubebuilder:object:generate=true
19+
//+groupName=data.my.domain
20+
package v1alpha1
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: "data.my.domain", Version: "v1alpha1"}
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+
)

api/v1alpha1/widget_types.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 v1alpha1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
22+
23+
// WidgetSpec defines the desired state of Widget
24+
type WidgetSpec struct {
25+
Foo string `json:"foo,omitempty"`
26+
}
27+
28+
// WidgetStatus defines the observed state of Widget
29+
type WidgetStatus struct {
30+
Total int `json:"total,omitempty"`
31+
}
32+
33+
// +kubebuilder:object:root=true
34+
// +kubebuilder:subresource:status
35+
36+
// Widget is the Schema for the widgets API
37+
type Widget struct {
38+
metav1.TypeMeta `json:",inline"`
39+
metav1.ObjectMeta `json:"metadata,omitempty"`
40+
41+
Spec WidgetSpec `json:"spec,omitempty"`
42+
Status WidgetStatus `json:"status,omitempty"`
43+
}
44+
45+
// +kubebuilder:object:root=true
46+
47+
// WidgetList contains a list of Widget
48+
type WidgetList struct {
49+
metav1.TypeMeta `json:",inline"`
50+
metav1.ListMeta `json:"metadata,omitempty"`
51+
Items []Widget `json:"items"`
52+
}
53+
54+
func init() {
55+
SchemeBuilder.Register(&Widget{}, &WidgetList{})
56+
}

0 commit comments

Comments
 (0)