2020-08-02 06:27:46 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-09 01:41:12 +00:00
|
|
|
"time"
|
2020-08-02 06:27:46 +00:00
|
|
|
|
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
2020-11-09 01:41:12 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2020-11-05 03:00:34 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2020-08-02 06:27:46 +00:00
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
2020-11-09 01:41:12 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
2020-10-29 14:31:06 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
2020-08-02 06:27:46 +00:00
|
|
|
|
|
|
|
seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1"
|
2020-11-09 01:41:12 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs-operator/controllers/label"
|
2020-08-02 06:27:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r *SeaweedReconciler) ensureMaster(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.ensureMasterPeerService(seaweedCR); done {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-02 23:52:05 +00:00
|
|
|
if done, result, err = r.ensureMasterService(seaweedCR); done {
|
2020-10-15 04:45:53 +00:00
|
|
|
return
|
2020-08-02 06:27:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 11:29:11 +00:00
|
|
|
if done, result, err = r.ensureMasterConfigMap(seaweedCR); done {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-02 23:52:05 +00:00
|
|
|
if done, result, err = r.ensureMasterStatefulSet(seaweedCR); done {
|
2020-10-15 04:45:53 +00:00
|
|
|
return
|
2020-08-02 06:27:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 07:02:09 +00:00
|
|
|
if !*seaweedCR.Spec.Master.ConcurrentStart {
|
|
|
|
if done, result, err = r.waitForMasterStatefulSet(seaweedCR); done {
|
|
|
|
return
|
|
|
|
}
|
2020-11-09 01:41:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 04:45:53 +00:00
|
|
|
return
|
2020-08-02 06:27:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 01:41:12 +00:00
|
|
|
func (r *SeaweedReconciler) waitForMasterStatefulSet(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
|
|
|
log := r.Log.WithValues("sw-master-statefulset", seaweedCR.Name)
|
|
|
|
|
|
|
|
podList := &corev1.PodList{}
|
|
|
|
listOpts := []client.ListOption{
|
|
|
|
client.InNamespace(seaweedCR.Namespace),
|
|
|
|
client.MatchingLabels(labelsForMaster(seaweedCR.Name)),
|
|
|
|
}
|
|
|
|
if err := r.List(context.Background(), podList, listOpts...); err != nil {
|
|
|
|
log.Error(err, "Failed to list master pods", "namespace", seaweedCR.Namespace, "name", seaweedCR.Name)
|
|
|
|
return true, ctrl.Result{RequeueAfter: 3 * time.Second}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("pods", "count", len(podList.Items))
|
|
|
|
runningCounter := 0
|
|
|
|
for _, pod := range podList.Items {
|
|
|
|
if pod.Status.Phase == corev1.PodRunning {
|
2020-11-09 02:20:33 +00:00
|
|
|
for _, containerStatus := range pod.Status.ContainerStatuses {
|
|
|
|
if containerStatus.Image == seaweedCR.Spec.Image {
|
|
|
|
runningCounter++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-11-09 01:41:12 +00:00
|
|
|
} else {
|
|
|
|
log.Info("pod", "name", pod.Name, "status", pod.Status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 06:34:04 +00:00
|
|
|
if runningCounter < int(seaweedCR.Spec.Master.Replicas)/2+1 {
|
|
|
|
log.Info("some masters are not ready", "missing", int(seaweedCR.Spec.Master.Replicas)-runningCounter)
|
2020-11-09 01:41:12 +00:00
|
|
|
return true, ctrl.Result{RequeueAfter: 3 * time.Second}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("masters are ready")
|
|
|
|
return ReconcileResult(nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-02 06:27:46 +00:00
|
|
|
func (r *SeaweedReconciler) ensureMasterStatefulSet(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
|
|
|
log := r.Log.WithValues("sw-master-statefulset", seaweedCR.Name)
|
|
|
|
|
2020-11-05 03:00:34 +00:00
|
|
|
masterStatefulSet := r.createMasterStatefulSet(seaweedCR)
|
|
|
|
if err := controllerutil.SetControllerReference(seaweedCR, masterStatefulSet, r.Scheme); err != nil {
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-02 06:27:46 +00:00
|
|
|
}
|
2020-11-05 03:00:34 +00:00
|
|
|
_, err := r.CreateOrUpdate(masterStatefulSet, func(existing, desired runtime.Object) error {
|
|
|
|
existingStatefulSet := existing.(*appsv1.StatefulSet)
|
|
|
|
desiredStatefulSet := desired.(*appsv1.StatefulSet)
|
|
|
|
|
|
|
|
existingStatefulSet.Spec.Template.Spec = desiredStatefulSet.Spec.Template.Spec
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
log.Info("ensure master stateful set " + masterStatefulSet.Name)
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-02 06:27:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 11:29:11 +00:00
|
|
|
func (r *SeaweedReconciler) ensureMasterConfigMap(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
|
|
|
log := r.Log.WithValues("sw-master-configmap", seaweedCR.Name)
|
|
|
|
|
|
|
|
masterConfigMap := r.createMasterConfigMap(seaweedCR)
|
2020-10-30 02:42:34 +00:00
|
|
|
if err := controllerutil.SetControllerReference(seaweedCR, masterConfigMap, r.Scheme); err != nil {
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
2020-10-28 11:29:11 +00:00
|
|
|
_, err := r.CreateOrUpdateConfigMap(masterConfigMap)
|
|
|
|
|
|
|
|
log.Info("Get master ConfigMap " + masterConfigMap.Name)
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
|
|
|
|
2020-08-02 23:12:52 +00:00
|
|
|
func (r *SeaweedReconciler) ensureMasterService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
2020-08-02 06:27:46 +00:00
|
|
|
log := r.Log.WithValues("sw-master-service", seaweedCR.Name)
|
|
|
|
|
2020-10-18 08:59:38 +00:00
|
|
|
masterService := r.createMasterService(seaweedCR)
|
2020-10-29 14:31:06 +00:00
|
|
|
if err := controllerutil.SetControllerReference(seaweedCR, masterService, r.Scheme); err != nil {
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
2020-10-18 08:59:38 +00:00
|
|
|
_, err := r.CreateOrUpdateService(masterService)
|
|
|
|
|
2020-08-02 06:27:46 +00:00
|
|
|
log.Info("Get master service " + masterService.Name)
|
2020-10-15 04:45:53 +00:00
|
|
|
return ReconcileResult(err)
|
2020-08-02 06:27:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 08:57:16 +00:00
|
|
|
func (r *SeaweedReconciler) ensureMasterPeerService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
|
|
|
log := r.Log.WithValues("sw-master-peer-service", seaweedCR.Name)
|
|
|
|
|
|
|
|
masterPeerService := r.createMasterPeerService(seaweedCR)
|
2020-10-30 02:42:34 +00:00
|
|
|
if err := controllerutil.SetControllerReference(seaweedCR, masterPeerService, r.Scheme); err != nil {
|
|
|
|
return ReconcileResult(err)
|
|
|
|
}
|
2020-10-29 08:57:16 +00:00
|
|
|
_, err := r.CreateOrUpdateService(masterPeerService)
|
|
|
|
|
|
|
|
log.Info("Get master peer service " + masterPeerService.Name)
|
|
|
|
return ReconcileResult(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-02 06:27:46 +00:00
|
|
|
func labelsForMaster(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: "master",
|
|
|
|
label.InstanceLabelKey: name,
|
|
|
|
}
|
2020-08-02 06:27:46 +00:00
|
|
|
}
|