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

Issue #3304547 by Xiaohua Guan, yas, baldwinlouie: Create a plugin mechanism...

Issue #3304547 by Xiaohua Guan, yas, baldwinlouie: Create a plugin mechanism to provide repository for new k8s resource
parent d3a79101
No related branches found
No related tags found
3 merge requests!1316Issue #3310263: Release 4.5.0,!1260Issue #3307397: Release 4.4.0,!1223Issue #3304547 by Xiaohua Guan, yas: Create a plugin mechanism to provide...
Showing
with 255 additions and 46 deletions
services:
k8s.factory:
class: Drupal\k8s\Service\K8sServiceFactory
arguments: ['@entity_type.manager', '@config.factory', '@current_user', '@plugin.manager.cloud_config_plugin', '@plugin.manager.field.field_type', '@entity_field.manager', '@lock', '@queue', '@module_handler', '@class_resolver', '@request_stack', '@cloud.cache', '@cloud']
arguments: ['@entity_type.manager', '@config.factory', '@current_user', '@plugin.manager.cloud_config_plugin', '@plugin.manager.field.field_type', '@entity_field.manager', '@lock', '@queue', '@module_handler', '@class_resolver', '@request_stack', '@cloud.cache', '@cloud', '@plugin.manager.k8s_repository_provider']
k8s:
factory: ['@k8s.factory', 'create']
......@@ -19,3 +19,9 @@ services:
k8s.cloud_orchestrator_manager:
class: Drupal\k8s\Service\K8sCloudOrchestratorManager
arguments: ['@k8s', '@entity_field.manager', '@entity_type.manager']
plugin.manager.k8s_repository_provider:
class: Drupal\k8s\Plugin\k8s\K8sRepositoryProviderManager
parent: default_plugin_manager
tags:
- { name: plugin_manager_cache_clear }
<?php
namespace Drupal\k8s\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines an K8sRepositoryProvider annotation object.
*
* @see \Drupal\k8s\Plugin\k8s\K8sRepositoryProviderBase
* @see \Drupal\k8s\Plugin\k8s\K8sRepositoryProviderInterface
* @see \Drupal\k8s\Plugin\k8s\K8sRepositoryProviderManager
* @see plugin_api
*
* @Annotation
*/
class K8sRepositoryProvider extends Plugin {
/**
* The plugin ID.
*
* @var string
*/
public $id;
/**
* The name of the module providing the plugin.
*
* @var string
*/
public $module;
}
<?php
namespace Drupal\k8s\Plugin\k8s;
use Drupal\k8s\Service\K8sClientExtension\Repositories\ApiServiceRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\ClusterRoleBindingRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\ClusterRoleRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\LimitRangeRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\MetricsNodeRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\MetricsPodRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\PriorityClassRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\K8sQuotaRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\StatefulSetRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\StorageClassRepository;
/**
* Defines the basic k8s repository provider.
*
* @K8sRepositoryProvider(
* id = "basic_k8s_repository_provider"
* )
*/
class BasicK8sRepositoryProvider extends K8sRepositoryProviderBase {
/**
* {@inheritdoc}
*/
public function getResourceMapping(): array {
return [
'apiServices' => ApiServiceRepository::class,
'clusterRoles' => ClusterRoleRepository::class,
'clusterRoleBindings' => ClusterRoleBindingRepository::class,
'limitRanges' => LimitRangeRepository::class,
'metricsNodes' => MetricsNodeRepository::class,
'metricsPods' => MetricsPodRepository::class,
'priorityClasses' => PriorityClassRepository::class,
'quotas' => K8sQuotaRepository::class,
'storageClasses' => StorageClassRepository::class,
'statefulSets' => StatefulSetRepository::class,
];
}
}
<?php
namespace Drupal\k8s\Plugin\k8s;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a base K8sRepositoryProvider implementation.
*
* @see \Drupal\k8s\Annotation\K8sRepositoryProvider
* @see \Drupal\k8s\Plugin\k8s\K8sRepositoryProviderInterface
* @see \Drupal\k8s\Plugin\k8s\K8sRepositoryProviderManager
* @see plugin_api
*/
abstract class K8sRepositoryProviderBase extends PluginBase implements K8sRepositoryProviderInterface, ContainerFactoryPluginInterface {
/**
* The Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* K8sRepositoryProviderBase constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The Entity type manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')
);
}
}
<?php
namespace Drupal\k8s\Plugin\k8s;
use Drupal\Component\Plugin\PluginInspectionInterface;
/**
* Defines an interface for K8sRepositoryProvider plugins.
*
* @see \Drupal\k8s\Annotation\K8sRepositoryProvider
* @see \Drupal\k8s\Plugin\k8s\K8sRepositoryProviderBase
* @see \Drupal\k8s\Plugin\k8s\K8sRepositoryProviderManager
* @see plugin_api
*/
interface K8sRepositoryProviderInterface extends PluginInspectionInterface {
/**
* Get resource mapping.
*
* @return array
* The resource mapping.
*/
public function getResourceMapping(): array;
}
<?php
namespace Drupal\k8s\Plugin\k8s;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\k8s\Annotation\K8sRepositoryProvider;
/**
* Provides a K8sRepositoryProvider manager.
*
* @see \Drupal\k8s\Annotation\K8sRepositoryProvider
* @see \Drupal\k8s\Plugin\k8s\K8sRepositoryProviderBase
* @see \Drupal\k8s\Plugin\k8s\K8sRepositoryProviderInterface
* @see plugin_api
*/
class K8sRepositoryProviderManager extends DefaultPluginManager {
/**
* Constructs a K8sRepositoryProviderManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/k8s', $namespaces, $module_handler, K8sRepositoryProviderInterface::class, K8sRepositoryProvider::class);
$this->alterInfo('k8s_repository_provider');
$this->setCacheBackend($cache_backend, 'k8s:k8s_repository_provider');
}
/**
* Get all resource mappings.
*
* @return array
* The results of all resource mappings.
*/
public function getAllResourceMappings(): array {
$mappings = [];
foreach ($this->getDefinitions() ?: [] as $id => $definition) {
$plugin = $this->createInstance($id);
$mappings = array_merge($mappings, $plugin->getResourceMapping());
}
return $mappings;
}
}
......@@ -2,16 +2,7 @@
namespace Drupal\k8s\Service\K8sClientExtension;
use Drupal\k8s\Service\K8sClientExtension\Repositories\ApiServiceRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\ClusterRoleBindingRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\ClusterRoleRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\LimitRangeRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\MetricsNodeRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\MetricsPodRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\PriorityClassRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\K8sQuotaRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\StatefulSetRepository;
use Drupal\k8s\Service\K8sClientExtension\Repositories\StorageClassRepository;
use Drupal\k8s\Plugin\k8s\K8sRepositoryProviderManager;
use Maclof\Kubernetes\RepositoryRegistry;
/**
......@@ -20,40 +11,16 @@ use Maclof\Kubernetes\RepositoryRegistry;
class K8sRepositoryRegistry extends RepositoryRegistry {
/**
* The constructor.
* Constructs a new constraint validator.
*
* @param \Drupal\k8s\Plugin\k8s\K8sRepositoryProviderManager $k8s_repository_provider_manager
* The K8s repository provider manager.
*/
public function __construct() {
parent::__construct();
// Add the repository of api services.
$this->map['apiServices'] = ApiServiceRepository::class;
// Add the repository of cluster roles.
$this->map['clusterRoles'] = ClusterRoleRepository::class;
// Add the repository of cluster role bindings.
$this->map['clusterRoleBindings'] = ClusterRoleBindingRepository::class;
// Change the repository of K8s LimitRanges.
$this->map['limitRanges'] = LimitRangeRepository::class;
public function __construct(K8sRepositoryProviderManager $k8s_repository_provider_manager) {
// Add the repository of metrics nodes.
$this->map['metricsNodes'] = MetricsNodeRepository::class;
// Add the repository of metrics pods.
$this->map['metricsPods'] = MetricsPodRepository::class;
// Add the repository of priority classes.
$this->map['priorityClasses'] = PriorityClassRepository::class;
// Change the repository of quotas.
$this->map['quotas'] = K8sQuotaRepository::class;
// Add the repository of storage classes.
$this->map['storageClasses'] = StorageClassRepository::class;
parent::__construct();
// Add the repository of K8s StatefulSets.
$this->map['statefulSets'] = StatefulSetRepository::class;
$this->map = array_merge($this->map, $k8s_repository_provider_manager->getAllResourceMappings());
}
}
......@@ -30,6 +30,7 @@ use Drupal\k8s\Entity\K8sNamespace;
use Drupal\k8s\Entity\K8sNode;
use Drupal\k8s\Entity\K8sPod;
use Drupal\k8s\Form\K8sContentFormInterface;
use Drupal\k8s\Plugin\k8s\K8sRepositoryProviderManager;
use Drupal\k8s\Service\K8sClientExtension\K8sClient;
use Drupal\k8s\Service\K8sClientExtension\K8sRepositoryRegistry;
use Drupal\k8s\Service\K8sClientExtension\Models\ApiService;
......@@ -160,6 +161,13 @@ class K8sService extends CloudServiceBase implements K8sServiceInterface {
*/
protected $cloudService;
/**
* The k8s repository provider manager.
*
* @var \Drupal\k8s\Plugin\k8s\K8sRepositoryProviderManager
*/
protected $k8sRepositoryProviderManager;
/**
* Constructs a new K8sService object.
*
......@@ -187,6 +195,8 @@ class K8sService extends CloudServiceBase implements K8sServiceInterface {
* The Cloud cache service.
* @param \Drupal\cloud\Service\CloudServiceInterface $cloud_service
* The Cloud service.
* @param \Drupal\k8s\Plugin\k8s\K8sRepositoryProviderManager $k8s_repository_provider_manager
* The K8s repository provider manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager,
ConfigFactoryInterface $config_factory,
......@@ -199,7 +209,8 @@ class K8sService extends CloudServiceBase implements K8sServiceInterface {
ModuleHandlerInterface $module_handler,
ClassResolverInterface $class_resolver,
CloudCacheServiceInterface $cloud_cache_service,
CloudServiceInterface $cloud_service) {
CloudServiceInterface $cloud_service,
K8sRepositoryProviderManager $k8s_repository_provider_manager) {
// The parent constructor takes care of $this->messenger object.
parent::__construct();
......@@ -220,6 +231,7 @@ class K8sService extends CloudServiceBase implements K8sServiceInterface {
$this->classResolver = $class_resolver;
$this->cloudCacheService = $cloud_cache_service;
$this->cloudService = $cloud_service;
$this->k8sRepositoryProviderManager = $k8s_repository_provider_manager;
}
/**
......@@ -258,7 +270,7 @@ class K8sService extends CloudServiceBase implements K8sServiceInterface {
'verify' => FALSE,
'namespace' => $namespace,
],
new K8sRepositoryRegistry(),
new K8sRepositoryRegistry($this->k8sRepositoryProviderManager),
GuzzleAdapter::createWithConfig(['verify' => FALSE])
);
}
......
......@@ -14,6 +14,7 @@ use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Session\AccountInterface;
use Drupal\k8s\Plugin\k8s\K8sRepositoryProviderManager;
use Symfony\Component\HttpFoundation\RequestStack;
/**
......@@ -138,6 +139,13 @@ class K8sServiceFactory {
*/
protected $cloudService;
/**
* The k8s repository provider manager.
*
* @var \Drupal\k8s\Plugin\k8s\K8sRepositoryProviderManager
*/
protected $k8sRepositoryProviderManager;
/**
* Constructs a new K8sService object.
*
......@@ -167,6 +175,8 @@ class K8sServiceFactory {
* The Cloud cache service.
* @param \Drupal\cloud\Service\CloudServiceInterface $cloud_service
* The Cloud cache service.
* @param \Drupal\k8s\Plugin\k8s\K8sRepositoryProviderManager $k8s_repository_provider_manager
* The K8s repository provider manager.
*/
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
......@@ -181,7 +191,8 @@ class K8sServiceFactory {
ClassResolverInterface $class_resolver,
RequestStack $request_stack,
CloudCacheServiceInterface $cloud_cache_service,
CloudServiceInterface $cloud_service
CloudServiceInterface $cloud_service,
K8sRepositoryProviderManager $k8s_repository_provider_manager
) {
// Setup the entity type manager for querying entities.
$this->entityTypeManager = $entity_type_manager;
......@@ -200,6 +211,7 @@ class K8sServiceFactory {
$this->request = $request_stack->getCurrentRequest();
$this->cloudCacheService = $cloud_cache_service;
$this->cloudService = $cloud_service;
$this->k8sRepositoryProviderManager = $k8s_repository_provider_manager;
}
/**
......@@ -240,7 +252,8 @@ class K8sServiceFactory {
$this->moduleHandler,
$this->classResolver,
$this->cloudCacheService,
$this->cloudService
$this->cloudService,
$this->k8sRepositoryProviderManager
);
}
......
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