Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 8710b55

Browse files
committed
Initial commit
1 parent 0342f4e commit 8710b55

File tree

12 files changed

+478
-1
lines changed

12 files changed

+478
-1
lines changed

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM python:3.6-alpine
2+
3+
RUN pip install \
4+
"kubernetes==1.0.0b1" \
5+
"jinja2~=2.9.5" \
6+
"pyyaml~=3.12" \
7+
"click~=6.7"
8+
9+
WORKDIR /usr/src/app
10+
COPY . /usr/src/app
11+
12+
CMD ["python", "./opy.py"]

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,45 @@
1-
# operator-example-python
1+
# Kubernetes Operator
2+
3+
Experimantal example written in Python. Work in progress. Improve me if you like :)
4+
5+
6+
Some commands to test:
7+
8+
```bash
9+
docker build --tag local/opy .
10+
11+
context="my-kubectl-config-context"
12+
13+
14+
kubectl --context $context apply --filename ./ghost/ghost-thirdparty.yaml
15+
16+
docker run -v $HOME/.kube:/root/.kube -t local/opy python opy.py \
17+
--context $context \
18+
--api-version experimantal.giantswarm.com/v1 \
19+
--kind Ghost
20+
21+
kubectl --context $context create --filename ./ghost/ghost-1.yaml
22+
23+
kubectl --context $context create --filename ./ghost/ghost-2.yaml
24+
25+
kubectl --context $context get --all-namespaces ghosts
26+
27+
28+
kubectl --context $context get --all-namespaces ingress -o wide
29+
30+
kubectl --context $context run -ti --rm tiny-tools --image giantswarm/tiny-tools curl ghost-1.ghost-test-1.svc:2368
31+
32+
33+
kubectl --context $context --namespace ghost-test-1 delete ghost ghost-1
34+
35+
kubectl --context $context --namespace ghost-another-test delete ghost ghost-2
36+
37+
kubectl --context $context delete thirdpartyresource ghost.experimantal.giantswarm.com
38+
39+
kubectl --context $context delete namespace --now ghost-test-1
40+
41+
kubectl --context $context delete namespace --now ghost-another-test
42+
43+
kubectl --context $context get namespaces
44+
45+
```

generic_api.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from __future__ import absolute_import
2+
import sys
3+
import os
4+
import re
5+
6+
# python 2 and python 3 compatibility library
7+
from six import iteritems
8+
9+
from kubernetes.client.configuration import Configuration
10+
from kubernetes.client.api_client import ApiClient
11+
12+
13+
class GenericApi(object):
14+
15+
def __init__(self, api_client=None):
16+
config = Configuration()
17+
if api_client:
18+
self.api_client = api_client
19+
else:
20+
if not config.api_client:
21+
config.api_client = ApiClient()
22+
self.api_client = config.api_client
23+
24+
def list_generic(self, **kwargs):
25+
# print(kwargs)
26+
kwargs['_return_http_data_only'] = True
27+
if kwargs.get('callback'):
28+
return self._list_generic(**kwargs)
29+
else:
30+
(data) = self._list_generic(**kwargs)
31+
print(data)
32+
return data
33+
34+
def _list_generic(self, **kwargs):
35+
resource_path = kwargs.pop('resource_path')
36+
kwargs['header_params'] = {
37+
'Accept': self.api_client.select_header_accept(['application/json', 'application/json;stream=watch']),
38+
'Content-Type': self.api_client.select_header_content_type(['*/*'])
39+
}
40+
kwargs['auth_settings'] = ['BearerToken']
41+
kwargs['response_type'] = object
42+
if 'query_params' not in kwargs:
43+
kwargs['query_params'] = {}
44+
if 'timeout_seconds' in kwargs:
45+
kwargs['query_params']['timeoutSeconds'] = kwargs.pop('timeout_seconds')
46+
if 'watch' in kwargs:
47+
kwargs['query_params']['watch'] = kwargs.pop('watch')
48+
return self.api_client.call_api(resource_path, 'GET', **kwargs)
49+
50+
def call_api(self, resource_path, method, **kwargs):
51+
kwargs['response_type'] = object
52+
return self.api_client.call_api(resource_path, method, **kwargs)

ghost/ghost-1.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: experimantal.giantswarm.com/v1
2+
kind: Ghost
3+
metadata:
4+
name: ghost-1
5+
namespace: ghost-test-1
6+
spec:
7+
url: "ghost-1.kube.thisone.rocks"
8+
mail:
9+
from: '"Custom Name" <myemail@address.com>'
10+
service:
11+
type: Sparkpost
12+
auth:
13+
user: SMTP_Injection
14+
pass:
15+
database:
16+
client: sqlite3

ghost/ghost-2.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: experimantal.giantswarm.com/v1
2+
kind: Ghost
3+
metadata:
4+
name: ghost-2
5+
namespace: ghost-another-test
6+
spec:
7+
# url: "ghost-2.some.other.url"
8+
mail:
9+
from: '"Custom Name" <myemail@address.com>'
10+
service:
11+
type: Sparkpost
12+
auth:
13+
user: SMTP_Injection
14+
pass:
15+
database:
16+
client: sqlite3

ghost/ghost-thirdparty.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: extensions/v1beta1
2+
kind: ThirdPartyResource
3+
description: "An operator to manage Ghost deployments."
4+
metadata:
5+
name: "ghost.experimantal.giantswarm.com"
6+
versions:
7+
- name: v1

ghost/manifests/0-namespace.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: {{ metadata.namespace }}

ghost/manifests/configmap.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: {{ metadata.name }}
5+
namespace: {{ metadata.namespace }}
6+
labels:
7+
app: {{ metadata.name }}
8+
thirdparty: ghost
9+
data:
10+
config.example.js: |
11+
var path = require('path'),
12+
config;
13+
14+
config = {
15+
production: {
16+
url: '{{ spec.url }}',
17+
mail: {
18+
transport: 'SMTP',
19+
options: {
20+
service: '{{ spec.mail.service.type }}',
21+
auth: {
22+
user: '{{ spec.mail.service.auth.user }}',
23+
pass: '{{ spec.mail.service.auth.pass }}'
24+
}
25+
}
26+
},
27+
database: {
28+
# if mariadb, sqlite3, ..
29+
client: 'sqlite3',
30+
connection: {
31+
filename: path.join(__dirname, '/content/data/ghost.db')
32+
},
33+
debug: false
34+
},
35+
server: {
36+
host: '0.0.0.0',
37+
port: '2368'
38+
}
39+
}
40+
};
41+
42+
module.exports = config;

ghost/manifests/deployment.yaml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
apiVersion: extensions/v1beta1
2+
kind: Deployment
3+
metadata:
4+
name: {{ metadata.name }}
5+
namespace: {{ metadata.namespace }}
6+
labels:
7+
app: {{ metadata.name }}
8+
thirdparty: ghost
9+
spec:
10+
replicas: 1
11+
template:
12+
metadata:
13+
labels:
14+
app: {{ metadata.name }}
15+
thirdparty: ghost
16+
spec:
17+
containers:
18+
- name: {{ metadata.name }}
19+
# image: bitnami/ghost
20+
image: ghost:0.11.3
21+
imagePullPolicy: IfNotPresent
22+
# env:
23+
# - name: MARIADB_HOST
24+
# value: mariadb
25+
# - name: MARIADB_PORT
26+
# value: "3306"
27+
# - name: MARIADB_PASSWORD
28+
# value: ghost
29+
# # valueFrom:
30+
# # secretKeyRef:
31+
# - name: GHOST_HOST
32+
# value: {{ host }}
33+
# # - name: GHOST_PORT
34+
# - name: GHOST_USERNAME
35+
# value: admin
36+
# - name: GHOST_PASSWORD
37+
# value: password
38+
# # valueFrom:
39+
# # secretKeyRef:
40+
# # name:
41+
# # key: ghost-password
42+
ports:
43+
- name: http
44+
containerPort: 2368
45+
livenessProbe:
46+
httpGet:
47+
path: /
48+
port: http
49+
initialDelaySeconds: 120
50+
timeoutSeconds: 5
51+
# readinessProbe:
52+
# exec:
53+
# command:
54+
# - sh
55+
# - -c
56+
# - nami status ghost | grep "ghost is running"
57+
# initialDelaySeconds: 5
58+
# timeoutSeconds: 1
59+
# resources:
60+
# volumeMounts:
61+
# - name: ghost-data
62+
# mountPath: /bitnami/ghost
63+
# volumes:
64+
# - name: ghost-data
65+
# emptyDir: {}
66+
67+
# - name: config
68+
# configmap > /etc/ghost
69+
70+
# /var/lib/ghost/content for content?
71+
# apps, data, images and themes
72+
# ^needed, maybe init-container

ghost/manifests/ingress.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: extensions/v1beta1
2+
kind: Ingress
3+
metadata:
4+
name: {{ metadata.name }}
5+
namespace: {{ metadata.namespace }}
6+
labels:
7+
app: {{ metadata.name }}
8+
thirdparty: Ghost
9+
# annotations:
10+
# kubernetes.io/tls-acme: "true"
11+
spec:
12+
# rules:
13+
# - host: {{ spec.url }}
14+
# http:
15+
# paths:
16+
# - path: /
17+
# backend:
18+
# serviceName: {{ metadata.name }}
19+
# servicePort: 2368
20+
# tls:
21+
# - secretName: {{ metadata.name }}-tls
22+
# hosts:
23+
# - {{ spec.url }}
24+
25+
backend:
26+
serviceName: {{ metadata.name }}
27+
servicePort: 2368

0 commit comments

Comments
 (0)