job

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2021 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package job provides simple functions to run a job on kubernetes.

Usage:

import "github.com/showwin/kube-job/pkg/job"

Run a job overriding the commands

When you want to run a job on kubernetes, please use this package as follows.

At first, you have to prepare yaml for job, and provide a command to override the yaml.

For example:

j, err := job.NewJob("$HOME/.kube/config", "job-template.yaml", "echo hoge", "target-container-name", 0 * time.Second)
if err != nil {
    return err
}

// Run the job
running, err := j.RunJob()
if err != nil {
    return err
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

err = j.WaitJob(ctx, running)

Polling the logs

You can polling the logs with stream.

For example:

// j is a Job struct
watcher := NewWatcher(j.client, j.Container)

// running is a batchv1.Job struct
err := watcher.Watch(running, ctx)
if err != nil {
    return err
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CleanupType

type CleanupType int

CleanupType for enum.

const (
	// All is a clean up type. Remove the job and pods whether the job is succeeded or failed.
	All CleanupType = iota
	// Succeeded is a clean up type. Remove the job and pods when the job is succeeded.
	Succeeded
	// Failed is a cleanup type. Remove the job and pods when the job is failed.
	Failed
)

func (CleanupType) String

func (c CleanupType) String() string

type Job

type Job struct {

	// Batch v1 job struct.
	CurrentJob *v1.Job
	// Command which override the current job struct.
	Args []string
	// Target container name.
	Container string
	// If you set 0, timeout is ignored.
	Timeout time.Duration
	// contains filtered or unexported fields
}

Job has client of kubernetes, current job, command, timeout, and target container information.

func NewJob

func NewJob(configFile, currentFile, command, container string, timeout time.Duration) (*Job, error)

NewJob returns a new Job struct, and initialize kubernetes client. It read the job definition yaml file, and unmarshal to batch/v1/Job.

func (*Job) Cleanup

func (j *Job) Cleanup() error

Cleanup removes the job from the kubernetes cluster.

func (*Job) FindPods

func (j *Job) FindPods(ctx context.Context, job *v1.Job) ([]corev1.Pod, error)

FindPods finds pod in the job.

func (*Job) Run

func (j *Job) Run(ignoreSidecar bool) error

Run a command on kubernetes cluster, and watch the log.

func (*Job) RunAndCleanup

func (j *Job) RunAndCleanup(cleanupType string, ignoreSidecar bool) error

RunAndCleanup executes a command and clean up the job and pods.

func (*Job) RunJob

func (j *Job) RunJob() (*v1.Job, error)

RunJob is run a kubernetes job, and returns the job information.

func (*Job) Validate

func (j *Job) Validate() error

Validate checks job templates before run the job.

func (*Job) WaitJob

func (j *Job) WaitJob(ctx context.Context, job *v1.Job, ignoreSidecar bool) error

WaitJob waits response of the job.

func (*Job) WaitJobComplete

func (j *Job) WaitJobComplete(ctx context.Context, job *v1.Job, ignoreSidecar bool) error

WaitJobComplete waits the completion of the job. If the job is failed, this function returns error. If the job is succeeded, this function returns nil.

type Watcher

type Watcher struct {

	// Target container name.
	Container string
	// contains filtered or unexported fields
}

Watcher has client of kubernetes and target container information.

func NewWatcher

func NewWatcher(client kubernetes.Interface, container string) *Watcher

NewWatcher returns a new Watcher struct.

func (*Watcher) FindPods

func (w *Watcher) FindPods(ctx context.Context, job *v1.Job) ([]corev1.Pod, error)

FindPods finds pods in the job.

func (*Watcher) WaitToStartPod

func (w *Watcher) WaitToStartPod(ctx context.Context, pod corev1.Pod) (corev1.Pod, error)

WaitToStartPod wait until starting the pod. Because the job does not start immediately after call kubernetes API. So we have to wait to start the pod, before watch logs.

func (*Watcher) Watch

func (w *Watcher) Watch(job *v1.Job, ctx context.Context) error

Watch gets pods and tail the logs. We must create endless loop because sometimes jobs are configured restartPolicy. When restartPolicy is Never, the Job create a new Pod if the specified command is failed. So we must trace all Pods even though the Pod is failed. And it isn't necessary to stop the loop because the Job is watched in WaitJobComplete.

func (*Watcher) WatchPods

func (w *Watcher) WatchPods(ctx context.Context, pods []corev1.Pod) error

WatchPods gets wait to start pod and tail the logs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL