Skip to content

Commit

Permalink
Merge pull request #52 from soketi/feature/custom-kubernetes-label
Browse files Browse the repository at this point in the history
[4.x] Custom Kubernetes label from CLI
  • Loading branch information
rennokki authored Sep 25, 2021
2 parents dd6b483 + 6739506 commit 0530db7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Soketi Network Watcher
=======================
Network Watcher
===============

![CI](https://github.com/soketi/network-watcher/workflows/CI/badge.svg?branch=master)
[![codecov](https://codecov.io/gh/soketi/network-watcher/branch/master/graph/badge.svg)](https://codecov.io/gh/soketi/network-watcher)
Expand All @@ -10,7 +10,11 @@ Soketi Network Watcher
![v1.21.4 K8s Version](https://img.shields.io/badge/K8s%20v1.21.4-Ready-%23326ce5?colorA=306CE8&colorB=green)
![v1.22.1 K8s Version](https://img.shields.io/badge/K8s%20v1.22.1-Ready-%23326ce5?colorA=306CE8&colorB=green)

Monitor the [pWS server](https://github.com/soketi/pws) container for memory allowance and new connections when running in Kubernetes.
Monitor Kubernetes containers for memory allowance and redirect new HTTP/WebSocket connections to pods that have enough memory to sustain them.

This can be generally used for any kind of server, but its main purpose was to redirect new WebSocket connections to pods that have enough memory to withstand them in [pWS server](https://github.com/soketi/pws).

Under the hood, it works by setting a pod label to either `yes`/`no` and you should make the Kubernetes Service to seek for pods by that label, with the value `yes`. You can find examples [in the documentation](https://rennokki.gitbook.io/soketi-pws/network-watcher/getting-started).

## 🤝 Supporting

Expand Down
27 changes: 15 additions & 12 deletions app/Commands/WatchNetworkCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class WatchNetworkCommand extends Command implements SignalableCommandInterface
{--server-port=6001 : The Server port.}
{--memory-percent=75 : The threshold at which new connections close for a specific server.}
{--interval=1 : The interval in seconds between each checks.}
{--kubernetes-label=pws.soketi.app/accepts-new-connections : The label to attach to the Kubernetes services.}
{--test : Run only one loop for testing.}
';

Expand All @@ -50,8 +51,6 @@ class WatchNetworkCommand extends Command implements SignalableCommandInterface
public function __construct()
{
parent::__construct();

$this->registerPodMacros();
}

/**
Expand Down Expand Up @@ -91,6 +90,8 @@ public function handle()
{
$this->line('Starting the watcher...');

$this->registerPodMacros();

$podNamespace = env('POD_NAMESPACE') ?: $this->option('pod-namespace');
$podName = env('POD_NAME') ?: $this->option('pod-name');
$serverPort = env('SERVER_PORT') ?: $this->option('server-port');
Expand Down Expand Up @@ -141,46 +142,48 @@ public function schedule(Schedule $schedule): void
*/
protected function registerPodMacros(): void
{
$kubernetesLabel = env('KUBERNETES_LABEL') ?: $this->option('kubernetes-label');

K8sPod::macro('getLabel', function (string $name, $default = null) {
/** @var K8sPod $this */
return $this->getLabels()[$name] ?? $default;
});

K8sPod::macro('acceptsConnections', function () {
K8sPod::macro('acceptsConnections', function () use ($kubernetesLabel) {
/** @var K8sPod $this */
return $this->getLabel('pws.soketi.app/accepts-new-connections', 'yes') === 'yes';
return $this->getLabel($kubernetesLabel, 'yes') === 'yes';
});

K8sPod::macro('rejectsConnections', function () {
K8sPod::macro('rejectsConnections', function () use ($kubernetesLabel) {
/** @var K8sPod $this */
return $this->getLabel('pws.soketi.app/accepts-new-connections', 'yes') === 'no';
return $this->getLabel($kubernetesLabel, 'yes') === 'no';
});

K8sPod::macro('acceptNewConnections', function () {
K8sPod::macro('acceptNewConnections', function () use ($kubernetesLabel) {
/** @var K8sPod $this */
$labels = array_merge($this->getLabels(), [
'pws.soketi.app/accepts-new-connections' => 'yes',
$kubernetesLabel => 'yes',
]);

$this->refresh()->setLabels($labels)->update();

return true;
});

K8sPod::macro('rejectNewConnections', function () {
K8sPod::macro('rejectNewConnections', function () use ($kubernetesLabel) {
/** @var K8sPod $this */
$labels = array_merge($this->getLabels(), [
'pws.soketi.app/accepts-new-connections' => 'no',
$kubernetesLabel => 'no',
]);

$this->refresh()->setLabels($labels)->update();

return true;
});

K8sPod::macro('ensureItHasDefaultLabel', function () {
K8sPod::macro('ensureItHasDefaultLabel', function () use ($kubernetesLabel) {
/** @var K8sPod $this */
if (! $this->getLabel('pws.soketi.app/accepts-new-connections')) {
if (! $this->getLabel($kubernetesLabel)) {
$this->acceptNewConnections();
}
});
Expand Down

0 comments on commit 0530db7

Please sign in to comment.