Skip to content

Commit 146c36f

Browse files
authored
Merge pull request #34 from renoki-co/feature/watch-containers
[feature] Watch containers
2 parents 91e9859 + 032517b commit 146c36f

File tree

4 files changed

+80
-12
lines changed

4 files changed

+80
-12
lines changed

docs/kinds/Pod.md

+7
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ Retrieve a single string with all logs until the point of call:
122122
// Returns a long string with the logs
123123

124124
$logs = $pod->logs();
125+
126+
$mysqlLogs = $pod->containerLogs('mysql');
125127
```
126128

127129
Open up a websocket connection and watch for changes, line-by-line:
@@ -134,6 +136,11 @@ $pod->watchLogs(function ($line) {
134136
// Process the logic here
135137
// with the given line.
136138
});
139+
140+
$pod->watchContainerLogs('mysql', function ($line) {
141+
// Process the logic here
142+
// with the given line for the mysql container.
143+
})
137144
```
138145

139146
### Pod Status

src/Kinds/K8sResource.php

+70-9
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public function whereNamespace($namespace)
249249
/**
250250
* Get the namespace for the resource.
251251
*
252-
* @return void
252+
* @return string
253253
*/
254254
public function getNamespace()
255255
{
@@ -567,7 +567,7 @@ public function refreshOriginal(array $query = ['pretty' => 1])
567567
*
568568
* @param Closure $callback
569569
* @param array $query
570-
* @return void
570+
* @return mixed
571571
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
572572
*/
573573
public function watchAll(Closure $callback, array $query = ['pretty' => 1])
@@ -593,7 +593,7 @@ public function watchAll(Closure $callback, array $query = ['pretty' => 1])
593593
*
594594
* @param Closure $callback
595595
* @param array $query
596-
* @return void
596+
* @return mixed
597597
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
598598
*/
599599
public function watch(Closure $callback, array $query = ['pretty' => 1])
@@ -619,7 +619,8 @@ public function watch(Closure $callback, array $query = ['pretty' => 1])
619619
*
620620
* @param Closure $callback
621621
* @param array $query
622-
* @return void
622+
* @return mixed
623+
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
623624
*/
624625
public function watchByName(string $name, Closure $callback, array $query = ['pretty' => 1])
625626
{
@@ -630,7 +631,7 @@ public function watchByName(string $name, Closure $callback, array $query = ['pr
630631
* Get a specific resource's logs.
631632
*
632633
* @param array $query
633-
* @return \RenokiCo\PhpK8s\Kinds\K8sResource
634+
* @return string
634635
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
635636
*/
636637
public function logs(array $query = ['pretty' => 1])
@@ -651,24 +652,54 @@ public function logs(array $query = ['pretty' => 1])
651652
);
652653
}
653654

655+
/**
656+
* Get logs for a specific container.
657+
*
658+
* @param string $container
659+
* @param array $query
660+
* @return string
661+
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
662+
*/
663+
public function containerLogs(string $container, array $query = ['pretty' => 1])
664+
{
665+
return $this->logs(array_merge($query, ['container' => $container]));
666+
}
667+
654668
/**
655669
* Watch the specific resource by name.
656670
*
671+
* @param string $name
657672
* @param Closure $callback
658673
* @param array $query
659-
* @return void
674+
* @return string
675+
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
660676
*/
661-
public function logsByName(array $query = ['pretty' => 1])
677+
public function logsByName(string $name, array $query = ['pretty' => 1])
662678
{
663679
return $this->whereName($name)->logs($query);
664680
}
665681

682+
/**
683+
* Watch the specific resource by name.
684+
*
685+
* @param string $name
686+
* @param string $container
687+
* @param Closure $callback
688+
* @param array $query
689+
* @return string
690+
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
691+
*/
692+
public function containerLogsByName(string $name, string $container, array $query = ['pretty' => 1])
693+
{
694+
return $this->logsByName($name, array_merge($query, ['container' => $container]));
695+
}
696+
666697
/**
667698
* Watch the specific resource's logs until the closure returns true or false.
668699
*
669700
* @param Closure $callback
670701
* @param array $query
671-
* @return void
702+
* @return mixed
672703
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
673704
*/
674705
public function watchLogs(Closure $callback, array $query = ['pretty' => 1])
@@ -692,15 +723,45 @@ public function watchLogs(Closure $callback, array $query = ['pretty' => 1])
692723
);
693724
}
694725

726+
/**
727+
* Watch the specific resource's container logs until the closure returns true or false.
728+
*
729+
* @param string $container
730+
* @param Closure $callback
731+
* @param array $query
732+
* @return mixed
733+
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
734+
*/
735+
public function watchContainerLogs(string $container, Closure $callback, array $query = ['pretty' => 1])
736+
{
737+
return $this->watchLogs($callback, array_merge($query, ['container' => $container]));
738+
}
739+
695740
/**
696741
* Watch the specific resource's logs by name.
697742
*
698743
* @param Closure $callback
699744
* @param array $query
700-
* @return void
745+
* @return mixed
746+
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
701747
*/
702748
public function watchLogsByName(string $name, Closure $callback, array $query = ['pretty' => 1])
703749
{
704750
return $this->whereName($name)->watchLogs($callback, $query);
705751
}
752+
753+
/**
754+
* Watch the specific resource's container logs by names.
755+
*
756+
* @param string $name
757+
* @param string $container
758+
* @param Closure $callback
759+
* @param array $query
760+
* @return mixed
761+
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesWatchException
762+
*/
763+
public function watchContainerLogsByName(string $name, string $container, Closure $callback, array $query = ['pretty' => 1])
764+
{
765+
return $this->watchLogsByName($name, $callback, array_merge($query, ['container' => $container]));
766+
}
706767
}

src/Traits/Cluster/RunsClusterOperations.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function getClient()
9494
* @param string $path
9595
* @param string $payload
9696
* @param array $query
97-
* @return void
97+
* @return mixed
9898
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesAPIException
9999
*/
100100
protected function makeRequest(string $method, string $path, string $payload = '', array $query = ['pretty' => 1])

tests/PodTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public function runWatchTests()
243243

244244
public function runWatchLogsTests()
245245
{
246-
$this->cluster->pod()->watchLogsByName('mysql', function ($data) {
246+
$this->cluster->pod()->watchContainerLogsByName('mysql', 'mysql', function ($data) {
247247
// Debugging data to CI. :D
248248
dump($data);
249249

@@ -255,7 +255,7 @@ public function runWatchLogsTests()
255255

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

260260
// Debugging data to CI. :D
261261
dump($logs);

0 commit comments

Comments
 (0)