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 ...@@ -529,6 +529,7 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
->get('k8s_yaml_file_extensions'); ->get('k8s_yaml_file_extensions');
$regex = '/\.(' . preg_replace('/ +/', '|', preg_quote($extensions, NULL)) . ')$/i'; $regex = '/\.(' . preg_replace('/ +/', '|', preg_quote($extensions, NULL)) . ')$/i';
$idx = 0;
foreach ($files ?: [] as $file) { foreach ($files ?: [] as $file) {
$message = NULL; $message = NULL;
$validated = FALSE; $validated = FALSE;
...@@ -632,7 +633,6 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc ...@@ -632,7 +633,6 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
'#title' => $file->filename, '#title' => $file->filename,
'#open' => !$validated, '#open' => !$validated,
]; ];
$idx = count($form['yaml_files'][$directory]['files']) - 1;
if (!$validated && !empty($message)) { if (!$validated && !empty($message)) {
$form['yaml_files'][$directory]['files'][$idx] += [ $form['yaml_files'][$directory]['files'][$idx] += [
'message' => [ 'message' => [
...@@ -667,6 +667,7 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc ...@@ -667,6 +667,7 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
'#default_value' => str_replace($tmp_dir_name . '/', '', $file->uri), '#default_value' => str_replace($tmp_dir_name . '/', '', $file->uri),
], ],
]; ];
$idx++;
} }
} }
...@@ -1141,159 +1142,147 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc ...@@ -1141,159 +1142,147 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
$yaml_file_contents = []; $yaml_file_contents = [];
$yaml_file_names = []; $yaml_file_names = [];
$yaml_file_paths = []; $yaml_file_paths = [];
$pattern = '/\d+/';
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
if (strpos($key, 'launch_resource') !== FALSE) { if (strpos($key, 'launch_resource') !== FALSE) {
$match = []; $launch_resources[] = $value;
preg_match($pattern, $key, $match);
if (!empty($match)) {
$idx = $match[0];
$launch_resources[$idx] = $value;
}
} }
if (strpos($key, 'yaml_file_content') !== FALSE) { if (strpos($key, 'yaml_file_content') !== FALSE) {
$match = []; $yaml_file_contents[] = $value;
preg_match($pattern, $key, $match);
if (!empty($match)) {
$idx = $match[0];
$yaml_file_contents[$idx] = $value;
}
} }
if (strpos($key, 'yaml_file_name') !== FALSE) { if (strpos($key, 'yaml_file_name') !== FALSE) {
$match = []; $yaml_file_names[] = $value;
preg_match($pattern, $key, $match);
if (!empty($match)) {
$idx = $match[0];
$yaml_file_names[$idx] = $value;
}
} }
if (strpos($key, 'yaml_file_path') !== FALSE) { if (strpos($key, 'yaml_file_path') !== FALSE) {
$match = []; $yaml_file_paths[] = $value;
preg_match($pattern, $key, $match);
if (!empty($match)) {
$idx = $match[0];
$yaml_file_paths[$idx] = $value;
}
} }
} }
if (empty($yaml_file_contents) || empty($launch_resources) || empty($yaml_file_names) || empty($yaml_file_paths)) {
return;
}
$objects = []; $objects = [];
if (!empty($yaml_file_contents) && !empty($launch_resources) && !empty($yaml_file_names) && !empty($yaml_file_paths)) { $templates = $this->k8sService->supportedCloudLaunchTemplates();
$templates = $this->k8sService->supportedCloudLaunchTemplates(); $errors_arr = [];
$errors_arr = []; foreach (array_map(
foreach ($yaml_file_contents as $idx => $file_contents) { NULL,
if (empty($launch_resources[$idx])) { $yaml_file_contents,
continue; $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 { try {
// Render YAML. // Render YAML.
// Even if there is nothing to render, // Even if there is nothing to render,
// this will not affect the functionality. // this will not affect the functionality.
$rendered_yaml_contents = $this->cloudService->renderTemplate($file_contents, $this->getLaunchTemplateParameters($cloud_launch_template)); $rendered_yaml_contents = $this->cloudService->renderTemplate($file_contents, $this->getLaunchTemplateParameters($cloud_launch_template));
$yamls = $this->k8sService->decodeMultipleDocYaml($rendered_yaml_contents); $yamls = $this->k8sService->decodeMultipleDocYaml($rendered_yaml_contents);
$validated = FALSE; $validated = FALSE;
$object = NULL; $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. // Check if there have been any placeholders
foreach ($yamls ?: [] as $yaml) { // replaced by NULL during rendering.
if (!is_array($yaml)) { $undefined_placeholders = $this->checkUndefinedPlaceholders($file_contents, $this->getLaunchTemplateParameters($cloud_launch_template));
$this->messenger->addError($this->t('The file %file is not YAML format.', ['%file' => $yaml_file_names[$idx]])); if (!empty($undefined_placeholders)) {
break; $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 if (!isset($yaml['kind'])) {
// replaced by NULL during rendering. $this->messenger->addError($this->t("No 'Kind' element found in the file %file.", ['%file' => $yaml_file_name]));
$undefined_placeholders = $this->checkUndefinedPlaceholders($file_contents, $this->getLaunchTemplateParameters($cloud_launch_template)); break;
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'])) { $kind = $yaml['kind'];
$this->messenger->addError($this->t("No 'Kind' element found in the file %file.", ['%file' => $yaml_file_names[$idx]])); $object = array_search($kind, $templates);
break; if ($object === FALSE) {
} $this->messenger->addError($this->t("Unsupported 'Kind' element in the file %file.", ['%file' => $yaml_file_name]));
break;
}
$kind = $yaml['kind']; $validated = TRUE;
$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; // 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, if ($validated) {
// we should use the rendered $yaml string. $cloud_launch_template->get('field_detail')->setValue($rendered_yaml_contents);
$result = !empty($validated) $errors_before = count($this->messenger->messagesByType($this->messenger::TYPE_ERROR));
? $this->checkExistence( // Put an extra 2nd param to identify called by this function.
$rendered_yaml_contents, $this->launchK8sResources($cloud_launch_template, $launch_uid, $yaml_file_path);
$cloud_launch_template->getCloudContext(), $errors_after = count($this->messenger->messagesByType($this->messenger::TYPE_ERROR));
$cloud_launch_template->get('field_namespace')->value if ($errors_before === $errors_after) {
) : []; if (count($yamls) === 1) {
$objects[] = $object;
if ($validated && !empty($result)) { }
$errors_arr[] = $result; else {
continue; $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_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';
}
} }
} }
} }
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_arr is a two-level array, so we can't simply use array_merge.
$errors = call_user_func_array('array_merge', $errors_arr); $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),
]);
$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); $message = $this->formatPlural(count($errors),
$cloud_launch_template->get('field_detail')->setValue(NULL); 'The following resource still exists. Launch again after completely deleting it.<ul>@resources</ul>',
$cloud_launch_template->setValidationRequired(FALSE); 'The following resources still exist. Launch again after completely deleting them.<ul>@resources</ul>', [
$cloud_launch_template->save(); '@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