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
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'])

56
main.py
View File

@ -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")

View File

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

View File

@ -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/<tiller_ns>/<namespace>')
def namespaceLookup(tiller_ns, namespace):
charts = get_charts(tiller_ns, namespace)
@routes.route('/nsLookup/<ns>')
def namespaceLookup(ns):
charts = get_charts(ns)
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):
rollback = chartRollback(revision, chart, tiller_ns)
rollback = chartRollback(revision, chart, ns)
return rollback

View File

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