-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator.py
150 lines (130 loc) · 5.59 KB
/
operator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
import kopf
import subprocess
from pykube import Service, Deployment, Namespace, HTTPClient, KubeConfig, ObjectDoesNotExist
osenv = dict(os.environ)
def delete(namespace, names, logger):
api = HTTPClient(KubeConfig.from_file())
for name in names:
deploy = Deployment.objects(api, namespace=namespace).get(name=name)
deploy.delete()
logger.info(f'delete Deployment: {str(deploy)}')
service = Service.objects(api, namespace=namespace).get(name=name)
service.delete()
logger.info(f'delete Service: {str(service)}')
@kopf.on.field('tekton.dev', 'v1beta', 'companions', field='spec.namespace')
def namespace(spec, old, new, logger, **kwargs):
logger.info(f'namespace: {old=}, {new=}')
api = HTTPClient(KubeConfig.from_file())
if new:
obj = {
'apiVersion': 'v1',
'kind': 'Namespace',
'metadata': {
'name': new,
}
}
Namespace(api, obj).create()
elif old:
obj = {
'apiVersion': 'v1',
'kind': 'Namespace',
'metadata': {
'name': old,
}
}
Namespace(api, obj).delete()
@kopf.on.field('tekton.dev', 'v1beta', 'companions', field='spec.pipeline.version')
def pipeline(spec, old, new, logger, **kwargs):
logger.info(f'pipeline: {old=}, {new=}')
if new:
try:
subprocess.run(f"kubectl apply -f https://github.com/tektoncd/pipeline/releases/download/{new}/release.yaml", shell=True, check=True, env=osenv)
except subprocess.CalledProcessError as e:
logger.error(e.output)
raise e
elif old:
delete('tekton-pipelines',
["tekton-pipelines-controller", "tekton-pipelines-webhook"],
logger)
@kopf.on.field('tekton.dev', 'v1beta', 'companions', field='spec.triggers.version')
def triggers(spec, old, new, logger, **kwargs):
logger.info(f'triggers: {old=}, {new=}')
if new:
try:
subprocess.run(f"kubectl apply -f https://github.com/tektoncd/triggers/releases/download/{new}/release.yaml", shell=True, check=True, env=osenv)
except subprocess.CalledProcessError as e:
logger.error(e.output)
raise e
elif old:
delete('tekton-pipelines',
["tekton-triggers-controller", "tekton-triggers-webhook"],
logger)
@kopf.on.field('tekton.dev', 'v1beta', 'companions', field='spec.dashboard.version')
def dashboard(spec, old, new, logger, **kwargs):
logger.info(f'dashboard: {old=}, {new=}')
if new:
try:
subprocess.run(f"kubectl apply -f https://github.com/tektoncd/dashboard/releases/download/{new}/tekton-dashboard-release.yaml", shell=True, check=True, env=osenv)
except subprocess.CalledProcessError as e:
logger.error(e.output)
raise e
elif old:
delete('tekton-pipelines',
["tekton-dashboard"],
logger)
@kopf.on.field('tekton.dev', 'v1beta', 'companions', field='spec.kaniko.version')
def dashboard(spec, old, new, logger, **kwargs):
logger.info(f'kaniko: {old=}, {new=}')
if new:
try:
subprocess.run(f"kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/master/task/kaniko/{new}/kaniko.yaml -n {spec.get('namespace', 'default')}", shell=True, check=True, env=osenv)
except subprocess.CalledProcessError as e:
logger.error(e.output)
raise e
elif old:
try:
subprocess.run(f"kubectl delete task.tekton.dev/kaniko -n {spec.get('namespace', 'default')}", shell=True, check=True, env=osenv)
except subprocess.CalledProcessError as e:
logger.error(e.output)
raise e
@kopf.on.field('tekton.dev', 'v1beta', 'companions', field='spec.git-clone.version')
def dashboard(spec, old, new, logger, **kwargs):
logger.info(f'git-clone: {old=}, {new=}')
if new:
try:
subprocess.run(f"kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/master/task/git-clone/{new}/git-clone.yaml -n {spec.get('namespace', 'default')}", shell=True, check=True, env=osenv)
except subprocess.CalledProcessError as e:
logger.error(e.output)
raise e
elif old:
try:
subprocess.run(f"kubectl delete task.tekton.dev/git-clone -n {spec.get('namespace', 'default')}", shell=True, check=True, env=osenv)
except subprocess.CalledProcessError as e:
logger.error(e.output)
raise e
@kopf.on.delete('tekton.dev', 'v1beta', 'companions')
def uninstall(spec, logger, **kwargs):
logger.info('uninstall')
try:
delete('tekton-pipelines',
["tekton-pipelines-controller", "tekton-pipelines-webhook",
"tekton-triggers-controller", "tekton-triggers-webhook", "tekton-dashboard"],
logger)
try:
subprocess.run(f"kubectl delete task.tekton.dev/kaniko -n {spec.get('namespace', 'default')}", shell=True, check=False, env=osenv)
subprocess.run(f"kubectl delete task.tekton.dev/git-clone -n {spec.get('namespace', 'default')}", shell=True, check=False, env=osenv)
except subprocess.CalledProcessError as e:
logger.error(e.output)
raise e
api = HTTPClient(KubeConfig.from_file())
obj = {
'apiVersion': 'v1',
'kind': 'Namespace',
'metadata': {
'name': spec.get('namespace'),
}
}
Namespace(api, obj).delete()
except ObjectDoesNotExist:
pass