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

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

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 controllers

import (
	"context"

	"github.com/go-logr/logr"
	apierrs "k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/client-go/tools/record"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"

	"github.com/presslabs/controller-util/syncer"

	appsv1beta1 "git.indie.host/nextcloud-operator/api/v1beta1"
	application "git.indie.host/nextcloud-operator/components/app"
	"git.indie.host/nextcloud-operator/components/common"
	cron "git.indie.host/nextcloud-operator/components/cron"
Timothee Gosselin's avatar
Timothee Gosselin committed
	"git.indie.host/nextcloud-operator/components/web"
Timothee Gosselin's avatar
Timothee Gosselin committed
)

// NextcloudReconciler reconciles a Nextcloud object
type NextcloudReconciler struct {
	client.Client
	Log      logr.Logger
	Scheme   *runtime.Scheme
	Recorder record.EventRecorder
}

func ignoreNotFound(err error) error {
	if apierrs.IsNotFound(err) {
		return nil
	}
	return err
}

func (r *NextcloudReconciler) GetClient() client.Client          { return r.Client }
func (r *NextcloudReconciler) GetScheme() *runtime.Scheme        { return r.Scheme }
func (r *NextcloudReconciler) GetRecorder() record.EventRecorder { return r.Recorder }
func (r *NextcloudReconciler) GetLogger() logr.Logger            { return r.Log }

Timothee Gosselin's avatar
Timothee Gosselin committed
// +kubebuilder:rbac:groups=apps.libre.sh,resources=nextclouds,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apps.libre.sh,resources=nextclouds/status,verbs=get;update;patch

func (r *NextcloudReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
	ctx := context.Background()
	log := r.Log.WithValues("nextcloud", req.NamespacedName)
	log.Info("reconciling")

	app := &appsv1beta1.Nextcloud{}
	if err := r.Get(ctx, req.NamespacedName, app); err != nil {
		log.Error(err, "unable to fetch Nextcloud")
		// we'll ignore not-found errors, since they can't be fixed by an immediate
		// requeue (we'll need to wait for a new notification), and we can get them
		// on deleted requests.
		return ctrl.Result{}, ignoreNotFound(err)
	}
	// fmt.Println(app)

	common := common.CreateAndInit(app)

	componentApp := application.CreateAndInit(common)
	componentCron := cron.CreateAndInit(common)
Timothee Gosselin's avatar
Timothee Gosselin committed
	componentWeb := web.CreateAndInit(common)
	// 	componentCLI := cli.CreateAndInit(common)
Timothee Gosselin's avatar
Timothee Gosselin committed

	secretSyncer := componentApp.NewSecretSyncer(r)
Timothee Gosselin's avatar
Timothee Gosselin committed
	objectSyncer := componentApp.NewDeploymentSyncer(r)
	serviceSyncer := componentApp.NewServiceSyncer(r)
Timothee Gosselin's avatar
Timothee Gosselin committed
	cronSyncer := componentCron.NewCronJobSyncer(r)

Timothee Gosselin's avatar
Timothee Gosselin committed
	webConfigMapSyncer := componentWeb.NewConfigMapSyncer(r)
	webDeploymentSyncer := componentWeb.NewDeploymentSyncer(r)
	ingressSyncer := componentWeb.NewIngressSyncer(r)
	webServiceSyncer := componentWeb.NewServiceSyncer(r)
Timothee Gosselin's avatar
Timothee Gosselin committed

	// jobSyncer := componentCLI.NewJobSyncer(r)
	if err := syncer.Sync(context.TODO(), secretSyncer, r.Recorder); err != nil {
		return ctrl.Result{}, err
	}

Timothee Gosselin's avatar
Timothee Gosselin committed
	if err := syncer.Sync(context.TODO(), objectSyncer, r.Recorder); err != nil {
		return ctrl.Result{}, err
	}

	if err := syncer.Sync(context.TODO(), serviceSyncer, r.Recorder); err != nil {
		return ctrl.Result{}, err
	}

	if err := syncer.Sync(context.TODO(), ingressSyncer, r.Recorder); err != nil {
		return ctrl.Result{}, err
	}

	if err := syncer.Sync(context.TODO(), cronSyncer, r.Recorder); err != nil {
		return ctrl.Result{}, err
	}
Timothee Gosselin's avatar
Timothee Gosselin committed
	//	if err := syncer.Sync(context.TODO(), jobSyncer, r.Recorder); err != nil {
	//		return ctrl.Result{}, err
	//	}

	if err := syncer.Sync(context.TODO(), webConfigMapSyncer, r.Recorder); err != nil {
Timothee Gosselin's avatar
Timothee Gosselin committed
		return ctrl.Result{}, err
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

	if err := syncer.Sync(context.TODO(), webDeploymentSyncer, r.Recorder); err != nil {
		return ctrl.Result{}, err
	}

	if err := syncer.Sync(context.TODO(), webServiceSyncer, r.Recorder); err != nil {
		return ctrl.Result{}, err
	}

Timothee Gosselin's avatar
Timothee Gosselin committed
	return ctrl.Result{}, nil
}

func (r *NextcloudReconciler) SetupWithManager(mgr ctrl.Manager) error {
	return ctrl.NewControllerManagedBy(mgr).
		For(&appsv1beta1.Nextcloud{}).
		Complete(r)
}