Skip to content
job.go 1.47 KiB
Newer Older
Timothee Gosselin's avatar
Timothee Gosselin committed
package job

import (
	"github.com/ankitrgadiya/operatorlib/pkg/meta"
	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

	pod "github.com/ankitrgadiya/operatorlib/pkg/pod"

Timothee Gosselin's avatar
Timothee Gosselin committed
	"github.com/imdario/mergo"
Timothee Gosselin's avatar
Timothee Gosselin committed
	"github.com/pkg/errors"
	batchv1 "k8s.io/api/batch/v1"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Generate(m Mutate) (out batchv1.Job, err error) {
Timothee Gosselin's avatar
Timothee Gosselin committed
	om, err := meta.MutateObjectMeta(m)
	if err != nil {
		return out, errors.Wrap(err, "failed to generate objectmeta")
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

	p, err := pod.Generate(m)
Timothee Gosselin's avatar
Timothee Gosselin committed
	if err != nil {
		return out, errors.Wrap(err, "failed to generate pod template spec")
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	manualSelector, err := m.GenManualSelector()
	if err != nil {
		return out, errors.Wrap(err, "failed to generate volumes for pod template spec")
	}
Timothee Gosselin's avatar
Timothee Gosselin committed
	out.ObjectMeta = *om

	out.Spec = batchv1.JobSpec{
		//	Replicas:           c.Size,
		//			Parallelism: c.Strategy,
		//			Completions: metav1.SetAsLabelSelector(om.Labels),
		//			ActiveDeadlineSeconds:
		//			BackoffLimit:
		//			TTLSecondsAfterFinished:
		// Selector: metav1.SetAsLabelSelector(om.Labels),
		Template: corev1.PodTemplateSpec{
			ObjectMeta: *om,
			Spec:       p.Spec,
		},
		ManualSelector: manualSelector,
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	return out, err
}
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
func (c Conf) Mutate() controllerutil.MutateFn {
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	return func() error {
		out, err := Generate(c)
Timothee Gosselin's avatar
Timothee Gosselin committed
		if err != nil {
Timothee Gosselin's avatar
Timothee Gosselin committed
			return err
Timothee Gosselin's avatar
Timothee Gosselin committed
		}

Timothee Gosselin's avatar
Timothee Gosselin committed
		out.Spec.Selector = metav1.SetAsLabelSelector(out.ObjectMeta.Labels)
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
		mergo.Merge(c.Obj, out)

		return nil
Timothee Gosselin's avatar
Timothee Gosselin committed
	}
}