Skip to content

Commit

Permalink
Merge pull request #7 from drucker/feature/dump-kubernetes-yaml
Browse files Browse the repository at this point in the history
Merged!
  • Loading branch information
keigohtr authored Aug 28, 2018
2 parents dfde352 + 8ce4d51 commit ce0fdb5
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
94 changes: 93 additions & 1 deletion app/apis/api_kubernetes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os, uuid, shutil
import os, uuid, shutil, json, pathlib
from datetime import datetime, timedelta

from flask_restplus import Namespace, fields, Resource, reqparse
Expand Down Expand Up @@ -340,6 +340,7 @@ def update_dbs_kubernetes(kubernetes_id:int, applist:set=None, description:str=N
else:
sobj.confirm_date = datetime.utcnow()
db.session.flush()
dump_drucker_on_kubernetes(kobj.kubernetes_id, aobj.application_id, sobj.service_id)
for application_name in applist:
aobj = db.session.query(Application).filter(
Application.application_name == application_name,
Expand Down Expand Up @@ -811,6 +812,97 @@ def switch_drucker_service_model_assignment(
)
return response_body

def dump_drucker_on_kubernetes(
kubernetes_id:int, application_id:int, service_id:int):
kobj = Kubernetes.query.filter_by(
kubernetes_id=kubernetes_id).one_or_none()
if kobj is None:
raise Exception("No such kubernetes_id.")
aobj = Application.query.filter_by(
application_id=application_id).one_or_none()
if aobj is None:
raise Exception("No such application_id.")
sobj = Service.query.filter_by(
service_id=service_id).one_or_none()
if sobj is None:
raise Exception("No such service_id.")

config_path = kobj.config_path
from kubernetes import client, config
config.load_kube_config(config_path)
save_dir = pathlib.Path(DIR_KUBE_CONFIG, aobj.application_name)
if not os.path.isdir(save_dir):
os.mkdir(save_dir)
api_client = client.ApiClient()

apps_v1 = client.AppsV1Api()
v1_deployment = apps_v1.read_namespaced_deployment(
name="{0}-deployment".format(sobj.service_name),
namespace=sobj.service_level,
exact=True,
export=True
)
json.dump(api_client.sanitize_for_serialization(v1_deployment),
pathlib.Path(
DIR_KUBE_CONFIG,
aobj.application_name,
"{0}-deployment.json".format(sobj.service_name)).open("w"),
ensure_ascii = False, indent = 2)
core_vi = client.CoreV1Api()
v1_service = core_vi.read_namespaced_service(
name="{0}-service".format(sobj.service_name),
namespace=sobj.service_level,
exact=True,
export=True
)
json.dump(api_client.sanitize_for_serialization(v1_service),
pathlib.Path(
DIR_KUBE_CONFIG,
aobj.application_name,
"{0}-service.json".format(sobj.service_name)).open("w"),
ensure_ascii = False, indent = 2)
extensions_v1_beta = client.ExtensionsV1beta1Api()
v1_beta1_ingress = extensions_v1_beta.read_namespaced_ingress(
name="{0}-ingress".format(sobj.service_name),
namespace=sobj.service_level,
exact=True,
export=True
)
json.dump(api_client.sanitize_for_serialization(v1_beta1_ingress),
pathlib.Path(
DIR_KUBE_CONFIG,
aobj.application_name,
"{0}-ingress.json".format(sobj.service_name)).open("w"),
ensure_ascii = False, indent = 2)
autoscaling_v1 = client.AutoscalingV1Api()
v1_horizontal_pod_autoscaler = autoscaling_v1.read_namespaced_horizontal_pod_autoscaler(
name="{0}-autoscaling".format(sobj.service_name),
namespace=sobj.service_level,
exact=True,
export=True
)
json.dump(api_client.sanitize_for_serialization(v1_horizontal_pod_autoscaler),
pathlib.Path(
DIR_KUBE_CONFIG,
aobj.application_name,
"{0}-autoscaling.json".format(sobj.service_name)).open("w"),
ensure_ascii = False, indent = 2)
"""
autoscaling_v2_beta1 = client.AutoscalingV2beta1Api()
v2_beta1_horizontal_pod_autoscaler = autoscaling_v2_beta1.read_namespaced_horizontal_pod_autoscaler(
name="{0}-autoscaling".format(sobj.service_name),
namespace=sobj.service_level,
exact=True,
export=True
)
json.dump(api_client.sanitize_for_serialization(v2_beta1_horizontal_pod_autoscaler),
pathlib.Path(
DIR_KUBE_CONFIG,
aobj.application_name,
"{0}-autoscaling.json".format(sobj.service_name)).open("w"),
ensure_ascii = False, indent = 2)
"""


@kube_info_namespace.route('/')
class ApiKubernetes(Resource):
Expand Down
5 changes: 5 additions & 0 deletions app/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ db.mysql.port: 3306
db.mysql.dbname: management
db.mysql.user: user
db.mysql.password: pass

# Kubernetes
kube.datadir: kube-config

# LDAP
# auth:
# secret: 'super-secret'
# ldap:
Expand Down
2 changes: 1 addition & 1 deletion app/utils/env_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

DRUCKER_GRPC_VERSION = drucker_pb2.DESCRIPTOR.GetOptions().Extensions[drucker_pb2.drucker_grpc_proto_version]

DIR_KUBE_CONFIG = "kube-config"
DIR_KUBE_CONFIG = os.getenv('DRUCKER_KUBE_DATADIR', config.get('kube.datadir', 'kube-config'))

DB_MODE = os.getenv('DRUCKER_DB_MODE', config.get('use.db',"sqlite"))
DB_MYSQL_HOST = os.getenv('DRUCKER_DB_MYSQL_HOST', config.get('db.mysql.host',""))
Expand Down

0 comments on commit ce0fdb5

Please sign in to comment.