Skip to content
Snippets Groups Projects
Commit 0221d9ea authored by Takumaru Sekine's avatar Takumaru Sekine Committed by Yas Naoi
Browse files

Issue #3353180 by sekinet, yas: Fix a bug that K8s resources are not launched...

Issue #3353180 by sekinet, yas: Fix a bug that K8s resources are not launched when specifying multiple directories of Git repository
parent a4f2cff0
No related branches found
No related tags found
No related merge requests found
......@@ -529,6 +529,7 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
->get('k8s_yaml_file_extensions');
$regex = '/\.(' . preg_replace('/ +/', '|', preg_quote($extensions, NULL)) . ')$/i';
$idx = 0;
foreach ($files ?: [] as $file) {
$message = NULL;
$validated = FALSE;
......@@ -632,7 +633,6 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
'#title' => $file->filename,
'#open' => !$validated,
];
$idx = count($form['yaml_files'][$directory]['files']) - 1;
if (!$validated && !empty($message)) {
$form['yaml_files'][$directory]['files'][$idx] += [
'message' => [
......@@ -667,6 +667,7 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
'#default_value' => str_replace($tmp_dir_name . '/', '', $file->uri),
],
];
$idx++;
}
}
......@@ -1141,159 +1142,147 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
$yaml_file_contents = [];
$yaml_file_names = [];
$yaml_file_paths = [];
$pattern = '/\d+/';
foreach ($values as $key => $value) {
if (strpos($key, 'launch_resource') !== FALSE) {
$match = [];
preg_match($pattern, $key, $match);
if (!empty($match)) {
$idx = $match[0];
$launch_resources[$idx] = $value;
}
$launch_resources[] = $value;
}
if (strpos($key, 'yaml_file_content') !== FALSE) {
$match = [];
preg_match($pattern, $key, $match);
if (!empty($match)) {
$idx = $match[0];
$yaml_file_contents[$idx] = $value;
}
$yaml_file_contents[] = $value;
}
if (strpos($key, 'yaml_file_name') !== FALSE) {
$match = [];
preg_match($pattern, $key, $match);
if (!empty($match)) {
$idx = $match[0];
$yaml_file_names[$idx] = $value;
}
$yaml_file_names[] = $value;
}
if (strpos($key, 'yaml_file_path') !== FALSE) {
$match = [];
preg_match($pattern, $key, $match);
if (!empty($match)) {
$idx = $match[0];
$yaml_file_paths[$idx] = $value;
}
$yaml_file_paths[] = $value;
}
}
if (empty($yaml_file_contents) || empty($launch_resources) || empty($yaml_file_names) || empty($yaml_file_paths)) {
return;
}
$objects = [];
if (!empty($yaml_file_contents) && !empty($launch_resources) && !empty($yaml_file_names) && !empty($yaml_file_paths)) {
$templates = $this->k8sService->supportedCloudLaunchTemplates();
$errors_arr = [];
foreach ($yaml_file_contents as $idx => $file_contents) {
if (empty($launch_resources[$idx])) {
continue;
}
$templates = $this->k8sService->supportedCloudLaunchTemplates();
$errors_arr = [];
foreach (array_map(
NULL,
$yaml_file_contents,
$launch_resources,
$yaml_file_names,
$yaml_file_paths
) as [$file_contents, $launch_resource, $yaml_file_name, $yaml_file_path]) {
if (empty($launch_resource)) {
continue;
}
try {
// Render YAML.
// Even if there is nothing to render,
// this will not affect the functionality.
$rendered_yaml_contents = $this->cloudService->renderTemplate($file_contents, $this->getLaunchTemplateParameters($cloud_launch_template));
$yamls = $this->k8sService->decodeMultipleDocYaml($rendered_yaml_contents);
$validated = FALSE;
$object = NULL;
try {
// Render YAML.
// Even if there is nothing to render,
// this will not affect the functionality.
$rendered_yaml_contents = $this->cloudService->renderTemplate($file_contents, $this->getLaunchTemplateParameters($cloud_launch_template));
$yamls = $this->k8sService->decodeMultipleDocYaml($rendered_yaml_contents);
$validated = FALSE;
$object = NULL;
// Validate yamls.
foreach ($yamls ?: [] as $yaml) {
if (!is_array($yaml)) {
$this->messenger->addError($this->t('The file %file is not YAML format.', ['%file' => $yaml_file_name]));
break;
}
// Validate yamls.
foreach ($yamls ?: [] as $yaml) {
if (!is_array($yaml)) {
$this->messenger->addError($this->t('The file %file is not YAML format.', ['%file' => $yaml_file_names[$idx]]));
break;
// Check if there have been any placeholders
// replaced by NULL during rendering.
$undefined_placeholders = $this->checkUndefinedPlaceholders($file_contents, $this->getLaunchTemplateParameters($cloud_launch_template));
if (!empty($undefined_placeholders)) {
$missing_parameters = '';
foreach ($undefined_placeholders ?: [] as $missing_parameter) {
$missing_parameters .= "<li>{$missing_parameter}</li>";
}
$this->messenger->addError($this->t('The file <strong><a href=":url">%file</a></strong> contains undefined parameters:<ul>@missing_parameters</ul>',
[
':url' => $cloud_launch_template->toUrl('edit-form', ['absolute' => TRUE])->toString(),
'%file' => $yaml_file_name,
'@missing_parameters' => Markup::create($missing_parameters),
]));
break;
}
// Check if there have been any placeholders
// replaced by NULL during rendering.
$undefined_placeholders = $this->checkUndefinedPlaceholders($file_contents, $this->getLaunchTemplateParameters($cloud_launch_template));
if (!empty($undefined_placeholders)) {
$missing_parameters = '';
foreach ($undefined_placeholders ?: [] as $missing_parameter) {
$missing_parameters .= "<li>{$missing_parameter}</li>";
}
$this->messenger->addError($this->t('The file <strong><a href=":url">%file</a></strong> contains undefined parameters:<ul>@missing_parameters</ul>',
[
':url' => $cloud_launch_template->toUrl('edit-form', ['absolute' => TRUE])->toString(),
'%file' => $yaml_file_names[$idx],
'@missing_parameters' => Markup::create($missing_parameters),
]));
break;
}
if (!isset($yaml['kind'])) {
$this->messenger->addError($this->t("No 'Kind' element found in the file %file.", ['%file' => $yaml_file_name]));
break;
}
if (!isset($yaml['kind'])) {
$this->messenger->addError($this->t("No 'Kind' element found in the file %file.", ['%file' => $yaml_file_names[$idx]]));
break;
}
$kind = $yaml['kind'];
$object = array_search($kind, $templates);
if ($object === FALSE) {
$this->messenger->addError($this->t("Unsupported 'Kind' element in the file %file.", ['%file' => $yaml_file_name]));
break;
}
$kind = $yaml['kind'];
$object = array_search($kind, $templates);
if ($object === FALSE) {
$this->messenger->addError($this->t("Unsupported 'Kind' element in the file %file.", ['%file' => $yaml_file_names[$idx]]));
break;
}
$validated = TRUE;
}
$validated = TRUE;
}
// Whenever we need to check something,
// we should use the rendered $yaml string.
$result = !empty($validated)
? $this->checkExistence(
$rendered_yaml_contents,
$cloud_launch_template->getCloudContext(),
$cloud_launch_template->get('field_namespace')->value
) : [];
if ($validated && !empty($result)) {
$errors_arr[] = $result;
continue;
}
// Whenever we need to check something,
// we should use the rendered $yaml string.
$result = !empty($validated)
? $this->checkExistence(
$rendered_yaml_contents,
$cloud_launch_template->getCloudContext(),
$cloud_launch_template->get('field_namespace')->value
) : [];
if ($validated && !empty($result)) {
$errors_arr[] = $result;
continue;
}
if ($validated) {
$cloud_launch_template->get('field_detail')->setValue($rendered_yaml_contents);
$errors_before = count($this->messenger->messagesByType($this->messenger::TYPE_ERROR));
// Put an extra 2nd param to identify called by this function.
$this->launchK8sResources($cloud_launch_template, $launch_uid, $yaml_file_paths[$idx]);
$errors_after = count($this->messenger->messagesByType($this->messenger::TYPE_ERROR));
if ($errors_before === $errors_after) {
if (count($yamls) === 1) {
$objects[] = $object;
}
else {
$objects[] = 'mixed';
}
if ($validated) {
$cloud_launch_template->get('field_detail')->setValue($rendered_yaml_contents);
$errors_before = count($this->messenger->messagesByType($this->messenger::TYPE_ERROR));
// Put an extra 2nd param to identify called by this function.
$this->launchK8sResources($cloud_launch_template, $launch_uid, $yaml_file_path);
$errors_after = count($this->messenger->messagesByType($this->messenger::TYPE_ERROR));
if ($errors_before === $errors_after) {
if (count($yamls) === 1) {
$objects[] = $object;
}
else {
$objects[] = 'mixed';
}
}
}
catch (\Exception $e) {
$this->messenger->addError($this->t('Invalid Yaml format in the file %file: %message', [
'%file' => $yaml_file_names[$idx],
'%message' => $e->getMessage(),
]));
}
}
catch (\Exception $e) {
$this->messenger->addError($this->t('Invalid Yaml format in the file %file: %message', [
'%file' => $yaml_file_name,
'%message' => $e->getMessage(),
]));
}
}
// $errors_arr is a two-level array, so we can't simply use array_merge.
$errors = call_user_func_array('array_merge', $errors_arr);
if (!empty($errors)) {
$resources = '';
foreach ($errors as $idx => $error) {
$resources .= "<li>{$error->render()}</li>";
}
$message = $this->formatPlural(count($errors),
'The following resource still exists. Launch again after completely deleting it.<ul>@resources</ul>',
'The following resources still exist. Launch again after completely deleting them.<ul>@resources</ul>', [
'@resources' => Markup::create($resources),
]);
// $errors_arr is a two-level array, so we can't simply use array_merge.
$errors = call_user_func_array('array_merge', $errors_arr);
$this->messenger->addError($message);
if (!empty($errors)) {
$resources = '';
foreach ($errors ?: [] as $error) {
$resources .= "<li>{$error->render()}</li>";
}
$cloud_launch_template->get('field_object')->setValue($objects);
$cloud_launch_template->get('field_detail')->setValue(NULL);
$cloud_launch_template->setValidationRequired(FALSE);
$cloud_launch_template->save();
$message = $this->formatPlural(count($errors),
'The following resource still exists. Launch again after completely deleting it.<ul>@resources</ul>',
'The following resources still exist. Launch again after completely deleting them.<ul>@resources</ul>', [
'@resources' => Markup::create($resources),
]);
$this->messenger->addError($message);
}
$cloud_launch_template->get('field_object')->setValue($objects);
$cloud_launch_template->get('field_detail')->setValue(NULL);
$cloud_launch_template->setValidationRequired(FALSE);
$cloud_launch_template->save();
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment