Skip to content

Commit 0530db7

Browse files
authored
Merge pull request #52 from soketi/feature/custom-kubernetes-label
[4.x] Custom Kubernetes label from CLI
2 parents dd6b483 + 6739506 commit 0530db7

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Soketi Network Watcher
2-
=======================
1+
Network Watcher
2+
===============
33

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

13-
Monitor the [pWS server](https://github.com/soketi/pws) container for memory allowance and new connections when running in Kubernetes.
13+
Monitor Kubernetes containers for memory allowance and redirect new HTTP/WebSocket connections to pods that have enough memory to sustain them.
14+
15+
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).
16+
17+
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).
1418

1519
## 🤝 Supporting
1620

app/Commands/WatchNetworkCommand.php

+15-12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class WatchNetworkCommand extends Command implements SignalableCommandInterface
2525
{--server-port=6001 : The Server port.}
2626
{--memory-percent=75 : The threshold at which new connections close for a specific server.}
2727
{--interval=1 : The interval in seconds between each checks.}
28+
{--kubernetes-label=pws.soketi.app/accepts-new-connections : The label to attach to the Kubernetes services.}
2829
{--test : Run only one loop for testing.}
2930
';
3031

@@ -50,8 +51,6 @@ class WatchNetworkCommand extends Command implements SignalableCommandInterface
5051
public function __construct()
5152
{
5253
parent::__construct();
53-
54-
$this->registerPodMacros();
5554
}
5655

5756
/**
@@ -91,6 +90,8 @@ public function handle()
9190
{
9291
$this->line('Starting the watcher...');
9392

93+
$this->registerPodMacros();
94+
9495
$podNamespace = env('POD_NAMESPACE') ?: $this->option('pod-namespace');
9596
$podName = env('POD_NAME') ?: $this->option('pod-name');
9697
$serverPort = env('SERVER_PORT') ?: $this->option('server-port');
@@ -141,46 +142,48 @@ public function schedule(Schedule $schedule): void
141142
*/
142143
protected function registerPodMacros(): void
143144
{
145+
$kubernetesLabel = env('KUBERNETES_LABEL') ?: $this->option('kubernetes-label');
146+
144147
K8sPod::macro('getLabel', function (string $name, $default = null) {
145148
/** @var K8sPod $this */
146149
return $this->getLabels()[$name] ?? $default;
147150
});
148151

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

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

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

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

167170
return true;
168171
});
169172

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

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

178181
return true;
179182
});
180183

181-
K8sPod::macro('ensureItHasDefaultLabel', function () {
184+
K8sPod::macro('ensureItHasDefaultLabel', function () use ($kubernetesLabel) {
182185
/** @var K8sPod $this */
183-
if (! $this->getLabel('pws.soketi.app/accepts-new-connections')) {
186+
if (! $this->getLabel($kubernetesLabel)) {
184187
$this->acceptNewConnections();
185188
}
186189
});

0 commit comments

Comments
 (0)