add service for each volume server

related to https://github.com/seaweedfs/seaweedfs-operator/issues/16
This commit is contained in:
Chris Lu 2020-11-09 23:48:22 -08:00
parent 15fc33c506
commit e853e2870f
3 changed files with 28 additions and 7 deletions

View File

@ -21,7 +21,7 @@ func (r *SeaweedReconciler) ensureVolumeServers(seaweedCR *seaweedv1.Seaweed) (d
return
}
if done, result, err = r.ensureVolumeServerService(seaweedCR); done {
if done, result, err = r.ensureVolumeServerServices(seaweedCR); done {
return
}
@ -66,17 +66,29 @@ func (r *SeaweedReconciler) ensureVolumeServerPeerService(seaweedCR *seaweedv1.S
return ReconcileResult(err)
}
func (r *SeaweedReconciler) ensureVolumeServerService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
func (r *SeaweedReconciler) ensureVolumeServerServices(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
log := r.Log.WithValues("sw-volume-service", seaweedCR.Name)
for i := 0; i < int(seaweedCR.Spec.Volume.Replicas); i++ {
done, result, err := r.ensureVolumeServerService(seaweedCR, i)
if done {
return done, result, err
}
}
volumeServerService := r.createVolumeServerService(seaweedCR)
return ReconcileResult(nil)
}
func (r *SeaweedReconciler) ensureVolumeServerService(seaweedCR *seaweedv1.Seaweed, i int) (bool, ctrl.Result, error) {
log := r.Log.WithValues("sw-volume-service", seaweedCR.Name, "index", i)
volumeServerService := r.createVolumeServerService(seaweedCR, i)
if err := controllerutil.SetControllerReference(seaweedCR, volumeServerService, r.Scheme); err != nil {
return ReconcileResult(err)
}
_, err := r.CreateOrUpdateService(volumeServerService)
log.Info("ensure volume service " + volumeServerService.Name)
log.Info("ensure volume service "+volumeServerService.Name, "index", i)
return ReconcileResult(err)
}

View File

@ -1,6 +1,9 @@
package controllers
import (
"fmt"
"github.com/seaweedfs/seaweedfs-operator/controllers/label"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
@ -42,12 +45,14 @@ func (r *SeaweedReconciler) createVolumeServerPeerService(m *seaweedv1.Seaweed)
}
return dep
}
func (r *SeaweedReconciler) createVolumeServerService(m *seaweedv1.Seaweed) *corev1.Service {
func (r *SeaweedReconciler) createVolumeServerService(m *seaweedv1.Seaweed, i int) *corev1.Service {
labels := labelsForVolumeServer(m.Name)
serviceName := fmt.Sprintf("%s-volume-%d", m.Name, i)
labels[label.PodName] = serviceName
dep := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: m.Name + "-volume",
Name: serviceName,
Namespace: m.Namespace,
Labels: labels,
Annotations: map[string]string{

View File

@ -15,4 +15,8 @@ const (
InstanceLabelKey string = "app.kubernetes.io/instance"
// VersionLabelKey is Kubernetes recommended label key, it represents the version of the app
VersionLabelKey string = "app.kubernetes.io/version"
// PodName is to select pod by name
// https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-selector
PodName string = "statefulset.kubernetes.io/pod-name"
)