2020-08-02 06:27:46 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-10-28 11:19:17 +00:00
|
|
|
"strings"
|
2020-08-02 06:27:46 +00:00
|
|
|
|
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-10-19 05:49:11 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
2020-08-02 06:27:46 +00:00
|
|
|
|
|
|
|
seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1"
|
|
|
|
)
|
|
|
|
|
2020-10-28 11:19:17 +00:00
|
|
|
func buildMasterStartupScript(m *seaweedv1.Seaweed) string {
|
|
|
|
command := []string{"weed", "master"}
|
|
|
|
spec := m.Spec.Master
|
|
|
|
if spec.VolumePreallocate != nil && *spec.VolumePreallocate {
|
|
|
|
command = append(command, "-volumePreallocate")
|
|
|
|
}
|
|
|
|
|
|
|
|
if spec.VolumeSizeLimitMB != nil {
|
|
|
|
command = append(command, fmt.Sprintf("-volumeSizeLimitMB=%d", *spec.VolumeSizeLimitMB))
|
|
|
|
}
|
|
|
|
|
|
|
|
if spec.GarbageThreshold != nil {
|
|
|
|
command = append(command, fmt.Sprintf("-garbageThreshold=%f", *spec.GarbageThreshold))
|
|
|
|
}
|
|
|
|
|
|
|
|
if spec.PulseSeconds != nil {
|
|
|
|
command = append(command, fmt.Sprintf("-pulseSeconds=%d", *spec.PulseSeconds))
|
|
|
|
}
|
|
|
|
|
|
|
|
command = append(command, fmt.Sprintf("-ip=$(POD_NAME).%s-master", m.Name))
|
|
|
|
command = append(command, fmt.Sprintf("-peers=%s", getMasterPeersString(m.Name, spec.Replicas)))
|
|
|
|
return strings.Join(command, " ")
|
|
|
|
}
|
|
|
|
|
2020-08-02 06:27:46 +00:00
|
|
|
func (r *SeaweedReconciler) createMasterStatefulSet(m *seaweedv1.Seaweed) *appsv1.StatefulSet {
|
|
|
|
labels := labelsForMaster(m.Name)
|
2020-10-28 05:06:25 +00:00
|
|
|
replicas := m.Spec.Master.Replicas
|
2020-08-02 23:12:52 +00:00
|
|
|
rollingUpdatePartition := int32(0)
|
|
|
|
enableServiceLinks := false
|
2020-08-02 06:27:46 +00:00
|
|
|
|
2020-10-28 06:57:38 +00:00
|
|
|
masterPodSpec := m.BaseMasterSpec().BuildPodSpec()
|
2020-10-28 11:19:17 +00:00
|
|
|
masterPodSpec.Volumes = []corev1.Volume{
|
|
|
|
{
|
|
|
|
Name: "master-config",
|
|
|
|
VolumeSource: corev1.VolumeSource{
|
|
|
|
ConfigMap: &corev1.ConfigMapVolumeSource{
|
|
|
|
LocalObjectReference: corev1.LocalObjectReference{
|
|
|
|
Name: m.Name + "-master",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-10-28 06:57:38 +00:00
|
|
|
masterPodSpec.EnableServiceLinks = &enableServiceLinks
|
|
|
|
masterPodSpec.Containers = []corev1.Container{{
|
|
|
|
Name: "seaweedfs",
|
|
|
|
Image: m.Spec.Image,
|
|
|
|
ImagePullPolicy: corev1.PullIfNotPresent,
|
|
|
|
Env: append(m.BaseMasterSpec().Env(), kubernetesEnvVars...),
|
2020-10-28 11:19:17 +00:00
|
|
|
VolumeMounts: []corev1.VolumeMount{
|
|
|
|
{
|
|
|
|
Name: "master-config",
|
|
|
|
ReadOnly: true,
|
|
|
|
MountPath: "/etc/seaweedfs/master.toml",
|
|
|
|
},
|
|
|
|
},
|
2020-10-28 06:57:38 +00:00
|
|
|
Command: []string{
|
|
|
|
"/bin/sh",
|
|
|
|
"-ec",
|
2020-10-28 11:19:17 +00:00
|
|
|
buildMasterStartupScript(m),
|
2020-10-28 06:57:38 +00:00
|
|
|
},
|
|
|
|
Ports: []corev1.ContainerPort{
|
|
|
|
{
|
|
|
|
ContainerPort: seaweedv1.MasterHTTPPort,
|
|
|
|
Name: "swfs-master",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ContainerPort: seaweedv1.MasterGRPCPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ReadinessProbe: &corev1.Probe{
|
|
|
|
Handler: corev1.Handler{
|
|
|
|
HTTPGet: &corev1.HTTPGetAction{
|
|
|
|
Path: "/cluster/status",
|
|
|
|
Port: intstr.FromInt(seaweedv1.MasterHTTPPort),
|
|
|
|
Scheme: corev1.URISchemeHTTP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
InitialDelaySeconds: 5,
|
|
|
|
TimeoutSeconds: 15,
|
|
|
|
PeriodSeconds: 15,
|
|
|
|
SuccessThreshold: 2,
|
|
|
|
FailureThreshold: 100,
|
|
|
|
},
|
|
|
|
LivenessProbe: &corev1.Probe{
|
|
|
|
Handler: corev1.Handler{
|
|
|
|
HTTPGet: &corev1.HTTPGetAction{
|
|
|
|
Path: "/cluster/status",
|
|
|
|
Port: intstr.FromInt(seaweedv1.MasterHTTPPort),
|
|
|
|
Scheme: corev1.URISchemeHTTP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
InitialDelaySeconds: 15,
|
|
|
|
TimeoutSeconds: 15,
|
|
|
|
PeriodSeconds: 15,
|
|
|
|
SuccessThreshold: 1,
|
|
|
|
FailureThreshold: 6,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
2020-08-02 06:27:46 +00:00
|
|
|
dep := &appsv1.StatefulSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2020-08-02 23:12:52 +00:00
|
|
|
Name: m.Name + "-master",
|
2020-08-02 06:27:46 +00:00
|
|
|
Namespace: m.Namespace,
|
|
|
|
},
|
|
|
|
Spec: appsv1.StatefulSetSpec{
|
2020-08-02 23:12:52 +00:00
|
|
|
ServiceName: m.Name + "-master",
|
|
|
|
PodManagementPolicy: appsv1.ParallelPodManagement,
|
|
|
|
Replicas: &replicas,
|
|
|
|
UpdateStrategy: appsv1.StatefulSetUpdateStrategy{
|
|
|
|
Type: appsv1.RollingUpdateStatefulSetStrategyType,
|
|
|
|
RollingUpdate: &appsv1.RollingUpdateStatefulSetStrategy{
|
|
|
|
Partition: &rollingUpdatePartition,
|
|
|
|
},
|
|
|
|
},
|
2020-08-02 06:27:46 +00:00
|
|
|
Selector: &metav1.LabelSelector{
|
|
|
|
MatchLabels: labels,
|
|
|
|
},
|
|
|
|
Template: corev1.PodTemplateSpec{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Labels: labels,
|
|
|
|
},
|
2020-10-28 06:57:38 +00:00
|
|
|
Spec: masterPodSpec,
|
2020-08-02 06:27:46 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Set master instance as the owner and controller
|
|
|
|
// ctrl.SetControllerReference(m, dep, r.Scheme)
|
|
|
|
return dep
|
|
|
|
}
|