2020-08-05 05:13:36 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-29 08:57:16 +00:00
|
|
|
|
2020-10-18 08:59:38 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2020-08-05 05:13:36 +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-05 05:13:36 +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-05 05:13:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r *SeaweedReconciler) ensureVolumeServers(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.ensureVolumeServerPeerService(seaweedCR); done {
|
2020-10-15 04:45:53 +00:00
|
|
|
return
|
2020-08-05 05:13:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 07:14:24 +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-29 08:57:16 +00:00
|
|
|
if done, result, err = r.ensureVolumeServerStatefulSet(seaweedCR); done {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
log := r.Log.WithValues("sw-volume-statefulset", seaweedCR.Name)
|
|
|
|
|
2020-10-18 08:59:38 +00:00
|
|
|
volumeServerStatefulSet := r.createVolumeServerStatefulSet(seaweedCR)
|
2020-10-29 14:31:06 +00:00
|
|
|
if err := controllerutil.SetControllerReference(seaweedCR, volumeServerStatefulSet, r.Scheme); err != nil {
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
2020-10-18 08:59:38 +00:00
|
|
|
_, err := r.CreateOrUpdate(volumeServerStatefulSet, func(existing, desired runtime.Object) error {
|
|
|
|
existingStatefulSet := existing.(*appsv1.StatefulSet)
|
|
|
|
desiredStatefulSet := desired.(*appsv1.StatefulSet)
|
2020-10-17 06:25:46 +00:00
|
|
|
|
2020-10-18 08:59:38 +00:00
|
|
|
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 08:59:38 +00:00
|
|
|
return nil
|
|
|
|
})
|
2020-10-17 06:25:46 +00:00
|
|
|
|
2020-10-18 08:59:38 +00:00
|
|
|
log.Info("ensure volume stateful set " + volumeServerStatefulSet.Name)
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-05 05:13:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 08:57:16 +00:00
|
|
|
func (r *SeaweedReconciler) ensureVolumeServerPeerService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
|
|
|
|
|
|
|
log := r.Log.WithValues("sw-volume-peer-service", seaweedCR.Name)
|
|
|
|
|
|
|
|
volumeServerPeerService := r.createVolumeServerPeerService(seaweedCR)
|
2020-10-30 02:42:34 +00:00
|
|
|
if err := controllerutil.SetControllerReference(seaweedCR, volumeServerPeerService, r.Scheme); err != nil {
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
2020-10-29 08:57:16 +00:00
|
|
|
_, err := r.CreateOrUpdateService(volumeServerPeerService)
|
|
|
|
|
|
|
|
log.Info("ensure volume peer service " + volumeServerPeerService.Name)
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
|
|
|
|
2020-08-05 05:13:36 +00:00
|
|
|
func (r *SeaweedReconciler) ensureVolumeServerService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
2020-10-18 08:59:38 +00:00
|
|
|
|
2020-08-05 05:13:36 +00:00
|
|
|
log := r.Log.WithValues("sw-volume-service", seaweedCR.Name)
|
|
|
|
|
2020-10-18 08:59:38 +00:00
|
|
|
volumeServerService := r.createVolumeServerService(seaweedCR)
|
2020-10-29 14:31:06 +00:00
|
|
|
if err := controllerutil.SetControllerReference(seaweedCR, volumeServerService, r.Scheme); err != nil {
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
2020-10-18 08:59:38 +00:00
|
|
|
_, err := r.CreateOrUpdateService(volumeServerService)
|
|
|
|
|
|
|
|
log.Info("ensure 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 {
|
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: "volume",
|
|
|
|
label.InstanceLabelKey: name,
|
|
|
|
}
|
2020-08-05 05:13:36 +00:00
|
|
|
}
|