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

Issue #3217993 by sekinet, yas: Fix an Internal Server Error while creating a...

Issue #3217993 by sekinet, yas: Fix an Internal Server Error while creating a K8s resource with a YAML manifest incl. the separator '---'
parent 5c489f8b
No related branches found
No related tags found
No related merge requests found
......@@ -25,7 +25,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* The base form class for the k8s content form.
*/
class K8sContentForm extends CloudContentForm {
abstract class K8sContentForm extends CloudContentForm {
/**
* The K8s Service.
......@@ -192,6 +192,39 @@ class K8sContentForm extends CloudContentForm {
parent::save($form, $form_state);
}
/**
* Save the yaml of the entity detail.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function saveYamlsOfEntityDetail(FormStateInterface $form_state): bool {
$yamls = $this->k8sService->decodeMultipleDocYaml($this->entity->getDetail());
foreach ($yamls ?: [] as $yaml) {
if (empty($yaml)) {
continue;
}
// The process of saving each yaml to the entity
// is implemented by saveYamlInEntity().
if (empty($this->updateYamlToEntity($yaml, $form_state))) {
return FALSE;
}
}
return TRUE;
}
/**
* Update yaml information to entity.
*
* @param array $yaml
* The yaml.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
abstract protected function updateYamlToEntity(array $yaml, FormStateInterface $form_state): bool;
/**
* Copy values from #type=item elements to its original element type.
*
......
......@@ -79,33 +79,43 @@ class K8sCreateForm extends K8sContentForm {
$cloud_context = $this->routeMatch->getParameter('cloud_context');
$this->k8sService->setCloudContext($cloud_context);
$this->saveYamlsOfEntityDetail($form_state);
}
/**
* Update yaml information to entity.
*
* @param array $yaml
* The yaml.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function updateYamlToEntity(array $yaml, FormStateInterface $form_state): bool {
$entity = $this->entity;
$name_underscore = $this->getShortEntityTypeNameUnderscore($entity);
$name_camel = $this->getShortEntityTypeNameCamel($entity);
$params = Yaml::decode($entity->getDetail());
if (!empty($entity->getOwner())) {
// Add owner uid to annotations.
$params['metadata']['annotations'][K8sEntityBase::ANNOTATION_CREATED_BY_UID] = $entity->getOwner()->id();
$yaml['metadata']['annotations'][K8sEntityBase::ANNOTATION_CREATED_BY_UID] = $entity->getOwner()->id();
if ($name_underscore === 'deployment') {
// Add owner uid to pod template.
$params['spec']['template']['metadata']['annotations'][K8sEntityBase::ANNOTATION_CREATED_BY_UID] = $entity->getOwner()->id();
$yaml['spec']['template']['metadata']['annotations'][K8sEntityBase::ANNOTATION_CREATED_BY_UID] = $entity->getOwner()->id();
}
}
try {
$method_name = "create{$name_camel}";
if (method_exists($this->entity, 'getNamespace')) {
$result = $this->k8sService->$method_name($entity->getNamespace(), $params);
}
else {
$result = $this->k8sService->$method_name($params);
}
$result = method_exists($this->entity, 'getNamespace')
? $this->k8sService->$method_name($entity->getNamespace(), $yaml)
: $result = $this->k8sService->$method_name($yaml);
$entity->setName($result['metadata']['name']);
if (method_exists($entity, 'setCreationYaml')) {
$entity->setCreationYaml($entity->getDetail());
$entity->setCreationYaml(Yaml::encode($yaml));
}
$entity->save();
......@@ -123,6 +133,7 @@ class K8sCreateForm extends K8sContentForm {
catch (K8sServiceException
| EntityStorageException
| EntityMalformedException $e) {
$this->logger('k8s')->error($e->getMessage());
try {
$this->processOperationErrorStatus($entity, 'created');
......@@ -130,7 +141,10 @@ class K8sCreateForm extends K8sContentForm {
catch (EntityMalformedException $e) {
$this->handleException($e);
}
return FALSE;
}
return TRUE;
}
}
......@@ -111,24 +111,34 @@ class K8sDeploymentCreateForm extends K8sCreateForm {
public function save(array $form, FormStateInterface $form_state): void {
$this->trimTextfields($form, $form_state);
$entity = $this->entity;
$cloud_context = $this->routeMatch->getParameter('cloud_context');
$this->k8sService->setCloudContext($cloud_context);
$params = Yaml::decode($entity->getDetail());
$this->saveYamlsOfEntityDetail($form_state);
}
/**
* Update yaml information to entity.
*
* @param array $yaml
* The yaml.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function updateYamlToEntity(array $yaml, FormStateInterface $form_state): bool {
$entity = $this->entity;
if (!empty($entity->getOwner())) {
// Add owner uid to annotations.
$params['metadata']['annotations'][K8sEntityBase::ANNOTATION_CREATED_BY_UID] = $entity->getOwner()->id();
$yaml['metadata']['annotations'][K8sEntityBase::ANNOTATION_CREATED_BY_UID] = $entity->getOwner()->id();
}
try {
$result = $this->k8sService->createDeployment($entity->getNamespace(), $params);
$result = $this->k8sService->createDeployment($entity->getNamespace(), $yaml);
$entity->setName($result['metadata']['name']);
if (method_exists($entity, 'setCreationYaml')) {
$entity->setCreationYaml($entity->getDetail());
$entity->setCreationYaml(Yaml::encode($yaml));
}
$entity->save();
......@@ -146,7 +156,7 @@ class K8sDeploymentCreateForm extends K8sCreateForm {
$stop_hour = $form_state->getValue('stop_hour');
$stop_minute = $form_state->getValue('stop_minute');
$schedule = K8sSchedule::create([
'cloud_context' => $cloud_context,
'cloud_context' => $entity->getCloudContext(),
'name' => $entity->getNamespace() . '_' . $entity->getName(),
'kind' => 'Deployment',
'namespace_name' => $entity->getNamespace(),
......@@ -170,6 +180,7 @@ class K8sDeploymentCreateForm extends K8sCreateForm {
catch (K8sServiceException
| EntityStorageException
| EntityMalformedException $e) {
$this->logger('k8s')->error($e->getMessage());
try {
$this->processOperationErrorStatus($entity, 'created');
......@@ -177,7 +188,10 @@ class K8sDeploymentCreateForm extends K8sCreateForm {
catch (EntityMalformedException $e) {
$this->handleException($e);
}
return FALSE;
}
return TRUE;
}
}
......@@ -2,7 +2,6 @@
namespace Drupal\k8s\Form;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Form\FormStateInterface;
......@@ -75,6 +74,19 @@ class K8sEditForm extends K8sContentForm {
$this->copyFormItemValues($form);
$this->trimTextfields($form, $form_state);
$this->saveYamlsOfEntityDetail($form_state);
}
/**
* Update yaml information to entity.
*
* @param array $yaml
* The yaml.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function updateYamlToEntity(array $yaml, FormStateInterface $form_state): bool {
$entity = $this->entity;
$name_underscore = $this->getShortEntityTypeNameUnderscore($entity);
......@@ -84,27 +96,22 @@ class K8sEditForm extends K8sContentForm {
try {
$method_name = "update{$name_camel}";
$params = Yaml::decode($entity->getDetail());
if (!empty($entity->getOwner())) {
// Add owner uid to annotations.
$params['metadata']['annotations'][K8sEntityBase::ANNOTATION_CREATED_BY_UID] = $entity->getOwner()->id();
$yaml['metadata']['annotations'][K8sEntityBase::ANNOTATION_CREATED_BY_UID] = $entity->getOwner()->id();
if ($name_underscore === 'deployment') {
// Add owner uid to pod template.
$params['spec']['template']['metadata']['annotations'][K8sEntityBase::ANNOTATION_CREATED_BY_UID] = $entity->getOwner()->id();
$yaml['spec']['template']['metadata']['annotations'][K8sEntityBase::ANNOTATION_CREATED_BY_UID] = $entity->getOwner()->id();
}
}
if (method_exists($this->entity, 'getNamespace')) {
$this->k8sService->$method_name(
$entity->getNamespace(),
$params
);
}
else {
$this->k8sService->$method_name($params);
}
method_exists($this->entity, 'getNamespace')
? $this->k8sService->$method_name(
$entity->getNamespace(),
$yaml
)
: $this->k8sService->$method_name($yaml);
$entity->save();
......@@ -120,6 +127,7 @@ class K8sEditForm extends K8sContentForm {
catch (K8sServiceException
| EntityStorageException
| EntityMalformedException $e) {
$this->logger('k8s')->error($e->getMessage());
try {
......@@ -128,11 +136,14 @@ class K8sEditForm extends K8sContentForm {
catch (EntityMalformedException $e) {
$this->handleException($e);
}
return FALSE;
}
$form_state->setRedirect("view.k8s_{$name_underscore}.list", [
'cloud_context' => $entity->getCloudContext(),
]);
return TRUE;
}
}
......@@ -117,4 +117,16 @@ class K8sNamespaceCreateForm extends K8sContentForm {
}
}
/**
* Update yaml information to entity.
*
* @param array $yaml
* The yaml.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function updateYamlToEntity(array $yaml, FormStateInterface $form_state): bool {
return TRUE;
}
}
......@@ -115,4 +115,16 @@ class K8sNamespaceEditForm extends K8sContentForm {
]);
}
/**
* Update yaml information to entity.
*
* @param array $yaml
* The yaml.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function updateYamlToEntity(array $yaml, FormStateInterface $form_state): bool {
return TRUE;
}
}
......@@ -111,24 +111,34 @@ class K8sPodCreateForm extends K8sCreateForm {
public function save(array $form, FormStateInterface $form_state): void {
$this->trimTextfields($form, $form_state);
$entity = $this->entity;
$cloud_context = $this->routeMatch->getParameter('cloud_context');
$this->k8sService->setCloudContext($cloud_context);
$params = Yaml::decode($entity->getDetail());
$this->saveYamlsOfEntityDetail($form_state);
}
/**
* Update yaml information to entity.
*
* @param array $yaml
* The yaml.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function updateYamlToEntity(array $yaml, FormStateInterface $form_state): bool {
$entity = $this->entity;
if (!empty($entity->getOwner())) {
// Add owner uid to annotations.
$params['metadata']['annotations'][K8sEntityBase::ANNOTATION_CREATED_BY_UID] = $entity->getOwner()->id();
$yaml['metadata']['annotations'][K8sEntityBase::ANNOTATION_CREATED_BY_UID] = $entity->getOwner()->id();
}
try {
$result = $this->k8sService->createPod($entity->getNamespace(), $params);
$result = $this->k8sService->createPod($entity->getNamespace(), $yaml);
$entity->setName($result['metadata']['name']);
if (method_exists($entity, 'setCreationYaml')) {
$entity->setCreationYaml($entity->getDetail());
$entity->setCreationYaml(Yaml::encode($yaml));
}
$entity->save();
......@@ -146,7 +156,7 @@ class K8sPodCreateForm extends K8sCreateForm {
$stop_hour = $form_state->getValue('stop_hour');
$stop_minute = $form_state->getValue('stop_minute');
$schedule = K8sSchedule::create([
'cloud_context' => $cloud_context,
'cloud_context' => $entity->getCloudContext(),
'name' => $entity->getNamespace() . '_' . $entity->getName(),
'kind' => 'Pod',
'namespace_name' => $entity->getNamespace(),
......@@ -171,6 +181,7 @@ class K8sPodCreateForm extends K8sCreateForm {
catch (K8sServiceException
| EntityStorageException
| EntityMalformedException $e) {
$this->logger('k8s')->error($e->getMessage());
try {
$this->processOperationErrorStatus($entity, 'created');
......@@ -178,7 +189,10 @@ class K8sPodCreateForm extends K8sCreateForm {
catch (EntityMalformedException $e) {
$this->handleException($e);
}
return FALSE;
}
return TRUE;
}
}
......@@ -100,4 +100,16 @@ class K8sPodLogForm extends K8sContentForm {
return $form['log-wrapper'];
}
/**
* Update yaml information to entity.
*
* @param array $yaml
* The yaml.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function updateYamlToEntity(array $yaml, FormStateInterface $form_state): bool {
return TRUE;
}
}
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