Skip to content

Commit 837044b

Browse files
author
lijianguo
committed
chore(helmbroker): modify load_addons
1 parent 46ede22 commit 837044b

5 files changed

Lines changed: 83 additions & 25 deletions

File tree

rootfs/helmbroker/broker.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
from .meta import load_instance_meta, load_binding_meta, dump_binding_meta
99
from .utils import get_instance_path, get_chart_path, get_plan_path, \
10-
get_addon_path, get_addon_name, get_addon_updateable
10+
get_addon_path, get_addon_name, get_addon_updateable, get_addon_bindable
1111
from .tasks import provision, bind, deprovision, update
12-
from helmbroker.loader import read_addons_file
12+
from helmbroker.meta import load_addons_meta
1313

1414

1515
class HelmServiceBroker(ServiceBroker):
1616

1717
def catalog(self) -> Union[Service, List[Service]]:
18-
services = read_addons_file()
18+
services = load_addons_meta()
1919
return [Service(
2020
**addons
2121
) for _, addons in services.items()]
@@ -55,7 +55,10 @@ def bind(self,
5555
async_allowed: bool,
5656
**kwargs
5757
) -> Binding:
58-
instance_meta = dump_binding_meta(instance_id)
58+
is_addon_bindable = get_addon_bindable(instance_id)
59+
if not is_addon_bindable:
60+
raise ErrBadRequest("Instance %s does not bindable" % instance_id)
61+
instance_meta = load_instance_meta(instance_id)
5962
if not (instance_meta and
6063
instance_meta['last_operation']['state'] == 'Ready'):
6164
raise ErrBadRequest(msg="This instance %s is not ready" % instance_id)

rootfs/helmbroker/config.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import os
2-
import yaml
32

43
HELMBROKER_ROOT = os.environ.get("HELMBROKER_CELERY_BROKER", '/etc/helmbroker')
54

65
ADDONS_PATH = os.path.join(HELMBROKER_ROOT, 'addons')
76
CONFIG_PATH = os.path.join(HELMBROKER_ROOT, 'config')
87
INSTANCES_PATH = os.path.join(HELMBROKER_ROOT, 'instances')
98

9+
1010
class Config:
11-
DEBUG = True
11+
DEBUG = os.environ.get("DEBUG", False)
1212
if not os.path.exists(ADDONS_PATH):
1313
os.makedirs(ADDONS_PATH)
14-
with open(CONFIG_PATH, 'r') as f:
15-
repository = yaml.load(f.read())

rootfs/helmbroker/loader.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import requests
55
import yaml
66

7-
from helmbroker.config import ADDONS_PATH, CONFIG_PATH, Config
7+
from .config import ADDONS_PATH, CONFIG_PATH
8+
from .meta import dump_addons_meta
89

910

1011
def download_file(url, dest):
@@ -19,14 +20,6 @@ def download_file(url, dest):
1920
Loader=yaml.Loader)
2021

2122

22-
def read_addons_file():
23-
data = read_file(f'{ADDONS_PATH}/addons.yaml')
24-
if not data:
25-
return {}
26-
addons_info = yaml.load(data, Loader=yaml.Loader)
27-
return addons_info
28-
29-
3023
def read_file(filename):
3124
if not os.path.exists(filename):
3225
return
@@ -80,7 +73,7 @@ def addons_meta_file():
8073
with open(f'{ADDONS_PATH}/{"/".join(plan_meta)}', 'r') as f:
8174
addons_mata = yaml.load(f.read(), Loader=yaml.Loader)
8275
addons_dict[f'{"-".join(plan_meta[0].split("-")[0:-1])}']['plans'].append(addons_mata) # noqa
83-
save_file(yaml.dump(addons_dict), ADDONS_PATH, 'addons.yaml')
76+
dump_addons_meta(addons_dict)
8477

8578

8679
def load_addons(repository):

rootfs/helmbroker/meta.py

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import os
22
import json
33
from jsonschema import validate
4-
from .config import INSTANCES_PATH
5-
4+
from .config import INSTANCES_PATH, ADDONS_PATH
65

76
INSTANCE_META_SCHEMA = {
87
"type": "object",
@@ -40,7 +39,7 @@ def dump_instance_meta(instance_id, data):
4039
file = os.path.join(INSTANCES_PATH, instance_id, "instance.json")
4140
validate(instance=data, schema=INSTANCE_META_SCHEMA)
4241
with open(file, "w") as f:
43-
json.dump(f, data)
42+
f.write(json.dumps(data, sort_keys=True, indent=2))
4443

4544

4645
BINDING_META_SCHEMA = {
@@ -73,4 +72,64 @@ def dump_binding_meta(instance_id, data):
7372
file = os.path.join(INSTANCES_PATH, instance_id, "binding.json")
7473
validate(instance=data, schema=INSTANCE_META_SCHEMA)
7574
with open(file, "w") as f:
76-
f.write(json.dumps(data))
75+
f.write(json.dumps(data, sort_keys=True, indent=2))
76+
77+
78+
ADDONS_META_SCHEMA = {
79+
"type": "object",
80+
"patternProperties": {
81+
".*": {
82+
"id": {"type": "string"},
83+
"name": {"type": "string"},
84+
"version": {"type": "string"},
85+
"bindable": {"type": "boolean"},
86+
"instances_retrievable": {"type": "boolean"},
87+
"bindings_retrievable": {"type": "boolean"},
88+
"allow_context_updates": {"type": "boolean"},
89+
"description": {"type": "string"},
90+
"tags": {"type": "string"},
91+
"requires": {"type": "array"},
92+
"metadata": {"type": "object"},
93+
"plan_updateable": {"type": "boolean"},
94+
"dashboard_client": {"type": "object"},
95+
"plans": {
96+
"type": "object",
97+
"id": {"type": "string"},
98+
"name": {"type": "string"},
99+
"description": {"type": "string"},
100+
"metadata": {"type": "object"},
101+
"free": {"type": "boolean"},
102+
"bindable": {"type": "boolean"},
103+
"binding_rotatable": {"type": "boolean"},
104+
"plan_updateable": {"type": "boolean"},
105+
"schemas": {"type": "object"},
106+
"maximum_polling_duration": {"type": "integer"},
107+
"maintenance_info": {"type": "object"},
108+
"required": [
109+
"id", "name", "description"
110+
]
111+
},
112+
"required": [
113+
"id", "name", "description", "bindable", "version", "plans"
114+
]
115+
}
116+
}
117+
}
118+
119+
120+
def load_addons_meta():
121+
file = os.path.join(ADDONS_PATH, "addons.json")
122+
with open(file, 'r') as f:
123+
data = json.loads(f.read())
124+
if not data:
125+
return {}
126+
validate(instance=data, schema=INSTANCE_META_SCHEMA)
127+
return data
128+
129+
130+
def dump_addons_meta(data):
131+
file = os.path.join(ADDONS_PATH, "addons.json")
132+
validate(instance=data, schema=INSTANCE_META_SCHEMA)
133+
with open(file, "w") as f:
134+
print("save addons.json")
135+
f.write(json.dumps(data, sort_keys=True, indent=2))

rootfs/helmbroker/utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import subprocess
55

66
from .config import INSTANCES_PATH, ADDONS_PATH
7-
from .loader import read_addons_file
7+
from .meta import load_addons_meta
88

99

1010
def command(cmd, *args, output_type="text"):
@@ -22,7 +22,7 @@ def command(cmd, *args, output_type="text"):
2222

2323

2424
def get_addon_path(service_id, plan_id):
25-
services = read_addons_file()
25+
services = load_addons_meta()
2626
service = [addon for addon in [addons for _, addons in services.items()]
2727
if addon['id'] == service_id][0]
2828
plan = [plan for plan in service['plans'] if plan['id'] == plan_id][0]
@@ -34,7 +34,7 @@ def get_addon_path(service_id, plan_id):
3434

3535

3636
def get_addon_meta(service_id):
37-
services = read_addons_file()
37+
services = load_addons_meta()
3838
service = [addon for addon in [addons for _, addons in services.items()]
3939
if addon['id'] == service_id][0]
4040
return service
@@ -50,6 +50,11 @@ def get_addon_updateable(service_id):
5050
return service.get('plan_updateable', False)
5151

5252

53+
def get_addon_bindable(service_id):
54+
service = get_addon_meta(service_id)
55+
return service.get('bindable', False)
56+
57+
5358
def get_cred_value(ns, source):
5459
if source.get('serviceRef'):
5560
return get_service_key_value(ns, source['serviceRef'])

0 commit comments

Comments
 (0)