PHP K8s
Github Repo
master
master
  • ๐Ÿšข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
  • Pod Template Retrieval
  • Getting Pods
  • Custom Pod Labels
  • Job's Restart Policy
  • Job Status

Was this helpful?

Edit on GitHub
  1. Resources
  2. Workloads

Job

PreviousDaemonSetNextCronJob

Last updated 3 years ago

Was this helpful?

Default version: batch/v1

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 = $this->cluster
    ->job()
    ->setName('pi')
    ->setSelectors(['matchLabels' => ['tier' => 'backend']])
    ->setTemplate($pod)
    ->create();

Pod Template Retrieval

Jobs rely on pods, so you can get the pod template as K8sPod class:

$template = $job->getTemplate();

$podName = $template->getName();

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

$pod = $job->getTemplate(false);

$podName = $template['name'];

Getting Pods

To get the pods, the Pod template must have the job-name label set. This way, the labelSelector API parameter is issued and you may retrieve the associated pods:

metadata:
  name: [here it goes the job name]
spec:
  template:
    metadata:
      labels:
        job-name: [here it goes the job name]

You can retrieve the pods as resources controlled by the Job by issuing ->getPods():

foreach ($job->getPods() as $pod) {
    // $pod->logs()
}

Custom Pod Labels

If you cannot declare the job-name label or simply want to use something else, you may call selectPods from the resource:

use RenokiCo\PhpK8s\Kinds\K8sJob;

K8sJob::selectPods(function (K8sJob $job) {
    // $job is the current Job

    return [
        'some-label' => 'some-label-value',
        'some-other-label' => "{$job->getName()}-custom-name",
    ];
});

Job's Restart Policy

You might want to use OnFailure or Never as restart policies. These can be applied to the pod before passing it to the job creation chain:

$pod = K8s::pod()
    ->setName('pi')
    ->setLabels(['tier' => 'backend'])
    ->setContainers([$container])
    ->restartOnFailure(); // restartPolicy: OnFailure

$job->setTemplate($pod);
$pod = K8s::pod()
    ->setName('pi')
    ->setLabels(['tier' => 'backend'])
    ->setContainers([$container])
    ->neverRestart(); // restartPolicy: Never

$job->setTemplate($pod);

Job Status

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

$job->refresh();

$job->getActivePodsCount();
$job->getFailedPodsCount();
$job->getSuccededPodsCount();

You can check if the job completed:

if ($job->hasCompleted()) {
    //
}

You can retrieve the null/\DateTime instance for start and end times for the job:

$start = $job->getStartTime();
$end = $job->getCompletionTime();

You can also retrieve the amount of time the job ran for:

$seconds = $job->getDurationInSeconds();
๐Ÿ“ฆ
Official Documentation
PHP K8s Pod Kind