PHP K8s
Github Repo
3.x
3.x
  • ๐ŸšขIntroduction
  • ๐ŸŽ‰Support
  • โซUpgrading to 3.x
  • Getting Started
    • ๐Ÿš€Installation
    • ๐Ÿ™ŒShowcase
    • ๐Ÿ”’Authentication
    • โ™ป๏ธActive Development
      • ๐Ÿ“—Default Versions
      • ๐Ÿง™Supported Kubernetes versions
      • ๐Ÿ—ณ๏ธPackage versioning
  • Cluster Interaction
    • ๐ŸงญGetting Started
    • ๐ŸŽญCRUD Operations
    • ๐Ÿ“ฆImport from YAML
    • ๐Ÿ‘€Watching Resources
  • Resources
    • ๐Ÿ‘“Base Resource
      • Attributes Methods
      • Metadata Methods
      • Custom Callers
    • ๐Ÿง‘Namespace
    • ๐Ÿ–ฅ๏ธNode
    • ๐Ÿ“กEvent
    • ๐Ÿ“ฆWorkloads
      • Pod
      • Deployment
      • StatefulSet
      • DaemonSet
      • Job
      • CronJob
    • ๐ŸงตConfigurations
      • ConfigMap
      • Secret
    • ๐Ÿ“€Storage
      • StorageClass
      • PersistentVolume
      • PersistentVolumeClaim
    • ๐Ÿ“ถNetworking
      • Service
      • Ingress
    • โ†”๏ธScaling & Availability
      • HorizontalPodAutoscaler
      • PodDisruptionBudget
    • ๐Ÿ”‘RBAC
      • ClusterRole
      • ClusterRoleBinding
      • Role
      • RoleBinding
      • ServiceAccount
  • Instances
    • Affinity
    • Container
    • Container Probes
    • Expressions
    • Resource Metrics
    • RBAC Rules
    • Volumes
  • Advanced
    • ๐ŸฐMacros
    • โœจCreate classes for CRDs
      • ๐ŸŽ‡Getting started
      • ๐ŸฐMacros
      • ๐Ÿ‘€Watchable Resources
      • โ†”๏ธScalable Resources
      • ๐Ÿ’ŠPodable Resources
      • ๐Ÿ“„Loggable Resources
      • ๐Ÿš’Helper Traits
  • Frameworks
    • Laravel
    • PHP Helm
Powered by GitBook
On this page
  • Example
  • Job Template Retrieval
  • CronJob Status
  • Active Jobs

Was this helpful?

  1. Resources
  2. Workloads

CronJob

PreviousJobNextConfigurations

Last updated 3 years ago

Was this helpful?

Default version: batch/v1beta1

Example

use RenokiCo\PhpK8s\K8s;

$container = K8s::container()
    ->setName('pi')
    ->setImage('perl')
    ->setCommand(['perl',  '-Mbignum=bpi', '-wle', 'print bpi(2000)']);

$pod = K8s::pod()
    ->setName('pi')
    ->setLabels(['job-name' => 'pi']) // needs job-name: pi so that ->getPods() can work
    ->setContainers([$container])
    ->restartOnFailure();

$job = K8s::job()
    ->setName('pi')
    ->setSelectors(['matchLabels' => ['tier' => 'backend']])
    ->setTemplate($pod);

$cronjob = $this->cluster
    ->cronjob()
    ->setName('pi')
    ->setLabels(['tier' => 'backend'])
    ->setAnnotations(['perl/annotation' => 'yes'])
    ->setJobTemplate($job)
    ->setSchedule(CronExpression::factory('@hourly'))
    ->create();

Job Template Retrieval

CronJobs rely on jobs, so you can get the pod template as K8sJob class:

$template = $cronjob->getJobTemplate();

$jobName = $template->getName();

To retrieve the pod template as an array, pass false to the retrieval method:

$pod = $cronjob->getJobTemplate(false);

$jobName = $template['name'];

CronJob Status

The Status API is available to be accessed for fresh instances:

$cronjob->getStatus();

$lastSchedule = $cronjob->getLastSchedule(); // Carbon\Carbon instance with the last schedule time.

if ($lastSchedule->before(now())) {
    echo 'This job already ran...';
}

Active Jobs

while (! $job = $cronjob->getActiveJobs()->first()) {
    $cronjob->refresh();
    sleep(1);
}

// You can get the scheduled Job's pods.
$job->getPods();

You can access the active jobs for any cronjob. The active jobs return an \Illuminate\Support\Collection instance, on which you can chain various methods as described here:

The $job variable is a K8sJob instance class that is already synced with the existing job. Check for the K8sJob instance.

๐Ÿ“ฆ
Official Documentation
PHP K8s Pod Kind
PHP K8s Job Kind
https://laravel.com/docs/master/collections
Job documentation