Skip to content

Commit b23f408

Browse files
committed
Fix: K8sCronJob needed to find active Job by name and also have running job
Previous change to simpler Pi calculation exposed timing issue with test. Rather than make VM/CPU work harder or create potential for overflow again, sleep. Then, fixed lookup logic.
1 parent 35fcf56 commit b23f408

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

src/Kinds/K8sCronJob.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function getLastSchedule()
123123
public function getActiveJobs()
124124
{
125125
return collect($this->getStatus('active', []))->map(function ($job) {
126-
return $this->cluster->job($job)->refresh();
126+
return $this->cluster->getJobByName($job['name'], $this->getNamespace());
127127
});
128128
}
129129
}

tests/CronJobTest.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use RenokiCo\PhpK8s\Kinds\K8sJob;
1111
use RenokiCo\PhpK8s\ResourcesList;
1212

13-
class CronCronJobTest extends TestCase
13+
class CronJobTest extends TestCase
1414
{
1515
public function test_cronjob_build()
1616
{
@@ -87,28 +87,28 @@ public function test_cronjob_api_interaction()
8787

8888
public function runCreationTests()
8989
{
90-
$pi = K8s::container()
91-
->setName('pi')
92-
->setImage('public.ecr.aws/docker/library/perl', '5.36.0')
93-
->setCommand(['perl', '-Mbignum=bpi', '-wle', 'print bpi(200)']);
90+
$busybox = K8s::container()
91+
->setName('busybox-exec')
92+
->setImage('public.ecr.aws/docker/library/busybox')
93+
->setCommand(['/bin/sh', '-c', 'sleep 30']);
9494

9595
$pod = $this->cluster->pod()
96-
->setName('perl')
97-
->setContainers([$pi])
96+
->setName('sleep')
97+
->setContainers([$busybox])
9898
->restartOnFailure()
9999
->neverRestart();
100100

101101
$job = $this->cluster->job()
102-
->setName('pi')
103-
->setLabels(['tier' => 'backend'])
104-
->setAnnotations(['perl/annotation' => 'yes'])
102+
->setName('sleeper')
103+
->setLabels(['tier' => 'useless'])
104+
->setAnnotations(['perl/annotation' => 'no'])
105105
->setTTL(3600)
106106
->setTemplate($pod);
107107

108108
$cronjob = $this->cluster->cronjob()
109-
->setName('pi')
110-
->setLabels(['tier' => 'backend'])
111-
->setAnnotations(['perl/annotation' => 'yes'])
109+
->setName('periodic-sleep')
110+
->setLabels(['tier' => 'useless'])
111+
->setAnnotations(['perl/annotation' => 'no'])
112112
->setJobTemplate($job)
113113
->setSchedule(CronExpression::factory('* * * * *'));
114114

@@ -123,9 +123,9 @@ public function runCreationTests()
123123
$this->assertInstanceOf(K8sCronJob::class, $cronjob);
124124

125125
$this->assertEquals('batch/v1', $cronjob->getApiVersion());
126-
$this->assertEquals('pi', $cronjob->getName());
127-
$this->assertEquals(['tier' => 'backend'], $cronjob->getLabels());
128-
$this->assertEquals(['perl/annotation' => 'yes'], $cronjob->getAnnotations());
126+
$this->assertEquals('periodic-sleep', $cronjob->getName());
127+
$this->assertEquals(['tier' => 'useless'], $cronjob->getLabels());
128+
$this->assertEquals(['perl/annotation' => 'no'], $cronjob->getAnnotations());
129129
$this->assertEquals('Never', $pod->getRestartPolicy());
130130

131131
$this->assertInstanceOf(K8sJob::class, $cronjob->getJobTemplate());
@@ -135,6 +135,7 @@ public function runCreationTests()
135135

136136
$activeJobs = $cronjob->getActiveJobs();
137137

138+
// This check is sensitive to ensuring the jobs take some time to complete.
138139
while ($cronjob->getActiveJobs()->count() === 0) {
139140
dump("Waiting for the cronjob {$cronjob->getName()} to have active jobs...");
140141
sleep(1);
@@ -169,23 +170,23 @@ public function runGetAllTests()
169170

170171
public function runGetTests()
171172
{
172-
$cronjob = $this->cluster->getCronJobByName('pi');
173+
$cronjob = $this->cluster->getCronJobByName('periodic-sleep');
173174

174175
$this->assertInstanceOf(K8sCronJob::class, $cronjob);
175176

176177
$this->assertTrue($cronjob->isSynced());
177178

178179
$this->assertEquals('batch/v1', $cronjob->getApiVersion());
179-
$this->assertEquals('pi', $cronjob->getName());
180-
$this->assertEquals(['tier' => 'backend'], $cronjob->getLabels());
181-
$this->assertEquals(['perl/annotation' => 'yes'], $cronjob->getAnnotations());
180+
$this->assertEquals('periodic-sleep', $cronjob->getName());
181+
$this->assertEquals(['tier' => 'useless'], $cronjob->getLabels());
182+
$this->assertEquals(['perl/annotation' => 'no'], $cronjob->getAnnotations());
182183

183184
$this->assertInstanceOf(K8sJob::class, $cronjob->getJobTemplate());
184185
}
185186

186187
public function runUpdateTests()
187188
{
188-
$cronjob = $this->cluster->getCronJobByName('pi');
189+
$cronjob = $this->cluster->getCronJobByName('periodic-sleep');
189190

190191
$this->assertTrue($cronjob->isSynced());
191192

@@ -196,16 +197,16 @@ public function runUpdateTests()
196197
$this->assertTrue($cronjob->isSynced());
197198

198199
$this->assertEquals('batch/v1', $cronjob->getApiVersion());
199-
$this->assertEquals('pi', $cronjob->getName());
200-
$this->assertEquals(['tier' => 'backend'], $cronjob->getLabels());
200+
$this->assertEquals('periodic-sleep', $cronjob->getName());
201+
$this->assertEquals(['tier' => 'useless'], $cronjob->getLabels());
201202
$this->assertEquals([], $cronjob->getAnnotations());
202203

203204
$this->assertInstanceOf(K8sJob::class, $cronjob->getJobTemplate());
204205
}
205206

206207
public function runDeletionTests()
207208
{
208-
$cronjob = $this->cluster->getCronJobByName('pi');
209+
$cronjob = $this->cluster->getCronJobByName('periodic-sleep');
209210

210211
$this->assertTrue($cronjob->delete());
211212

@@ -216,13 +217,13 @@ public function runDeletionTests()
216217

217218
$this->expectException(KubernetesAPIException::class);
218219

219-
$this->cluster->getCronJobByName('pi');
220+
$this->cluster->getCronJobByName('periodic-sleep');
220221
}
221222

222223
public function runWatchAllTests()
223224
{
224225
$watch = $this->cluster->cronjob()->watchAll(function ($type, $cronjob) {
225-
if ($cronjob->getName() === 'pi') {
226+
if ($cronjob->getName() === 'periodic-sleep') {
226227
return true;
227228
}
228229
}, ['timeoutSeconds' => 10]);
@@ -232,8 +233,8 @@ public function runWatchAllTests()
232233

233234
public function runWatchTests()
234235
{
235-
$watch = $this->cluster->cronjob()->watchByName('pi', function ($type, $cronjob) {
236-
return $cronjob->getName() === 'pi';
236+
$watch = $this->cluster->cronjob()->watchByName('periodic-sleep', function ($type, $cronjob) {
237+
return $cronjob->getName() === 'periodic-sleep';
237238
}, ['timeoutSeconds' => 10]);
238239

239240
$this->assertTrue($watch);

0 commit comments

Comments
 (0)