Adding ability to export created k8s objects

--export-dir parameter was added to deploy command.
k8s objects will be created inside of this directory
with names <object_name>-<object_kind>.yaml.
Configmap objects will be created under subdirectory
"configmaps"

Objects can be created with kubectl in default namespace:
kubectl create -f <dir>/configmaps/ -f <dir>/
And removed:
kubectl delete -f <dir>/configmaps/ -f <dir>/

To create in another namespace:
kubectl create namespace <ns>
kubectl create -f <dir>/configmaps/ -f <dir>/ --namespace <ns>
kubectl delete -f <dir>/configmaps/ -f <dir>/ --namespace <ns>

Change-Id: I0d33e928526954c7a068118fe6af82a5a0ce4af6
This commit is contained in:
Andrey
2016-07-26 13:37:11 +00:00
committed by Andrey Pavlov
parent 428b7e0a94
commit 2eeac32d63
4 changed files with 20 additions and 0 deletions

View File

@@ -18,6 +18,8 @@ def add_parsers(subparsers):
action='store_true',
help="Print k8s objects definitions without"
"actual creation")
deploy_action.add_argument('--export-dir',
help='Directory to export created k8s objects')
fetch_action = subparsers.add_parser('fetch')
fetch_action.add_argument('-c', '--components',

View File

@@ -344,6 +344,8 @@ def _create_openrc(config, namespace):
def deploy_components(components=None):
if components is None:
components = CONF.repositories.names
if CONF.action.export_dir:
os.makedirs(os.path.join(CONF.action.export_dir, 'configmaps'))
config = utils.get_global_parameters("configs", "nodes", "roles")
config["topology"] = _make_topology(config.get("nodes"),

View File

@@ -1,3 +1,4 @@
import os
import yaml
from k8sclient.client import api_client
@@ -30,9 +31,22 @@ def get_client(kube_apiserver=None, key_file=None, cert_file=None,
def create_object_from_definition(object_dict, namespace=None, client=None):
LOG.info("Deploying %s: \"%s\"",
object_dict["kind"], object_dict["metadata"]["name"])
if CONF.action.export_dir:
file_name = '%s-%s.yaml' % (
object_dict['metadata']['name'], object_dict['kind'].lower())
if object_dict['kind'] == 'ConfigMap':
file_path = os.path.join(
CONF.action.export_dir, 'configmaps', file_name)
else:
file_path = os.path.join(CONF.action.export_dir, file_name)
with open(file_path, 'w') as object_file:
object_file.write(yaml.dump(
object_dict, default_flow_style=False))
if CONF.action.dry_run:
LOG.info(yaml.dump(object_dict, default_flow_style=False))
return
namespace = namespace or CONF.kubernetes.namespace
client = client or get_client()
if object_dict['kind'] == 'Deployment':

View File

@@ -37,6 +37,7 @@ class TestKubernetes(base.TestCase):
'k8sclient.client.apis.apisextensionsvbeta_api.ApisextensionsvbetaApi')
def test_create_deployment(self, api_beta):
CONF.action.dry_run = False
CONF.action.export_dir = False
api = mock.Mock()
api.create_namespaced_deployment = mock.Mock()
api_beta.return_value = api
@@ -50,6 +51,7 @@ class TestKubernetes(base.TestCase):
@mock.patch('k8sclient.client.apis.apiv_api.ApivApi')
def test_create_service(self, api_v1):
CONF.action.dry_run = False
CONF.action.export_dir = False
api = mock.Mock()
api.create_namespaced_service = mock.Mock()
api_v1.return_value = api