Skip to content
Snippets Groups Projects
Commit 38667b5e authored by baldwinlouie's avatar baldwinlouie Committed by Yas Naoi
Browse files

Issue #3085370 by baldwinlouie, yas: Add server template support for K8s Deployment

parent a1491fcf
No related branches found
No related tags found
No related merge requests found
......@@ -430,6 +430,7 @@ function k8s_namespace_allowed_values_function(FieldStorageConfig $definition, C
*/
function k8s_object_allowed_values_function(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable) {
return [
'deployment' => 'Deployment',
'pod' => 'Pod',
];
}
......
......@@ -173,6 +173,10 @@ class K8sCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
$route = $this->launchPod($cloud_server_template);
break;
case 'deployment':
$route = $this->launchDeployment($cloud_server_template);
break;
default:
$this->messenger->addError($this->t('@object launch not supported.', ['@object' => $k8s_obj]));
......@@ -235,6 +239,50 @@ class K8sCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
return $field;
}
/**
* Launch a K8s deployment from a server template.
*
* @param \Drupal\cloud\Entity\CloudServerTemplateInterface $cloud_server_template
* Cloud server template interface.
*
* @return array
* The route to redirect after launch.
*/
private function launchDeployment(CloudServerTemplateInterface $cloud_server_template) {
$params = Yaml::decode($cloud_server_template->get('field_detail')->value);
$route = [
'route_name' => 'view.k8s_deployment.list',
'params' => ['cloud_context' => $cloud_server_template->getCloudContext()],
];
try {
$result = $this->k8sService->createDeployment(
$cloud_server_template->get('field_namespace')->value,
$params
);
$this->k8sService->updateDeployments([
'metadata.name' => $result['metadata']['name'],
], FALSE);
$this->messenger->addStatus($this->t('Deployment @name launched.', ['@name' => $result['metadata']['name']]));
}
catch (K8sServiceException $e) {
$message = $this->t('The @label "%label" could not be launched.', [
'@label' => $cloud_server_template->getEntityType()->getLabel(),
'%label' => $cloud_server_template->label(),
]);
$this->messenger->addError($message);
$route = [
'route_name' => 'entity.cloud_server_template.canonical',
'params' => [
'cloud_server_template' => $cloud_server_template->id(),
'cloud_context' => $cloud_server_template->getCloudContext(),
],
];
}
return $route;
}
/**
* Launch a K8s pod from a server template.
*
......
......@@ -37,21 +37,49 @@ class CloudServerTemplateTest extends K8sTestCase {
'edit k8s pod',
'add k8s pod',
'delete k8s pod',
'list k8s deployment',
'view k8s deployment',
'edit k8s deployment',
'add k8s deployment',
'delete k8s deployment',
'launch cloud server template',
'view k8s namespace ' . $this->namespace,
];
}
/**
* CRUD test for k8s server template.
*/
public function testK8sServerTemplate() {
$objects = $this->getK8sObjects();
foreach ($objects as $object) {
$this->runServerTemplateCrudTest($object);
}
}
/**
* CRUD test for k8s server template launch.
*/
public function testK8sServerTemplateLaunch() {
$objects = $this->getK8sObjects();
foreach ($objects as $object) {
$this->runServerTemplateLaunchTest($object);
}
}
/**
* CRUD test for k8s pod server template.
*
* @param string $object
* Object to test.
*/
public function testK8sPodServerTemplate() {
private function runServerTemplateCrudTest($object) {
$cloud_context = $this->cloudContext;
// Create test.
$pods = $this->createPodTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
$add = $this->createServerTemplateTestFormData($pods, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
$add = $this->createServerTemplateTestFormData($pods, $object, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
$num = $i + 1;
$this->drupalPostForm(
......@@ -72,13 +100,24 @@ class CloudServerTemplateTest extends K8sTestCase {
);
}
// List test.
$this->drupalGet("/clouds/design/server_template/$cloud_context");
$this->assertResponse(200, t('HTTP 200: List | Cloud server template'));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
// Edit test.
$pods = $this->createPodTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
$edit = $this->createServerTemplateTestFormData($pods, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
$edit = $this->createServerTemplateTestFormData($pods, $object, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
$num = $i + 1;
// Go to listing page.
$this->drupalGet("/clouds/design/server_template/$cloud_context");
$this->clickLink('Edit', $i);
$this->drupalPostForm(
"/clouds/design/server_template/$cloud_context/$num/edit",
$this->getUrl(),
$edit[$i],
t('Save')
);
......@@ -101,49 +140,50 @@ class CloudServerTemplateTest extends K8sTestCase {
$this->assertText($edit[$i]['name[0][value]']);
}
// List test.
$this->drupalGet("/clouds/design/server_template/$cloud_context");
$this->assertResponse(200, t('HTTP 200: List | Cloud server template'));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
// Delete test.
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
$num = $i + 1;
$this->drupalGet("/clouds/design/server_template/$cloud_context/$num/delete");
$this->drupalPostForm("/clouds/design/server_template/$cloud_context/$num/delete",
// Go to listing page.
$this->drupalGet("/clouds/design/server_template/$cloud_context");
$this->clickLink('Delete', 0);
$this->drupalPostForm(
$this->getUrl(),
[],
t('Delete'));
$this->assertResponse(200, t('HTTP 200: Delete | Cloud server template #@num', ['@num' => $num]));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('warning'), t('Make sure w/o Warnings'));
}
// Make sure listing.
$this->drupalGet("/clouds/design/server_template/$cloud_context");
$this->assertResponse(200, t('HTTP 200: List | Cloud server te#@num', ['@num' => $num]));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
for ($j = 0; $j < $i + 1; $j++) {
$this->assertNoText($edit[$j]['name[0][value]'],
t("Make sure w/ Listing @num: @name", [
'@num' => $j + 1,
'@name' => $edit[$j]['name[0][value]'],
]));
}
// Make sure the deleted templates are not listed anymore.
$this->drupalGet("/clouds/design/server_template/$cloud_context");
$this->assertResponse(200, t('HTTP 200: List | Cloud server te#@num', ['@num' => $num]));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
for ($j = 0; $j < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $j++) {
$this->assertNoText($edit[$j]['name[0][value]'],
t("Make sure w/ Listing @num: @name", [
'@num' => $j + 1,
'@name' => $edit[$j]['name[0][value]'],
]));
}
}
/**
* Launch test for k8s pod server template.
*
* @param string $object
* Object to test.
*/
public function testK8sPodServerTemplateLaunch() {
private function runServerTemplateLaunchTest($object) {
$cloud_context = $this->cloudContext;
// Create templates.
$pods = $this->createPodTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
$add = $this->createServerTemplateTestFormData($pods, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
$data = $this->createPodTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
$add = $this->createServerTemplateTestFormData($data, $object, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
$num = $i + 1;
$this->drupalPostForm(
......@@ -166,13 +206,30 @@ class CloudServerTemplateTest extends K8sTestCase {
// Launch the templates.
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
$this->drupalGet("/clouds/design/server_template/$cloud_context");
// Update the mock data.
$this->getMetricsPodMockData([]);
$this->addPodMockData($pods[$i]);
switch ($object) {
case 'pod':
$this->getMetricsPodMockData([]);
$this->addPodMockData($data[$i]);
break;
case 'deployment':
$this->addDeploymentMockData($data[$i]);
break;
default;
break;
}
$num = $i + 1;
// Navigate to the server template, and launch it.
$this->clickLink($data[$i]['name']);
$this->clickLink('Launch');
$this->drupalPostForm(
"/clouds/design/server_template/$cloud_context/$num/launch",
$this->getUrl(),
[],
t('Launch')
);
......@@ -180,33 +237,56 @@ class CloudServerTemplateTest extends K8sTestCase {
$this->assertResponse(200, t('HTTP 200 for @num', ['@num' => $num]));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
$this->assertText(t('Pod @name launched.', ['@name' => $add[$i]['name[0][value]']]), t('Pod launched'));
$this->assertText(
t('@object @name launched.',
[
'@object' => ucfirst($object),
'@name' => $add[$i]['name[0][value]'],
]
),
t('@object launched', ['@object' => ucfirst($object)])
);
}
}
/**
* Get K8bs objects to test.
*
* @return array
* Array of K8s objects.
*/
protected function getK8sObjects() {
return [
'pod',
'deployment',
];
}
/**
* Create cloud server template test data.
*
* @param array $pods
* Pod array.
* @param array $test_data
* Test data array.
* @param string $object
* Object type.
* @param int $repeat_count
* Repeat count.
*
* @return array
* Test data.
*/
protected function createServerTemplateTestFormData(array $pods, $repeat_count = 1) {
protected function createServerTemplateTestFormData(array $test_data, $object, $repeat_count = 1) {
$data = [];
$random = $this->random;
for ($i = 0; $i < $repeat_count; $i++) {
$data[] = [
'cloud_context[0][value]' => $this->cloudContext,
'name[0][value]' => $pods[$i]['name'],
'name[0][value]' => $test_data[$i]['name'],
'field_namespace' => $this->namespace,
'field_detail[0][value]' => $pods[$i]['post_data']['detail[0][value]'],
'field_object' => 'pod',
'field_detail[0][value]' => $test_data[$i]['post_data']['detail[0][value]'],
'field_object' => $object,
];
}
return $data;
......
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