Skip to content

Commit

Permalink
Create commands to get volumes, and replica sets and to create and de…
Browse files Browse the repository at this point in the history
…lete pods #6
  • Loading branch information
fgsl committed Sep 10, 2019
1 parent 07ffde6 commit c6eaee1
Show file tree
Hide file tree
Showing 17 changed files with 363 additions and 13 deletions.
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ You can create a .BAT file that contains this call.

*samplenamespace* is the variable argument that you must provide, a Kubernetes valid namespace.

All output shows elapsed time like:

```bash
Elapsed time: 0.35s
```

but this footer part is not mentioned in samples below.

### app:get-namespace

**Input:**
Expand Down Expand Up @@ -77,6 +85,71 @@ pod=podsample1 ready=1/1 status=Running restarts=0 age=6d11h
pod=podsample2 ready=1/1 status=Running restarts=0 age=6d11h
pod=podsample3 ready=1/1 status=Running restarts=0 age=6d11h
```

### app:get-replicasets

**Input:**

```bash
pkc.phar app:get-replicasets samplenamespace
```
**Output:**

```bash
Namespace samplenamespace
name=backend1 desired=0 current=0 ready=0 age=16d
name=backend2 desired=2 current=2 ready=2 age=15d
name=frontend1 desired=0 current=0 ready=0 age=15d
name=frontend2 desired=1 current=1 ready=1 age=13d
name=nginx-sample1 desired=1 current=0 ready=0 age=12d
name=nginx-sample2 desired=1 current=0 ready=0 age=12d
name=nginx-sample3 desired=1 current=0 ready=0 age=12d
name=nginx-sample4 desired=1 current=0 ready=0 age=12d
name=nginx-sample5 desired=1 current=0 ready=0 age=12d
name=teste-sample desired=1 current=1 ready=0 age=5h44m
```

### app:get-volumes

**Input:**

```bash
pkc.phar app:get-volumes samplemodule
```
**Output:**

```bash
Module samplemodule
pod=backend-sample1 ready=1/1 status=Running restarts=0 age=10d
pod=backend-sample2 ready=1/1 status=Running restarts=0 age=10d
```

### app:create-pod

**Input:**

```bash
pkc.phar app:create-pod path-to-yaml-file
```
**Output:**

```bash
pod/nginx-sample created
```

### app:delete-pod

**Input:**

```bash
pkc.phar app:delete-pod samplenamespace samplemmodule
```
**Output:**

```bash
pod "nginx-sample" deleted

```

## Build

Expand Down Expand Up @@ -107,3 +180,20 @@ Finally, change **phar.readonly** to **On** **[DON'T FORGET IT]**
**pkc.phar app:get-pods**

![pkc.phar](images/pkc04.png)

**pkc.phar app:get-replicasets**

![pkc.phar](images/pkc05.png)

**pkc.phar app:get-volumes**

![pkc.phar](images/pkc06.png)

**pkc.phar app:create-pod**

![pkc.phar](images/pkc07.png)

**pkc.phar app:delete-pod**

![pkc.phar](images/pkc08.png)

13 changes: 12 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
{
"name" : "fgsl/pkc",
"description" : "PHP Kubectl Console Administrator",
"type" : "console",
"keywords" : [
"administration",
"console",
"fgsl",
"kubectl"
],
"homepage" : "https://github.com/fgsl/pkc",
"license" : "LGPL-3.0-only",
"require" : {
"symfony/console" : "^4.3",
"fgsl/kubectl" : "1.2.1",
"fgsl/kubectl" : "1.3.0",
"phing/phing" : "^2.16"
},
"autoload" : {
Expand Down
24 changes: 12 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified images/pkc01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pkc05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pkc06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pkc07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pkc08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions pkc.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
use Fgsl\Kubectl\KubectlProxy;
use App\Command\GetResourceQuotaCommand;
use App\Command\GetPodsCommand;
use App\Command\GetVolumesCommand;
use App\Command\GetReplicaSetsCommand;
use App\Command\CreatePodCommand;
use App\Command\DeletePodCommand;

// checks if kubectl is installed
if (!KubectlProxy::isInstalled()){
Expand All @@ -23,4 +27,8 @@
$application->add(new GetNamespaceCommand());
$application->add(new GetResourceQuotaCommand());
$application->add(new GetPodsCommand());
$application->add(new GetVolumesCommand());
$application->add(new GetReplicaSetsCommand());
$application->add(new CreatePodCommand());
$application->add(new DeletePodCommand());
$application->run();
49 changes: 49 additions & 0 deletions src/Command/CreatePodCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* PHP Kubectl terminal console
* @author Flávio Gomes da Silva Lisboa <[email protected]>
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html
*/
namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Fgsl\Kubectl\KubernetesPods;
use App\Helper\Timer;

class CreatePodCommand extends Command
{

// the name of the command (the part after "bin/console")
protected static $defaultName = 'app:create-pod';

protected function configure()
{
$this->
// the short description shown while running "php bin/console list"
setDescription('Create a pod.')
->
// the full command description shown when running the command with
// the "--help" option
setHelp('This command allows you to create a pod.')
->
// configure an argument
addArgument('yaml-file', InputArgument::REQUIRED, 'Name of a yaml file.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$yaml = file_get_contents($input->getArgument('yaml-file'));
Timer::start();
$response = KubernetesPods::create($yaml);
$time = Timer::stop();
$output->writeln($response);
$output->writeln("Elapsed time: {$time}s");
} catch (\Exception $e) {
$output->writeln($e->getMessage());
}
}
}
49 changes: 49 additions & 0 deletions src/Command/DeletePodCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* PHP Kubectl terminal console
* @author Flávio Gomes da Silva Lisboa <[email protected]>
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html
*/
namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Fgsl\Kubectl\KubernetesPods;
use App\Helper\Timer;

class DeletePodCommand extends Command
{

// the name of the command (the part after "bin/console")
protected static $defaultName = 'app:delete-pod';

protected function configure()
{
$this->
// the short description shown while running "php bin/console list"
setDescription('Delete a pod.')
->
// the full command description shown when running the command with
// the "--help" option
setHelp('This command allows you to delete a pod.')
->
// configure an argument
addArgument('namespace', InputArgument::REQUIRED, 'The namespace of cluster.')
->addArgument('module', InputArgument::REQUIRED, 'The module of namespace.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
try {
Timer::start();
$response = KubernetesPods::delete($input->getArgument('namespace'), $input->getArgument('module'));
$time = Timer::stop();
$output->writeln($response);
$output->writeln("Elapsed time: {$time}s");
} catch (\Exception $e) {
$output->writeln($e->getMessage());
}
}
}
4 changes: 4 additions & 0 deletions src/Command/GetNamespaceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Fgsl\Kubectl\KubectlProxy;
use Symfony\Component\Console\Input\InputOption;
use App\Helper\Timer;

class GetNamespaceCommand extends Command
{
Expand Down Expand Up @@ -42,8 +43,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$object = (boolean) $input->getOption('object') || (boolean) $input->getOption('labels') || (boolean) $input->getOption('annotations');
Timer::start();
$kn = KubectlProxy::getNamespace($input->getArgument('namespace'), $object);
$time = Timer::stop();
$output->writeln($input->getOption('labels') ? $kn->getLabels() : ($input->getOption('annotations') ? $kn->getAnnotations() : $kn));
$output->writeln("Elapsed time: {$time}s");
} catch (\Exception $e) {
$output->writeln($e->getMessage());
}
Expand Down
4 changes: 4 additions & 0 deletions src/Command/GetPodsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Fgsl\Kubectl\KubectlProxy;
use Symfony\Component\Console\Input\InputOption;
use App\Helper\Timer;

class GetPodsCommand extends Command
{
Expand Down Expand Up @@ -42,8 +43,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
try {
$object = (boolean) $input->getOption('object');
$showLabels = (boolean) $input->getOption('labels');
Timer::start();
$kn = KubectlProxy::getPods($input->getArgument('namespace'), $object, $showLabels);
$time = Timer::stop();
$output->writeln($kn);
$output->writeln("Elapsed time: {$time}s");
} catch (\Exception $e) {
$output->writeln($e->getMessage());
}
Expand Down
Loading

0 comments on commit c6eaee1

Please sign in to comment.