Shell-operator:用于简化Kubernetes operator的创建
Posted 分布式实验室
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shell-operator:用于简化Kubernetes operator的创建相关的知识,希望对你有一定的参考价值。
#!/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
以上是关于Shell-operator:用于简化Kubernetes operator的创建的主要内容,如果未能解决你的问题,请参考以下文章