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

Issue #3085602 by baldwinlouie, yas: Add auto-detect Object type in K8s server template

parent b0034da7
No related branches found
Tags 8.x-2.0-beta1
No related merge requests found
......@@ -429,6 +429,16 @@ function k8s_namespace_allowed_values_function(FieldStorageConfig $definition, C
* @see options_allowed_values()
*/
function k8s_object_allowed_values_function(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable) {
return k8s_supported_cloud_server_templates();
}
/**
* Defines the supported cloud server template types.
*
* @return array
* An array of supported templates.
*/
function k8s_supported_cloud_server_templates() {
return [
'deployment' => 'Deployment',
'pod' => 'Pod',
......@@ -505,10 +515,28 @@ function k8s_entity_view_alter(array &$build, EntityInterface $entity, EntityVie
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function k8s_cloud_server_template_presave(EntityInterface $entity) {
if ($entity->bundle() == 'k8s') {
$yaml = Yaml::decode($entity->field_detail->value);
$kind = $yaml['kind'];
if (isset($kind)) {
$templates = k8s_supported_cloud_server_templates();
$object = array_search($kind, $templates);
if ($object != FALSE) {
$entity->field_object->value = $object;
}
}
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function k8s_form_cloud_server_template_k8s_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['field_object']['#access'] = FALSE;
k8s_form_cloud_server_template_k8s_form_common_alter($form, $form_state, $form_id);
}
......@@ -516,8 +544,8 @@ function k8s_form_cloud_server_template_k8s_add_form_alter(&$form, FormStateInte
* Implements hook_form_FORM_ID_alter().
*/
function k8s_form_cloud_server_template_k8s_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['field_object']['#access'] = FALSE;
k8s_form_cloud_server_template_k8s_form_common_alter($form, $form_state, $form_id);
// Hide new revision checkbox.
$form['new_revision']['#access'] = FALSE;
}
......@@ -526,6 +554,7 @@ function k8s_form_cloud_server_template_k8s_edit_form_alter(&$form, FormStateInt
* Implements hook_form_FORM_ID_alter().
*/
function k8s_form_cloud_server_template_k8s_copy_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['field_object']['#access'] = FALSE;
k8s_form_cloud_server_template_k8s_form_common_alter($form, $form_state, $form_id);
$name = $form['pod']['name']['widget'][0]['value']['#default_value'];
......@@ -662,6 +691,7 @@ function k8s_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entit
if (isset($fields['field_detail'])) {
// Use the ID as defined in the annotation of the constraint definition.
$fields['field_detail']->addConstraint('yaml_array_data', []);
$fields['field_detail']->addConstraint('yaml_object_support', []);
}
}
}
......
<?php
namespace Drupal\k8s\Plugin\Validation\Constraint;
/**
* Check the "Kind" element in the YAML file.
*
* The kind element needs to be supported by the K8s cloud server template.
* Currently only Pod and Deployment are supported.
*
* @Constraint(
* id = "yaml_object_support",
* label = @Translation("Yaml", context = "Validation"),
* )
*/
class YamlObjectSupportConstraint extends YamlArrayDataConstraint {
/**
* Error if object in "Kind" is not supported.
*
* @var string
*/
public $unsupportedObjectType = 'Unsupported object in "Kind" element. Only
Pod or Deployment supported';
/**
* Error if no "Kind" element found in K8s yaml file.
*
* @var string
*/
public $noObjectFound = 'No "Kind" element found.';
}
<?php
namespace Drupal\k8s\Plugin\Validation\Constraint;
use Drupal\Component\Serialization\Yaml;
use Symfony\Component\Validator\Constraint;
/**
* Validates the "kind" element.
*/
class YamlObjectSupportConstraintValidator extends YamlArrayDataConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) {
foreach ($items as $item) {
try {
$yaml = Yaml::decode($item->value);
$kind = $yaml['kind'];
if (isset($kind)) {
$templates = k8s_supported_cloud_server_templates();
$object = array_search($kind, $templates);
if ($object == FALSE) {
$this->context->addViolation($constraint->unsupportedObjectType);
}
}
else {
$this->context->addViolation($constraint->noObjectFound);
}
}
catch (\Exception $e) {
$this->context->addViolation($constraint->invalidYaml . $e->getMessage());
break;
}
}
}
}
......@@ -81,6 +81,8 @@ class CloudServerTemplateTest extends K8sTestCase {
$pods = $this->createPodTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
$add = $this->createServerTemplateTestFormData($pods, $object, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
// field_object is automatically calculated.
unset($add[$i]['field_object']);
$num = $i + 1;
$this->drupalPostForm(
"/clouds/design/server_template/$cloud_context/k8s/add",
......@@ -110,6 +112,8 @@ class CloudServerTemplateTest extends K8sTestCase {
$pods = $this->createPodTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
$edit = $this->createServerTemplateTestFormData($pods, $object, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
// field_object is automatically calculated.
unset($edit[$i]['field_object']);
$num = $i + 1;
// Go to listing page.
......@@ -181,10 +185,24 @@ class CloudServerTemplateTest extends K8sTestCase {
$cloud_context = $this->cloudContext;
// Create templates.
$data = $this->createPodTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
switch ($object) {
case 'pod':
$data = $this->createPodTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
break;
case 'deployment':
$data = $this->createDeploymentTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT, $this->namespace);
break;
default:
break;
}
$add = $this->createServerTemplateTestFormData($data, $object, self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
// field_object is automatically calculated.
unset($add[$i]['field_object']);
$num = $i + 1;
$this->drupalPostForm(
"/clouds/design/server_template/$cloud_context/k8s/add",
......
......@@ -127,7 +127,7 @@ trait K8sTestFormDataTrait {
$name = 'name-' . $random->name(8, TRUE);
$detail = <<<EOS
apiVersion: apps/v1
kind: Deployment
kind: Pod
metadata:
name: $name
labels:
......@@ -202,7 +202,7 @@ EOS;
$name = 'name-' . $random->name(8, TRUE);
$detail = <<<EOS
apiVersion: v1
kind: Service
kind: Deployment
metadata:
name: $name
spec:
......
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