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

import (
	"fmt"

	"github.com/ankitrgadiya/operatorlib/pkg/container"
	"github.com/ankitrgadiya/operatorlib/pkg/interfaces"
	"github.com/ankitrgadiya/operatorlib/pkg/meta"
	"github.com/pkg/errors"
	corev1 "k8s.io/api/core/v1"
Timothee Gosselin's avatar
Timothee Gosselin committed
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/client-go/tools/record"
	"sigs.k8s.io/controller-runtime/pkg/client"
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
)

Timothee Gosselin's avatar
Timothee Gosselin committed
// stripSecrets returns the object without secretData
func stripSecrets(obj runtime.Object) runtime.Object {
	// if obj is secret, don't print secret data
	s, ok := obj.(*corev1.Secret)
	if ok {
		s.Data = nil
		s.StringData = nil

		return s
	}

	return obj
}

Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
// GenerateConfigMap generates ConfigMap object as per the `Conf` struct passed
func MutatePod(c Mutate) (pod corev1.Pod, err error) {
	//	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.MutateContainer(con)
		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
}

func (c Conf) Containers() []container.Mutate {
	var spec []container.Mutate
	if c.GenContainers != nil {
		return c.GenContainers
	}
	return spec
}

func (c Conf) Volumes() ([]corev1.Volume, error) {
	var spec []corev1.Volume
	if c.GenVolumesFunc != nil {
		return c.GenVolumesFunc()
	}
	return spec, nil
}
func (c Conf) SecurityContext() (*corev1.PodSecurityContext, error) {
	var spec *corev1.PodSecurityContext
	if c.GenSecurityContextFunc != nil {
		return c.GenSecurityContextFunc()
	}
	return spec, nil
}
func (c Conf) RestartPolicy() (corev1.RestartPolicy, error) {
	var spec corev1.RestartPolicy
	if c.GenRestartPolicyFunc != nil {
		return c.GenRestartPolicyFunc()
	}
	return spec, nil
}

func (c Conf) ObjectName() string {
	return c.ObjectName()
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
}
func (c Conf) ObjectNamespace() string {
	return c.ObjectNamespace()
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
}

func (c Conf) GenLabelsFunc() (map[string]string, error) {
	var labels map[string]string
	if c.GenLabelsFunc != nil {
		return c.GenLabelsFunc()
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
	}
	return labels, nil
}
func (c Conf) GenAnnotationsFunc() (map[string]string, error) {
	var annotations map[string]string
	if c.GenAnnotationsFunc != nil {
		return c.GenAnnotationsFunc()
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
	}
	return annotations, nil

}
func (c Conf) GenFinalizersFunc() ([]string, error) {
	if c.GenFinalizersFunc != nil {
		return c.GenFinalizersFunc()
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
	}
	return nil, nil

}

Timothee Gosselin's avatar
Timothee Gosselin committed
// Object returns the ObjectSyncer subject
func (s Conf) Object() interfaces.Object {
	return s.Obj
}

// ObjectOwner returns the ObjectSyncer owner
func (s Conf) ObjectOwner() interfaces.Object {
	return s.Owner
}

// ObjectWithoutSecretData returns the ObjectSyncer subject without secret data
func (s Conf) ObjectWithoutSecretData() runtime.Object {
	return stripSecrets(s.Obj)
}

// PreviousWithoutSecretData returns the ObjectSyncer previous subject without secret data
func (s Conf) PreviousWithoutSecretData() runtime.Object {
	return stripSecrets(s.previousObject)
}

Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
// ObjectType returns the type of the ObjectSyncer subject
func (s Conf) ObjectType() string {
Timothee Gosselin's avatar
Timothee Gosselin committed
	return fmt.Sprintf("%T", s.Obj)
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
}

// OwnerType returns the type of the ObjectSyncer owner
func (s Conf) ObjectOwnerType() string {
Timothee Gosselin's avatar
Timothee Gosselin committed
	return fmt.Sprintf("%T", s.Owner)
}

// OwnerType returns the type of the ObjectSyncer owner
func (s Conf) SetPreviousObject() {
	//	obj := s.Obj.DeepCopyObject().(*appsv1.Deployment)
	//	s.previousObject = obj
	s.previousObject = s.Obj.DeepCopyObject()
	//s.previousObject = s.Object()
	return
}

// OwnerType returns the type of the ObjectSyncer owner
func (s Conf) GetClient() client.Client {
	return s.Reconcile.GetClient()
}

// OwnerType returns the type of the ObjectSyncer owner
func (s Conf) GetScheme() *runtime.Scheme {
	return s.Reconcile.GetScheme()
}

// OwnerType returns the type of the ObjectSyncer owner
func (s Conf) GetRecorder() record.EventRecorder {
	return s.Reconcile.GetRecorder()
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
}