Commit a8a29865 authored by Ryo Yamashita's avatar Ryo Yamashita Committed by Yas Naoi
Browse files

Issue #3269377 by Ryo Yamashita, yas: Add the function to add K8s Namespace in the SPA

parent b14c32d6
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import ItemCountLabel from 'molecules/ItemCountLabel';
import PageSelector from 'molecules/PageSelector';
import EntityTable from 'organisms/EntityTable';
import MenuBar from 'organisms/MenuBar';
import K8sNamespaceCreatePage from 'pages/k8s/K8sNamespaceCreatePage';
import K8sPodCreatePage from 'pages/k8s/K8sPodCreatePage';
import React, { useContext, useEffect, useState } from 'react';
import { AppContext } from 'service/state';
@@ -96,6 +97,8 @@ const EntityPage = ({ menuTemplate }: {
    switch (menuTemplate.entityName) {
      case 'pod':
        return <K8sPodCreatePage />;
      case 'namespace':
        return <K8sNamespaceCreatePage />;
    }
  }

+76 −0
Original line number Diff line number Diff line
import PanelWrapper from 'atoms/PanelWrapper';
import Button from 'bootstrap3-components/Button';
import Col from 'bootstrap3-components/Col';
import FluidContainer from 'bootstrap3-components/FluidContainer';
import Form from 'bootstrap3-components/Form';
import GlyphIcon from 'bootstrap3-components/GlyphIcon';
import Row from 'bootstrap3-components/Row';
import KeyValueItem from 'model/KeyValueItem';
import KeyValueTableBlock from 'molecules/KeyValueTableBlock';
import MenuBar from 'organisms/MenuBar';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useEntityData } from 'service/state';

const K8sNamespaceCreatePage = () => {

  const [name, setName] = useState('');
  const [labels, setLabels] = useState<KeyValueItem[]>([
    { item_key: '', item_value: '' },
  ]);
  const [annotations, setAnnotations] = useState<KeyValueItem[]>([
    { item_key: '', item_value: '' },
  ]);
  const { t } = useTranslation();
  const { create } = useEntityData({
    cloudServiceProvider: 'k8s',
    entityName: 'namespace',
    entityRecords: []
  });

  return <>
    <MenuBar />
    <FluidContainer className="px-0">
      <Row className="mx-0">
        <Col>
          <Form>
            <PanelWrapper titleKey="namespace" title="Namespace">
              <Form.Group>
                <Form.Label className="form-required">Name</Form.Label>
                <input className="form-text form-control" type="text" value={name} onChange={(e) => {
                  setName(e.currentTarget.value);
                }} />
              </Form.Group>
              <Form.Group>
                <KeyValueTableBlock
                  title="Labels"
                  keyValueList={labels}
                  setKeyValueList={setLabels}
                />
                <KeyValueTableBlock
                  title="Annotations"
                  keyValueList={annotations}
                  setKeyValueList={setAnnotations}
                />
              </Form.Group>
            </PanelWrapper>
            <Button className="icon-before btn-success"
              disabled={name === ''}
              onClick={() => {
                create({
                  'name': name,
                  'labels': JSON.stringify(labels),
                  'annotations': JSON.stringify(annotations),
                });
              }}>
              <GlyphIcon iconName="ok" />{' ' + t('Save')}
            </Button>
          </Form>
        </Col>
      </Row>
    </FluidContainer>
  </>;

}

export default K8sNamespaceCreatePage;
+56 −9
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\k8s\Entity\K8sNamespace;
use Drupal\k8s\Entity\K8sNode;
use Drupal\k8s\Entity\K8sPod;
use Drupal\k8s\Form\K8sContentFormInterface;
@@ -695,7 +696,8 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    $detail = $request->get('detail', $entity->getDetail());
    $service = new K8sOperationsService(
      $this->k8sService,
      $this->cloudService
      $this->cloudService,
      $this->cloudConfigPluginManager
    );

    $result = $service->updateEntity($entity, $detail);
@@ -724,7 +726,8 @@ class ApiController extends ControllerBase implements ApiControllerInterface {

    $service = new K8sOperationsService(
      $this->k8sService,
      $this->cloudService
      $this->cloudService,
      $this->cloudConfigPluginManager
    );

    $result = $service->updateNamespace($entity, $labels, $annotations);
@@ -754,7 +757,8 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    $stopMinute = (int) ($request->get('stopMinute', (string) $entity->getStopMinute()));
    $service = new K8sOperationsService(
      $this->k8sService,
      $this->cloudService
      $this->cloudService,
      $this->cloudConfigPluginManager
    );

    $result = $service->updateSchedule($entity, $startHour, $startMinute, $stopHour, $stopMinute);
@@ -781,7 +785,8 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    $detail = $request->get('detail', $entity->getDetail());
    $service = new K8sOperationsService(
      $this->k8sService,
      $this->cloudService
      $this->cloudService,
      $this->cloudConfigPluginManager
    );

    $result = $service->updateEntity($entity, $detail);
@@ -800,11 +805,6 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
   * {@inheritdoc}
   */
  public function createPod(Request $request, string $cloud_context): JsonResponse {
    // Get entity data.
    $entity = K8sPod::create([
      'cloud_context' => $cloud_context,
    ]);

    $namespace = $request->get('namespace', 'default');
    $detail = $request->get('detail', '');
    $enable_time_scheduler = $request->get('enable_time_scheduler', 'true') === 'true';
@@ -814,6 +814,14 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
    $start_minute = $request->get('start_minute', 0);
    $stop_hour = $request->get('stop_hour', 18);
    $stop_minute = $request->get('stop_minute', 0);

    $entity = K8sPod::create([
      'cloud_context' => $cloud_context,
      'namespace' => $namespace,
      'detail' => $detail,
      'yaml_url' => $yaml_url,
    ]);

    $service = new K8sOperationsService(
      $this->k8sService,
      $this->cloudService,
@@ -844,4 +852,43 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
      ], 500);
  }

  /**
   * {@inheritdoc}
   */
  public function createNamespace(Request $request, string $cloud_context): JsonResponse {
    $name = $request->get('name', 'default');
    $labels = json_decode($request->get('labels', '[]'), TRUE);
    $annotations = json_decode($request->get('annotations', '[]'), TRUE);

    // Get entity data.
    $entity = K8sNamespace::create([
      'cloud_context' => $cloud_context,
      'name' => $name,
      'labels' => $labels,
      'annotations' => $annotations,
    ]);

    $service = new K8sOperationsService(
      $this->k8sService,
      $this->cloudService,
      $this->cloudConfigPluginManager
    );

    $result = $service->createNamespace(
      $entity,
      $cloud_context,
      $name,
      $labels
    );

    return !empty($result)
      ? new JsonResponse([
        'result' => 'OK',
      ], 200)
      : new JsonResponse([
        'result' => 'NG',
        'reason' => 'Internal Server Error',
      ], 500);
  }

}
+13 −0
Original line number Diff line number Diff line
@@ -501,4 +501,17 @@ interface ApiControllerInterface {
   */
  public function createPod(Request $request, string $cloud_context): JsonResponse;

  /**
   * Create K8s Namespace.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   Request.
   * @param string $cloud_context
   *   Cloud context string.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   The JSON response.
   */
  public function createNamespace(Request $request, string $cloud_context): JsonResponse;

}
+13 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\k8s\Entity\K8sSchedule;
use Drupal\cloud\Service\CloudServiceInterface;
use Drupal\k8s\Service\K8sOperationsServiceInterface;
use Drupal\k8s\Service\K8sServiceException;
use Drupal\k8s\Service\K8sServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -100,11 +101,20 @@ abstract class K8sContentForm extends CloudContentForm implements K8sContentForm
   */
  protected $cloudService;

  /**
   * The K8s operation service.
   *
   * @var \Drupal\k8s\Service\K8sOperationsServiceInterface
   */
  protected $k8sOperationsService;

  /**
   * K8sCloudContentForm constructor.
   *
   * @param \Drupal\k8s\Service\K8sServiceInterface $k8s_service
   *   The K8s Service.
   * @param \Drupal\k8s\Service\K8sOperationsServiceInterface $k8s_operations_service
   *   The K8s operation service.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository service.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
@@ -135,6 +145,7 @@ abstract class K8sContentForm extends CloudContentForm implements K8sContentForm
   *   The Cloud service.
   */
  public function __construct(K8sServiceInterface $k8s_service,
                              K8sOperationsServiceInterface $k8s_operations_service,
                              EntityRepositoryInterface $entity_repository,
                              EntityTypeBundleInfoInterface $entity_type_bundle_info,
                              TimeInterface $time,
@@ -151,6 +162,7 @@ abstract class K8sContentForm extends CloudContentForm implements K8sContentForm
                              CloudServiceInterface $cloud_service) {
    parent::__construct($entity_repository, $entity_type_bundle_info, $time, $messenger, $cloud_config_plugin_manager);
    $this->k8sService = $k8s_service;
    $this->k8sOperationsService = $k8s_operations_service;
    $this->entityLinkRenderer = $entity_link_renderer;
    $this->entityTypeManager = $entity_type_manager;
    $this->cacheRender = $cacheRender;
@@ -168,6 +180,7 @@ abstract class K8sContentForm extends CloudContentForm implements K8sContentForm
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('k8s'),
      $container->get('k8s.operations'),
      $container->get('entity.repository'),
      $container->get('entity_type.bundle.info'),
      $container->get('datetime.time'),
Loading