-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a new kubectl transport (#60)
* Implement a new kubectl transport for accessing Drupal instances running in Kubernetes. * Add tests for kubectl transport. * Update CHANGELOG.md * Do not escape arguments after "--" * PHPCS Beautify
- Loading branch information
1 parent
ca41dc8
commit 8f371ef
Showing
6 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Consolidation\SiteProcess\Factory; | ||
|
||
use Consolidation\SiteAlias\SiteAliasInterface; | ||
use Consolidation\SiteProcess\Transport\KubectlTransport; | ||
|
||
/** | ||
* KubectlTransportFactory will create an KubectlTransport for applicable site aliases. | ||
*/ | ||
class KubectlTransportFactory implements TransportFactoryInterface | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function check(SiteAliasInterface $siteAlias) | ||
{ | ||
return $siteAlias->has('kubectl'); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function create(SiteAliasInterface $siteAlias) | ||
{ | ||
return new KubectlTransport($siteAlias); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Consolidation\SiteProcess\Transport; | ||
|
||
use Consolidation\SiteProcess\SiteProcess; | ||
use Consolidation\SiteAlias\SiteAliasInterface; | ||
use Consolidation\SiteProcess\Util\Shell; | ||
|
||
/** | ||
* KubectlTransport knows how to wrap a command such that it runs in a container | ||
* on Kubernetes via kubectl. | ||
*/ | ||
class KubectlTransport implements TransportInterface | ||
{ | ||
/** @var bool */ | ||
protected $tty; | ||
|
||
/** @var \Consolidation\SiteAlias\SiteAliasInterface */ | ||
protected $siteAlias; | ||
|
||
public function __construct(SiteAliasInterface $siteAlias) | ||
{ | ||
$this->siteAlias = $siteAlias; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function configure(SiteProcess $process) | ||
{ | ||
$this->tty = $process->isTty(); | ||
} | ||
|
||
/** | ||
* inheritdoc | ||
*/ | ||
public function wrap($args) | ||
{ | ||
# TODO: How/where do we complain if a required argument is not available? | ||
$namespace = $this->siteAlias->get('kubectl.namespace'); | ||
$tty = $this->tty && $this->siteAlias->get('kubectl.tty', false) ? "true" : "false"; | ||
$interactive = $this->tty && $this->siteAlias->get('kubectl.interactive', false) ? "true" : "false"; | ||
$resource = $this->siteAlias->get('kubectl.resource'); | ||
$container = $this->siteAlias->get('kubectl.container'); | ||
|
||
$transport = [ | ||
'kubectl', | ||
"--namespace=$namespace", | ||
'exec', | ||
"--tty=$tty", | ||
"--stdin=$interactive", | ||
$resource, | ||
]; | ||
if ($container) { | ||
$transport[] = "--container=$container"; | ||
} | ||
$transport[] = "--"; | ||
|
||
return array_merge($transport, $args); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function addChdir($cd_remote, $args) | ||
{ | ||
return array_merge( | ||
[ | ||
'cd', | ||
$cd_remote, | ||
Shell::op('&&'), | ||
], | ||
$args | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Consolidation\SiteProcess; | ||
|
||
use Consolidation\SiteProcess\Transport\KubectlTransport; | ||
use PHPUnit\Framework\TestCase; | ||
use Consolidation\SiteAlias\SiteAlias; | ||
|
||
class KubectlTransportTest extends TestCase | ||
{ | ||
/** | ||
* Data provider for testWrap. | ||
*/ | ||
public function wrapTestValues() | ||
{ | ||
return [ | ||
// Everything explicit. | ||
[ | ||
'kubectl --namespace=vv exec --tty=false --stdin=false deploy/drupal --container=drupal -- ls', | ||
['ls'], | ||
[ | ||
'kubectl' => [ | ||
'tty' => false, | ||
'interactive' => false, | ||
'namespace' => 'vv', | ||
'resource' => 'deploy/drupal', | ||
'container' => 'drupal', | ||
] | ||
], | ||
], | ||
|
||
// Minimal. Kubectl will pick a container. | ||
[ | ||
'kubectl --namespace=vv exec --tty=false --stdin=false deploy/drupal -- ls', | ||
['ls'], | ||
[ | ||
'kubectl' => [ | ||
'namespace' => 'vv', | ||
'resource' => 'deploy/drupal', | ||
] | ||
], | ||
], | ||
|
||
// Don't escape arguments after "--" | ||
[ | ||
'kubectl --namespace=vv exec --tty=false --stdin=false deploy/drupal -- asdf "double" \'single\'', | ||
['asdf', '"double"', "'single'"], | ||
[ | ||
'kubectl' => [ | ||
'namespace' => 'vv', | ||
'resource' => 'deploy/drupal', | ||
] | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider wrapTestValues | ||
*/ | ||
public function testWrap($expected, $args, $siteAliasData) | ||
{ | ||
$siteAlias = new SiteAlias($siteAliasData, '@alias.dev'); | ||
$dockerTransport = new KubectlTransport($siteAlias); | ||
$actual = $dockerTransport->wrap($args); | ||
$this->assertEquals($expected, implode(' ', $actual)); | ||
} | ||
} |