#!/bin/bash
if [[ $1 == "--config" ]] ; then
cat <<EOF
{
"onKubernetesEvent": [
{
"kind": "namespace",
"event": [ "add" ]
}
]
}
EOF
fi
#!/bin/bash
if [[ $1 == "--config" ]] ; then
# configuration
cat <<EOF
{
"onKubernetesEvent": [
{
"kind": "namespace",
"event": [ "add" ]
}
]
}
EOF
else
# response:
# find out what namespace has emerged
createdNamespace=$(jq -r '.[0].resourceName' $BINDING_CONTEXT_PATH)
# create the appropriate secret in it
kubectl create -n ${createdNamespace} -f - <<EOF
apiVersion: v1
kind: Secret
metadata:
...
data:
...
EOF
fi
$ cat Dockerfile
FROM flant/shell-operator:v1.0.0-beta.1-alpine3.9
ADD namespace-hook.sh /hooks
$ docker build -t registry.example.com/my-operator:v1 .
$ docker push registry.example.com/my-operator:v1
-
它訂閱了 namespace的建立事件;
-
它在不與它所執行的名稱空間相同的空間建立一個secret。
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: monitor-namespaces-acc
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
name: monitor-namespaces
rules:
- apiGroups: [""]
resources: ["namespaces"]
verbs: ["get", "watch", "list"]
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "list", "create", "patch"]
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: monitor-namespaces
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: monitor-namespaces
subjects:
- kind: ServiceAccount
name: monitor-namespaces-acc
namespace: example-monitor-namespaces
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-operator
spec:
template:
spec:
containers:
- name: my-operator
image: registry.example.com/my-operator:v1
serviceAccountName: monitor-namespaces-acc
$ kubectl create ns example-monitor-namespaces
$ kubectl -n example-monitor-namespaces apply -f rbac.yaml
$ kubectl -n example-monitor-namespaces apply -f deployment.yaml
cat <<EOF
{
"onKubernetesEvent": [
{
"kind": "deployment",
"event":["update"],
"jqFilter": ".metadata.labels"
}
]
}
EOF
{
"onKubernetesEvent": [
{
"name": "OnCreatePod",
"kind": "pod",
"event": [
"add"
]
},
{
"name": "OnModifiedNamespace",
"kind": "namespace",
"event": [
"update"
],
"jqFilter": ".metadata.labels"
}
],
"schedule": [
{
"name": "every 10 min",
"crontab": "0 */10 * * * *"
},
{
"name": "on Mondays at 12:10",
"crontab": "0 10 12 * * 1"
}
]
}
[{ "binding": "every 10 min" }]
[{ "binding": "every 10 min" }, { "binding": "on Mondays at 12:10" }]
[
{
"binding": "onCreatePod",
"resourceEvent": "add",
"resourceKind": "pod",
"resourceName": "foo",
"resourceNamespace": "bar"
}
]
jq -r '.[0].resourceName' $BINDING_CONTEXT_PATH
-
https://github.com/flant/shell-operator
-
https://flant.com/
-
https://github.com/flant/shell-operator/blob/master/HOOKS.md
-
https://github.com/flant/shell-operator/blob/master/HOOKS.md#binding-context
-
https://github.com/flant/shell-operator/tree/master/examples
-
https://github.com/flant/shell-operator/blob/master/METRICS.md
-
https://github.com/flant/shell-operator
朋友會在“發現-看一看”看到你“在看”的內容