Skip to content
pod.go 1.54 KiB
Newer Older
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
package pod

import (
	"github.com/ankitrgadiya/operatorlib/pkg/container"
	"github.com/ankitrgadiya/operatorlib/pkg/meta"
	"github.com/pkg/errors"
	corev1 "k8s.io/api/core/v1"
)

// GenerateConfigMap generates ConfigMap object as per the `Conf` struct passed
func Generate(c Mutate) (pod corev1.Pod, err error) {
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
	//	var con corev1.Container
	var containers []corev1.Container
	//	var mutateContainers []container.Mutate
	var volumes []corev1.Volume
	var securityContext *corev1.PodSecurityContext
	var restartPolicy corev1.RestartPolicy

Timothee Gosselin's avatar
Timothee Gosselin committed
	om, err := meta.MutateObjectMeta(c)
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed

	if err != nil {
		return pod, errors.Wrap(err, "failed to generate objectmeta")
	}

	mutateContainers := c.Containers()
	for _, con := range mutateContainers {
		cont, err := container.Generate(con)
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
		if err != nil {
			return pod, errors.Wrap(err, "failed to generate pod template spec")
		}
Timothee Gosselin's avatar
Timothee Gosselin committed
		containers = append(containers, cont)
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
	}

	volumes, err = c.Volumes()
	if err != nil {
		return pod, errors.Wrap(err, "failed to generate volumes for pod template spec")
	}
	securityContext, err = c.SecurityContext()
	if err != nil {
		return pod, errors.Wrap(err, "failed to generate securityContext pod template spec")
	}

	restartPolicy, err = c.RestartPolicy()
	if err != nil {
		return pod, errors.Wrap(err, "failed to generate securityContext pod template spec")
	}

	// pod = c.GetObject()
	pod.ObjectMeta = *om

	pod.Spec = corev1.PodSpec{
		Containers:      containers,
		Volumes:         volumes,
		SecurityContext: securityContext,
		RestartPolicy:   restartPolicy,
	}

	return pod, err
}