Skip to content
resource.go 2.75 KiB
Newer Older
Timothee Gosselin's avatar
Timothee Gosselin committed
/*

Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.gnu.org/licenses/agpl-3.0.html

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package reconciler

import (
	"context"
	"sort"

	"cloud.google.com/go/logging"
	"github.com/presslabs/controller-util/syncer"
Timothee Gosselin's avatar
Timothee Gosselin committed
	interfaces "k8s.libre.sh/controller-utils/interfaces"
	"k8s.libre.sh/controller-utils/objects"
	"k8s.libre.sh/controller-utils/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
}