Commit cf2fe237 authored by Timothee Gosselin's avatar Timothee Gosselin

use operatorlib for pvc

parent f02cb4dc
Pipeline #187 failed with stages
......@@ -84,7 +84,7 @@ func (app *Nextcloud) EnvDB(spec *appcat.AppBindingSpec) []corev1.EnvVar {
{
Name: "DB_NAME",
// TOFIX
Value: "nextcloud_prod",
Value: "nextcloud_db",
},
{
Name: "DB_USER",
......
......@@ -50,9 +50,8 @@ func (r *NextcloudReconciler) SyncIngress(app Nextcloud) (reconcile.Result, erro
r.Log.Error(err, "Ingress web reconcile failed")
return result, err
}
fmt.Println(app.Spec.IngressAnnotations)
return result, nil
return result, nil
}
func GenIngressAnnotations(obj interfaces.Object) (map[string]string, error) {
......
......@@ -79,17 +79,19 @@ func (r *NextcloudReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
// Sync resources if nextcloud spec are different
oldSpec := app.Nextcloud.Spec.DeepCopy()
if oldSpec != &app.Nextcloud.Spec {
var err error
var res ctrl.Result
app.Nextcloud.SetDefaults()
// Check for local storage backend and sync pvc
// if app.Nextcloud.Spec.Storage.Local != nil {
// r.SyncPVC(*app)
// }
if app.Nextcloud.Spec.Storage.Local != nil {
res, err = r.SyncPVC(*app)
if err != nil {
return res, err
}
// syncers := []syncer.Interface{
// NewCodePVCSyncer(app.Nextcloud, r.Client, r.Scheme),
// }
}
res, err := r.SyncService(*app)
res, err = r.SyncService(*app)
if err != nil {
return res, err
}
......@@ -170,10 +172,6 @@ func (r *NextcloudReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}
// r.SyncServiceMonitor(*app)
// if err = r.sync(syncers); err != nil {
// return ctrl.Result{}, err
// }
}
app.Nextcloud.Status.Version = app.Nextcloud.Spec.Version
......
......@@ -16,82 +16,65 @@ limitations under the License.
package controllers
import (
// "k8s.io/apimachinery/pkg/labels"
"fmt"
appsv1alpha1 "nextcloud-operator/api/v1alpha1"
"reflect"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/presslabs/controller-util/syncer"
"github.com/ankitrgadiya/operatorlib/pkg/interfaces"
"github.com/ankitrgadiya/operatorlib/pkg/pvc"
"github.com/pkg/errors"
)
// SyncPVC syncs pvc resource for application
// func (r *NextcloudReconciler) SyncPVC(app Nextcloud) error {
// pvc := &corev1.PersistentVolumeClaim{
// ObjectMeta: metav1.ObjectMeta{
// Name: app.Nextcloud.Name,
// Namespace: app.Nextcloud.Namespace,
// },
// }
// op, err := ctrl.CreateOrUpdate(context.TODO(), r, pvc, func() error {
// // PVC spec is immutable
// if !reflect.DeepEqual(pvc.Spec, corev1.PersistentVolumeClaimSpec{}) {
// return nil
// }
// r.pvcForApp(&app, pvc)
// return nil
// })
// if err != nil {
// r.Log.Error(err, "PVC reconcile failed")
/// r.Recorder.Event(app.Nextcloud, "Normal", fmt.Sprintf("%s", op), fmt.Sprintf("PVC %s/%s", pvc.Namespace, pvc.Name))
// return err
// }
// r.Log.Info("PVC successfully reconciled", "operation", op)
// r.Recorder.Event(app.Nextcloud, "Normal", fmt.Sprintf("%s", op), fmt.Sprintf("PVC %s/%s", pvc.Namespace, pvc.Name))
// return nil
// }
// func (r *NextcloudReconciler) pvcForApp(app *Nextcloud, pvc *corev1.PersistentVolumeClaim) *corev1.PersistentVolumeClaim {
// ls := app.Labels("api")
// pvc.Labels = ls
// pvc.Spec = *app.Nextcloud.Spec.Storage.Local.PersistentVolumeClaimSpec
// ctrl.SetControllerReference(app.Nextcloud, pvc, r.Scheme)
// return pvc
//}
// NewCodePVCSyncer returns a new sync.Interface for reconciling codePVC
func NewCodePVCSyncer(app *appsv1alpha1.Nextcloud, c client.Client, scheme *runtime.Scheme) syncer.Interface {
nc := New(app)
objLabels := nc.Labels("app")
obj := &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: app.Name,
Namespace: app.Namespace,
},
// SyncDeployment syncs deployments for apoplibcation api
func (r *NextcloudReconciler) SyncPVC(app Nextcloud) (reconcile.Result, error) {
app.Nextcloud.Spec.Component = "app"
result, err := pvc.CreateOrUpdate(pvc.Conf{
Instance: app.Nextcloud,
Name: app.Nextcloud.Name,
Namespace: app.Nextcloud.Namespace,
GenLabelsFunc: GenLabels,
Reconcile: r,
OwnerReference: true,
StorageClassName: app.Spec.Storage.Local.PersistentVolumeClaimSpec.StorageClassName,
GenResourcesFunc: GenPVCResources,
GenAccessModesFunc: GenPVCAccessModes,
})
if err != nil {
r.Log.Error(err, "PVC reconcile failed")
return result, err
}
return syncer.NewObjectSyncer("PVC", app, obj, c, scheme, func() error {
obj.Labels = labels.Merge(obj.Labels, objLabels)
r.Log.Info("PVC successfully reconciled", "operation", "test")
if app.Spec.Storage.Local.PersistentVolumeClaimSpec == nil { // || app.Spec.CodeVolumeSpec.PersistentVolumeClaim == nil {
return fmt.Errorf("app.Spec.Storage.Local.PersistentVolumeClaimSpec is not defined")
}
return result, nil
// PVC spec is immutable
if !reflect.DeepEqual(obj.Spec, corev1.PersistentVolumeClaimSpec{}) {
return nil
}
}
obj.Spec = *app.Spec.Storage.Local.PersistentVolumeClaimSpec
func GenPVCResources(obj interfaces.Object) (corev1.ResourceRequirements, error) {
var resources corev1.ResourceRequirements
app, ok := obj.(*appsv1alpha1.Nextcloud)
return nil
})
if !ok {
return resources, errors.New("failed to assert the original object")
}
resources = app.Spec.Storage.Local.PersistentVolumeClaimSpec.Resources
return resources, nil
}
func GenPVCAccessModes(obj interfaces.Object) ([]corev1.PersistentVolumeAccessMode, error) {
var accessModes []corev1.PersistentVolumeAccessMode
app, ok := obj.(*appsv1alpha1.Nextcloud)
if !ok {
return accessModes, errors.New("failed to assert the original object")
}
accessModes = app.Spec.Storage.Local.PersistentVolumeClaimSpec.AccessModes
return accessModes, nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment