From e853e2870f1911fab6e4536f65a80b2a3e397b4f Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Mon, 9 Nov 2020 23:48:22 -0800 Subject: [PATCH] add service for each volume server related to https://github.com/seaweedfs/seaweedfs-operator/issues/16 --- controllers/controller_volume.go | 22 +++++++++++++++++----- controllers/controller_volume_service.go | 9 +++++++-- controllers/label/label.go | 4 ++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/controllers/controller_volume.go b/controllers/controller_volume.go index f624d4e..361c5bf 100644 --- a/controllers/controller_volume.go +++ b/controllers/controller_volume.go @@ -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) } diff --git a/controllers/controller_volume_service.go b/controllers/controller_volume_service.go index 57a78e0..c7e2441 100644 --- a/controllers/controller_volume_service.go +++ b/controllers/controller_volume_service.go @@ -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{ diff --git a/controllers/label/label.go b/controllers/label/label.go index cfa4f7e..13048fe 100644 --- a/controllers/label/label.go +++ b/controllers/label/label.go @@ -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" )