This commit is contained in:
2024-02-20 17:15:27 +08:00
committed by huty
parent 6706e1a633
commit 34158042ad
1529 changed files with 177765 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
# Ch20 lab
## Setup
Deploy the custom controller:
```
kubectl apply -f lab/timecheck-controller/
```
Try to create custom resource:
```
kubectl apply -f lab/timecheck-test.yaml
```
Check the controller logs:
```
kubectl logs -l app=timecheck-controller --tail 100
```
> This controller doesn't handle the situation where the CRD doesn't exist, so the container exits and the Pod will keep restarting
## Sample Solution
The [CRD](./solution/timecheck-crd.yaml) specifies the structure of the custom timecheck resource.
```
kubectl apply -f lab/solution/timecheck-crd.yaml
```
Restart the controller - it will be in CrashLoopBackOff status by now:
```
kubectl delete pods -l app=timecheck-controller
```
Now you can create the custom resource:
```
kubectl apply -f lab/timecheck-test.yaml
```
Check the controller has created a Deployment:
```
kubectl logs -l app=timecheck-controller --tail 4
```
List the timecheck resources:
```
kubectl get tc
```
And check the logs from the timecheck app:
```
kubectl logs -l app=timecheck -c logger
```
## Teardown
Delete the CRD:
```
kubectl delete crd timechecks.ch20.kiamol.net
```
And the controller:
```
kubectl delete -f lab/timecheck-controller/
```

View File

@@ -0,0 +1,39 @@
apiVersion: apiextensions.k8s.io/v1 # minimum 1.16
kind: CustomResourceDefinition
metadata:
name: timechecks.ch20.kiamol.net
labels:
kiamol: ch20
spec:
group: ch20.kiamol.net
scope: Namespaced
names:
plural: timechecks
singular: timecheck
kind: TimeCheck
shortNames:
- tc
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
environment:
type: string
interval:
type: integer
additionalPrinterColumns:
- name: Environment
type: string
description: Environment
jsonPath: .spec.environment
- name: Interval
type: string
description: Interval
jsonPath: .spec.interval

View File

@@ -0,0 +1,7 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: timecheck-controller
labels:
kiamol: ch20-lab
automountServiceAccountToken: false

View File

@@ -0,0 +1,28 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: timecheck-controller
labels:
kiamol: ch20-lab
rules:
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["list", "create", "delete"]
- apiGroups: ["ch20.kiamol.net"]
resources: ["timechecks"]
verbs: ["list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: timecheck-controller
labels:
kiamol: ch20-lab
subjects:
- kind: ServiceAccount
name: timecheck-controller
namespace: default
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: timecheck-controller

View File

@@ -0,0 +1,20 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: timecheck-controller
labels:
kiamol: ch20-lab
spec:
selector:
matchLabels:
app: timecheck-controller
template:
metadata:
labels:
app: timecheck-controller
spec:
serviceAccountName: timecheck-controller
automountServiceAccountToken: true
containers:
- image: kiamol/ch20-timecheck-controller
name: controller

View File

@@ -0,0 +1,9 @@
apiVersion: "ch20.kiamol.net/v1"
kind: TimeCheck
metadata:
name: test
labels:
kiamol: ch20-lab
spec:
environment: TEST
interval: 10