feat(juicefs)!: abstract all under pv/pvc

This commit is contained in:
Hugo Renard 2024-06-12 17:21:53 +02:00
parent 24a9b63ec9
commit 7b15bfb073
Signed by: hougo
GPG key ID: 3A285FD470209C59
2 changed files with 49 additions and 38 deletions

View file

@ -4,9 +4,11 @@ kind: StorageClass
metadata:
name: juicefs.libre.sh
provisioner: csi.juicefs.com
allowVolumeExpansion: true
reclaimPolicy: Retain
parameters:
csi.storage.k8s.io/provisioner-secret-name: ${pvc.name}-juicefs
csi.storage.k8s.io/provisioner-secret-namespace: ${pvc.namespace}
csi.storage.k8s.io/node-publish-secret-name: ${pvc.name}-juicefs
csi.storage.k8s.io/node-publish-secret-namespace: ${pvc.namespace}
csi.storage.k8s.io/provisioner-secret-name: ${pv.name}.juicefs.libre.sh
csi.storage.k8s.io/provisioner-secret-namespace: libresh-juicefs
csi.storage.k8s.io/node-publish-secret-name: ${pv.name}.juicefs.libre.sh
csi.storage.k8s.io/node-publish-secret-namespace: libresh-juicefs
juicefs/mount-delete-delay: 5m

View file

@ -19,8 +19,8 @@ package k8s
import (
"context"
"fmt"
"time"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/runtime/conditions"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
@ -69,68 +69,85 @@ func (r *PersistentVolumeClaimReconciler) Reconcile(ctx context.Context, req ctr
log.Info("Reconciling")
labelOpts := lshr.LabelOpts{
Component: "juicefs",
name := "pvc-" + string(pvc.UID)
ns := "libresh-juicefs"
var pv *corev1.PersistentVolume
if pvc.Spec.VolumeName != "" {
pv = &corev1.PersistentVolume{}
pv.Name = name
err = r.Get(ctx, client.ObjectKeyFromObject(pv), pv)
if err != nil {
return ctrl.Result{}, err
}
}
var postgres lshcore.Postgres
lshr.SetResourceNamespacedName(&pvc, &postgres)
err = lshr.CreateOrPatch(ctx, r, &postgres, func() error {
lshr.ApplyLabels(&pvc, &postgres, &labelOpts)
postgres.Spec.Database = "juicefs"
return controllerutil.SetControllerReference(&pvc, &postgres, r.Scheme())
var redis lshcore.Redis
redis.Name = name
redis.Namespace = ns
err = lshr.CreateOrPatch(ctx, r, &redis, func() error {
redis.Spec.Persistence.Enabled = true
if pv != nil {
return controllerutil.SetOwnerReference(pv, &redis, r.Scheme())
}
return nil
})
if err != nil {
return ctrl.Result{}, err
}
var bucket lshcore.Bucket
lshr.SetResourceNamespacedName(&pvc, &bucket)
bucket.Name = name
bucket.Namespace = ns
err = lshr.CreateOrPatch(ctx, r, &bucket, func() error {
lshr.ApplyLabels(&pvc, &bucket, &labelOpts)
bucket.Spec.Provider = lshcore.BucketDataProvider
return controllerutil.SetControllerReference(&pvc, &bucket, r.Scheme())
if pv != nil {
return controllerutil.SetOwnerReference(pv, &bucket, r.Scheme())
}
return nil
})
if err != nil {
return ctrl.Result{}, err
}
if !conditions.IsReady(&postgres) || !conditions.IsReady(&bucket) {
if !conditions.IsReady(&redis) || !conditions.IsReady(&bucket) {
log.Info("Waiting for dependencies to be ready")
return ctrl.Result{}, err
return ctrl.Result{RequeueAfter: 5 * time.Second}, err
}
var bucketSecret corev1.Secret
err = r.Get(ctx, types.NamespacedName{
Namespace: pvc.GetNamespace(),
Namespace: ns,
Name: bucket.SecretName(),
}, &bucketSecret)
if err != nil {
return ctrl.Result{}, err
}
var postgresSecret corev1.Secret
var redisSecret corev1.Secret
err = r.Get(ctx, types.NamespacedName{
Namespace: pvc.GetNamespace(),
Name: postgres.SecretName(),
}, &postgresSecret)
Namespace: ns,
Name: redis.SecretName(),
}, &redisSecret)
if err != nil {
return ctrl.Result{}, err
}
var secret corev1.Secret
secret.Namespace = pvc.Namespace
secret.Name = fmt.Sprintf("%s-juicefs", pvc.Name)
secret.Name = name + ".juicefs.libre.sh"
secret.Namespace = ns
err = lshr.CreateOrPatch(ctx, r, &secret, func() error {
lshr.ApplyLabels(&pvc, &secret, &labelOpts)
secret.StringData = map[string]string{
"name": "juicefs",
"metaurl": string(postgresSecret.Data["url"]),
"metaurl": string(redisSecret.Data["url"]),
"storage": "minio",
"bucket": fmt.Sprintf("%s/%s", bucketSecret.Data["url"], bucketSecret.Data[lshmeta.SecretPathKey]),
"access-key": string(bucketSecret.Data[lshmeta.SecretUsernameKey]),
"secret-key": string(bucketSecret.Data[lshmeta.SecretPasswordKey]),
}
return controllerutil.SetOwnerReference(&pvc, &secret, r.Scheme())
if pv != nil {
return controllerutil.SetOwnerReference(pv, &secret, r.Scheme())
}
return nil
})
if err != nil {
return ctrl.Result{}, err
@ -138,16 +155,13 @@ func (r *PersistentVolumeClaimReconciler) Reconcile(ctx context.Context, req ctr
log.Info("Reconciled")
return ctrl.Result{}, nil
return ctrl.Result{RequeueAfter: 1 * time.Minute}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *PersistentVolumeClaimReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.PersistentVolumeClaim{}).
Owns(&lshcore.Postgres{}).
Owns(&lshcore.Bucket{}).
Owns(&corev1.Secret{}).
Complete(r)
}
@ -156,10 +170,5 @@ func (r *PersistentVolumeClaimReconciler) Name() string {
}
func (r *PersistentVolumeClaimReconciler) OwnedConditions() []string {
return []string{
meta.ReconcilingCondition,
meta.StalledCondition,
// lshmeta.DependenciesNotReady,
// lshmeta.HookJobNotComleted,
}
return []string{}
}