2020-10-15 04:45:53 +00:00
|
|
|
package controllers
|
|
|
|
|
2020-10-28 05:06:25 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-11-09 00:12:31 +00:00
|
|
|
seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1"
|
2020-10-28 06:57:38 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2020-10-28 05:06:25 +00:00
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-11-09 01:40:30 +00:00
|
|
|
masterPeerAddressPattern = "%s-master-%d.%s-master-peer.%s:9333"
|
|
|
|
filerPeerAddressPattern = "%s-filer-%d.%s-filer-peer:8888"
|
2020-10-28 05:06:25 +00:00
|
|
|
)
|
2020-10-15 04:45:53 +00:00
|
|
|
|
2020-10-28 06:57:38 +00:00
|
|
|
var (
|
|
|
|
kubernetesEnvVars = []corev1.EnvVar{
|
|
|
|
{
|
|
|
|
Name: "POD_IP",
|
|
|
|
ValueFrom: &corev1.EnvVarSource{
|
|
|
|
FieldRef: &corev1.ObjectFieldSelector{
|
|
|
|
FieldPath: "status.podIP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "POD_NAME",
|
|
|
|
ValueFrom: &corev1.EnvVarSource{
|
|
|
|
FieldRef: &corev1.ObjectFieldSelector{
|
|
|
|
FieldPath: "metadata.name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "NAMESPACE",
|
|
|
|
ValueFrom: &corev1.EnvVarSource{
|
|
|
|
FieldRef: &corev1.ObjectFieldSelector{
|
|
|
|
FieldPath: "metadata.namespace",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-10-15 04:45:53 +00:00
|
|
|
func ReconcileResult(err error) (bool, ctrl.Result, error) {
|
|
|
|
if err != nil {
|
|
|
|
return true, ctrl.Result{}, err
|
|
|
|
}
|
|
|
|
return false, ctrl.Result{}, nil
|
|
|
|
}
|
2020-10-28 05:06:25 +00:00
|
|
|
|
2020-10-30 09:15:16 +00:00
|
|
|
func getFilerAddresses(name string, replicas int32) []string {
|
|
|
|
peersAddresses := make([]string, 0, replicas)
|
|
|
|
for i := int32(0); i < replicas; i++ {
|
|
|
|
peersAddresses = append(peersAddresses, fmt.Sprintf(filerPeerAddressPattern, name, i, name))
|
|
|
|
}
|
|
|
|
return peersAddresses
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFilerPeersString(name string, replicas int32) string {
|
|
|
|
return strings.Join(getFilerAddresses(name, replicas), ",")
|
|
|
|
}
|
|
|
|
|
2020-11-09 00:12:31 +00:00
|
|
|
func getMasterAddresses(namespace string, name string, replicas int32) []string {
|
2020-10-28 05:06:25 +00:00
|
|
|
peersAddresses := make([]string, 0, replicas)
|
|
|
|
for i := int32(0); i < replicas; i++ {
|
2020-11-09 00:12:31 +00:00
|
|
|
peersAddresses = append(peersAddresses, fmt.Sprintf(masterPeerAddressPattern, name, i, name, namespace))
|
2020-10-28 05:06:25 +00:00
|
|
|
}
|
|
|
|
return peersAddresses
|
|
|
|
}
|
|
|
|
|
2020-11-09 00:12:31 +00:00
|
|
|
func getMasterPeersString(m *seaweedv1.Seaweed) string {
|
|
|
|
return strings.Join(getMasterAddresses(m.Namespace, m.Name, m.Spec.Master.Replicas), ",")
|
2020-10-28 05:06:25 +00:00
|
|
|
}
|
2020-10-30 09:15:16 +00:00
|
|
|
|
2020-10-28 06:57:38 +00:00
|
|
|
func copyAnnotations(src map[string]string) map[string]string {
|
|
|
|
if src == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dst := map[string]string{}
|
|
|
|
for k, v := range src {
|
|
|
|
dst[k] = v
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|