Support from Acquia helps fund testing for Drupal Acquia logo

Comments

liuchanggang created an issue. See original summary.

liuchanggang’s picture

Issue summary: View changes
liuchanggang’s picture

Status: Active » Needs review
FileSize
39.06 KB
yas’s picture

Status: Needs review » Needs work
  1. +++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
    @@ -573,6 +713,48 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    +        $this->messageUser($this->t('Updated @name in @cloudContext.', ['@name' => $entity_type_name_capital_plural, '@cloudContext' => $cloud_context]));
    

    Please use @cloud_context as the internal string identifier.

  2. +++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
    @@ -136,197 +136,337 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    +  public function updateNodeList($cloud_context = '') {
    +    if (empty($cloud_context)) {
    ...
    +    }
    +    else {
    ...
    +    }
    

    ApiController::updateAllEntityList() code can be refactored into ApiController::updateEntityList().

  3. +++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
    @@ -136,197 +136,337 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    +  public function updateDaemonSetList($cloud_context = '') {
    +    if (empty($cloud_context)) {
    +      return $this->updateAllEntityList('ingress', 'ingresses');
    +    }
    +    else {
    +      return $this->updateEntityList('daemon_set', 'daemon_sets', $cloud_context);
    +    }
    

    Could you please double-check this code?

liuchanggang’s picture

Status: Needs work » Needs review
FileSize
33.34 KB

@yas
Thanks for insight. I have modified ApiController.php as we discussed. Thanks.

yas’s picture

Status: Needs review » Needs work
FileSize
463.41 KB

@liuchanggang

Thank you for the update. I tested the patch and it looks good to me from the point view of the functionality, but could you please refactor the status message? See also my comment at #3162214-5.

29.png

yas’s picture

yas’s picture

@liuchanggang

One more thing,

+++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
@@ -573,6 +576,48 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
+        $this->messageUser($this->t('Updated @name in @cloud_context.', ['@name' => $entity_type_name_capital_plural, '@cloud_context' => $cloud_context]));

Please change 'Updated @name in ...' to 'Updated @name of ...'.

liuchanggang’s picture

Status: Needs work » Needs review
FileSize
34.48 KB

@yas
I changed according to your comments. Thanks.

yas’s picture

Status: Needs review » Needs work
+++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
@@ -573,6 +578,52 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
+          Url::fromUri('internal:/clouds/k8s/' . $cloud_context . '/' . $entity_type_name)

Can you change to:

Url::fromUri("internal:/clouds/k8s/${cloud_context}/${entity_type_name}")
+++ b/modules/cloud_service_providers/k8s/src/Service/K8sBatchOperations.php
@@ -180,7 +182,12 @@ class K8sBatchOperations {
+      \Drupal::messenger()->addWarning(t('Unable to retrieve CPU and Memory usage of nodes of @name. Please install @$metrics_server_link to K8s.', [
+        '@name' => $cloud_config_link,

Can you change @name to %cloud_config?
liuchanggang’s picture

Status: Needs work » Needs review
FileSize
34.77 KB
77.7 KB

@yas,
I update code according to your comment.
Also I changed message format to as same as Jigish's
Screeshot

yas’s picture

Status: Needs review » Needs work

@liuchanggang

Thank you for updating the patch.

  1. +++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
    @@ -550,7 +552,10 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
       private function updateEntityList($entity_type_name, $entity_type_name_plural, $cloud_context) {
    

    Can we use CloudContentEntityTrait::getDisplayLabels() instead of passing two parameters such as $entity_type_name and $entity_type_name_plural? Currently if the entity name has two words e.g. cron jobs, then cron_jobs is displayed. We expect cron jobs in that case.

  2. +++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
    @@ -550,7 +552,10 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    +    if (empty($cloud_context)) {
    
    @@ -573,6 +578,59 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    +        $this->messageUser($this->t('Updated @resource_link of @cloud_config_link cloud service provider.', ['@resource_link' => $resource_link, '@cloud_config_link' => $cloud_config_link]));
    ...
    +        $this->messageUser($this->t('Unable to update @resource_link of @cloud_config_link cloud service provider.', ['@resource_link' => $resource_link, '@cloud_config_link' => $cloud_config_link]), 'error');
    

    @resource_link and @cloud_config_link should be %resource_link and %cloud_config_link (Bold and Italic).

liuchanggang’s picture

Status: Needs work » Needs review
FileSize
37.48 KB
232.8 KB

I have removed $entity_type_name_plural from whole controller with getPluralLabel()
On message it shows upper case now, If you think it should be lower case, let me know.
changed fonts to bold and Italic

screenshot

Status: Needs review » Needs work

The last submitted patch, 13: 3162125-13.patch, failed testing. View results

liuchanggang’s picture

Status: Needs work » Needs review
FileSize
37.96 KB
yas’s picture

Status: Needs review » Needs work

@liuchanggang

Thank you for updating the patch.

  1. +++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
    @@ -549,20 +549,25 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    +    $resource_name = $this->entityTypeManager
    +      ->getStorage('k8s_' . $entity_type_name)
    +      ->getEntityType()
    +      ->getPluralLabel();
    +    $update_method_name = 'update' . str_replace(' ', '', ucwords($resource_name, ' '));
    

    Can you change this code to use CloudContentEntityTrait::getDisplayLabels()? Please refer to the patch at #3162214-11
    and match the variable names as possible.

  2. +++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
    @@ -573,6 +578,59 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    +    $resource_name = $this->entityTypeManager
    +      ->getStorage('k8s_' . $entity_type_name)
    +      ->getEntityType()
    +      ->getPluralLabel();
    +    $update_method_name = 'update' . str_replace(' ', '', ucwords($resource_name, ' '));
    

    Ditto.

  3. +++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
    @@ -573,6 +578,59 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    +      $cloud_config_id = $cloud_config->id();
    +      $cloud_config_link = Link::fromTextAndUrl(
    +        $cloud_config->getName(),
    +        Url::fromUri("internal:/admin/structure/cloud_config/${cloud_config_id}")
    +      )->toString();
    

    We need to validate the permission before adding the link. Please refer to the patch at #3162214-11 and match the variable names as possible.

liuchanggang’s picture

Status: Needs work » Needs review
FileSize
38.12 KB

@yas
Thanks for review,
Updated to getDisplayLabels() and added permission check for resource link.

yas’s picture

Status: Needs review » Needs work

@liuchanggang

Thank you for updating the patch:

  1. +++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
    @@ -573,6 +575,61 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    +      $account = \Drupal::currentUser();
    +      $permission = 'list k8s ' . str_replace('_', ' ', $entity_type_name);
    +      if ($account->hasPermission($permission)
    ...
    +        $resource_link = Link::fromTextAndUrl(
    +          $labels['plural'],
    +          Url::fromUri("internal:/clouds/k8s/${cloud_context}/${entity_type_name}")
    +        )->toString();
    +      }
    

    Do we need the condition
    $account->hasPermission('view all cloud service providers') in this block? (because this logic handles $resource_link, not handling $cloud_config_link)

  2. +++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
    @@ -573,6 +575,61 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    +      $cloud_config_id = $cloud_config->id();
    +      $cloud_config_link = Link::fromTextAndUrl(
    +        $cloud_config->getName(),
    +        Url::fromUri("internal:/admin/structure/cloud_config/${cloud_config_id}")
    +      )->toString();
    

    We need $account->hasPermission('view all cloud service providers') here but it is not enough to check the permissions. FYI, could you please refer to https://git.drupalcode.org/project/cloud/-/blob/3.x/modules/cloud_servic...?

    I suggest we should do refactoring to move from ApiController::getCloudConfigLink() to CloudConfig::getCloudConfigLink(). (So we need to refactor OpenStack module, too)

liuchanggang’s picture

Status: Needs work » Needs review
FileSize
39.5 KB

@yas,

Thanks for review.

1, I am not very sure if we should use hasPermission('view all cloud service providers') at resource link, I just used same checking from Openstack side code. However, since the link will go to the resource page of a specific service provider, we might still need checking the permission for that service provider? In other word, do we need checking permissions to both Node and clound-context to access cloud_context/node? If so, we might need this check.

2, I moved getCloudConfigLink() to Cloud_config class, but I left permission checking in controller function. I think permission need be checked in routing or controller level.

yas’s picture

Status: Needs review » Needs work

@liuchanggang

Thank you for the update.

  1. +++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
    @@ -573,6 +575,70 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    +        && $account->hasPermission('view all cloud service providers')) {
    

    I don't think we need to have this condition since each resource and cloud service provider are different entities. I guess OpenStack code should be refactored.

  2. +++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
    @@ -573,6 +575,70 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    +      if ($account->hasPermission('view all cloud service providers')
    +        && ($account->hasPermission('view published cloud service providers')
    +        || $account->hasPermission('view own published cloud service providers')
    +        || $account->hasPermission('view unpublished cloud service providers')
    +        || $account->hasPermission('view own unpublished cloud service providers'))) {
    +        $cloud_config_link = $cloud_config->getCloudConfigLink();
    +      }
    

    This can be fine but can you replace these condition to AccessCheckTrait::allowedIfCanAccessCloudConfig() or some other way?

@xiaohua-guan

What do you think?

Xiaohua Guan’s picture

@yas @liuchanggang

About the permission, I don't think it should be checked in the function updateAllEntityList. I think the permission should be defined in k8s.route.yml.

For example, About the route below,

entity.k8s_node.list_update.all:
  path: '/clouds/k8s/node/update'
  defaults:
    _controller: '\Drupal\k8s\Controller\ApiController::updateNodeList'
  requirements:
    _permission: 'view k8s node'

The permission of the route below can be modified like this if you want to add a new permission to restrict.

  requirements:
    _permission: 'view k8s node,view all cloud service providers'

In addition, the code below is OK, but it will be better to use route to create URL if you consider the possibility of changing URL of a route in the future.

Url::fromUri("internal:/clouds/k8s/${cloud_context}/${entity_type_name}")

TO

Url::fromRoute(
  "view.${entity_type_name}.all",
  [
    'cloud_context' => $cloud_context,
  ]
)
liuchanggang’s picture

Status: Needs work » Needs review
FileSize
40.89 KB

@yas @xiaohua-guan

Thanks for review. I made following changes

1, add permissions "view all cloud service providers" in routing yml and removed it from controller
2, Use AccessCheckTrait::allowedIfCanAccessCloudConfig() to check permissions of provider edit links.
3, changed resource link to Url::fromRoute in ApiController.php and K8sBatchOperations.php

yas’s picture

@liuchanggang

Thank you for the update. For the better user experience, I understand that you kept this logic ; that is, if the user does have the permission, the Cloud Orchestrator displays the status message w/ the link to the cloud service provider however I want to double-check if @xiaohua-guan agrees to this code or not.

+++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
@@ -573,6 +577,69 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
+      $account = \Drupal::currentUser();
+      if ($this->allowedIfCanAccessCloudConfig($cloud_config, $account, 'view all cloud service providers')
+        && ($this->allowedIfCanAccessCloudConfig($cloud_config, $account, 'view published cloud service providers')
+        || $this->allowedIfCanAccessCloudConfig($cloud_config, $account, 'view own published cloud service providers')
+        || $this->allowedIfCanAccessCloudConfig($cloud_config, $account, 'view unpublished cloud service providers')
+        || $this->allowedIfCanAccessCloudConfig($cloud_config, $account, 'view own unpublished cloud service providers'))) {
+        $cloud_config_link = $cloud_config->getCloudConfigLink();
+      }
Xiaohua Guan’s picture

@yas @liuchanggang

Firstly, I think the code like below is not correct.

+    _permission: 'edit k8s network policy+view all cloud service providers'

The code means that somebody has permission "edit k8s network policy" OR "view all cloud service providers", but the relationship should be AND.

I don't think the code below is necessary, because the person refreshing the resources should has the permission "view all cloud service providers" which is defined in k8s.routing.yml.

+++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
@@ -573,6 +577,69 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
+      $account = \Drupal::currentUser();
+      if ($this->allowedIfCanAccessCloudConfig($cloud_config, $account, 'view all cloud service providers')
+        && ($this->allowedIfCanAccessCloudConfig($cloud_config, $account, 'view published cloud service providers')
+        || $this->allowedIfCanAccessCloudConfig($cloud_config, $account, 'view own published cloud service providers')
+        || $this->allowedIfCanAccessCloudConfig($cloud_config, $account, 'view unpublished cloud service providers')
+        || $this->allowedIfCanAccessCloudConfig($cloud_config, $account, 'view own unpublished cloud service providers'))) {
+        $cloud_config_link = $cloud_config->getCloudConfigLink();
+      }
liuchanggang’s picture

FileSize
39.83 KB

@yas @xiaohua-guan

Thanks for review. I made following changes
1, removed all permission checking logic from ApiControllers.
2, updated permission in yml, and changed the permission to AND condition, like 'view k8s node,view all cloud service providers'

yas’s picture

@liuchanggang

+++ b/modules/cloud_service_providers/k8s/src/Controller/ApiController.php
@@ -573,6 +575,62 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
+        K8sService::clearCacheValue();
...
+      K8sService::clearCacheValue();

As we discussed, let's move those two K8sService::clearCacheValue(); to outside of the foreach() loop

I think the patch should be fine in the other portions.

@xiaohua-guan

What do you think?

liuchanggang’s picture

Moved clearCacheValue() to end of the function.

Xiaohua Guan’s picture

Status: Needs review » Reviewed & tested by the community

@yas @liuchanggang

It looks good to me now. So I changed the status to RTBC.

yas’s picture

@xiaohua-guan

Thank you for your review. I'll merge the patch to 8.x-2.x and 3.x and close this issue as Fixed.

  • yas committed 03ba47b on 8.x-2.x authored by liuchanggang
    Issue #3162125 by liuchanggang, yas, Xiaohua Guan: Add a Refresh button...

  • yas committed 9d424b3 on 3.x authored by liuchanggang
    Issue #3162125 by liuchanggang, yas, Xiaohua Guan: Add a Refresh button...
yas’s picture

Status: Reviewed & tested by the community » Fixed
yas’s picture

Status: Fixed » Needs work

I'll merge to 8.x-1.x, too for the patch at #3163932

  • yas committed 9cdf633 on 8.x-1.x authored by liuchanggang
    Issue #3162125 by liuchanggang, yas, Xiaohua Guan: Add a Refresh button...
yas’s picture

Status: Needs work » Fixed

Merged the patch and closing this issue marked as Fixed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.