2020-08-06 04:11:40 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
|
|
|
|
|
|
seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (r *SeaweedReconciler) ensureFilerServers(seaweedCR *seaweedv1.Seaweed) (done bool, result ctrl.Result, err error) {
|
|
|
|
_ = context.Background()
|
|
|
|
_ = r.Log.WithValues("seaweed", seaweedCR.Name)
|
|
|
|
|
2020-10-17 20:51:07 +00:00
|
|
|
if done, result, err = r.ensureFilerHeadlessService(seaweedCR); done {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, result, err = r.ensureFilerNodePortService(seaweedCR); done {
|
2020-10-15 04:45:53 +00:00
|
|
|
return
|
2020-08-06 04:11:40 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
ctx := context.Background()
|
|
|
|
log := r.Log.WithValues("sw-filer-statefulset", seaweedCR.Name)
|
|
|
|
|
|
|
|
filerStatefulSet := &appsv1.StatefulSet{}
|
|
|
|
err := r.Get(ctx, types.NamespacedName{Name: seaweedCR.Name + "-filer", Namespace: seaweedCR.Namespace}, filerStatefulSet)
|
|
|
|
if err != nil && errors.IsNotFound(err) {
|
|
|
|
// Define a new deployment
|
|
|
|
dep := r.createFilerStatefulSet(seaweedCR)
|
|
|
|
log.Info("Creating a new filer statefulset", "Namespace", dep.Namespace, "Name", dep.Name)
|
|
|
|
err = r.Create(ctx, dep)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "Failed to create new filer statefulset", "Namespace", dep.Namespace, "Name", dep.Name)
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-06 04:11:40 +00:00
|
|
|
}
|
|
|
|
// Deployment created successfully - return and requeue
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-06 04:11:40 +00:00
|
|
|
} else if err != nil {
|
|
|
|
log.Error(err, "Failed to get filer statefulset")
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-06 04:11:40 +00:00
|
|
|
}
|
2020-10-17 06:25:46 +00:00
|
|
|
|
2020-10-17 09:25:23 +00:00
|
|
|
if *filerStatefulSet.Spec.Replicas != seaweedCR.Spec.FilerCount ||
|
|
|
|
filerStatefulSet.Spec.Template.Spec.Containers[0].Image != seaweedCR.Spec.Image {
|
2020-10-17 06:25:46 +00:00
|
|
|
filerStatefulSet.Spec.Replicas = &seaweedCR.Spec.FilerCount
|
2020-10-17 09:25:23 +00:00
|
|
|
filerStatefulSet.Spec.Template.Spec.Containers[0].Image = seaweedCR.Spec.Image
|
2020-10-17 06:25:46 +00:00
|
|
|
if err = r.Update(ctx, filerStatefulSet); err != nil {
|
|
|
|
log.Error(err, "Failed to update filer statefulset", "Namespace", filerStatefulSet.Namespace, "Name", filerStatefulSet.Name)
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
|
|
|
// Deployment created successfully - return and requeue
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
|
|
|
|
2020-08-06 04:11:40 +00:00
|
|
|
log.Info("Get 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-17 20:51:07 +00:00
|
|
|
func (r *SeaweedReconciler) ensureFilerHeadlessService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
2020-10-17 21:07:06 +00:00
|
|
|
return r.ensureService(seaweedCR, "filer-headless", r.createFilerHeadlessService)
|
2020-10-17 20:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SeaweedReconciler) ensureFilerNodePortService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
2020-10-17 21:07:06 +00:00
|
|
|
return r.ensureService(seaweedCR, "filer", r.createFilerNodePortService)
|
2020-10-17 20:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func labelsForFiler(name string) map[string]string {
|
|
|
|
return map[string]string{"app": "seaweedfs", "role": "filer", "name": name}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateServiceFunc func(m *seaweedv1.Seaweed) *corev1.Service
|
|
|
|
|
|
|
|
func (r *SeaweedReconciler) ensureService(seaweedCR *seaweedv1.Seaweed, nameSuffix string, serviceFunc CreateServiceFunc) (bool, ctrl.Result, error) {
|
2020-08-06 04:11:40 +00:00
|
|
|
ctx := context.Background()
|
2020-10-17 20:51:07 +00:00
|
|
|
log := r.Log.WithValues("sw", seaweedCR.Name, "service", nameSuffix)
|
2020-08-06 04:11:40 +00:00
|
|
|
|
2020-10-17 20:51:07 +00:00
|
|
|
aService := &corev1.Service{}
|
2020-10-17 21:07:06 +00:00
|
|
|
err := r.Get(ctx, types.NamespacedName{Name: seaweedCR.Name + "-" + nameSuffix, Namespace: seaweedCR.Namespace}, aService)
|
2020-08-06 04:11:40 +00:00
|
|
|
if err != nil && errors.IsNotFound(err) {
|
|
|
|
// Define a new deployment
|
2020-10-17 20:51:07 +00:00
|
|
|
dep := serviceFunc(seaweedCR)
|
|
|
|
log.Info("Creating a new service", "Namespace", dep.Namespace, "Name", dep.Name)
|
2020-08-06 04:11:40 +00:00
|
|
|
err = r.Create(ctx, dep)
|
|
|
|
if err != nil {
|
2020-10-17 20:51:07 +00:00
|
|
|
log.Error(err, "Failed to create service", "Namespace", dep.Namespace, "Name", dep.Name)
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-06 04:11:40 +00:00
|
|
|
}
|
|
|
|
// Deployment created successfully - return and requeue
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-06 04:11:40 +00:00
|
|
|
} else if err != nil {
|
2020-10-17 20:51:07 +00:00
|
|
|
log.Error(err, "Failed to get server service")
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-06 04:11:40 +00:00
|
|
|
}
|
2020-10-17 20:51:07 +00:00
|
|
|
log.Info("Get service " + aService.Name)
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-06 04:11:40 +00:00
|
|
|
}
|