diff --git a/controllers/controller_filer.go b/controllers/controller_filer.go index 7e050d1..5ab0bf1 100644 --- a/controllers/controller_filer.go +++ b/controllers/controller_filer.go @@ -16,7 +16,11 @@ func (r *SeaweedReconciler) ensureFilerServers(seaweedCR *seaweedv1.Seaweed) (do _ = context.Background() _ = r.Log.WithValues("seaweed", seaweedCR.Name) - if done, result, err = r.ensureFilerService(seaweedCR); done { + if done, result, err = r.ensureFilerHeadlessService(seaweedCR); done { + return + } + + if done, result, err = r.ensureFilerNodePortService(seaweedCR); done { return } @@ -65,31 +69,41 @@ func (r *SeaweedReconciler) ensureFilerStatefulSet(seaweedCR *seaweedv1.Seaweed) return ReconcileResult(err) } -func (r *SeaweedReconciler) ensureFilerService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) { - ctx := context.Background() - log := r.Log.WithValues("sw-filer-service", seaweedCR.Name) +func (r *SeaweedReconciler) ensureFilerHeadlessService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) { + return r.ensureService(seaweedCR, "-filer-headless", r.createFilerHeadlessService) +} - volumeServerService := &corev1.Service{} - err := r.Get(ctx, types.NamespacedName{Name: seaweedCR.Name + "-filer", Namespace: seaweedCR.Namespace}, volumeServerService) - if err != nil && errors.IsNotFound(err) { - // Define a new deployment - dep := r.createFilerService(seaweedCR) - log.Info("Creating a new filer service", "Namespace", dep.Namespace, "Name", dep.Name) - err = r.Create(ctx, dep) - if err != nil { - log.Error(err, "Failed to create new filer service", "Namespace", dep.Namespace, "Name", dep.Name) - return ReconcileResult(err) - } - // Deployment created successfully - return and requeue - return ReconcileResult(err) - } else if err != nil { - log.Error(err, "Failed to get filer server service") - return ReconcileResult(err) - } - log.Info("Get filer service " + volumeServerService.Name) - return ReconcileResult(err) +func (r *SeaweedReconciler) ensureFilerNodePortService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) { + return r.ensureService(seaweedCR, "-filer", r.createFilerNodePortService) } func labelsForFiler(name string) map[string]string { return map[string]string{"app": "seaweedfs", "role": "filer", "name": name} } + +type CreateServiceFunc func(m *seaweedv1.Seaweed) *corev1.Service + +func (r *SeaweedReconciler) ensureService(seaweedCR *seaweedv1.Seaweed, nameSuffix string, serviceFunc CreateServiceFunc) (bool, ctrl.Result, error) { + ctx := context.Background() + log := r.Log.WithValues("sw", seaweedCR.Name, "service", nameSuffix) + + aService := &corev1.Service{} + err := r.Get(ctx, types.NamespacedName{Name: seaweedCR.Name + nameSuffix, Namespace: seaweedCR.Namespace}, aService) + if err != nil && errors.IsNotFound(err) { + // Define a new deployment + dep := serviceFunc(seaweedCR) + log.Info("Creating a new service", "Namespace", dep.Namespace, "Name", dep.Name) + err = r.Create(ctx, dep) + if err != nil { + log.Error(err, "Failed to create service", "Namespace", dep.Namespace, "Name", dep.Name) + return ReconcileResult(err) + } + // Deployment created successfully - return and requeue + return ReconcileResult(err) + } else if err != nil { + log.Error(err, "Failed to get server service") + return ReconcileResult(err) + } + log.Info("Get service " + aService.Name) + return ReconcileResult(err) +} diff --git a/controllers/controller_filer_service.go b/controllers/controller_filer_service.go index dbd05e4..9714892 100644 --- a/controllers/controller_filer_service.go +++ b/controllers/controller_filer_service.go @@ -8,12 +8,12 @@ import ( seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1" ) -func (r *SeaweedReconciler) createFilerService(m *seaweedv1.Seaweed) *corev1.Service { +func (r *SeaweedReconciler) createFilerHeadlessService(m *seaweedv1.Seaweed) *corev1.Service { labels := labelsForFiler(m.Name) dep := &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ - Name: m.Name + "-filer", + Name: m.Name + "-filer-headless", Namespace: m.Namespace, Labels: labels, Annotations: map[string]string{ @@ -57,3 +57,56 @@ func (r *SeaweedReconciler) createFilerService(m *seaweedv1.Seaweed) *corev1.Ser } return dep } + +func (r *SeaweedReconciler) createFilerNodePortService(m *seaweedv1.Seaweed) *corev1.Service { + labels := labelsForFiler(m.Name) + + dep := &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: m.Name + "-filer", + Namespace: m.Namespace, + Labels: labels, + Annotations: map[string]string{ + "service.alpha.kubernetes.io/tolerate-unready-endpoints": "true", + }, + }, + Spec: corev1.ServiceSpec{ + Type: corev1.ServiceTypeNodePort, + PublishNotReadyAddresses: true, + Ports: []corev1.ServicePort{ + { + Name: "swfs-filer", + Protocol: corev1.Protocol("TCP"), + Port: 8888, + TargetPort: intstr.IntOrString{ + Type: intstr.Int, + IntVal: 8888, + }, + NodePort: 30888, + }, + { + Name: "swfs-filer-grpc", + Protocol: corev1.Protocol("TCP"), + Port: 18888, + TargetPort: intstr.IntOrString{ + Type: intstr.Int, + IntVal: 18888, + }, + NodePort: 31888, + }, + { + Name: "swfs-s3", + Protocol: corev1.Protocol("TCP"), + Port: 8333, + TargetPort: intstr.IntOrString{ + Type: intstr.Int, + IntVal: 8333, + }, + NodePort: 30833, + }, + }, + Selector: labels, + }, + } + return dep +}