2020-08-05 05:13:36 +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) ensureVolumeServers(seaweedCR *seaweedv1.Seaweed) (done bool, result ctrl.Result, err error) {
|
|
|
|
_ = context.Background()
|
|
|
|
_ = r.Log.WithValues("seaweed", seaweedCR.Name)
|
|
|
|
|
|
|
|
if done, result, err = r.ensureVolumeServerStatefulSet(seaweedCR); done {
|
2020-10-15 04:45:53 +00:00
|
|
|
return
|
2020-08-05 05:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if done, result, err = r.ensureVolumeServerService(seaweedCR); done {
|
2020-10-15 04:45:53 +00:00
|
|
|
return
|
2020-08-05 05:13:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 04:45:53 +00:00
|
|
|
return
|
2020-08-05 05:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SeaweedReconciler) ensureVolumeServerStatefulSet(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
log := r.Log.WithValues("sw-volume-statefulset", seaweedCR.Name)
|
|
|
|
|
|
|
|
volumeServerStatefulSet := &appsv1.StatefulSet{}
|
|
|
|
err := r.Get(ctx, types.NamespacedName{Name: seaweedCR.Name + "-volume", Namespace: seaweedCR.Namespace}, volumeServerStatefulSet)
|
|
|
|
if err != nil && errors.IsNotFound(err) {
|
|
|
|
// Define a new deployment
|
|
|
|
dep := r.createVolumeServerStatefulSet(seaweedCR)
|
|
|
|
log.Info("Creating a new volume statefulset", "Namespace", dep.Namespace, "Name", dep.Name)
|
|
|
|
err = r.Create(ctx, dep)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "Failed to create new volume statefulset", "Namespace", dep.Namespace, "Name", dep.Name)
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-05 05:13:36 +00:00
|
|
|
}
|
|
|
|
// Deployment created successfully - return and requeue
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-05 05:13:36 +00:00
|
|
|
} else if err != nil {
|
|
|
|
log.Error(err, "Failed to get volume server statefulset")
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-05 05:13:36 +00:00
|
|
|
}
|
2020-10-17 06:25:46 +00:00
|
|
|
|
2020-10-17 09:25:23 +00:00
|
|
|
if *volumeServerStatefulSet.Spec.Replicas != seaweedCR.Spec.VolumeServerCount ||
|
|
|
|
volumeServerStatefulSet.Spec.Template.Spec.Containers[0].Image != seaweedCR.Spec.Image {
|
2020-10-17 06:25:46 +00:00
|
|
|
volumeServerStatefulSet.Spec.Replicas = &seaweedCR.Spec.VolumeServerCount
|
2020-10-17 09:25:23 +00:00
|
|
|
volumeServerStatefulSet.Spec.Template.Spec.Containers[0].Image = seaweedCR.Spec.Image
|
2020-10-17 06:25:46 +00:00
|
|
|
if err = r.Update(ctx, volumeServerStatefulSet); err != nil {
|
|
|
|
log.Error(err, "Failed to update volume statefulset", "Namespace", volumeServerStatefulSet.Namespace, "Name", volumeServerStatefulSet.Name)
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
|
|
|
// Deployment created successfully - return and requeue
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
|
|
|
|
2020-08-05 05:13:36 +00:00
|
|
|
log.Info("Get volume stateful set " + volumeServerStatefulSet.Name)
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-05 05:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SeaweedReconciler) ensureVolumeServerService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
log := r.Log.WithValues("sw-volume-service", seaweedCR.Name)
|
|
|
|
|
|
|
|
volumeServerService := &corev1.Service{}
|
|
|
|
err := r.Get(ctx, types.NamespacedName{Name: seaweedCR.Name + "-volume", Namespace: seaweedCR.Namespace}, volumeServerService)
|
|
|
|
if err != nil && errors.IsNotFound(err) {
|
|
|
|
// Define a new deployment
|
|
|
|
dep := r.createVolumeServerService(seaweedCR)
|
|
|
|
log.Info("Creating a new volume service", "Namespace", dep.Namespace, "Name", dep.Name)
|
|
|
|
err = r.Create(ctx, dep)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "Failed to create new volume service", "Namespace", dep.Namespace, "Name", dep.Name)
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-05 05:13:36 +00:00
|
|
|
}
|
|
|
|
// Deployment created successfully - return and requeue
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-05 05:13:36 +00:00
|
|
|
} else if err != nil {
|
|
|
|
log.Error(err, "Failed to get volume server service")
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-05 05:13:36 +00:00
|
|
|
}
|
|
|
|
log.Info("Get volume service " + volumeServerService.Name)
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-05 05:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func labelsForVolumeServer(name string) map[string]string {
|
|
|
|
return map[string]string{"app": "seaweedfs", "role": "volume", "name": name}
|
|
|
|
}
|