Skip to content

Commit

Permalink
Add deployment support to operator - Dockerfile, rbac and deployment …
Browse files Browse the repository at this point in the history
…yaml
  • Loading branch information
gautamp8 committed Jul 8, 2020
1 parent de584c9 commit b1c6499
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 5 deletions.
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:3.8

WORKDIR /usr/src/

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD kopf run handlers.py --verbose
2 changes: 1 addition & 1 deletion deploy/cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: example-celery-obj
spec:
common:
appName: celery-crd-example
appName: celery-example
celeryApp: 'app:celery_app'
image: example-image
workerSpec:
Expand Down
37 changes: 37 additions & 0 deletions deploy/operator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: celery-operator
name: celery-operator
namespace: default
spec:
minReadySeconds: 5
progressDeadlineSeconds: 600
replicas: 1
selector:
matchLabels:
app: celery-operator
strategy:
rollingUpdate:
maxSurge: 20%
maxUnavailable: 0%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: celery-operator
spec:
serviceAccountName: celery-account
containers:
- name: celery-operator
image: celery-operator
imagePullPolicy: Never
resources:
requests:
cpu: "100m"
memory: "64Mi"
limits:
cpu: "200m"
memory: "128Mi"
88 changes: 88 additions & 0 deletions deploy/rbac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
apiVersion: v1
kind: ServiceAccount
metadata:
namespace: default
name: celery-account
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
name: celery-role-cluster
rules:

# Framework: knowing which other operators are running (i.e. peering).
- apiGroups: [zalando.org]
resources: [clusterkopfpeerings]
verbs: [list, watch, patch, get]
- apiGroups: [apiextensions.k8s.io]
resources: [customresourcedefinitions]
verbs: [list, get]

# Application: read-only access for watching cluster-wide.
- apiGroups: [celeryproject.org]
resources: [celery]
verbs: [list, watch]
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:
namespace: default
name: celery-role-namespaced
rules:

# Framework: knowing which other operators are running (i.e. peering).
- apiGroups: [zalando.org]
resources: [kopfpeerings]
verbs: [list, watch, patch, get]

# Framework: posting the events about the handlers progress/errors.
- apiGroups: [events.k8s.io]
resources: [events]
verbs: [create]
- apiGroups: [""]
resources: [events]
verbs: [create]

# Application: watching & handling for the custom resource we declare.
- apiGroups: [celeryproject.org]
resources: [celery]
verbs: [list, watch, patch]

# Application: other resources it produces and manipulates.
# Here, we create Jobs+PVCs+Pods, but we do not patch/update/delete them ever.
- apiGroups: [batch, extensions]
resources: [jobs]
verbs: [create]
- apiGroups: [""]
resources: [pods, services]
verbs: ['*']
- apiGroups: ["apps"]
resources: [deployments, replicasets]
verbs: ['*']
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: celery-rolebinding-cluster
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: celery-role-cluster
subjects:
- kind: ServiceAccount
name: celery-account
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
namespace: default
name: celery-rolebinding-namespaced
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: celery-role-namespaced
subjects:
- kind: ServiceAccount
name: celery-account
24 changes: 20 additions & 4 deletions handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,33 @@ def check_flower_label(value, spec, **_):
return value == f"{spec['common']['appName']}-flower"


def get_flower_svc_host(status):
"""
Get latest flower SVC host from parent's status
"""
handler = status.get('update_fn') or status.get('create_fn')

for child in handler.get('children'):
if child.get('kind') == constants.SERVICE_KIND and child.get('type') == constants.FLOWER_TYPE: # NOQA
return f"{child.get('name')}:{child['spec']['ports'][0]['port']}"

return None


@kopf.timer('celeryproject.org', 'v1alpha1', 'celery',
initial_delay=50000, interval=100000, idle=10)
initial_delay=5, interval=10, idle=10)
def message_queue_length(spec, status, **kwargs):
flower_svc_host = "http://192.168.64.2:32289"
url = f"{flower_svc_host}/api/queues/length"
flower_svc_host = get_flower_svc_host(status)
if not flower_svc_host:
return

url = f"http://{flower_svc_host}/api/queues/length"
response = requests.get(url=url)
if response.status_code == 200:
return response.json().get('active_queues')

return {
"queue_length": None
"queue_length": 0
}


Expand Down

0 comments on commit b1c6499

Please sign in to comment.