-
Notifications
You must be signed in to change notification settings - Fork 25
/
namespace.py
58 lines (47 loc) · 1.86 KB
/
namespace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import json
import yaml
from collections import OrderedDict
from hokusai import CWD
from hokusai.lib.exceptions import HokusaiError
from hokusai.lib.common import print_green, clean_string, shout
from hokusai.lib.config import HOKUSAI_CONFIG_DIR, config
from hokusai.lib.constants import YAML_HEADER
from hokusai.services.kubectl import Kubectl
from hokusai.services.yaml_spec import YamlSpec
def create_new_app_yaml(source_file, app_name):
yaml_spec = YamlSpec(source_file).to_file()
with open(yaml_spec, 'r') as stream:
try:
yaml_content = list(yaml.load_all(stream, Loader=yaml.FullLoader))
except yaml.YAMLError as exc:
raise HokusaiError("Cannot read source yaml file %s." % source_file)
for c in yaml_content: update_namespace(c, clean_string(app_name))
new_namespace = OrderedDict([
('apiVersion', 'v1'),
('kind', 'Namespace'),
('metadata', {
'name': clean_string(app_name),
'labels': {
'app-name': config.project_name,
'app-phase': 'review'
}
})
])
yaml_content = [new_namespace] + yaml_content
with open(os.path.join(CWD, HOKUSAI_CONFIG_DIR, "%s.yml" % app_name), 'w') as output:
output.write(YAML_HEADER)
yaml.safe_dump_all(yaml_content, output, default_flow_style=False)
print_green("Created %s/%s.yml" % (HOKUSAI_CONFIG_DIR, app_name))
def list_namespaces(context, labels=None):
''' list Kubernetes namespaces that match the given labels '''
kctl = Kubectl(context)
namespaces = kctl.get_objects('namespaces', labels)
for ns in namespaces:
print(ns['metadata']['name'])
def update_namespace(yaml_section, destination_namespace):
if 'apiVersion' in yaml_section:
if 'metadata' in yaml_section:
yaml_section['metadata']['namespace'] = destination_namespace
else:
yaml_section['metadata'] = { 'namespace': destination_namespace }