Skip to content
component.go 2.09 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"

	"github.com/presslabs/controller-util/syncer"
Timothee Gosselin's avatar
Timothee Gosselin committed
	"k8s.libre.sh/controller-utils/application"
	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 ComponentReconciler struct {
	ReconcilerBase *ReconcilerBase
	Component      application.Component
	Status         status.ComponentStatus
	experiments    map[string]chan struct{}
	Owner          interfaces.Object
}

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

	for _, o := range r.Component.GetObjects() {
		reconciler := NewResourceReconciler(r.ReconcilerBase, o, r.Owner, context.TODO())
		res, err := reconciler.Reconcile(request)
		if err != nil {
			return res, err
		}
		r.Status.Resources.Objects = append(r.Status.Resources.Objects, reconciler.Status)

	}

	return ctrl.Result{}, nil
}

func SyncComponent(ctx context.Context, r interfaces.Reconcile, m application.ComponentMutate, owner interfaces.Object) (resources status.Resources, err error) {
	keys := []int{}

	// TODO return warning or error ?
	if m.GetObjects() == nil {
		return resources, nil
	}

	syncers := make(map[int]syncer.Interface, len(m.GetObjects()))

	for k, o := range m.GetObjects() {
		keys = append(keys, k)
		syncers[k] = objects.NewObjectSyncer(o, owner, r)
	}

	resources, err = SyncObjects(context.TODO(), r, syncers)
	if err != nil {
		return resources, err
	}

	return resources, nil
}