Skip to content

Commit d7a1f80

Browse files
committed
solution for assigment2
1 parent b4bdc89 commit d7a1f80

Some content is hidden

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

41 files changed

+2499
-0
lines changed

assignment2/.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/

assignment2/.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# Binaries for programs and plugins
3+
*.exe
4+
*.exe~
5+
*.dll
6+
*.so
7+
*.dylib
8+
bin
9+
testbin/*
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+
*.swp
24+
*.swo
25+
*~

assignment2/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.18 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"]

assignment2/Makefile

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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.24.1
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: generate
47+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
48+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
49+
50+
.PHONY: fmt
51+
fmt: ## Run go fmt against code.
52+
go fmt ./...
53+
54+
.PHONY: vet
55+
vet: ## Run go vet against code.
56+
go vet ./...
57+
58+
.PHONY: test
59+
test: manifests generate fmt vet envtest ## Run tests.
60+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
61+
62+
##@ Build
63+
64+
.PHONY: build
65+
build: generate fmt vet ## Build manager binary.
66+
go build -o bin/manager main.go
67+
68+
.PHONY: run
69+
run: manifests generate fmt vet ## Run a controller from your host.
70+
go run ./main.go
71+
72+
.PHONY: docker-build
73+
docker-build: test ## Build docker image with the manager.
74+
docker build -t ${IMG} .
75+
76+
.PHONY: docker-push
77+
docker-push: ## Push docker image with the manager.
78+
docker push ${IMG}
79+
80+
##@ Deployment
81+
82+
ifndef ignore-not-found
83+
ignore-not-found = false
84+
endif
85+
86+
.PHONY: install
87+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
88+
$(KUSTOMIZE) build config/crd | kubectl apply -f -
89+
90+
.PHONY: uninstall
91+
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.
92+
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
93+
94+
.PHONY: deploy
95+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
96+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
97+
$(KUSTOMIZE) build config/default | kubectl apply -f -
98+
99+
.PHONY: undeploy
100+
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.
101+
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
102+
103+
##@ Build Dependencies
104+
105+
## Location to install dependencies to
106+
LOCALBIN ?= $(shell pwd)/bin
107+
$(LOCALBIN):
108+
mkdir -p $(LOCALBIN)
109+
110+
## Tool Binaries
111+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
112+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
113+
ENVTEST ?= $(LOCALBIN)/setup-envtest
114+
115+
## Tool Versions
116+
KUSTOMIZE_VERSION ?= v3.8.7
117+
CONTROLLER_TOOLS_VERSION ?= v0.9.0
118+
119+
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
120+
.PHONY: kustomize
121+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
122+
$(KUSTOMIZE): $(LOCALBIN)
123+
curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN)
124+
125+
.PHONY: controller-gen
126+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
127+
$(CONTROLLER_GEN): $(LOCALBIN)
128+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
129+
130+
.PHONY: envtest
131+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
132+
$(ENVTEST): $(LOCALBIN)
133+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

assignment2/PROJECT

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
domain: assignment.com
2+
layout:
3+
- go.kubebuilder.io/v3
4+
projectName: assignment2
5+
repo: assignment.com/bookstore
6+
resources:
7+
- api:
8+
crdVersion: v1
9+
namespaced: true
10+
controller: true
11+
domain: assignment.com
12+
group: myapps
13+
kind: BookStore
14+
path: assignment.com/bookstore/api/v1
15+
version: v1
16+
version: "3"

assignment2/README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# assignment2
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>/assignment2: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>/assignment2: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+

assignment2/api/v1/bookstore_types.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
22+
23+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
24+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
25+
26+
// BookStoreSpec defines the desired state of BookStore
27+
type BookStoreSpec struct {
28+
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
29+
// Important: Run "make" to regenerate code after modifying this file
30+
31+
// Name of the book store
32+
// +kubebuilder:validation:MinLength=4
33+
// +kubebuilder:validation:MaxLength=15
34+
// +kubebuilder:validation:Required
35+
Name string `json:"name"`
36+
// +kubebuilder:validation:MinLength=3
37+
// +kubebuilder:validation:Required
38+
// Location of the book store
39+
Location string `json:"location"`
40+
// +kubebuilder:validation:MinLength=3
41+
// +kubebuilder:validation:Required
42+
// Owner of the book store
43+
Owner string `json:"owner"`
44+
// +kubebuilder:validation:MinLength=10
45+
// +kubebuilder:validation:MaxLength=10
46+
// +kubebuilder:validation:Required
47+
// +kubebuilder:validation:Pattern=`^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$`
48+
// Date of establishment of the book store (YYYY-MM-DD)
49+
Established string `json:"established"`
50+
}
51+
52+
// BookStoreStatus defines the observed state of BookStore
53+
type BookStoreStatus struct {
54+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
55+
// Important: Run "make" to regenerate code after modifying this file
56+
// +kubebuilder:validation:Minimum=0
57+
CurrentBooksCount int32 `json:"currentBooksCount"`
58+
LastBookAdded string `json:"lastBookAdded"`
59+
LastUpdateDate string `json:"lastUpdateDate"`
60+
LastUpdateTime string `json:"lastUpdateTime"`
61+
}
62+
63+
//+kubebuilder:object:root=true
64+
//+kubebuilder:subresource:status
65+
66+
// BookStore is the Schema for the bookstores API
67+
type BookStore struct {
68+
metav1.TypeMeta `json:",inline"`
69+
metav1.ObjectMeta `json:"metadata,omitempty"`
70+
71+
Spec BookStoreSpec `json:"spec,omitempty"`
72+
Status BookStoreStatus `json:"status,omitempty"`
73+
}
74+
75+
//+kubebuilder:object:root=true
76+
77+
// BookStoreList contains a list of BookStore
78+
type BookStoreList struct {
79+
metav1.TypeMeta `json:",inline"`
80+
metav1.ListMeta `json:"metadata,omitempty"`
81+
Items []BookStore `json:"items"`
82+
}
83+
84+
func init() {
85+
SchemeBuilder.Register(&BookStore{}, &BookStoreList{})
86+
}

0 commit comments

Comments
 (0)