Skip to content
Snippets Groups Projects
Commit 25d73f8c authored by jchang_docomo's avatar jchang_docomo Committed by Yas Naoi
Browse files

Issue #3108441 by jchang_docomo, yas: Manage K8s Persistent Volume Claims

parent fd6f1222
No related branches found
No related tags found
No related merge requests found
Showing
with 1642 additions and 0 deletions
langcode: en
status: true
dependencies:
module:
- k8s
id: k8s_persistent_volume_claim_delete_action
label: 'Delete Persistent Volume Claim(s)'
type: k8s_persistent_volume_claim
plugin: entity:delete_action:k8s_persistent_volume_claim
configuration: { }
......@@ -77,3 +77,7 @@ views.field.daemon_set_bulk_form:
views.field.endpoint_bulk_form:
type: views_field_bulk_form
label: 'endpoint bulk form'
views.field.persistent_volume_claim_bulk_form:
type: views_field_bulk_form
label: 'persistent volume claim bulk form'
......@@ -1283,6 +1283,24 @@ function k8s_update_8258() {
}
}
/**
* Add entity type k8s_persistent_volume_claim and view it.
*/
function k8s_update_8259() {
// Add entity type k8s_persistent_volume_claim.
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = \Drupal::entityTypeManager()->getDefinition('k8s_persistent_volume_claim');
$definition_update_manager->uninstallEntityType($entity_type);
$definition_update_manager->installEntityType($entity_type);
// Add view k8s_persistent_volume_claim.
$files = [
'views.view.k8s_persistent_volume_claim.yml',
'system.action.k8s_persistent_volume_claim_delete_action.yml',
];
cloud_update_yml_definitions($files, 'k8s');
}
/**
* Helper function to add default icon.
*/
......
......@@ -803,3 +803,40 @@ entity.k8s_event.collection:
title: 'List K8s Events'
appears_on:
- entity.k8s_event.canonical
###############################
# K8s Persistent Volume Claim
###############################
entity.k8s_persistent_volume_claim.add_form:
route_name: entity.k8s_persistent_volume_claim.add_form
title: 'Add K8s Persistent Volume Claim'
appears_on:
- view.k8s_persistent_volume_claim.list
entity.k8s_persistent_volume_claim.refresh:
route_name: entity.k8s_persistent_volume_claim.list_update
title: 'Refresh'
appears_on:
- view.k8s_persistent_volume_claim.list
entity.k8s_persistent_volume_claim.collection:
route_name: view.k8s_persistent_volume_claim.list
title: 'List K8s Persistent Volume Claims'
appears_on:
- entity.k8s_persistent_volume_claim.add_form
- entity.k8s_persistent_volume_claim.edit_form
- entity.k8s_persistent_volume_claim.delete_form
- entity.k8s_persistent_volume_claim.canonical
entity.k8s_persistent_volume_claim.edit:
route_name: entity.k8s_persistent_volume_claim.edit_form
title: 'Edit'
appears_on:
- entity.k8s_persistent_volume_claim.canonical
entity.k8s_persistent_volume_claim.delete:
route_name: entity.k8s_persistent_volume_claim.delete_form
title: 'Delete'
appears_on:
- entity.k8s_persistent_volume_claim.canonical
......@@ -112,6 +112,12 @@ k8s.local_tasks.persistent_volume:
base_route: 'k8s.local_tasks.cloud_context'
weight: 160
k8s.local_tasks.persistent_volume_claim:
title: 'Persisent Volume Claims'
route_name: 'view.k8s_persistent_volume_claim.list'
base_route: 'k8s.local_tasks.cloud_context'
weight: 165
k8s.local_tasks.storage_class:
title: 'Storage Classes'
route_name: 'view.k8s_storage_class.list'
......
......@@ -78,6 +78,7 @@ function k8s_update_resources($cloud_context) {
$k8s_service->updateDaemonSets();
$k8s_service->updateEndpoints();
$k8s_service->updateEvents();
$k8s_service->updatePersistentVolumeClaims();
}
/**
......@@ -429,6 +430,7 @@ function k8s_form_views_exposed_form_alter(array &$form, FormStateInterface $for
'views-exposed-form-k8s-daemon-set-list',
'views-exposed-form-k8s-endpoint-list',
'views-exposed-form-k8s-event-list',
'views-exposed-form-k8s-persistent-volume-claim-list',
])) {
return FALSE;
}
......
......@@ -451,3 +451,22 @@ view k8s event:
title: 'View k8s event'
description: 'Allow users to view k8s event'
###############################
# K8s Persistent Volume Claim
###############################
add k8s persistent volume claim:
title: 'Add k8s persistent volume claim'
description: 'Allow users to add k8s persistent volume claim'
list k8s persistent volume claim:
title: 'List k8s persistent volume claim'
description: 'Allow users to list persistent volume claim'
view k8s persistent volume claim:
title: 'View k8s persistent volume claim'
description: 'Allow users to view k8s persistent volume claim'
edit k8s persistent volume claim:
title: 'Edit k8s persistent volume claim'
description: 'Allow users to edit k8s persistent volume claim'
delete k8s persistent volume claim:
title: 'Delete k8s persistent volume claim'
description: 'Allow users to delete k8s persistent volume claim'
......@@ -357,6 +357,17 @@ entity.k8s_event.list_update:
options:
perm: 'view k8s event'
entity.k8s_persistent_volume_claim.list_update:
path: '/clouds/k8s/{cloud_context}/persistent_volume_claim/update'
defaults:
_controller: '\Drupal\k8s\Controller\ApiController::updatePersistentVolumeClaimList'
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 persistent volume claim'
entity.k8s_namespace.all_costs:
path: '/clouds/k8s/all_k8s_namespace_costs'
defaults:
......
......@@ -284,6 +284,13 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
return $this->updateEntityList('event', 'events', $cloud_context);
}
/**
* {@inheritdoc}
*/
public function updatePersistentVolumeClaimList($cloud_context) {
return $this->updateEntityList('persistent_volume_claim', 'persistent_volume_claims', $cloud_context);
}
/**
* {@inheritdoc}
*/
......
......@@ -263,6 +263,17 @@ interface ApiControllerInterface {
*/
public function updateEventList($cloud_context);
/**
* Update all persistent volume claim.
*
* @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 updatePersistentVolumeClaimList($cloud_context);
/**
* Get node metrics.
*
......
<?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 Persistent Volume Claim entity.
*
* @see \Drupal\k8s\Entity\K8sPersistentVolumeClaim.
*/
class K8sPersistentVolumeClaimAccessControlHandler extends EntityAccessControlHandler {
use AccessCheckTrait;
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
switch ($operation) {
case 'view':
return $this->allowedIfCanAccessCloudConfig(
$entity,
$account,
'view k8s persistent volume claim'
);
case 'update':
case 'edit':
return $this->allowedIfCanAccessCloudConfig(
$entity,
$account,
'edit k8s persistent volume claim'
);
case 'delete':
return $this->allowedIfCanAccessCloudConfig(
$entity,
$account,
'delete k8s persistent volume claim'
);
}
// 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 persistent volume claim'
);
}
}
<?php
namespace Drupal\k8s\Entity;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Defines the Persistent Volume Claim entity.
*
* @ingroup k8s
*
* @ContentEntityType(
* id = "k8s_persistent_volume_claim",
* id_plural = "k8s_persistent_volume_claims",
* label = @Translation("Kubernetes Persistent Volume Claim"),
* label_collection = @Translation("Kubernetes Persistent Volume Claims"),
* handlers = {
* "view_builder" = "Drupal\k8s\Entity\K8sPersistentVolumeClaimViewBuilder",
* "list_builder" = "Drupal\cloud\Controller\CloudContentListBuilder",
* "views_data" = "Drupal\k8s\Entity\K8sPersistentVolumeClaimViewsData",
* "access" = "Drupal\k8s\Controller\K8sPersistentVolumeClaimAccessControlHandler",
* "form" = {
* "add" = "Drupal\k8s\Form\K8sCreateForm",
* "edit" = "Drupal\k8s\Form\K8sEditForm",
* "delete" = "Drupal\k8s\Form\K8sDeleteForm",
* "delete-multiple-confirm" = "Drupal\k8s\Form\K8sDeleteMultipleForm",
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
* },
* },
* base_table = "k8s_persistent_volume_claim",
* admin_permission = "administer k8s persistent volume claim",
* fieldable = TRUE,
* entity_keys = {
* "id" = "id",
* "label" = "name",
* "uuid" = "uuid",
* },
* links = {
* "canonical" = "/clouds/k8s/{cloud_context}/persistent_volume_claim/{k8s_persistent_volume_claim}",
* "collection" = "/clouds/k8s/{cloud_context}/persistent_volume_claim",
* "add-form" = "/clouds/k8s/{cloud_context}/persistent_volume_claim/add",
* "edit-form" = "/clouds/k8s/{cloud_context}/persistent_volume_claim/{k8s_persistent_volume_claim}/edit",
* "delete-form" = "/clouds/k8s/{cloud_context}/persistent_volume_claim/{k8s_persistent_volume_claim}/delete",
* "delete-multiple-form" = "/clouds/k8s/{cloud_context}/persistent_volume_claim/delete_multiple",
* },
* field_ui_base_route = "k8s_persistent_volume_claim.settings"
* )
*/
class K8sPersistentVolumeClaim extends K8sEntityBase implements K8sPersistentVolumeClaimInterface {
/**
* {@inheritdoc}
*/
public function getNamespace() {
return $this->get('namespace')->value;
}
/**
* {@inheritdoc}
*/
public function setNamespace($val) {
return $this->set('namespace', $val);
}
/**
* {@inheritdoc}
*/
public function getPhase() {
return $this->get('phase')->value;
}
/**
* {@inheritdoc}
*/
public function setPhase($val) {
return $this->set('phase', $val);
}
/**
* {@inheritdoc}
*/
public function getVolumeName() {
return $this->get('volume_name')->value;
}
/**
* {@inheritdoc}
*/
public function setVolumeName($val) {
return $this->set('volume_name', $val);
}
/**
* {@inheritdoc}
*/
public function getCapacity() {
return $this->get('capacity')->value;
}
/**
* {@inheritdoc}
*/
public function setCapacity($val) {
return $this->set('capacity', $val);
}
/**
* {@inheritdoc}
*/
public function getRequest() {
return $this->get('request')->value;
}
/**
* {@inheritdoc}
*/
public function setRequest($val) {
return $this->set('request', $val);
}
/**
* {@inheritdoc}
*/
public function getAccessMode() {
return $this->get('access_mode')->value;
}
/**
* {@inheritdoc}
*/
public function setAccessMode($val) {
return $this->set('access_mode', $val);
}
/**
* {@inheritdoc}
*/
public function getStorageClass() {
return $this->get('storage_class')->value;
}
/**
* {@inheritdoc}
*/
public function setStorageClass($val) {
return $this->set('storage_class', $val);
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = K8sEntityBase::baseFieldDefinitions($entity_type);
$fields['namespace'] = BaseFieldDefinition::create('string')
->setLabel(t('Namespace'))
->setDescription(t('The namespace of persistent volume claim.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
])
->setReadOnly(TRUE);
$fields['phase'] = BaseFieldDefinition::create('string')
->setLabel(t('Phase'))
->setDescription(t('The status phase of persistent volume claim.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
]);
$fields['volume_name'] = BaseFieldDefinition::create('string')
->setLabel(t('VolumeName'))
->setDescription(t('The volume name of persistent volume claim.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
]);
$fields['capacity'] = BaseFieldDefinition::create('string')
->setLabel(t('Capacity'))
->setDescription(t('The capacity storage of persistent volume claim.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
]);
$fields['request'] = BaseFieldDefinition::create('string')
->setLabel(t('Request'))
->setDescription(t('The request storage of persistent volume claim.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
]);
$fields['access_mode'] = BaseFieldDefinition::create('string')
->setLabel(t('AccessMode'))
->setDescription(t('The access mode of persistent volume claim.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
]);
$fields['storage_class'] = BaseFieldDefinition::create('string')
->setLabel(t('StorageClass'))
->setDescription(t('The storage class of persistent volume claim.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
]);
return $fields;
}
}
<?php
namespace Drupal\k8s\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\user\EntityOwnerInterface;
/**
* Provides an interface defining a Persistent Volume Claim entity.
*
* @ingroup k8s
*/
interface K8sPersistentVolumeClaimInterface extends ContentEntityInterface, EntityOwnerInterface {
/**
* {@inheritdoc}
*/
public function getNamespace();
/**
* {@inheritdoc}
*/
public function setNamespace($val);
/**
* {@inheritdoc}
*/
public function getPhase();
/**
* {@inheritdoc}
*/
public function setPhase($val);
/**
* {@inheritdoc}
*/
public function getVolumeName();
/**
* {@inheritdoc}
*/
public function setVolumeName($val);
/**
* {@inheritdoc}
*/
public function getCapacity();
/**
* {@inheritdoc}
*/
public function setCapacity($val);
/**
* {@inheritdoc}
*/
public function getRequest();
/**
* {@inheritdoc}
*/
public function setRequest($val);
/**
* {@inheritdoc}
*/
public function getAccessMode();
/**
* {@inheritdoc}
*/
public function setAccessMode($val);
/**
* {@inheritdoc}
*/
public function getStorageClass();
/**
* {@inheritdoc}
*/
public function setStorageClass($val);
}
<?php
namespace Drupal\k8s\Entity;
use Drupal\cloud\Entity\CloudViewBuilder;
/**
* Provides the persistent volume claim view builders.
*/
class K8sPersistentVolumeClaimViewBuilder extends CloudViewBuilder {
/**
* {@inheritdoc}
*/
protected function getFieldsetDefs() {
return [
[
'name' => 'persistent_volume_claim',
'title' => $this->t('Persistent Volume Claim'),
'open' => TRUE,
'fields' => [
'name',
'namespace',
'phase',
'volume_name',
'capacity',
'request',
'access_mode',
'storage_class',
'created',
'labels',
'annotations',
],
],
[
'name' => 'persistent_volume_claim_detail',
'title' => $this->t('Detail'),
'open' => FALSE,
'fields' => [
'detail',
],
],
[
'name' => 'others',
'title' => $this->t('Others'),
'open' => FALSE,
'fields' => [
'cloud_context',
'uid',
],
],
];
}
}
<?php
namespace Drupal\k8s\Entity;
use Drupal\views\EntityViewsData;
/**
* Provides the views data for the Persistent Volume Claim entity type.
*/
class K8sPersistentVolumeClaimViewsData extends EntityViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
$data['k8s_persistent_volume_claim']['persistent_volume_claim_bulk_form'] = [
'title' => $this->t('Persistent Volume Claim operations bulk form'),
'help' => $this->t('Add a form element that lets you run operations on multiple Persistent Volume Claims.'),
'field' => [
'id' => 'persistent_volume_claim_bulk_form',
],
];
return $data;
}
}
......@@ -304,6 +304,7 @@ class K8sCreateProjectForm extends FormBase {
'ingress',
'daemon set',
'endpoint',
'persistent volume claim',
];
foreach ($resources as $resource) {
$permissions[] = "add k8s $resource";
......
<?php
namespace Drupal\k8s\Plugin\Action;
use Drupal\Core\Action\Plugin\Action\DeleteAction;
/**
* Redirects to a Persistent Volume Claim form.
*
* @Action(
* id = "entity:delete_action:k8s_persistent_volume_claim",
* label = @Translation("Delete Persistent Volume Claim"),
* type = "k8s_persistent_volume_claim"
* )
*/
class DeletePersistentVolumeClaim extends DeleteAction {
}
......@@ -108,6 +108,7 @@ class K8sMenuLinks extends DeriverBase implements ContainerDeriverInterface {
'k8s_role',
'k8s_cluster_role',
'k8s_persistent_volume',
'k8s_persistent_volume_claim',
'k8s_storage_class',
'k8s_stateful_set',
'k8s_ingress',
......
......@@ -28,6 +28,7 @@ use Drupal\k8s\Entity\K8sSecret;
use Drupal\k8s\Entity\K8sServiceEntity;
use Drupal\k8s\Entity\K8sStatefulSet;
use Drupal\k8s\Entity\K8sStorageClass;
use Drupal\k8s\Entity\K8sPersistentVolumeClaim;
/**
* Entity update methods for Batch API processing.
......@@ -1938,6 +1939,72 @@ class K8sBatchOperations {
$entity->save();
}
/**
* Update or create a k8s persistent volume claim entity.
*
* @param string $cloud_context
* The cloud context.
* @param array $persistent_volume_claim
* The persistent volume claim array.
* @param array $extra_data
* The extra data.
*/
public static function updatePersistentVolumeClaim($cloud_context, array $persistent_volume_claim, array $extra_data) {
$k8s_service = \Drupal::service('k8s');
$k8s_service->setCloudContext($cloud_context);
$timestamp = time();
$name = $persistent_volume_claim['metadata']['name'] ?? '';
$namespace = $persistent_volume_claim['metadata']['namespace'] ?? '';
$entity_id = $k8s_service->getEntityId(
'k8s_persistent_volume_claim',
'name',
$name,
['namespace' => $namespace]
);
if (!empty($entity_id)) {
$entity = K8sPersistentVolumeClaim::load($entity_id);
}
else {
$entity = K8sPersistentVolumeClaim::create([
'cloud_context' => $cloud_context,
'name' => $name,
'created' => self::getCreationTimestamp($persistent_volume_claim, $timestamp),
'changed' => self::getCreationTimestamp($persistent_volume_claim, $timestamp),
'refreshed' => $timestamp,
]);
}
// Labels.
self::setKeyValueTypeFieldValue(
$entity,
'labels',
$persistent_volume_claim['metadata']['labels'] ?? []
);
// Annotations.
self::setKeyValueTypeFieldValue(
$entity,
'annotations',
$persistent_volume_claim['metadata']['annotations'] ?? []
);
// Attributes.
$entity->setPhase($persistent_volume_claim['status']['phase'] ?? '');
$entity->setVolumeName($persistent_volume_claim['spec']['volumeName'] ?? '');
$entity->setCapacity($persistent_volume_claim['status']['capacity']['storage'] ?? '');
$entity->setRequest($persistent_volume_claim['spec']['resources']['requests']['storage'] ?? '');
$entity->setAccessMode($persistent_volume_claim['spec']['accessModes'] ?? '');
$entity->setStorageClass($persistent_volume_claim['spec']['storageClassName'] ?? '');
// Detail.
$entity->setDetail(Yaml::encode($persistent_volume_claim));
$entity->setNamespace($namespace);
$entity->setRefreshed($timestamp);
$entity->save();
}
/**
* Set key_value type field value.
*
......
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