Skip to content
Snippets Groups Projects
Commit 920a7871 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3079076 by Xiaohua Guan, yas, baldwinlouie: Manage cron jobs on K8s

parent 02620dd9
No related branches found
No related tags found
No related merge requests found
Showing
with 1688 additions and 2 deletions
langcode: en
status: true
dependencies:
module:
- k8s
id: k8s_cron_job_delete_action
label: 'Delete Cron Job(s)'
type: k8s_cron_job
plugin: entity:delete_action:k8s_cron_job
configuration: { }
...@@ -13,3 +13,7 @@ views.field.deployment_bulk_form: ...@@ -13,3 +13,7 @@ views.field.deployment_bulk_form:
views.field.service_bulk_form: views.field.service_bulk_form:
type: views_field_bulk_form type: views_field_bulk_form
label: 'service bulk form' label: 'service bulk form'
views.field.cron_job_bulk_form:
type: views_field_bulk_form
label: 'cron job bulk form'
...@@ -154,3 +154,21 @@ function k8s_update_8208() { ...@@ -154,3 +154,21 @@ function k8s_update_8208() {
]; ];
cloud_update_yml_definitions($files, 'k8s'); cloud_update_yml_definitions($files, 'k8s');
} }
/**
* Add entity type k8s_cron_job and view k8s_cron_job.
*/
function k8s_update_8209() {
// Add entity type k8s_cron_job.
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = \Drupal::entityTypeManager()->getDefinition('k8s_cron_job');
$definition_update_manager->uninstallEntityType($entity_type);
$definition_update_manager->installEntityType($entity_type);
// Add view k8s_cron_job.
$files = [
'views.view.k8s_cron_job.yml',
'system.action.k8s_cron_job_delete_action.yml',
];
cloud_update_yml_definitions($files, 'k8s');
}
...@@ -164,3 +164,40 @@ entity.k8s_service.delete: ...@@ -164,3 +164,40 @@ entity.k8s_service.delete:
title: 'Delete' title: 'Delete'
appears_on: appears_on:
- entity.k8s_service.canonical - entity.k8s_service.canonical
####################
# K8s Cron Job
####################
entity.k8s_cron_job.add_form:
route_name: entity.k8s_cron_job.add_form
title: 'Add K8s Cron Job'
appears_on:
- view.k8s_cron_job.list
entity.k8s_cron_job.refresh:
route_name: entity.k8s_cron_job.list_update
title: 'Refresh'
appears_on:
- view.k8s_cron_job.list
entity.k8s_cron_job.collection:
route_name: view.k8s_cron_job.list
title: 'List K8s Cron Jobs'
appears_on:
- entity.k8s_cron_job.add_form
- entity.k8s_cron_job.edit_form
- entity.k8s_cron_job.delete_form
- entity.k8s_cron_job.canonical
entity.k8s_cron_job.edit:
route_name: entity.k8s_cron_job.edit_form
title: 'Edit'
appears_on:
- entity.k8s_cron_job.canonical
entity.k8s_cron_job.delete:
route_name: entity.k8s_cron_job.delete_form
title: 'Delete'
appears_on:
- entity.k8s_cron_job.canonical
...@@ -31,3 +31,9 @@ k8s.local_tasks.service: ...@@ -31,3 +31,9 @@ k8s.local_tasks.service:
route_name: 'view.k8s_service.list' route_name: 'view.k8s_service.list'
base_route: 'k8s.local_tasks.cloud_context' base_route: 'k8s.local_tasks.cloud_context'
weight: 50 weight: 50
k8s.local_tasks.cron_job:
title: 'Cron Jobs'
route_name: 'view.k8s_cron_job.list'
base_route: 'k8s.local_tasks.cloud_context'
weight: 60
...@@ -46,6 +46,9 @@ function k8s_update_resources($cloud_context) { ...@@ -46,6 +46,9 @@ function k8s_update_resources($cloud_context) {
$k8s_service->updateNodes(); $k8s_service->updateNodes();
$k8s_service->updateNamespaces(); $k8s_service->updateNamespaces();
$k8s_service->updatePods(); $k8s_service->updatePods();
$k8s_service->updateDeployments();
$k8s_service->updateServices();
$k8s_service->updateCronJobs();
} }
/** /**
...@@ -288,6 +291,7 @@ function k8s_form_views_exposed_form_alter(&$form, FormStateInterface $form_stat ...@@ -288,6 +291,7 @@ function k8s_form_views_exposed_form_alter(&$form, FormStateInterface $form_stat
'views-exposed-form-k8s-pod-list', 'views-exposed-form-k8s-pod-list',
'views-exposed-form-k8s-deployment-list', 'views-exposed-form-k8s-deployment-list',
'views-exposed-form-k8s-service-list', 'views-exposed-form-k8s-service-list',
'views-exposed-form-k8s-cron-job-list',
])) { ])) {
return FALSE; return FALSE;
} }
......
...@@ -98,3 +98,23 @@ edit k8s service: ...@@ -98,3 +98,23 @@ edit k8s service:
delete k8s service: delete k8s service:
title: 'Delete k8s service' title: 'Delete k8s service'
description: 'Allow users to delete k8s service' description: 'Allow users to delete k8s service'
############################
# K8s Cron Job
############################
add k8s cron job:
title: 'Add k8s cron job'
description: 'Allow users to add k8s cron job'
list k8s cron job:
title: 'List k8s cron job'
description: 'Allow users to list k8s cron job'
view k8s cron job:
title: 'View k8s cron job'
description: 'Allow users to view k8s cron job'
edit k8s cron job:
title: 'Edit k8s cron job'
description: 'Allow users to edit k8s cron job'
delete k8s cron job:
title: 'Delete k8s cron job'
description: 'Allow users to delete k8s cron job'
...@@ -71,3 +71,14 @@ entity.k8s_service.list_update: ...@@ -71,3 +71,14 @@ entity.k8s_service.list_update:
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access' _custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options: options:
perm: 'edit k8s service' perm: 'edit k8s service'
entity.k8s_cron_job.list_update:
path: '/clouds/k8s/{cloud_context}/cron_job/update'
defaults:
_controller: '\Drupal\k8s\Controller\ApiController::updateCronJobList'
requirements:
# Use custom access that will check for cloud_context and the desired permission.
# Desired permission is passed as an option in the "perm" variable
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options:
perm: 'edit k8s cron job'
<?php
namespace Drupal\k8s\Plugin\Action;
use Drupal\Core\Action\Plugin\Action\DeleteAction;
/**
* Redirects to a Cron Job form.
*
* @Action(
* id = "entity:delete_action:k8s_cron_job",
* label = @Translation("Delete Cron Job"),
* type = "k8s_cron_job"
* )
*/
class DeleteCronJob extends DeleteAction {
}
...@@ -112,6 +112,13 @@ class ApiController extends ControllerBase implements ApiControllerInterface { ...@@ -112,6 +112,13 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
return $this->updateEntityList('service', $cloud_context); return $this->updateEntityList('service', $cloud_context);
} }
/**
* {@inheritdoc}
*/
public function updateCronJobList($cloud_context) {
return $this->updateEntityList('cron_job', $cloud_context);
}
/** /**
* Helper method to update entities. * Helper method to update entities.
* *
...@@ -121,7 +128,7 @@ class ApiController extends ControllerBase implements ApiControllerInterface { ...@@ -121,7 +128,7 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
* The cloud context. * The cloud context.
*/ */
private function updateEntityList($entity_type_name, $cloud_context) { private function updateEntityList($entity_type_name, $cloud_context) {
$entity_type_name_capital = ucfirst($entity_type_name); $entity_type_name_capital = str_replace('_', '', ucwords($entity_type_name, '_'));
$entity_type_name_capital_plural = $entity_type_name_capital . 's'; $entity_type_name_capital_plural = $entity_type_name_capital . 's';
$this->k8sService->setCloudContext($cloud_context); $this->k8sService->setCloudContext($cloud_context);
$update_method_name = 'update' . $entity_type_name_capital_plural; $update_method_name = 'update' . $entity_type_name_capital_plural;
......
...@@ -62,4 +62,15 @@ interface ApiControllerInterface { ...@@ -62,4 +62,15 @@ interface ApiControllerInterface {
*/ */
public function updateServiceList($cloud_context); public function updateServiceList($cloud_context);
/**
* Update all cron jobs.
*
* @param string $cloud_context
* Cloud context string.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* A redirect response object that may be returned by the controller.
*/
public function updateCronJobList($cloud_context);
} }
<?php
namespace Drupal\k8s\Controller;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\cloud\Traits\AccessCheckTrait;
/**
* Access controller for the Cron Job entity.
*
* @see \Drupal\k8s\Entity\K8sCronJob.
*/
class K8sCronJobAccessControlHandler extends EntityAccessControlHandler {
use AccessCheckTrait;
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
$view_namespace_perm = 'view k8s namespace ' . $entity->getNamespace();
switch ($operation) {
case 'view':
return $this->allowedIfCanAccessCloudConfig(
$entity,
$account,
[$view_namespace_perm, 'view k8s cron job']
);
case 'update':
case 'edit':
return $this->allowedIfCanAccessCloudConfig(
$entity,
$account,
[$view_namespace_perm, 'edit k8s cron job']
);
case 'delete':
return $this->allowedIfCanAccessCloudConfig(
$entity,
$account,
[$view_namespace_perm, 'delete k8s cron job']
);
}
// Unknown operation, no opinion.
return AccessResult::neutral();
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return $this->allowedIfCanAccessCloudConfig(
NULL,
$account,
'add k8s cron job'
);
}
}
<?php
namespace Drupal\k8s\Entity;
use Drupal\cloud\Entity\CloudContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
/**
* Defines the Cron Job entity.
*
* @ingroup k8s
*
* @ContentEntityType(
* id = "k8s_cron_job",
* label = @Translation("Kubernetes Cron Job"),
* handlers = {
* "view_builder" = "Drupal\k8s\Entity\K8sCronJobViewBuilder",
* "list_builder" = "Drupal\cloud\Controller\CloudContentListBuilder",
* "views_data" = "Drupal\k8s\Entity\K8sCronJobViewsData",
* "access" = "Drupal\k8s\Controller\K8sCronJobAccessControlHandler",
* "form" = {
* "add" = "Drupal\k8s\Form\K8sCronJobCreateForm",
* "edit" = "Drupal\k8s\Form\K8sCronJobEditForm",
* "delete" = "Drupal\k8s\Form\K8sCronJobDeleteForm",
* "delete-multiple-confirm" = "Drupal\k8s\Form\K8sCronJobDeleteMultipleForm",
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
* },
* },
* base_table = "k8s_cron_job",
* admin_permission = "administer k8s cron job",
* fieldable = TRUE,
* entity_keys = {
* "id" = "id",
* "label" = "name",
* "uuid" = "uuid",
* },
* links = {
* "canonical" = "/clouds/k8s/{cloud_context}/cron_job/{k8s_cron_job}",
* "collection" = "/clouds/k8s/{cloud_context}/cron_job",
* "add-form" = "/clouds/k8s/{cloud_context}/cron_job/add",
* "edit-form" = "/clouds/k8s/{cloud_context}/cron_job/{k8s_cron_job}/edit",
* "delete-form" = "/clouds/k8s/{cloud_context}/cron_job/{k8s_cron_job}/delete",
* "delete-multiple-form" = "/clouds/k8s/{cloud_context}/cron_job/delete_multiple",
* },
* field_ui_base_route = "k8s_cron_job.settings"
* )
*/
class K8sCronJob extends CloudContentEntityBase implements K8sCronJobInterface {
/**
* {@inheritdoc}
*/
public function getName() {
return $this->get('name')->value;
}
/**
* {@inheritdoc}
*/
public function setName($name) {
return $this->set('name', $name);
}
/**
* {@inheritdoc}
*/
public function getNamespace() {
return $this->get('namespace')->value;
}
/**
* {@inheritdoc}
*/
public function setNamespace($namespace) {
return $this->set('namespace', $namespace);
}
/**
* {@inheritdoc}
*/
public function getLabels() {
return $this->get('labels');
}
/**
* {@inheritdoc}
*/
public function setLabels($labels) {
return $this->set('labels', $labels);
}
/**
* {@inheritdoc}
*/
public function getAnnotations() {
return $this->get('annotations');
}
/**
* {@inheritdoc}
*/
public function setAnnotations($annotations) {
return $this->set('annotations', $annotations);
}
/**
* {@inheritdoc}
*/
public function getSchedule() {
return $this->get('schedule');
}
/**
* {@inheritdoc}
*/
public function setSchedule($schedule) {
return $this->set('schedule', $schedule);
}
/**
* {@inheritdoc}
*/
public function getActive() {
return $this->get('active');
}
/**
* {@inheritdoc}
*/
public function setActive($active) {
return $this->set('active', $active);
}
/**
* {@inheritdoc}
*/
public function isSuspend() {
return $this->get('suspend');
}
/**
* {@inheritdoc}
*/
public function setSuspend($suspend) {
return $this->set('suspend', $suspend);
}
/**
* {@inheritdoc}
*/
public function getLastScheduleTime() {
return $this->get('last_schedule_time');
}
/**
* {@inheritdoc}
*/
public function setLastScheduleTime($last_schedule_time) {
return $this->set('last_schedule_time', $last_schedule_time);
}
/**
* {@inheritdoc}
*/
public function getConcurrencyPolicy() {
return $this->get('concurrency_policy');
}
/**
* {@inheritdoc}
*/
public function setConcurrencyPolicy($concurrency_policy) {
return $this->set('concurrency_policy', $concurrency_policy);
}
/**
* {@inheritdoc}
*/
public function getStartingDeadlineSeconds() {
return $this->get('starting_deadline_seconds');
}
/**
* {@inheritdoc}
*/
public function setStartingDeadlineSeconds($starting_deadline_seconds) {
return $this->set('starting_deadline_seconds', $starting_deadline_seconds);
}
/**
* {@inheritdoc}
*/
public function getDetail() {
return $this->get('detail')->value;
}
/**
* {@inheritdoc}
*/
public function setDetail($detail) {
return $this->set('detail', $detail);
}
/**
* {@inheritdoc}
*/
public function getRefreshed() {
return $this->get('refreshed')->value;
}
/**
* {@inheritdoc}
*/
public function setRefreshed($time) {
return $this->set('refreshed', $time);
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Cron Job entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Cron Job entity.'))
->setReadOnly(TRUE);
$fields['cloud_context'] = BaseFieldDefinition::create('string')
->setRequired(TRUE)
->setLabel(t('Cloud ID'))
->setDescription(t('A unique machine name for the cloud provider.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
]);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The cron job name.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
])
->setReadOnly(TRUE);
$fields['namespace'] = BaseFieldDefinition::create('string')
->setLabel(t('Namespace'))
->setDescription(t('The namespace of cron job.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
])
->setReadOnly(TRUE);
$fields['labels'] = BaseFieldDefinition::create('key_value')
->setLabel(t('Labels'))
->setDescription(t('Map of string keys and values that can be used to organize and categorize (scope and select) objects.'))
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setDisplayOptions('view', [
'type' => 'key_value_formatter',
'weight' => -5,
]);
$fields['annotations'] = BaseFieldDefinition::create('key_value')
->setLabel(t('Annotations'))
->setDescription(t('Annotations is an unstructured key value map stored with a resource that may be set by external tools to store and retrieve arbitrary metadata.'))
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setSetting('max_length', 4096)
->setDisplayOptions('view', [
'type' => 'key_value_formatter',
'weight' => -5,
]);
$fields['schedule'] = BaseFieldDefinition::create('string')
->setLabel(t('Schedule'))
->setDescription(t('The schedule in Cron format.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
]);
$fields['active'] = BaseFieldDefinition::create('integer')
->setLabel(t('Active'))
->setDescription(t('The number of currently running jobs.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
]);
$fields['suspend'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Suspend'))
->setDescription(t('This flag tells the controller to suspend subsequent executions, it does not apply to already started executions. Defaults to false.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'boolean',
'weight' => -5,
'settings' => [
'format' => 'true-false',
],
]);
$fields['last_schedule_time'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Last Schedule Time'))
->setDescription('Information when was the last time the job was successfully scheduled.')
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'timestamp',
'weight' => -5,
]);
$fields['concurrency_policy'] = BaseFieldDefinition::create('string')
->setLabel(t('Concurrency Policy'))
->setDescription(t('Specifies how to treat concurrent executions of a Job.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
]);
$fields['starting_deadline_seconds'] = BaseFieldDefinition::create('integer')
->setLabel(t('Starting Deadline Seconds'))
->setDescription(t('Optional deadline in seconds for starting the job if it misses scheduled time for any reason.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
]);
$fields['detail'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Detail'))
->setDescription(t('Cron Job detail.'))
->setRequired(TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'pre_string_formatter',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textarea',
'settings' => [
'rows' => 20,
],
])
->addConstraint('yaml_array_data');
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
$fields['refreshed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Refreshed'))
->setDescription(t('The time that the entity was last refreshed.'));
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of the Pod entity author.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\node\Entity\Node::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
return $fields;
}
}
<?php
namespace Drupal\k8s\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\user\EntityOwnerInterface;
/**
* Provides an interface defining a Cron Job entity.
*
* @ingroup k8s
*/
interface K8sCronJobInterface extends ContentEntityInterface, EntityOwnerInterface {
/**
* {@inheritdoc}
*/
public function getCloudContext();
/**
* {@inheritdoc}
*/
public function getName();
/**
* {@inheritdoc}
*/
public function setName($name);
/**
* {@inheritdoc}
*/
public function getNamespace();
/**
* {@inheritdoc}
*/
public function setNamespace($namespace);
/**
* {@inheritdoc}
*/
public function getLabels();
/**
* {@inheritdoc}
*/
public function setLabels($labels);
/**
* {@inheritdoc}
*/
public function getAnnotations();
/**
* {@inheritdoc}
*/
public function setAnnotations($annotations);
/**
* {@inheritdoc}
*/
public function getSchedule();
/**
* {@inheritdoc}
*/
public function setSchedule($schedule);
/**
* {@inheritdoc}
*/
public function getActive();
/**
* {@inheritdoc}
*/
public function setActive($active);
/**
* {@inheritdoc}
*/
public function isSuspend();
/**
* {@inheritdoc}
*/
public function setSuspend($suspend);
/**
* {@inheritdoc}
*/
public function getLastScheduleTime();
/**
* {@inheritdoc}
*/
public function setLastScheduleTime($last_schedule_time);
/**
* {@inheritdoc}
*/
public function getConcurrencyPolicy();
/**
* {@inheritdoc}
*/
public function setConcurrencyPolicy($concurrency_policy);
/**
* {@inheritdoc}
*/
public function getStartingDeadlineSeconds();
/**
* {@inheritdoc}
*/
public function setStartingDeadlineSeconds($starting_deadline_seconds);
/**
* {@inheritdoc}
*/
public function getDetail();
/**
* {@inheritdoc}
*/
public function setDetail($detail);
/**
* {@inheritdoc}
*/
public function created();
/**
* {@inheritdoc}
*/
public function changed();
/**
* {@inheritdoc}
*/
public function getRefreshed();
/**
* {@inheritdoc}
*/
public function setRefreshed($time);
}
<?php
namespace Drupal\k8s\Entity;
use Drupal\cloud\Entity\CloudViewBuilder;
/**
* Provides the cron job view builders.
*/
class K8sCronJobViewBuilder extends CloudViewBuilder {
/**
* {@inheritdoc}
*/
protected function getFieldsetDefs() {
return [
[
'name' => 'cron_job',
'title' => t('Cron Job'),
'open' => TRUE,
'fields' => [
'name',
'namespace',
'schedule',
'active',
'suspend',
'last_schedule_time',
'concurrency_policy',
'created',
'labels',
'annotations',
],
],
[
'name' => 'cron_job_detail',
'title' => t('Detail'),
'open' => FALSE,
'fields' => [
'detail',
],
],
[
'name' => 'others',
'title' => t('Others'),
'open' => FALSE,
'fields' => [
'cloud_context',
'uid',
],
],
];
}
}
<?php
namespace Drupal\k8s\Entity;
use Drupal\views\EntityViewsData;
/**
* Provides the views data for the Cron Job entity type.
*/
class K8sCronJobViewsData extends EntityViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
$data['k8s_cron_job']['cron_job_bulk_form'] = [
'title' => $this->t('Cron job operations bulk form'),
'help' => $this->t('Add a form element that lets you run operations on multiple Cron Jobs.'),
'field' => [
'id' => 'cron_job_bulk_form',
],
];
// Additional information for Views integration, such as table joins, can be
// put here.
$data['k8s_cron_job']['table']['base']['access query tag'] = 'k8s_entity_views_access_with_namespace';
return $data;
}
}
...@@ -181,7 +181,7 @@ class K8sServiceEntity extends CloudContentEntityBase implements K8sServiceEntit ...@@ -181,7 +181,7 @@ class K8sServiceEntity extends CloudContentEntityBase implements K8sServiceEntit
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getExternalEndpoints() { public function getExternalEndpoints() {
return $this->get('internal_endpoints'); return $this->get('external_endpoints');
} }
/** /**
......
...@@ -317,4 +317,21 @@ class K8sContentForm extends CloudContentForm { ...@@ -317,4 +317,21 @@ class K8sContentForm extends CloudContentForm {
return $t->render() . ': '; return $t->render() . ': ';
} }
/**
* Get select options of namespace.
*
* @return array
* The options of namespace.
*/
protected function getNamespaceOptions() {
$namespaces = $this->k8sService->getNamespaces();
$options = [];
foreach ($namespaces as $namespace) {
$name = $namespace['metadata']['name'];
$options[$name] = $name;
}
return $options;
}
} }
<?php
namespace Drupal\k8s\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\Component\Serialization\Yaml;
/**
* Form controller for the cron job entity create form.
*
* @ingroup k8s
*/
class K8sCronJobCreateForm extends K8sContentForm {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $cloud_context = '') {
$form = parent::buildForm($form, $form_state);
$this->k8sService->setCloudContext($cloud_context);
$weight = -50;
$form['cron_job'] = [
'#type' => 'details',
'#title' => $this->t('Cron Job'),
'#open' => TRUE,
'#weight' => $weight++,
];
$form['cron_job']['namespace'] = [
'#type' => 'select',
'#title' => $this->t('Namespace'),
'#options' => $this->getNamespaceOptions(),
'#default_value' => 'default',
'#required' => TRUE,
];
$form['cron_job']['detail'] = $form['detail'];
unset($form['detail']);
$this->addOthersFieldset($form, $weight++, $cloud_context);
$form['actions'] = $this->actions($form, $form_state, $cloud_context);
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$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());
$result = $this->k8sService->createCronJob($entity->getNamespace(), $params);
if (!empty($result)
&& ($entity->setName($result['metadata']['name']))
&& ($entity->save())
) {
// Update the cron jobs.
$this->k8sService->updateCronJobs([
'metadata.name' => $entity->getName(),
], FALSE);
$message = $this->t('The @label "%label" has been created.', [
'@label' => $entity->getEntityType()->getLabel(),
'%label' => $entity->label(),
]);
$this->messenger->addMessage($message);
$form_state->setRedirect('view.k8s_cron_job.list', ['cloud_context' => $entity->getCloudContext()]);
}
else {
$message = $this->t('The @label "%label" couldn\'t create.', [
'@label' => $entity->getEntityType()->getLabel(),
'%label' => $entity->label(),
]);
$this->messenger->addError($message);
}
}
}
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