...
 
Commits (6)
......@@ -187,6 +187,7 @@ k8s.io/apiextensions-apiserver v0.0.0-20190409022649-727a075fdec8/go.mod h1:Ixke
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d h1:Jmdtdt1ZnoGfWWIIik61Z7nKYgO3J+swQJtPYsP9wHA=
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d/go.mod h1:ccL7Eh7zubPUSh9A3USN90/OzHNSVN6zxzde07TDCL0=
k8s.io/apimachinery v0.0.0-20190927035529-0104e33c351d h1:oYLB5Nk2IOm17BHdatnaWAgzNGzq/5dlWy7Bzo5Htdc=
k8s.io/apimachinery v0.0.0-20191111054156-6eb29fdf75dc h1:hC0UI7qlplCVlRexiPMHwcOCT3IPk9Pgo599vKGOOS4=
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible h1:U5Bt+dab9K8qaUmXINrkXO135kA11/i5Kg1RUydgaMQ=
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s=
k8s.io/klog v0.3.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
......
package configmap
import (
"github.com/ankitrgadiya/operatorlib/pkg/meta"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func Generate(m Mutate) (out corev1.ConfigMap, err error) {
om, err := meta.MutateObjectMeta(m)
if err != nil {
return out, errors.Wrap(err, "failed to generate objectmeta")
}
data, err := m.GenData()
if err != nil {
return out, errors.Wrap(err, "failed to generate configmap data")
}
binData, err := m.GenBinaryData()
if err != nil {
return out, errors.Wrap(err, "failed to generate configmap binary data")
}
out.ObjectMeta = *om
out.Data = data
out.BinaryData = binData
return out, err
}
func (c Conf) Mutate() controllerutil.MutateFn {
return func() error {
out, err := Generate(c)
if err != nil {
return err
}
// mergo.Merge(c.Obj, out)
c.Obj = &out
return nil
}
}
func (c Conf) MakeObject() *corev1.ConfigMap {
obj := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: c.ObjectName(),
Namespace: c.ObjectNamespace(),
},
}
return obj
}
package configmap
import (
"fmt"
"github.com/ankitrgadiya/operatorlib/pkg/interfaces"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func (c Conf) GenData() (out map[string]string, err error) {
if c.GenDataFunc != nil {
return c.GenDataFunc()
}
return out, nil
}
func (c Conf) GenBinaryData() (out map[string][]byte, err error) {
if c.GenBinaryDataFunc != nil {
return c.GenBinaryDataFunc()
}
return out, nil
}
func (c Conf) GenLabelsFunc() (map[string]string, error) {
var labels map[string]string
if c.GenLabels != nil {
return c.GenLabels()
}
return labels, nil
}
func (c Conf) GenAnnotationsFunc() (map[string]string, error) {
var annotations map[string]string
if c.GenAnnotations != nil {
return c.GenAnnotations()
}
return annotations, nil
}
func (c Conf) GenFinalizersFunc() ([]string, error) {
if c.GenFinalizers != nil {
return c.GenFinalizers()
}
return nil, nil
}
// Object returns the ObjectSyncer subject
func (s Conf) Object() interfaces.Object {
return s.Obj
}
// Object returns the ObjectSyncer subject
func (s Conf) ObjectName() string {
return s.Name
}
// Object returns the ObjectSyncer subject
func (s Conf) ObjectNamespace() string {
return s.Namespace
}
// 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)
}
// ObjectType returns the type of the ObjectSyncer subject
func (s Conf) ObjectType() string {
return fmt.Sprintf("%T", s.Obj)
}
// OwnerType returns the type of the ObjectSyncer owner
func (s Conf) ObjectOwnerType() string {
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.Object()
//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()
}
// 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
}
package configmap
import (
"github.com/ankitrgadiya/operatorlib/pkg/interfaces"
"github.com/ankitrgadiya/operatorlib/pkg/meta"
corev1 "k8s.io/api/core/v1"
)
// GenConfigMapFunc defines a function which generates ConfigMap.
type GenConfigMapFunc func(Conf) (*corev1.ConfigMap, error)
// GenDataFunc defines a function which generates data (string map)
// for Configmap.
type GenDataFunc func() (map[string]string, error)
// GenBinaryDataFunc defines a function which generates binary data
// (map of string to byte slice) for Configmap.
type GenBinaryDataFunc func() (map[string][]byte, error)
type Mutate interface {
meta.Mutate
GenData() (map[string]string, error)
GenBinaryData() (map[string][]byte, error)
}
type Conf struct {
Namespace string
Name string
Obj interfaces.Object
Reconcile interfaces.Reconcile
previousObject interfaces.Object
Owner interfaces.Object
OwnerReference bool
GenFinalizers meta.GenFinalizers
GenLabels meta.GenLabels
GenAnnotations meta.GenAnnotations
GenConfigMapFunc
GenDataFunc
GenBinaryDataFunc
}
......@@ -5,7 +5,7 @@ import (
corev1 "k8s.io/api/core/v1"
)
func MutateContainer(c Mutate) (obj *corev1.Container, err error) {
func Generate(c Mutate) (obj corev1.Container, err error) {
var container corev1.Container
var ports []corev1.ContainerPort
var cmd []string
......@@ -18,31 +18,31 @@ func MutateContainer(c Mutate) (obj *corev1.Container, err error) {
ports, err = c.ContainerPorts()
if err != nil {
return nil, errors.Wrap(err, "failed to generate container ports")
return container, errors.Wrap(err, "failed to generate container ports")
}
args, err = c.ContainerArgs()
if err != nil {
return nil, errors.Wrap(err, "failed to generate container ports")
return container, errors.Wrap(err, "failed to generate container ports")
}
resources, err = c.ContainerResources()
if err != nil {
return nil, errors.Wrap(err, "failed to generate container ports")
return container, errors.Wrap(err, "failed to generate container ports")
}
volumeMounts, err = c.ContainerVolumeMounts()
if err != nil {
return nil, errors.Wrap(err, "failed to generate container ports")
return container, errors.Wrap(err, "failed to generate container ports")
}
readiness, err = c.ContainerReadinessProbe()
if err != nil {
return nil, errors.Wrap(err, "failed to generate container ports")
return container, errors.Wrap(err, "failed to generate container ports")
}
liveness, err = c.ContainerLivennessProbe()
if err != nil {
return nil, errors.Wrap(err, "failed to generate container ports")
return container, errors.Wrap(err, "failed to generate container ports")
}
envList, err = c.ContainerEnvVar()
if err != nil {
return nil, errors.Wrap(err, "failed to generate container ports")
return container, errors.Wrap(err, "failed to generate container ports")
}
container = corev1.Container{
Name: c.ContainerName(),
......@@ -58,5 +58,5 @@ func MutateContainer(c Mutate) (obj *corev1.Container, err error) {
Resources: resources,
}
return &container, err
return container, err
}
package cronjob
import (
"github.com/ankitrgadiya/operatorlib/pkg/job"
"github.com/ankitrgadiya/operatorlib/pkg/meta"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/pkg/errors"
batchv1beta1 "k8s.io/api/batch/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func Generate(m Mutate) (out batchv1beta1.CronJob, err error) {
om, err := meta.MutateObjectMeta(m)
if err != nil {
return out, errors.Wrap(err, "failed to generate objectmeta")
}
child, err := job.Generate(m)
if err != nil {
return out, errors.Wrap(err, "failed to generate job template spec")
}
schedule, err := m.GenSchedule()
if err != nil {
return out, errors.Wrap(err, "failed to generate cron schedule spec")
}
out.ObjectMeta = *om
out.Spec = batchv1beta1.CronJobSpec{
Schedule: schedule,
JobTemplate: batchv1beta1.JobTemplateSpec{
ObjectMeta: *om,
Spec: child.Spec,
},
}
return out, err
}
func (c Conf) Mutate() controllerutil.MutateFn {
return func() error {
out, err := Generate(c)
if err != nil {
return err
}
c.Obj = &out
return nil
}
}
func (c Conf) MakeObject() *batchv1beta1.CronJob {
obj := &batchv1beta1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: c.ObjectName(),
Namespace: c.ObjectNamespace(),
},
}
return obj
}
package cronjob
import (
"fmt"
"github.com/ankitrgadiya/operatorlib/pkg/container"
"github.com/ankitrgadiya/operatorlib/pkg/interfaces"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func (c Conf) GenSuccessfulJobsHistoryLimit() (*int32, error) {
var spec *int32
if c.GenSuccessfulJobsHistoryLimitFunc != nil {
return c.GenSuccessfulJobsHistoryLimitFunc()
}
return spec, nil
}
func (c Conf) GenFailedJobsHistoryLimitHistoryLimit() (*int32, error) {
var spec *int32
if c.GenFailedJobsHistoryLimitHistoryLimitFunc != nil {
return c.GenFailedJobsHistoryLimitHistoryLimitFunc()
}
return spec, nil
}
func (c Conf) GenConcurrencyPolicy() (batchv1beta1.ConcurrencyPolicy, error) {
var spec batchv1beta1.ConcurrencyPolicy
if c.GenConcurrencyPolicyFunc != nil {
return c.GenConcurrencyPolicyFunc()
}
return spec, nil
}
func (c Conf) GenSuspendPolicy() (bool, error) {
var spec bool
if c.GenSuspendPolicyFunc != nil {
return c.GenSuspendPolicyFunc()
}
return spec, nil
}
func (c Conf) GenStartingDeadlineSecondsSeconds() (*int64, error) {
var spec *int64
if c.GenStartingDeadlineSecondsSecondsFunc != nil {
return c.GenStartingDeadlineSecondsSecondsFunc()
}
return spec, nil
}
func (c Conf) GenSchedule() (string, error) {
var spec string
if c.GenScheduleFunc != nil {
return c.GenScheduleFunc()
}
return spec, nil
}
func (c Conf) GenManualSelector() (*bool, error) {
return &c.ManualSelector, nil
}
func (c Conf) GenParallelism() (*int32, error) {
var spec *int32
if c.GenParallelismFunc != nil {
return c.GenParallelismFunc()
}
return spec, nil
}
func (c Conf) GenCompletions() (*int32, error) {
var spec *int32
if c.GenCompletionsFunc != nil {
return c.GenCompletionsFunc()
}
return spec, nil
}
func (c Conf) GenActiveDeadlineSeconds() (*int64, error) {
var spec *int64
if c.GenActiveDeadlineSecondsFunc != nil {
return c.GenActiveDeadlineSecondsFunc()
}
return spec, nil
}
func (c Conf) GenBackoffLimit() (*int32, error) {
var spec *int32
if c.GenBackoffLimitFunc != nil {
return c.GenBackoffLimitFunc()
}
return spec, nil
}
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) GenLabelsFunc() (map[string]string, error) {
var labels map[string]string
if c.GenLabels != nil {
return c.GenLabels()
}
return labels, nil
}
func (c Conf) GenAnnotationsFunc() (map[string]string, error) {
var annotations map[string]string
if c.GenAnnotations != nil {
return c.GenAnnotations()
}
return annotations, nil
}
func (c Conf) GenFinalizersFunc() ([]string, error) {
if c.GenFinalizers != nil {
return c.GenFinalizers()
}
return nil, nil
}
// Object returns the ObjectSyncer subject
func (s Conf) Object() interfaces.Object {
return s.Obj
}
// Object returns the ObjectSyncer subject
func (s Conf) ObjectName() string {
return s.Name
}
// Object returns the ObjectSyncer subject
func (s Conf) ObjectNamespace() string {
return s.Namespace
}
// 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)
}
// ObjectType returns the type of the ObjectSyncer subject
func (s Conf) ObjectType() string {
return fmt.Sprintf("%T", s.Obj)
}
// OwnerType returns the type of the ObjectSyncer owner
func (s Conf) ObjectOwnerType() string {
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()
}
// 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
}
package cronjob
import (
"github.com/ankitrgadiya/operatorlib/pkg/container"
"github.com/ankitrgadiya/operatorlib/pkg/interfaces"
"github.com/ankitrgadiya/operatorlib/pkg/job"
"github.com/ankitrgadiya/operatorlib/pkg/meta"
"github.com/ankitrgadiya/operatorlib/pkg/pod"
batchv1beta1 "k8s.io/api/batch/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
)
// GenConfigMapFunc defines a function which generates ConfigMap.
type GenCronJobFunc func(Conf) (*batchv1beta1.CronJob, error)
type GenSuccessfulJobsHistoryLimitFunc func() (*int32, error)
type GenFailedJobsHistoryLimitHistoryLimitFunc func() (*int32, error)
type GenConcurrencyPolicyFunc func() (batchv1beta1.ConcurrencyPolicy, error)
type GenSuspendPolicyFunc func() (bool, error)
type GenStartingDeadlineSecondsSecondsFunc func() (*int64, error)
type GenScheduleFunc func() (string, error)
// type GenSelectorFunc func(interfaces.Object) (*metav1.LabelSelector, error)
type Mutate interface {
job.Mutate
GenSuccessfulJobsHistoryLimit() (*int32, error)
GenFailedJobsHistoryLimitHistoryLimit() (*int32, error)
GenConcurrencyPolicy() (batchv1beta1.ConcurrencyPolicy, error)
GenSuspendPolicy() (bool, error)
GenStartingDeadlineSecondsSeconds() (*int64, error)
GenSchedule() (string, error)
}
type Conf struct {
Namespace string
Name string
Obj interfaces.Object
Reconcile interfaces.Reconcile
previousObject runtime.Object
Owner interfaces.Object
OwnerReference bool
GenFinalizers meta.GenFinalizers
GenLabels meta.GenLabels
GenAnnotations meta.GenAnnotations
ManualSelector bool
GenParallelismFunc job.GenParallelismFunc
GenCompletionsFunc job.GenCompletionsFunc
GenActiveDeadlineSecondsFunc job.GenActiveDeadlineSecondsFunc
GenBackoffLimitFunc job.GenBackoffLimitFunc
// GenSelectorFunc
GenSuccessfulJobsHistoryLimitFunc
GenFailedJobsHistoryLimitHistoryLimitFunc
GenConcurrencyPolicyFunc
GenSuspendPolicyFunc
GenStartingDeadlineSecondsSecondsFunc
GenScheduleFunc
GenVolumesFunc pod.GenVolumesFunc
GenSecurityContextFunc pod.GenSecurityContextFunc
GenRestartPolicyFunc pod.GenRestartPolicyFunc
GenContainers []container.Mutate
}
......@@ -3,50 +3,54 @@ package deployment
import (
"fmt"
"github.com/ankitrgadiya/operatorlib/pkg/container"
"github.com/ankitrgadiya/operatorlib/pkg/interfaces"
"github.com/ankitrgadiya/operatorlib/pkg/meta"
"github.com/ankitrgadiya/operatorlib/pkg/pod"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/imdario/mergo"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
)
// GenerateConfigMap generates ConfigMap object as per the `Conf` struct passed
func (c Conf) Mutate() controllerutil.MutateFn {
func Generate(m Mutate) (out appsv1.Deployment, err error) {
om, err := meta.MutateObjectMeta(m)
if err != nil {
return out, errors.Wrap(err, "failed to generate objectmeta")
}
return func() error {
child, err := pod.Generate(m)
om, err := meta.MutateObjectMeta(c)
if err != nil {
return out, errors.Wrap(err, "failed to generate job template spec")
}
if err != nil {
return errors.Wrap(err, "failed to generate objectmeta")
}
out.ObjectMeta = *om
out.Spec = appsv1.DeploymentSpec{
// Replicas: c.Size,
// Strategy: c.Strategy,
Selector: metav1.SetAsLabelSelector(om.Labels),
Template: corev1.PodTemplateSpec{
ObjectMeta: *om,
Spec: child.Spec,
},
}
p, err := pod.MutatePod(c)
return out, err
}
if err != nil {
return errors.Wrap(err, "failed to generate pod template spec")
}
func (c Conf) Mutate() controllerutil.MutateFn {
c.Obj.ObjectMeta = *om
c.Obj.Spec = appsv1.DeploymentSpec{
// Replicas: c.Size,
Strategy: c.Strategy,
Selector: metav1.SetAsLabelSelector(om.Labels),
Template: corev1.PodTemplateSpec{
ObjectMeta: *om,
Spec: p.Spec,
},
return func() error {
out, err := Generate(c)
fmt.Println(out)
if err != nil {
return err
}
mergo.Merge(c.Obj, out)
return err
return nil
}
}
......@@ -60,134 +64,3 @@ func (c Conf) MakeObject() *appsv1.Deployment {
return obj
}
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) GenLabelsFunc() (map[string]string, error) {
var labels map[string]string
if c.GenLabels != nil {
return c.GenLabels()
}
return labels, nil
}
func (c Conf) GenAnnotationsFunc() (map[string]string, error) {
var annotations map[string]string
if c.GenAnnotations != nil {
return c.GenAnnotations()
}
return annotations, nil
}
func (c Conf) GenFinalizersFunc() ([]string, error) {
if c.GenFinalizers != nil {
return c.GenFinalizers()
}
return nil, nil
}
// Object returns the ObjectSyncer subject
func (s Conf) Object() interfaces.Object {
return s.Obj
}
// Object returns the ObjectSyncer subject
func (s Conf) ObjectName() string {
return s.Name
}
// Object returns the ObjectSyncer subject
func (s Conf) ObjectNamespace() string {
return s.Namespace
}
// 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)
}
// ObjectType returns the type of the ObjectSyncer subject
func (s Conf) ObjectType() string {
return fmt.Sprintf("%T", s.Obj)
}
// OwnerType returns the type of the ObjectSyncer owner
func (s Conf) ObjectOwnerType() string {
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()
}
// 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
}
package deployment
package deployment
import (
"fmt"
"github.com/ankitrgadiya/operatorlib/pkg/container"
"github.com/ankitrgadiya/operatorlib/pkg/interfaces"
"github.com/ankitrgadiya/operatorlib/pkg/meta"
"github.com/ankitrgadiya/operatorlib/pkg/pod"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
)
func (c Conf) Containers() []container.Mutate {
var spec []container.Mutate
if c.GenContainers != nil {
......@@ -148,4 +141,4 @@ func stripSecrets(obj runtime.Object) runtime.Object {
}
return obj
}
\ No newline at end of file
}
......@@ -13,11 +13,11 @@ import (
// GenConfigMapFunc defines a function which generates ConfigMap.
type GenDeploymentFunc func(Conf) (*appsv1.Deployment, error)
// Conf is used to pass parameters to functions in this package to
// perform operations on Configmap objects.
type Mutate interface {
pod.Mutate
}
type Conf struct {
// Meta meta.Mutate
// Pod pod.Mutate
Obj *appsv1.Deployment
Reconcile interfaces.Reconcile
previousObject runtime.Object
......@@ -36,8 +36,6 @@ type Conf struct {
GenContainers []container.Mutate
// Pod pod.Mutate
GenVolumesFunc pod.GenVolumesFunc
GenSecurityContextFunc pod.GenSecurityContextFunc
GenRestartPolicyFunc pod.GenRestartPolicyFunc
......
package ingress
import (
"fmt"
"github.com/ankitrgadiya/operatorlib/pkg/interfaces"
corev1 "k8s.io/api/core/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func (c Conf) GenIngressBackend() (out *networkingv1beta1.IngressBackend, err error) {
if c.GenIngressBackendFunc != nil {
return c.GenIngressBackendFunc()
}
return out, nil
}
func (c Conf) GenIngressTLS() (out []networkingv1beta1.IngressTLS, err error) {
if c.GenIngressTLSFunc != nil {
return c.GenIngressTLSFunc()
}
return out, nil
}
func (c Conf) GenIngressRules() (out []networkingv1beta1.IngressRule, err error) {
if c.GenIngressRulesFunc != nil {
return c.GenIngressRulesFunc()
}
return out, nil
}
func (c Conf) GenLabelsFunc() (map[string]string, error) {
var labels map[string]string
if c.GenLabels != nil {
return c.GenLabels()
}
return labels, nil
}
func (c Conf) GenAnnotationsFunc() (map[string]string, error) {
var annotations map[string]string
if c.GenAnnotations != nil {
return c.GenAnnotations()
}
return annotations, nil
}
func (c Conf) GenFinalizersFunc() ([]string, error) {
if c.GenFinalizers != nil {
return c.GenFinalizers()
}
return nil, nil
}
// Object returns the ObjectSyncer subject
func (s Conf) Object() interfaces.Object {
return s.Obj
}
// Object returns the ObjectSyncer subject
func (s Conf) ObjectName() string {
return s.Name
}
// Object returns the ObjectSyncer subject
func (s Conf) ObjectNamespace() string {
return s.Namespace
}
// 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)
}
// ObjectType returns the type of the ObjectSyncer subject
func (s Conf) ObjectType() string {
return fmt.Sprintf("%T", s.Obj)
}
// OwnerType returns the type of the ObjectSyncer owner
func (s Conf) ObjectOwnerType() string {
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.Object()
//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()
}
// 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
}
package ingress
import (
"fmt"
"github.com/ankitrgadiya/operatorlib/pkg/meta"
"github.com/imdario/mergo"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/pkg/errors"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func Generate(m Mutate) (out networkingv1beta1.Ingress, err error) {
om, err := meta.MutateObjectMeta(m)
tls, err := m.GenIngressTLS()
if err != nil {
return out, errors.Wrap(err, "failed to generate ingress tls spec")
}
rules, err := m.GenIngressRules()
if err != nil {
return out, errors.Wrap(err, "failed to generate ingress rules spec")
}
out.ObjectMeta = *om
out.Spec = networkingv1beta1.IngressSpec{
// Backend: backend,
TLS: tls,
Rules: rules,
}
return out, err
}
func (c Conf) Mutate() controllerutil.MutateFn {
return func() error {
out, err := Generate(c)
fmt.Println(out)
if err != nil {
return err
}
mergo.Merge(c.Obj, out)
return nil
}
}
func (c Conf) MakeObject() *networkingv1beta1.Ingress {
obj := &networkingv1beta1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: c.ObjectName(),
Namespace: c.ObjectNamespace(),
},
}
return obj
}
package ingress
import (
"github.com/ankitrgadiya/operatorlib/pkg/interfaces"
"github.com/ankitrgadiya/operatorlib/pkg/meta"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
)
// GenConfigMapFunc defines a function which generates ConfigMap.
type GenIngressFunc func(Conf) (*networkingv1beta1.Ingress, error)
type GenIngressBackendFunc func() (*networkingv1beta1.IngressBackend, error)
type GenIngressTLSFunc func() ([]networkingv1beta1.IngressTLS, error)
type GenIngressRulesFunc func() ([]networkingv1beta1.IngressRule, error)
type Conf struct {
Namespace string
Name string
Obj interfaces.Object
Reconcile interfaces.Reconcile
previousObject interfaces.Object
Owner interfaces.Object
OwnerReference bool
GenFinalizers meta.GenFinalizers
GenLabels meta.GenLabels
GenAnnotations meta.GenAnnotations
GenIngressFunc
GenIngressBackendFunc
GenIngressTLSFunc
GenIngressRulesFunc
}
type Mutate interface {
meta.Mutate
GenIngressBackend() (*networkingv1beta1.IngressBackend, error)
GenIngressTLS() ([]networkingv1beta1.IngressTLS, error)
GenIngressRules() ([]networkingv1beta1.IngressRule, error)
}
package job
import (
"fmt"
"github.com/ankitrgadiya/operatorlib/pkg/container"
"github.com/ankitrgadiya/operatorlib/pkg/interfaces"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func (c Conf) MakeObject() *batchv1.Job {
obj := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: c.ObjectName(),
Namespace: c.ObjectNamespace(),
},
}
return obj
}
func (c Conf) GenManualSelector() (*bool, error) {
return &c.ManualSelector, nil
}
func (c Conf) GenParallelism() (*int32, error) {
var spec *int32
if c.GenParallelismFunc != nil {
return c.GenParallelismFunc()
}
return spec, nil
}
func (c Conf) GenCompletions() (*int32, error) {
var spec *int32
if c.GenCompletionsFunc != nil {
return c.GenCompletionsFunc()
}
return spec, nil
}
func (c Conf) GenActiveDeadlineSeconds() (*int64, error) {
var spec *int64
if c.GenActiveDeadlineSecondsFunc != nil {
return c.GenActiveDeadlineSecondsFunc()
}
return spec, nil
}
func (c Conf) GenBackoffLimit() (*int32, error) {
var spec *int32
if c.GenBackoffLimitFunc != nil {
return c.GenBackoffLimitFunc()
}
return spec, nil
}
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) GenLabelsFunc() (map[string]string, error) {
var labels map[string]string
if c.GenLabels != nil {
return c.GenLabels()
}
return labels, nil
}
func (c Conf) GenAnnotationsFunc() (map[string]string, error) {
var annotations map[string]string
if c.GenAnnotations != nil {
return c.GenAnnotations()
}
return annotations, nil
}
func (c Conf) GenFinalizersFunc() ([]string, error) {
if c.GenFinalizers != nil {
return c.GenFinalizers()
}
return nil, nil
}
// Object returns the ObjectSyncer subject
func (s Conf) Object() interfaces.Object {
return s.Obj
}
// Object returns the ObjectSyncer subject
func (s Conf) ObjectName() string {
return s.Name
}
// Object returns the ObjectSyncer subject
func (s Conf) ObjectNamespace() string {
return s.Namespace
}
// 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)
}
// ObjectType returns the type of the ObjectSyncer subject
func (s Conf) ObjectType() string {
return fmt.Sprintf("%T", s.Obj)
}
// OwnerType returns the type of the ObjectSyncer owner
func (s Conf) ObjectOwnerType() string {
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()
}
// 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
}
package job
import (
"fmt"
"reflect"
"github.com/ankitrgadiya/operatorlib/pkg/container"
"github.com/ankitrgadiya/operatorlib/pkg/interfaces"
"github.com/ankitrgadiya/operatorlib/pkg/meta"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
pod "github.com/ankitrgadiya/operatorlib/pkg/pod"
"github.com/imdario/mergo"
"github.com/pkg/errors"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
)
func (c Conf) Mutate() controllerutil.MutateFn {
return func() error {
// in := c.Obj.(*batchv1.Job)
out := c.Obj.(*batchv1.Job)
om, err := meta.MutateObjectMeta(c)
if err != nil {
return errors.Wrap(err, "failed to generate objectmeta")
}
p, err := pod.MutatePod(c)
if err != nil {
return errors.Wrap(err, "failed to generate pod template spec")
}
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: &c.ManualSelector,
}
// if in.Spec.Selector != out.Spec.Selector {
// out.Spec.Selector = in.Spec.Selector
// }
// mergo.Merge(out, in)
//fmt.Println(in)
//fmt.Println(out)
c.Obj = out
return err
}
}
// MaybeUpdate implements MaybeUpdateFunc for Secret object. It
// compares the two Secrets being passed and update the first one if
// required.
func MaybeUpdate(original interfaces.Object, new interfaces.Object) (bool, error) {
o, ok := original.(*batchv1.Job)
if !ok {
return false, errors.New("failed to assert the original object")
func Generate(m Mutate) (out batchv1.Job, err error) {
om, err := meta.MutateObjectMeta(m)
if err != nil {
return out, errors.Wrap(err, "failed to generate objectmeta")
}
n, ok := new.(*batchv1.Job)
if !ok {
return false, errors.New("failed to assert the new object")
p, err := pod.Generate(m)
if err != nil {
return out, errors.Wrap(err, "failed to generate pod template spec")
}
result := reflect.DeepEqual(o.Spec.Template, n.Spec.Template)
if result {
return false, nil
manualSelector, err := m.GenManualSelector()
if err != nil {
return out, errors.Wrap(err, "failed to generate volumes for pod template spec")
}
o.Spec = n.Spec
return true, nil
}
func (c Conf) MakeObject() *batchv1.Job {
obj := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: c.ObjectName(),
Namespace: c.ObjectNamespace(),
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,
}
return obj
return out, 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) GenLabelsFunc() (map[string]string, error) {
var labels map[string]string
if c.GenLabels != nil {
return c.GenLabels()
}
return labels, nil
}
func (c Conf) GenAnnotationsFunc() (map[string]string, error) {
var annotations map[string]string
if c.GenAnnotations != nil {
return c.GenAnnotations()
}
return annotations, nil
}
func (c Conf) GenFinalizersFunc() ([]string, error) {
if c.GenFinalizers != nil {
return c.GenFinalizers()
}
return nil, nil
}
// Object returns the ObjectSyncer subject
func (s Conf) Object() interfaces.Object {
return s.Obj
}
// Object returns the ObjectSyncer subject
func (s Conf) ObjectName() string {
return s.Name
}
// Object returns the ObjectSyncer subject
func (s Conf) ObjectNamespace() string {
return s.Namespace
}
// 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)
}
// ObjectType returns the type of the ObjectSyncer subject
func (s Conf) ObjectType() string {
return fmt.Sprintf("%T", s.Obj)
}
// OwnerType returns the type of the ObjectSyncer owner
func (s Conf) ObjectOwnerType() string {
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()
}
func (c Conf) Mutate() controllerutil.MutateFn {
// OwnerType returns the type of the ObjectSyncer owner
func (s Conf) GetScheme() *runtime.Scheme {
return s.Reconcile.GetScheme()
}
return func() error {
out, err := Generate(c)
if err != nil {
return err
}
// OwnerType returns the type of the ObjectSyncer owner
func (s Conf) GetRecorder() record.EventRecorder {
return s.Reconcile.GetRecorder()