Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more examples #15

Merged
merged 1 commit into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions composite-controller/resync-period/deploy/controller.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: metacontroller.k8s.io/v1alpha1
kind: CompositeController
metadata:
name: ping-pong-controller
spec:
generateSelector: true
parentResource:
apiVersion: example.com/v1
resource: pings
childResources:
- apiVersion: example.com/v1
resource: pongs
updateStrategy:
method: InPlace
resyncPeriodSeconds: 10
hooks:
sync:
webhook:
url: http://192.168.1.15:8080/sync
29 changes: 29 additions & 0 deletions composite-controller/resync-period/deploy/crd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: pings.example.com
spec:
group: example.com
version: v1
names:
kind: Ping
plural: pings
singular: ping
scope: Namespaced
subresources:
status: {}
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: pongs.example.com
spec:
group: example.com
version: v1
names:
kind: Pong
plural: pongs
singular: pong
scope: Namespaced
subresources:
status: {}
6 changes: 6 additions & 0 deletions composite-controller/resync-period/deploy/ping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: example.com/v1
kind: Ping
metadata:
name: shovan
spec:
name: Shovan Maity
40 changes: 40 additions & 0 deletions composite-controller/resync-period/python/sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from http.server import BaseHTTPRequestHandler, HTTPServer
import json


class Controller(BaseHTTPRequestHandler):

def do_POST(self):

# Observed ping object
observed = json.loads(self.rfile.read(
int(self.headers.get("content-length"))))
ping = observed["parent"]

name = ping.get("spec", {}).get("name", "Unknown")

pong = [
{
"apiVersion": "example.com/v1",
"kind": "Pong",
"metadata": {
"name": ping["metadata"]["name"]
},
"spec": {
"message": "Hello %s !!" % (name)
}
}
]

# Generate desired children
desired = {
"children": pong
}

self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(desired).encode())


HTTPServer(("", 8080), Controller).serve_forever()
53 changes: 53 additions & 0 deletions composite-controller/resync-period/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
### Concept
By default, your sync hook will only be called when something changes in one of the resources you’re watching, or when the local cache is flushed. The `resyncPeriodSeconds` value specifies how often to do this. Each time it triggers, Metacontroller will send sync hook requests for all objects of the parent resource type, with the latest observed values of all the necessary objects.

### Prerequisite

Make sure metacontroller is [installed](https://github.com/shovanmaity/metacontroller-by-example/tree/master/metacontroller).

Make sure you are inside `composite-controller/resync-period` directory.

Edit the `deploy/controller.yaml` file and update the webhook URL.
```yaml
spec:
hooks:
sync:
webhook:
url: http://192.168.1.15:8080/sync
```

Apply the crd and controller.
```bash
kubectl apply -f deploy/crd.yaml
kubectl apply -f deploy/controller.yaml
```

Execute python files
```bash
python3 python/sync.py
```

Create a ping cr. Find the sample `Ping` is [here](https://github.com/shovanmaity/metacontroller-by-example/blob/master/composite-controller/resync-period/deploy/ping.yaml).
```bash
cat <<EOF | kubectl apply -f -
apiVersion: example.com/v1
kind: Ping
metadata:
name: shovan
spec:
name: Shovan Maity
EOF
```

Try the below processes -

- Check the logs of python file execution.

### Cleanup

```bash
kubectl delete ping -A --all
kubectl delete -f deploy/controller.yaml
kubectl delete -f deploy/crd.yaml
# Stop the python files execution.
```
22 changes: 22 additions & 0 deletions composite-controller/sync-finalize-hook/deploy/controller.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: metacontroller.k8s.io/v1alpha1
kind: CompositeController
metadata:
name: ping-pong-controller
spec:
generateSelector: true
parentResource:
apiVersion: example.com/v1
resource: pings
childResources:
- apiVersion: example.com/v1
resource: pongs
updateStrategy:
method: InPlace
resyncPeriodSeconds: 10
hooks:
sync:
webhook:
url: http://192.168.1.15:8080/sync
finalize:
webhook:
url: http://192.168.1.15:9090/sync
29 changes: 29 additions & 0 deletions composite-controller/sync-finalize-hook/deploy/crd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: pings.example.com
spec:
group: example.com
version: v1
names:
kind: Ping
plural: pings
singular: ping
scope: Namespaced
subresources:
status: {}
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: pongs.example.com
spec:
group: example.com
version: v1
names:
kind: Pong
plural: pongs
singular: pong
scope: Namespaced
subresources:
status: {}
6 changes: 6 additions & 0 deletions composite-controller/sync-finalize-hook/deploy/ping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: example.com/v1
kind: Ping
metadata:
name: shovan
spec:
name: Shovan Maity
24 changes: 24 additions & 0 deletions composite-controller/sync-finalize-hook/python/finalize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from http.server import BaseHTTPRequestHandler, HTTPServer
import json


class Controller(BaseHTTPRequestHandler):

def do_POST(self):
observed = json.loads(self.rfile.read(
int(self.headers.get("content-length"))))
observed_pong_map = observed["children"].get(
"Pong.example.com/v1", {})

finalized = len(observed_pong_map) == 0
desired = {
"finalized": finalized
}

self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(desired).encode())


HTTPServer(("", 9090), Controller).serve_forever()
40 changes: 40 additions & 0 deletions composite-controller/sync-finalize-hook/python/sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from http.server import BaseHTTPRequestHandler, HTTPServer
import json


class Controller(BaseHTTPRequestHandler):

def do_POST(self):

# Observed ping object
observed = json.loads(self.rfile.read(
int(self.headers.get("content-length"))))
ping = observed["parent"]

name = ping.get("spec", {}).get("name", "Unknown")

pong = [
{
"apiVersion": "example.com/v1",
"kind": "Pong",
"metadata": {
"name": ping["metadata"]["name"]
},
"spec": {
"message": "Hello %s !!" % (name)
}
}
]

# Generate desired children
desired = {
"children": pong
}

self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(desired).encode())


HTTPServer(("", 8080), Controller).serve_forever()
86 changes: 86 additions & 0 deletions composite-controller/sync-finalize-hook/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
### Concept

`CompositeController` contains 2 hooks `sync` and `finalize`.

The `sync` hook is how you specify which children to create/maintain for a given parent – in other words, your desired state. After you return your desired state, Metacontroller begins to take action to converge towards it – creating, deleting, and updating objects as appropriate.

Sync Hook Request
- `controller` - The whole `CompositeController` cr, like - `kubectl get compositecontroller <name> -o json`.
- `parent` - The parent object, like `kubectl get <parent-resource> <parent-name> -o json`.
- `children` - An associative array of child objects that already exist.
- `finalizing` - This is always false for the sync hook.

Sync Hook Response
- `status` - A JSON object that will completely replace the status field within the parent object.
- `children` - A list of JSON objects representing all the desired children for this parent object.
- `resyncAfterSeconds` - Set the delay (in seconds, as a float) before an optional, one-time, per-object resync.

The `finalize` hook is useful for doing ordered teardown of children. Metacontroller will add a finalizer to the parent object, which will prevent it from being deleted until your hook has had a chance to run and the response indicates that you’re done cleaning up. To perform ordered teardown, you can generate children just like you would for sync, but omit some children from the desired state depending on the observed set of children that are left. For example, if you observe [A,B,C], generate only [A,B] as your desired state; if you observe [A,B], generate only [A]; if you observe [A], return an empty desired list [].

Finalize Hook Request
- `controller` - The whole `CompositeController` cr, like - `kubectl get compositecontroller <name> -o json`.
- `parent` - The parent object, like `kubectl get <parent-resource> <parent-name> -o json`.
- `children` - An associative array of child objects that already exist.
- `finalizing` - This is always true for the sync hook.

Finalize Hook Response
- `status` - A JSON object that will completely replace the status field within the parent object.
- `children` - A list of JSON objects representing all the desired children for this parent object.
- `resyncAfterSeconds` - Set the delay (in seconds, as a float) before an optional, one-time, per-object resync.
- `finalized` - A boolean indicating whether you are done finalizing.

### Prerequisite

Make sure metacontroller is [installed](https://github.com/shovanmaity/metacontroller-by-example/tree/master/metacontroller).

Make sure you are inside `composite-controller/sync-finalize-hook` directory.

Edit the `deploy/controller.yaml` file and update the webhook URL.
```yaml
spec:
hooks:
sync:
webhook:
url: http://192.168.1.15:8080/sync
finalize:
webhook:
url: http://192.168.1.15:9090/sync
```

Apply the crd and controller.
```bash
kubectl apply -f deploy/crd.yaml
kubectl apply -f deploy/controller.yaml
```

Execute python files
```bash
python3 python/sync.py
python3 python/finalize.py
```

Create a ping cr. Find the sample `Ping` is [here](https://github.com/shovanmaity/metacontroller-by-example/blob/master/composite-controller/sync-finalize-hook/deploy/ping.yaml).
```bash
cat <<EOF | kubectl apply -f -
apiVersion: example.com/v1
kind: Ping
metadata:
name: shovan
spec:
name: Shovan Maity
EOF
```

Try the below processes -

- Get the `metadata.finalizers` of the ping cr using `kubectl get ping -o=jsonpath='{range .items[*]}{@.metadata.finalizers}{"\n"}{end}'`.
- Delete the `Ping` cr using `kubectl delete ping -A --all`.
- List `Pong` cr using `kubectl get ping -A`.

### Cleanup

```bash
kubectl delete -f deploy/controller.yaml
kubectl delete -f deploy/crd.yaml
# Stop the python files execution.
```
18 changes: 18 additions & 0 deletions composite-controller/target-status/deploy/controller.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: metacontroller.k8s.io/v1alpha1
kind: CompositeController
metadata:
name: ping-pong-controller
spec:
generateSelector: true
parentResource:
apiVersion: example.com/v1
resource: pings
childResources:
- apiVersion: example.com/v1
resource: pongs
updateStrategy:
method: InPlace
hooks:
sync:
webhook:
url: http://192.168.1.15:8080/sync
Loading