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: metadata:
name: juicefs.libre.sh name: juicefs.libre.sh
provisioner: csi.juicefs.com provisioner: csi.juicefs.com
allowVolumeExpansion: true
reclaimPolicy: Retain
parameters: parameters:
csi.storage.k8s.io/provisioner-secret-name: ${pvc.name}-juicefs csi.storage.k8s.io/provisioner-secret-name: ${pv.name}.juicefs.libre.sh
csi.storage.k8s.io/provisioner-secret-namespace: ${pvc.namespace} csi.storage.k8s.io/provisioner-secret-namespace: libresh-juicefs
csi.storage.k8s.io/node-publish-secret-name: ${pvc.name}-juicefs csi.storage.k8s.io/node-publish-secret-name: ${pv.name}.juicefs.libre.sh
csi.storage.k8s.io/node-publish-secret-namespace: ${pvc.namespace} csi.storage.k8s.io/node-publish-secret-namespace: libresh-juicefs
juicefs/mount-delete-delay: 5m juicefs/mount-delete-delay: 5m

View file

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