diff --git a/forms.py b/forms.py index b8afd7d..4d59444 100644 --- a/forms.py +++ b/forms.py @@ -8,7 +8,7 @@ from wtforms.validators import InputRequired, Email, Length from flask_bootstrap import Bootstrap class deploySelectForm(FlaskForm): - tiller_ns = SelectField('tiller_ns', choices=[], coerce=str) + ns = SelectField('ns', choices=[], coerce=str) chart = SelectField('chart', choices=[]) version = SelectField('remember me') # Added if/when I want to add additional helm version options records = SelectField('records', choices=[('10', '10'), ('20', '20'), ('30', '30'), ('40', '40'), ('50', '50'), ('100', '100'), ('150', '150'), ('200', '200'), ('256', '256 (max)')], default=['10']) diff --git a/main.py b/main.py index a661d42..f205039 100644 --- a/main.py +++ b/main.py @@ -13,48 +13,18 @@ from routes import * from logging import error, info from subprocess import STDOUT, CalledProcessError, check_output from itertools import islice +from kubernetes import client, config app.register_blueprint(routes) def get_namespaces(): - command = "/usr/local/bin/kubectl get ns -ojson" - info(f"Running command: {command}") - try: - output = check_output(command.split(" "), stderr=STDOUT).decode("utf-8") - except CalledProcessError as err: - error(err.output.decode("utf-8")) - raise err - info(f"Output from command:\n{output}") - data = json.loads(output) - return data + config.load_incluster_config() + v1 = client.CoreV1Api() + ns = v1.list_namespace() + return ns -def get_tiller_namespaces(): - command = "/usr/local/bin/kubectl get deploy --all-namespaces -l name=tiller -ojson" - info(f"Running command: {command}") - try: - output = check_output(command.split(" "), stderr=STDOUT).decode("utf-8") - except CalledProcessError as err: - error(err.output.decode("utf-8")) - raise err - info(f"Output from command:\n{output}") - data = json.loads(output) - return data - -def get_deployments(namespace): - command = "/usr/local/bin/kubectl -n " + namespace + " get deploy -ojson" - info(f"Running command: {command}") - try: - output = check_output(command.split(" "), stderr=STDOUT).decode("utf-8") - except CalledProcessError as err: - error(err.output.decode("utf-8")) - raise err - info(f"Output from command:\n{output}") - data = json.loads(output) - return data - -def get_charts(tiller_ns, namespace): - command = "/usr/local/bin/helm --tiller-namespace " + tiller_ns + " list --output json" #helm2 - #command = "/usr/local/bin/helm -n " + namespace + " list -ojson" #helm3 +def get_charts(ns): + command = "/usr/local/bin/helm -n " + ns + " list -ojson" #helm3 info(f"Running command: {command}") try: output = check_output(command.split(" "), stderr=STDOUT).decode("utf-8") @@ -71,9 +41,8 @@ def get_charts(tiller_ns, namespace): def sortRevision(n): return n['revision'] -def get_chartdata(tiller_ns, namespace, chart, records): - command = "/usr/local/bin/helm --tiller-namespace " + tiller_ns + " history " + chart + " --max " + records + " --output json" #helm2 - #command = "/usr/local/bin/helm -n " + namespace + " history " + chart + " -ojson" #helm3 +def get_chartdata(ns, chart, records): + command = "/usr/local/bin/helm -n " + ns + " history " + chart + " --max " + records + " -ojson" #helm3 info(f"Running command: {command}") try: output = check_output(command.split(" "), stderr=STDOUT).decode("utf-8") @@ -85,12 +54,11 @@ def get_chartdata(tiller_ns, namespace, chart, records): data.sort(reverse=True, key=sortRevision) for revision in data: revision['chartName'] = chart - revision["namespace"] = namespace - revision["tiller_ns"] = tiller_ns + revision["ns"] = ns return data -def chartRollback(revision, chart, tiller_ns): - command = "/usr/local/bin/helm --tiller-namespace " + tiller_ns + " rollback " + chart + " " + revision # helm2 +def chartRollback(revision, chart, ns): + command = "/usr/local/bin/helm -n " + ns + " rollback " + chart + " " + revision # helm3? info(f"Running command: {command}") try: output = check_output(command.split(" "), stderr=STDOUT).decode("utf-8") diff --git a/requirements.txt b/requirements.txt index 70bf29a..fa13ce7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ flask_table Flask-Bootstrap flask_wtf wtforms_components +kubernetes diff --git a/routes/kube_helm_routes.py b/routes/kube_helm_routes.py index 3ee87ec..a18efa9 100644 --- a/routes/kube_helm_routes.py +++ b/routes/kube_helm_routes.py @@ -1,36 +1,35 @@ from flask import render_template, request, jsonify from forms import deploySelectForm -from main import get_namespaces, get_charts, get_chartdata, get_tiller_namespaces, chartRollback +from main import get_namespaces, get_charts, get_chartdata, chartRollback from . import routes from tables import chartVersionTable import json @routes.route('/', methods=['GET', 'POST']) def index(): - tiller_ns = get_tiller_namespaces() + ns = get_namespaces() form = deploySelectForm() - form.tiller_ns.choices = [(name['metadata']['namespace'], name['metadata']['namespace']) for name in tiller_ns['items']] - return render_template('nameChartSelect.html', tiller_ns=tiller_ns, form=form) + form.ns.choices = [(name.metadata.name, name.metadata.name) for name in ns.items] + return render_template('nameChartSelect.html', ns=ns, form=form) @routes.route('/chartSelect', methods=['POST']) def chartVersions(): - tiller_ns = request.form['tiller_ns'] chart = request.form['chart'] records = request.form['records'] - namespace = 'default' - chartVersions = get_chartdata(tiller_ns, namespace, chart, records) + ns = request.form['ns'] + chartVersions = get_chartdata(ns, chart, records) table = chartVersionTable(chartVersions) table.border = True table.classes = ['table-striped', 'table-condensed', 'table-hover'] return render_template('chartRevisionList.html', table=table) -@routes.route('/nsLookup//') -def namespaceLookup(tiller_ns, namespace): - charts = get_charts(tiller_ns, namespace) +@routes.route('/nsLookup/') +def namespaceLookup(ns): + charts = get_charts(ns) return jsonify(charts) -@routes.route('/deployChartRevision///', methods=['POST']) +@routes.route('/deployChartRevision///', methods=['POST']) def deployChartRevision(revision, chart, tiller_ns): - rollback = chartRollback(revision, chart, tiller_ns) + rollback = chartRollback(revision, chart, ns) return rollback \ No newline at end of file diff --git a/tables.py b/tables.py index 85bb003..773a91b 100644 --- a/tables.py +++ b/tables.py @@ -4,7 +4,7 @@ class chartVersionTable(Table): revision = Col('Chart Revision') updated = Col('Updated') status = Col('Status') - tiller_ns = Col('Tiiler Namespace') + ns = Col('Namespace') chart = Col('Chart Version') description = Col('Description') - deploy = ButtonCol('Deploy', 'routes.deployChartRevision', url_kwargs=dict(revision='revision', chart='chartName', tiller_ns='tiller_ns'), button_attrs={"type" : "submit", "class" : "btn btn-danger"}) \ No newline at end of file + deploy = ButtonCol('Deploy', 'routes.deployChartRevision', url_kwargs=dict(revision='revision', chart='chartName', ns='ns'), button_attrs={"type" : "submit", "class" : "btn btn-danger"}) \ No newline at end of file diff --git a/templates/nameChartSelect.html b/templates/nameChartSelect.html index 61c0c0f..2b6c06c 100644 --- a/templates/nameChartSelect.html +++ b/templates/nameChartSelect.html @@ -18,8 +18,8 @@
- Tiller Namespace: - {{ form.tiller_ns(class_="form-control") }} + Namespace: + {{ form.ns(class_="form-control") }}
@@ -50,21 +50,21 @@