2020-08-06 04:11:40 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-28 06:57:38 +00:00
|
|
|
|
2020-10-18 07:12:33 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2020-08-06 04:11:40 +00:00
|
|
|
|
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
2020-10-29 14:31:06 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
2020-08-06 04:11:40 +00:00
|
|
|
|
|
|
|
seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1"
|
2020-11-01 06:43:45 +00:00
|
|
|
label "github.com/seaweedfs/seaweedfs-operator/controllers/label"
|
2020-08-06 04:11:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r *SeaweedReconciler) ensureFilerServers(seaweedCR *seaweedv1.Seaweed) (done bool, result ctrl.Result, err error) {
|
|
|
|
_ = context.Background()
|
|
|
|
_ = r.Log.WithValues("seaweed", seaweedCR.Name)
|
|
|
|
|
2020-10-29 08:57:16 +00:00
|
|
|
if done, result, err = r.ensureFilerPeerService(seaweedCR); done {
|
2020-10-17 20:51:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-28 06:57:38 +00:00
|
|
|
if done, result, err = r.ensureFilerService(seaweedCR); done {
|
2020-10-15 04:45:53 +00:00
|
|
|
return
|
2020-08-06 04:11:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 11:29:11 +00:00
|
|
|
if done, result, err = r.ensureFilerConfigMap(seaweedCR); done {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-17 07:59:38 +00:00
|
|
|
if done, result, err = r.ensureFilerStatefulSet(seaweedCR); done {
|
2020-10-15 04:45:53 +00:00
|
|
|
return
|
2020-08-06 04:11:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 04:45:53 +00:00
|
|
|
return
|
2020-08-06 04:11:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SeaweedReconciler) ensureFilerStatefulSet(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
2020-10-18 08:59:38 +00:00
|
|
|
log := r.Log.WithValues("sw-filer-statefulset", seaweedCR.Name)
|
|
|
|
|
2020-10-18 07:12:33 +00:00
|
|
|
filerStatefulSet := r.createFilerStatefulSet(seaweedCR)
|
2020-10-29 14:31:06 +00:00
|
|
|
if err := controllerutil.SetControllerReference(seaweedCR, filerStatefulSet, r.Scheme); err != nil {
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
2020-10-18 08:59:38 +00:00
|
|
|
_, err := r.CreateOrUpdate(filerStatefulSet, func(existing, desired runtime.Object) error {
|
2020-10-18 07:12:33 +00:00
|
|
|
existingStatefulSet := existing.(*appsv1.StatefulSet)
|
|
|
|
desiredStatefulSet := desired.(*appsv1.StatefulSet)
|
|
|
|
|
|
|
|
existingStatefulSet.Spec.Replicas = desiredStatefulSet.Spec.Replicas
|
2020-11-05 03:01:19 +00:00
|
|
|
existingStatefulSet.Spec.Template.Spec = desiredStatefulSet.Spec.Template.Spec
|
2020-10-18 07:12:33 +00:00
|
|
|
return nil
|
|
|
|
})
|
2020-10-18 08:59:38 +00:00
|
|
|
log.Info("ensure filer stateful set " + filerStatefulSet.Name)
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-06 04:11:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 08:57:16 +00:00
|
|
|
func (r *SeaweedReconciler) ensureFilerPeerService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
2020-10-18 08:59:38 +00:00
|
|
|
|
2020-10-29 08:57:16 +00:00
|
|
|
log := r.Log.WithValues("sw-filer-peer-service", seaweedCR.Name)
|
2020-10-18 08:59:38 +00:00
|
|
|
|
2020-10-29 08:57:16 +00:00
|
|
|
filerPeerService := r.createFilerPeerService(seaweedCR)
|
2020-10-30 02:40:34 +00:00
|
|
|
if err := controllerutil.SetControllerReference(seaweedCR, filerPeerService, r.Scheme); err != nil {
|
2020-10-29 14:31:06 +00:00
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
2020-10-18 08:59:38 +00:00
|
|
|
|
2020-10-30 02:40:34 +00:00
|
|
|
_, err := r.CreateOrUpdateService(filerPeerService)
|
2020-10-29 08:57:16 +00:00
|
|
|
log.Info("ensure filer peer service " + filerPeerService.Name)
|
2020-10-18 08:59:38 +00:00
|
|
|
|
|
|
|
return ReconcileResult(err)
|
2020-10-17 20:51:07 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 06:57:38 +00:00
|
|
|
func (r *SeaweedReconciler) ensureFilerService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
2020-10-18 08:59:38 +00:00
|
|
|
|
|
|
|
log := r.Log.WithValues("sw-filer-service", seaweedCR.Name)
|
|
|
|
|
2020-10-28 06:57:38 +00:00
|
|
|
filerService := r.createFilerService(seaweedCR)
|
2020-10-29 14:31:06 +00:00
|
|
|
if err := controllerutil.SetControllerReference(seaweedCR, filerService, r.Scheme); err != nil {
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
2020-10-18 08:59:38 +00:00
|
|
|
_, err := r.CreateOrUpdateService(filerService)
|
|
|
|
|
|
|
|
log.Info("ensure filer service " + filerService.Name)
|
|
|
|
|
|
|
|
return ReconcileResult(err)
|
2020-10-17 20:51:07 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 11:29:11 +00:00
|
|
|
func (r *SeaweedReconciler) ensureFilerConfigMap(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
|
|
|
log := r.Log.WithValues("sw-filer-configmap", seaweedCR.Name)
|
|
|
|
|
|
|
|
filerConfigMap := r.createFilerConfigMap(seaweedCR)
|
2020-10-30 02:42:34 +00:00
|
|
|
if err := controllerutil.SetControllerReference(seaweedCR, filerConfigMap, r.Scheme); err != nil {
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
2020-10-28 11:29:11 +00:00
|
|
|
_, err := r.CreateOrUpdateConfigMap(filerConfigMap)
|
|
|
|
|
|
|
|
log.Info("Get filer ConfigMap " + filerConfigMap.Name)
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
2020-10-17 20:51:07 +00:00
|
|
|
|
|
|
|
func labelsForFiler(name string) map[string]string {
|
2020-11-01 06:43:45 +00:00
|
|
|
return map[string]string{
|
2020-11-01 06:46:34 +00:00
|
|
|
label.ManagedByLabelKey: "seaweedfs-operator",
|
2020-11-01 06:43:45 +00:00
|
|
|
label.NameLabelKey: "seaweedfs",
|
|
|
|
label.ComponentLabelKey: "filer",
|
|
|
|
label.InstanceLabelKey: name,
|
|
|
|
}
|
2020-10-17 20:51:07 +00:00
|
|
|
}
|