Skip to content

Commit

Permalink
[VCDA-2768] Expose TKGm template list API endpoint (vmware#1168)
Browse files Browse the repository at this point in the history
Expose GET /api/cse/template/tkgm endpoint to retrieve TKGm templates in CSE catalog.
Additionally during CSE startup, read all TKGm template into memory.
  • Loading branch information
rocknes authored Aug 20, 2021
1 parent d176fe2 commit a8a7c28
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ def api_path_format(self):
SYSTEM_INFO = ('get info of system', '/cse/system')
SYSTEM_UPDATE = ('update system status', '/cse/system')
TEMPLATE_LIST = ('list all templates', '/cse/templates')
TKGM_TEMPLATE_LIST = ('list TKGm templates', '/cse/templates/tkgm')

V35_OVDC_LIST = ('list ovdcs for v35', '/cse/3.0/ovdcs')
V35_ORG_VDC_LIST = ('list org VDCs', '/cse/3.0/orgvdcs')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

from pyvcloud.vcd.client import MetadataDomain
from pyvcloud.vcd.client import MetadataVisibility
from pyvcloud.vcd.utils import metadata_to_dict

import container_service_extension.common.constants.server_constants as server_constants # noqa: E501
import container_service_extension.common.constants.shared_constants as shared_constants # noqa: E501
import container_service_extension.common.utils.core_utils as utils
import container_service_extension.common.utils.pyvcloud_utils as vcd_utils
from container_service_extension.logging.logger import NULL_LOGGER
Expand Down Expand Up @@ -118,3 +121,38 @@ def save_metadata(
domain=MetadataDomain.SYSTEM,
visibility=MetadataVisibility.PRIVATE
)


def read_all_tkgm_template(
client,
org_name,
catalog_name,
logger=NULL_LOGGER,
msg_update_callback=utils.NullPrinter()
):
"""."""
org = vcd_utils.get_org(client, org_name=org_name)
catalog_item_names = [
entry['name'] for entry in org.list_catalog_items(catalog_name)
]

templates = []
for item_name in catalog_item_names:
md = org.get_all_metadata_from_catalog_item(
catalog_name=catalog_name,
item_name=item_name
)
metadata_dict = metadata_to_dict(md)
filtered_dict = {}
kind = metadata_dict.get(server_constants.TKGmTemplateKey.KIND)
if kind == shared_constants.ClusterEntityKind.TKG_M.value:
msg = f"Found TKGm template {item_name}."
logger.debug(msg)
msg_update_callback.general(msg)
for item in server_constants.TKGmTemplateKey:
key = item.value
value = metadata_dict.get(key, '')
filtered_dict[key] = value
templates.append(filtered_dict)

return templates
11 changes: 11 additions & 0 deletions container_service_extension/server/request_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@
'handler': template_handler.template_list
}
}
},
{
'url': "cse/templates/tkgm",
RequestMethod.GET: {
('36.0', ): {
'allowed_params': [],
'required_params': [],
'operation': CseOperation.TEMPLATE_LIST,
'handler': template_handler.tkgm_template_list
}
}
}
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: BSD-2-Clause

from container_service_extension.common.constants.server_constants import LocalTemplateKey # noqa: E501
from container_service_extension.common.constants.server_constants import TKGmTemplateKey # noqa: E501
import container_service_extension.common.utils.server_utils as server_utils
from container_service_extension.lib.telemetry.constants import CseOperation
from container_service_extension.lib.telemetry.telemetry_handler import record_user_action_telemetry # noqa: E501
Expand Down Expand Up @@ -42,3 +43,35 @@ def template_list(request_data, op_ctx):
})

return sorted(templates, key=lambda i: (i['name'], i['revision']), reverse=True) # noqa: E501


def tkgm_template_list(request_data, op_ctx):
"""Request handler for template list operation.
:return: List of dictionaries with template info.
"""
config = server_utils.get_server_runtime_config()
tkgm_templates = []

for t in config['broker']['tkgm_templates']:
tkgm_templates.append({
'catalog': config['broker']['catalog'],
'catalog_item': t[TKGmTemplateKey.NAME],
'cni': t[TKGmTemplateKey.CNI],
'cni_version': t[TKGmTemplateKey.CNI_VERSION],
'cse_version': t[TKGmTemplateKey.CSE_VERSION],
'deprecated': 'n/a',
'description': '',
'docker_version': 'n/a',
'container_runtime': t[TKGmTemplateKey.CONTAINER_RUNTIME],
'container_runtime_version': t[TKGmTemplateKey.CONTAINER_RUNTIME_VERSION],
'is_default': 'No',
'kind': t[TKGmTemplateKey.KIND],
'kubernetes': t[TKGmTemplateKey.KUBERNETES],
'kubernetes_version': t[TKGmTemplateKey.KUBERNETES_VERSION],
'name': t[TKGmTemplateKey.NAME],
'os': f"{t[TKGmTemplateKey.OS]}-{t[TKGmTemplateKey.OS_VERSION]}",
'revision': str(t[TKGmTemplateKey.REVISION])
})

return sorted(tkgm_templates, key=lambda i: (i['name'], i['revision']), reverse=True) # noqa: E501
75 changes: 70 additions & 5 deletions container_service_extension/server/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import container_service_extension.installer.configure_cse as configure_cse
import container_service_extension.installer.templates.local_template_manager as ltm # noqa: E501
from container_service_extension.installer.templates.template_rule import TemplateRule # noqa: E501
import container_service_extension.installer.templates.tkgm_template_manager as ttm # noqa: E501
from container_service_extension.lib.telemetry.constants import CseOperation
from container_service_extension.lib.telemetry.constants import PayloadKey
from container_service_extension.lib.telemetry.telemetry_handler \
Expand Down Expand Up @@ -371,22 +372,32 @@ def run(self, msg_update_callback=utils.NullPrinter()):
# Read k8s catalog definition from catalog item metadata and append
# the same to to server run-time config
self._load_template_definition_from_catalog(
msg_update_callback=msg_update_callback)
msg_update_callback=msg_update_callback
)

# Read TKGm catalog definition from catalog item metadata and append
# the same to to server run-time config
self._load_tkgm_template_definition_from_catalog(
msg_update_callback=msg_update_callback
)

self._load_placement_policy_details(
msg_update_callback=msg_update_callback)
msg_update_callback=msg_update_callback
)

if self.config['service']['legacy_mode']:
# Read templates rules from config and update template definition
# in server run-time config
self._process_template_rules(
msg_update_callback=msg_update_callback)
msg_update_callback=msg_update_callback
)

# Make sure that all vms in templates are compliant with the
# compute policy specified in template definition (can be affected
# by rules).
self._process_template_compute_policy_compliance(
msg_update_callback=msg_update_callback)
msg_update_callback=msg_update_callback
)
else:
msg = "Template rules are not supported by CSE for vCD api " \
"version 35.0 or above. Skipping template rule processing."
Expand Down Expand Up @@ -591,7 +602,9 @@ def _load_placement_policy_details(
raise

def _load_template_definition_from_catalog(
self, msg_update_callback=utils.NullPrinter()):
self,
msg_update_callback=utils.NullPrinter()
):
# NOTE: If `enable_tkg_plus` in the config file is set to false,
# CSE server will skip loading the TKG+ template this will prevent
# users from performing TKG+ related operations.
Expand Down Expand Up @@ -667,6 +680,58 @@ def _load_template_definition_from_catalog(
if client:
client.logout()

def _load_tkgm_template_definition_from_catalog(
self,
msg_update_callback=utils.NullPrinter()
):
msg = "Loading TKGm template definition from catalog"
logger.SERVER_LOGGER.info(msg)
msg_update_callback.general_no_color(msg)

client = None
try:
log_filename = None
log_wire = utils.str_to_bool(
self.config['service'].get('log_wire')
)
if log_wire:
log_filename = logger.SERVER_DEBUG_WIRELOG_FILEPATH

# Since the config param has been read from file by
# get_validated_config method, we can safely use the
# default_api_version key, it will be set to the highest api
# version supported by VCD and CSE.
client = Client(
self.config['vcd']['host'],
api_version=self.config['service']['default_api_version'],
verify_ssl_certs=self.config['vcd']['verify'],
log_file=log_filename,
log_requests=log_wire,
log_headers=log_wire,
log_bodies=log_wire
)
credentials = BasicLoginCredentials(
self.config['vcd']['username'],
shared_constants.SYSTEM_ORG_NAME,
self.config['vcd']['password']
)
client.set_credentials(credentials)

org_name = self.config['broker']['org']
catalog_name = self.config['broker']['catalog']
tkgm_templates = ttm.read_all_tkgm_template(
client=client,
org_name=org_name,
catalog_name=catalog_name,
logger=logger.SERVER_LOGGER,
msg_update_callback=msg_update_callback
)

self.config['broker']['tkgm_templates'] = tkgm_templates
finally:
if client:
client.logout()

def _process_template_rules(self, msg_update_callback=utils.NullPrinter()):
if 'template_rules' not in self.config:
return
Expand Down

0 comments on commit a8a7c28

Please sign in to comment.