2020-08-02 06:27:46 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-03 05:48:15 +00:00
|
|
|
"time"
|
2020-08-02 06:27:46 +00:00
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
MasterClusterSize = 3
|
|
|
|
)
|
|
|
|
|
|
|
|
func (r *SeaweedReconciler) ensureMaster(seaweedCR *seaweedv1.Seaweed) (done bool, result ctrl.Result, err error) {
|
|
|
|
_ = context.Background()
|
|
|
|
_ = r.Log.WithValues("seaweed", seaweedCR.Name)
|
|
|
|
|
2020-08-02 23:52:05 +00:00
|
|
|
if done, result, err = r.ensureMasterService(seaweedCR); done {
|
2020-08-02 06:27:46 +00:00
|
|
|
return done, result, err
|
|
|
|
}
|
|
|
|
|
2020-08-02 23:52:05 +00:00
|
|
|
if done, result, err = r.ensureMasterStatefulSet(seaweedCR); done {
|
2020-08-02 23:12:52 +00:00
|
|
|
return done, result, err
|
2020-08-02 06:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false, ctrl.Result{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SeaweedReconciler) ensureMasterStatefulSet(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
log := r.Log.WithValues("sw-master-statefulset", seaweedCR.Name)
|
|
|
|
|
|
|
|
masterStatefulSet := &appsv1.StatefulSet{}
|
2020-08-03 05:48:15 +00:00
|
|
|
err := r.Get(ctx, types.NamespacedName{Name: seaweedCR.Name + "-master", Namespace: seaweedCR.Namespace}, masterStatefulSet)
|
2020-08-02 06:27:46 +00:00
|
|
|
if err != nil && errors.IsNotFound(err) {
|
|
|
|
// Define a new deployment
|
|
|
|
dep := r.createMasterStatefulSet(seaweedCR)
|
|
|
|
log.Info("Creating a new master statefulset", "Namespace", dep.Namespace, "Name", dep.Name)
|
|
|
|
err = r.Create(ctx, dep)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "Failed to create new statefulset", "Namespace", dep.Namespace, "Name", dep.Name)
|
|
|
|
return true, ctrl.Result{}, err
|
|
|
|
}
|
2020-08-03 05:48:15 +00:00
|
|
|
// sleep 60 seconds for DNS to have pod IP addresses ready
|
|
|
|
time.Sleep(time.Minute)
|
2020-08-02 06:27:46 +00:00
|
|
|
// Deployment created successfully - return and requeue
|
2020-08-02 23:12:52 +00:00
|
|
|
return false, ctrl.Result{}, nil
|
2020-08-02 06:27:46 +00:00
|
|
|
} else if err != nil {
|
|
|
|
log.Error(err, "Failed to get Deployment")
|
|
|
|
return true, ctrl.Result{}, err
|
|
|
|
}
|
2020-08-03 05:48:15 +00:00
|
|
|
log.Info("Get master stateful set " + masterStatefulSet.Name)
|
2020-08-02 06:27:46 +00:00
|
|
|
return false, ctrl.Result{}, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
ctx := context.Background()
|
|
|
|
log := r.Log.WithValues("sw-master-service", seaweedCR.Name)
|
|
|
|
|
|
|
|
masterService := &corev1.Service{}
|
2020-08-03 05:48:15 +00:00
|
|
|
err := r.Get(ctx, types.NamespacedName{Name: seaweedCR.Name + "-master", Namespace: seaweedCR.Namespace}, masterService)
|
2020-08-02 06:27:46 +00:00
|
|
|
if err != nil && errors.IsNotFound(err) {
|
|
|
|
// Define a new deployment
|
2020-08-02 23:12:52 +00:00
|
|
|
dep := r.createMasterService(seaweedCR)
|
2020-08-03 05:48:15 +00:00
|
|
|
log.Info("Creating a new master service", "Namespace", dep.Namespace, "Name", dep.Name)
|
2020-08-02 06:27:46 +00:00
|
|
|
err = r.Create(ctx, dep)
|
|
|
|
if err != nil {
|
2020-08-03 05:48:15 +00:00
|
|
|
log.Error(err, "Failed to create master service", "Namespace", dep.Namespace, "Name", dep.Name)
|
2020-08-02 06:27:46 +00:00
|
|
|
return true, ctrl.Result{}, err
|
|
|
|
}
|
|
|
|
// Deployment created successfully - return and requeue
|
2020-08-02 23:12:52 +00:00
|
|
|
return false, ctrl.Result{}, nil
|
2020-08-02 06:27:46 +00:00
|
|
|
} else if err != nil {
|
2020-08-03 05:48:15 +00:00
|
|
|
log.Error(err, "Failed to get master service", "Namespace", seaweedCR.Namespace, "Name", seaweedCR.Name+"-master")
|
2020-08-02 06:27:46 +00:00
|
|
|
return true, ctrl.Result{}, err
|
|
|
|
}
|
|
|
|
log.Info("Get master service " + masterService.Name)
|
|
|
|
return false, ctrl.Result{}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func labelsForMaster(name string) map[string]string {
|
|
|
|
return map[string]string{"app": "seaweedfs", "role": "master", "name": name}
|
|
|
|
}
|