Skip to content
resource.go 2.17 KiB
Newer Older
package reconciler

import (
	"context"
	"sort"

	"cloud.google.com/go/logging"
	"github.com/presslabs/controller-util/syncer"
	interfaces "k8s.libre.sh/interfaces"
	"k8s.libre.sh/objects"
	"k8s.libre.sh/status"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

type ResourceReconciler struct {
	*ReconcilerBase
	Object objects.Object
	Owner  interfaces.Object
	Status status.ObjectStatus
	log    logging.Logger
	ctx    context.Context
}

func NewResourceReconciler(r *ReconcilerBase, o objects.Object, owner interfaces.Object, ctx context.Context) *ResourceReconciler {
	return &ResourceReconciler{
		ReconcilerBase: r,
		Object:         o,
		Owner:          owner,
		ctx:            ctx,
	}
}

func (r *ResourceReconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) {

	return ctrl.Result{}, nil
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func SyncObject(ctx context.Context, r interfaces.Reconcile, syncer syncer.Interface) (status.ObjectStatus, error) {
Timothee Gosselin's avatar
Timothee Gosselin committed
	resource := status.ObjectStatus{}
	syncResult, err := syncer.Sync(ctx)
Timothee Gosselin's avatar
Timothee Gosselin committed
	obj := syncer.GetObject().(interfaces.Object)

	if err != nil {
		Record(syncer.GetOwner(), r.GetRecorder(), syncResult)
		// TODO TOFIX object cannot be set as it is not recognized
		//		resource.Status = "NotReady"
		return resource, err
	}

	err = WaitUntilCompletion(r, obj)
	// TODO TO fix gvk is not recognized, need to get resource with client before
	resource = status.ObjectToStatus(obj)
	if err != nil {
		// TODO replace syncResult by sync failed
		resource.Status = "NotReady"
		Record(syncer.GetOwner(), r.GetRecorder(), syncResult)
		return resource, err
	}

	//	resource = status.ObjectToStatus(obj)
Timothee Gosselin's avatar
Timothee Gosselin committed

	resource.Status = "Ready"

	Record(syncer.GetOwner(), r.GetRecorder(), syncResult)

	return resource, nil
}

func SyncObjects(ctx context.Context, r interfaces.Reconcile, syncers map[int]syncer.Interface) (resources status.Resources, err error) {
	keys := []int{}

	for k := range syncers {
		keys = append(keys, k)
	}

	sort.Ints(keys)

	for _, k := range keys {
		objStatus, err := SyncObject(ctx, r, syncers[k])
		resources.Objects = append(resources.Objects, objStatus)
		if err != nil {
			return resources, err
		}
	}

	return resources, nil
}