Skip to content
job.go 4.55 KiB
Newer Older
Timothee Gosselin's avatar
Timothee Gosselin committed
/*

Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.gnu.org/licenses/agpl-3.0.html

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package job

import (
Timothee Gosselin's avatar
Timothee Gosselin committed
	batchv1 "k8s.io/api/batch/v1"
Timothee Gosselin's avatar
Timothee Gosselin committed
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	"k8s.libre.sh/controller-utils/interfaces"
	"k8s.libre.sh/controller-utils/meta"
	"k8s.libre.sh/controller-utils/objects/pod"
Timothee Gosselin's avatar
Timothee Gosselin committed
)

Timothee Gosselin's avatar
Timothee Gosselin committed
// +kubebuilder:object:generate=true
Timothee Gosselin's avatar
Timothee Gosselin committed
type Job struct {
	// Standard object metadata.
	// +optional
	*meta.ObjectMeta `json:"meta,omitempty"`
	// Specification of the desired behavior of the Job.
	// +optional
	*JobSpec `json:",inline"`
}

// +kubebuilder:object:generate=true
type JobSpec struct {
	// Standard object metadata.
	// +optional
	*meta.ObjectMeta `json:"meta,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed
	// Specifies the maximum desired number of pods the job should
	// run at any given time. The actual number of pods running in steady state will
	// be less than this number when ((.spec.completions - .status.successful) < .spec.parallelism),
	// i.e. when the work left to do is less than max parallelism.
	// +optional
	Parallelism *int32 `json:"parallelism,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed
	// Specifies the desired number of successfully finished pods the
	// job should be run with.  Setting to nil means that the success of any
	// pod signals the success of all pods, and allows parallelism to have any positive
	// value.  Setting to 1 means that parallelism is limited to 1 and the success of that
	// pod signals the success of the job.
	// +optional
	Completions *int32 `json:"completions,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed

	// Optional duration in seconds relative to the startTime that the job may be active
	// before the system tries to terminate it; value must be positive integer
	// +optional
	ActiveDeadlineSeconds *int64 `json:"activeDeadlineSeconds,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed

	// Optional number of retries before marking this job failed.
	// Defaults to 6
	// +optional
	BackoffLimit *int32 `json:"backoffLimit,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed

	// ttlSecondsAfterFinished limits the lifetime of a Job that has finished
	// execution (either Complete or Failed). If this field is set,
	// ttlSecondsAfterFinished after the Job finishes, it is eligible to be
	// automatically deleted. When the Job is being deleted, its lifecycle
	// guarantees (e.g. finalizers) will be honored. If this field is unset,
	// the Job won't be automatically deleted. If this field is set to zero,
	// the Job becomes eligible to be deleted immediately after it finishes.
	// This field is alpha-level and is only honored by servers that enable the
	// TTLAfterFinished feature.
	// +optional
	TTLSecondsAfterFinished *int32 `json:"ttlSecondsAfterFinished,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed
	// PodSpec describes the pods that will be created.
	*pod.Pod `json:",inline"`
Timothee Gosselin's avatar
Timothee Gosselin committed
}

func (spec *Job) GetParallelism() *int32 {
	return spec.Parallelism
}

func (spec *Job) GetCompletions() *int32 {
	return spec.Completions
}

func (spec *Job) GetActiveDeadlineSeconds() *int64 {
	return spec.ActiveDeadlineSeconds
}

func (spec *Job) GetBackoffLimit() *int32 {
	return spec.BackoffLimit
}

func (spec *Job) GetTTLSecondsAfterFinished() *int32 {
	return spec.TTLSecondsAfterFinished
}

func (o *Job) GetObject() interfaces.Object {
	obj := &batchv1.Job{
Timothee Gosselin's avatar
Timothee Gosselin committed
		TypeMeta: metav1.TypeMeta{
			Kind:       "Job",
			APIVersion: "batch/v1",
		},
	}

	obj.SetName(o.GetName())
	obj.SetNamespace(o.GetNamespace())

	return obj
Timothee Gosselin's avatar
Timothee Gosselin committed
}

func (o *Job) Mutate(obj interfaces.Object) error {

	job, ok := obj.(*batchv1.Job)
	if !ok {
		return errors.New("object is not a Job")
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

	// TODO TO FIX merge meta
	o.Pod.ObjectMeta = o.ObjectMeta
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	err := MutateJob(o, &job.Spec)
Timothee Gosselin's avatar
Timothee Gosselin committed
	if err != nil {
		return err
	}

	err = meta.MutateMeta(o.ObjectMeta, obj)
	if err != nil {
		return err
	}

	job.Spec.Template.ObjectMeta.Name = job.ObjectMeta.Name
	job.Spec.Template.ObjectMeta.Labels = o.ObjectMeta.Labels

	return nil
}
Timothee Gosselin's avatar
Timothee Gosselin committed

func (o *Job) Init() {
	if o.ObjectMeta == nil {
		o.ObjectMeta = &meta.ObjectMeta{}
	}

	if o.ObjectMeta.Labels == nil {
		o.ObjectMeta.Labels = make(map[string]string)
	}

	if o.JobSpec == nil {
		o.JobSpec = &JobSpec{}
	}

	if o.Pod == nil {
		o.Pod = &pod.Pod{}
	}

	o.Pod.Init()
Timothee Gosselin's avatar
Timothee Gosselin committed

func (o *Job) GetObjectKind() string  { return "Job" }
func (o *Job) GetObjectGroup() string { return "batch" }