Merge pull request 'issue-#1' (#5) from issue-#1 into master

Reviewed-on: #5
This commit is contained in:
Jonny Ervine 2020-12-16 15:48:53 +00:00
commit 9bf9dae1ba
6 changed files with 35 additions and 67 deletions

View File

@ -8,7 +8,7 @@ from wtforms.validators import InputRequired, Email, Length
from flask_bootstrap import Bootstrap from flask_bootstrap import Bootstrap
class deploySelectForm(FlaskForm): class deploySelectForm(FlaskForm):
tiller_ns = SelectField('tiller_ns', choices=[], coerce=str) ns = SelectField('ns', choices=[], coerce=str)
chart = SelectField('chart', choices=[]) chart = SelectField('chart', choices=[])
version = SelectField('remember me') # Added if/when I want to add additional helm version options 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']) 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'])

56
main.py
View File

@ -13,48 +13,18 @@ from routes import *
from logging import error, info from logging import error, info
from subprocess import STDOUT, CalledProcessError, check_output from subprocess import STDOUT, CalledProcessError, check_output
from itertools import islice from itertools import islice
from kubernetes import client, config
app.register_blueprint(routes) app.register_blueprint(routes)
def get_namespaces(): def get_namespaces():
command = "/usr/local/bin/kubectl get ns -ojson" config.load_incluster_config()
info(f"Running command: {command}") v1 = client.CoreV1Api()
try: ns = v1.list_namespace()
output = check_output(command.split(" "), stderr=STDOUT).decode("utf-8") return ns
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_tiller_namespaces(): def get_charts(ns):
command = "/usr/local/bin/kubectl get deploy --all-namespaces -l name=tiller -ojson" 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")
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
info(f"Running command: {command}") info(f"Running command: {command}")
try: try:
output = check_output(command.split(" "), stderr=STDOUT).decode("utf-8") output = check_output(command.split(" "), stderr=STDOUT).decode("utf-8")
@ -71,9 +41,8 @@ def get_charts(tiller_ns, namespace):
def sortRevision(n): def sortRevision(n):
return n['revision'] return n['revision']
def get_chartdata(tiller_ns, namespace, chart, records): def get_chartdata(ns, chart, records):
command = "/usr/local/bin/helm --tiller-namespace " + tiller_ns + " history " + chart + " --max " + records + " --output json" #helm2 command = "/usr/local/bin/helm -n " + ns + " history " + chart + " --max " + records + " -ojson" #helm3
#command = "/usr/local/bin/helm -n " + namespace + " history " + chart + " -ojson" #helm3
info(f"Running command: {command}") info(f"Running command: {command}")
try: try:
output = check_output(command.split(" "), stderr=STDOUT).decode("utf-8") 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) data.sort(reverse=True, key=sortRevision)
for revision in data: for revision in data:
revision['chartName'] = chart revision['chartName'] = chart
revision["namespace"] = namespace revision["ns"] = ns
revision["tiller_ns"] = tiller_ns
return data return data
def chartRollback(revision, chart, tiller_ns): def chartRollback(revision, chart, ns):
command = "/usr/local/bin/helm --tiller-namespace " + tiller_ns + " rollback " + chart + " " + revision # helm2 command = "/usr/local/bin/helm -n " + ns + " rollback " + chart + " " + revision # helm3?
info(f"Running command: {command}") info(f"Running command: {command}")
try: try:
output = check_output(command.split(" "), stderr=STDOUT).decode("utf-8") output = check_output(command.split(" "), stderr=STDOUT).decode("utf-8")

View File

@ -4,3 +4,4 @@ flask_table
Flask-Bootstrap Flask-Bootstrap
flask_wtf flask_wtf
wtforms_components wtforms_components
kubernetes

View File

@ -1,36 +1,35 @@
from flask import render_template, request, jsonify from flask import render_template, request, jsonify
from forms import deploySelectForm 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 . import routes
from tables import chartVersionTable from tables import chartVersionTable
import json import json
@routes.route('/', methods=['GET', 'POST']) @routes.route('/', methods=['GET', 'POST'])
def index(): def index():
tiller_ns = get_tiller_namespaces() ns = get_namespaces()
form = deploySelectForm() form = deploySelectForm()
form.tiller_ns.choices = [(name['metadata']['namespace'], name['metadata']['namespace']) for name in tiller_ns['items']] form.ns.choices = [(name.metadata.name, name.metadata.name) for name in ns.items]
return render_template('nameChartSelect.html', tiller_ns=tiller_ns, form=form) return render_template('nameChartSelect.html', ns=ns, form=form)
@routes.route('/chartSelect', methods=['POST']) @routes.route('/chartSelect', methods=['POST'])
def chartVersions(): def chartVersions():
tiller_ns = request.form['tiller_ns']
chart = request.form['chart'] chart = request.form['chart']
records = request.form['records'] records = request.form['records']
namespace = 'default' ns = request.form['ns']
chartVersions = get_chartdata(tiller_ns, namespace, chart, records) chartVersions = get_chartdata(ns, chart, records)
table = chartVersionTable(chartVersions) table = chartVersionTable(chartVersions)
table.border = True table.border = True
table.classes = ['table-striped', 'table-condensed', 'table-hover'] table.classes = ['table-striped', 'table-condensed', 'table-hover']
return render_template('chartRevisionList.html', table=table) return render_template('chartRevisionList.html', table=table)
@routes.route('/nsLookup/<tiller_ns>/<namespace>') @routes.route('/nsLookup/<ns>')
def namespaceLookup(tiller_ns, namespace): def namespaceLookup(ns):
charts = get_charts(tiller_ns, namespace) charts = get_charts(ns)
return jsonify(charts) return jsonify(charts)
@routes.route('/deployChartRevision/<revision>/<chart>/<tiller_ns>', methods=['POST']) @routes.route('/deployChartRevision/<revision>/<chart>/<ns>', methods=['POST'])
def deployChartRevision(revision, chart, tiller_ns): def deployChartRevision(revision, chart, tiller_ns):
rollback = chartRollback(revision, chart, tiller_ns) rollback = chartRollback(revision, chart, ns)
return rollback return rollback

View File

@ -4,7 +4,7 @@ class chartVersionTable(Table):
revision = Col('Chart Revision') revision = Col('Chart Revision')
updated = Col('Updated') updated = Col('Updated')
status = Col('Status') status = Col('Status')
tiller_ns = Col('Tiiler Namespace') ns = Col('Namespace')
chart = Col('Chart Version') chart = Col('Chart Version')
description = Col('Description') 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"}) deploy = ButtonCol('Deploy', 'routes.deployChartRevision', url_kwargs=dict(revision='revision', chart='chartName', ns='ns'), button_attrs={"type" : "submit", "class" : "btn btn-danger"})

View File

@ -18,8 +18,8 @@
<div class = "row"> <div class = "row">
<div class = "col-sm-6"> <div class = "col-sm-6">
<div class="input-group"> <div class="input-group">
<span class="input-group-addon" id="basic-addon1">Tiller Namespace:</span> <span class="input-group-addon" id="basic-addon1">Namespace:</span>
{{ form.tiller_ns(class_="form-control") }} {{ form.ns(class_="form-control") }}
</div> </div>
</div> </div>
</div> </div>
@ -50,21 +50,21 @@
</dl> </dl>
<script> <script>
var tillerSelect = document.getElementById("tiller_ns"); var nsSelect = document.getElementById("ns");
var chartSelect = document.getElementById("chart"); var chartSelect = document.getElementById("chart");
tillerSelect.onchange = function() {myFunction()}; nsSelect.onchange = function() {myFunction()};
function myFunction() { function myFunction() {
tiller_ns = tillerSelect.value; ns = nsSelect.value;
fetch('/nsLookup/' + tiller_ns + '/default').then(function(response) { fetch('/nsLookup/' + ns).then(function(response) {
response.json().then(function(data) { response.json().then(function(data) {
if (data != 'EMPTY') { if (data != 'EMPTY') {
var optionHTML = ''; var optionHTML = '';
for (var chart of data.Releases) { for (var chart of data) {
optionHTML += '<option value="' + chart.Name + '">' + chart.Name + '</option>'; optionHTML += '<option value="' + chart.name + '">' + chart.name + '</option>';
} }
chartSelect.innerHTML = optionHTML; chartSelect.innerHTML = optionHTML;
} }