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 ¶
- type CleanupType
- type Job
- func (j *Job) Cleanup() error
- func (j *Job) FindPods(ctx context.Context, job *v1.Job) ([]corev1.Pod, error)
- func (j *Job) Run(ignoreSidecar bool) error
- func (j *Job) RunAndCleanup(cleanupType string, ignoreSidecar bool) error
- func (j *Job) RunJob() (*v1.Job, error)
- func (j *Job) Validate() error
- func (j *Job) WaitJob(ctx context.Context, job *v1.Job, ignoreSidecar bool) error
- func (j *Job) WaitJobComplete(ctx context.Context, job *v1.Job, ignoreSidecar bool) error
- type Watcher
- func (w *Watcher) FindPods(ctx context.Context, job *v1.Job) ([]corev1.Pod, error)
- func (w *Watcher) WaitToStartPod(ctx context.Context, pod corev1.Pod) (corev1.Pod, error)
- func (w *Watcher) Watch(job *v1.Job, ctx context.Context) error
- func (w *Watcher) WatchPods(ctx context.Context, pods []corev1.Pod) error
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) RunAndCleanup ¶
RunAndCleanup executes a command and clean up the job and pods.
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) WaitToStartPod ¶
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 ¶
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.