Skip to content

Commit

Permalink
Merge pull request #34 from renoki-co/feature/watch-containers
Browse files Browse the repository at this point in the history
[feature] Watch containers
  • Loading branch information
rennokki authored Oct 31, 2020
2 parents 91e9859 + 032517b commit 146c36f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
7 changes: 7 additions & 0 deletions docs/kinds/Pod.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ Retrieve a single string with all logs until the point of call:
// Returns a long string with the logs

$logs = $pod->logs();

$mysqlLogs = $pod->containerLogs('mysql');
```

Open up a websocket connection and watch for changes, line-by-line:
Expand All @@ -134,6 +136,11 @@ $pod->watchLogs(function ($line) {
// Process the logic here
// with the given line.
});

$pod->watchContainerLogs('mysql', function ($line) {
// Process the logic here
// with the given line for the mysql container.
})
```

### Pod Status
Expand Down
79 changes: 70 additions & 9 deletions src/Kinds/K8sResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function whereNamespace($namespace)
/**
* Get the namespace for the resource.
*
* @return void
* @return string
*/
public function getNamespace()
{
Expand Down Expand Up @@ -567,7 +567,7 @@ public function refreshOriginal(array $query = ['pretty' => 1])
*
* @param Closure $callback
* @param array $query
* @return void
* @return mixed
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
*/
public function watchAll(Closure $callback, array $query = ['pretty' => 1])
Expand All @@ -593,7 +593,7 @@ public function watchAll(Closure $callback, array $query = ['pretty' => 1])
*
* @param Closure $callback
* @param array $query
* @return void
* @return mixed
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
*/
public function watch(Closure $callback, array $query = ['pretty' => 1])
Expand All @@ -619,7 +619,8 @@ public function watch(Closure $callback, array $query = ['pretty' => 1])
*
* @param Closure $callback
* @param array $query
* @return void
* @return mixed
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
*/
public function watchByName(string $name, Closure $callback, array $query = ['pretty' => 1])
{
Expand All @@ -630,7 +631,7 @@ public function watchByName(string $name, Closure $callback, array $query = ['pr
* Get a specific resource's logs.
*
* @param array $query
* @return \RenokiCo\PhpK8s\Kinds\K8sResource
* @return string
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
*/
public function logs(array $query = ['pretty' => 1])
Expand All @@ -651,24 +652,54 @@ public function logs(array $query = ['pretty' => 1])
);
}

/**
* Get logs for a specific container.
*
* @param string $container
* @param array $query
* @return string
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
*/
public function containerLogs(string $container, array $query = ['pretty' => 1])
{
return $this->logs(array_merge($query, ['container' => $container]));
}

/**
* Watch the specific resource by name.
*
* @param string $name
* @param Closure $callback
* @param array $query
* @return void
* @return string
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
*/
public function logsByName(array $query = ['pretty' => 1])
public function logsByName(string $name, array $query = ['pretty' => 1])
{
return $this->whereName($name)->logs($query);
}

/**
* Watch the specific resource by name.
*
* @param string $name
* @param string $container
* @param Closure $callback
* @param array $query
* @return string
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
*/
public function containerLogsByName(string $name, string $container, array $query = ['pretty' => 1])
{
return $this->logsByName($name, array_merge($query, ['container' => $container]));
}

/**
* Watch the specific resource's logs until the closure returns true or false.
*
* @param Closure $callback
* @param array $query
* @return void
* @return mixed
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
*/
public function watchLogs(Closure $callback, array $query = ['pretty' => 1])
Expand All @@ -692,15 +723,45 @@ public function watchLogs(Closure $callback, array $query = ['pretty' => 1])
);
}

/**
* Watch the specific resource's container logs until the closure returns true or false.
*
* @param string $container
* @param Closure $callback
* @param array $query
* @return mixed
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
*/
public function watchContainerLogs(string $container, Closure $callback, array $query = ['pretty' => 1])
{
return $this->watchLogs($callback, array_merge($query, ['container' => $container]));
}

/**
* Watch the specific resource's logs by name.
*
* @param Closure $callback
* @param array $query
* @return void
* @return mixed
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
*/
public function watchLogsByName(string $name, Closure $callback, array $query = ['pretty' => 1])
{
return $this->whereName($name)->watchLogs($callback, $query);
}

/**
* Watch the specific resource's container logs by names.
*
* @param string $name
* @param string $container
* @param Closure $callback
* @param array $query
* @return mixed
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
*/
public function watchContainerLogsByName(string $name, string $container, Closure $callback, array $query = ['pretty' => 1])
{
return $this->watchLogsByName($name, $callback, array_merge($query, ['container' => $container]));
}
}
2 changes: 1 addition & 1 deletion src/Traits/Cluster/RunsClusterOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getClient()
* @param string $path
* @param string $payload
* @param array $query
* @return void
* @return mixed
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesAPIException
*/
protected function makeRequest(string $method, string $path, string $payload = '', array $query = ['pretty' => 1])
Expand Down
4 changes: 2 additions & 2 deletions tests/PodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function runWatchTests()

public function runWatchLogsTests()
{
$this->cluster->pod()->watchLogsByName('mysql', function ($data) {
$this->cluster->pod()->watchContainerLogsByName('mysql', 'mysql', function ($data) {
// Debugging data to CI. :D
dump($data);

Expand All @@ -255,7 +255,7 @@ public function runWatchLogsTests()

public function runGetLogsTests()
{
$logs = $this->cluster->pod()->getLogsByName('mysql');
$logs = $this->cluster->pod()->containerLogsByName('mysql', 'mysql');

// Debugging data to CI. :D
dump($logs);
Expand Down

0 comments on commit 146c36f

Please sign in to comment.