add filer nodeport service

This commit is contained in:
Chris Lu 2020-10-17 13:51:07 -07:00
parent b2274f19c1
commit 95164dc830
2 changed files with 92 additions and 25 deletions

View File

@ -16,7 +16,11 @@ func (r *SeaweedReconciler) ensureFilerServers(seaweedCR *seaweedv1.Seaweed) (do
_ = context.Background() _ = context.Background()
_ = r.Log.WithValues("seaweed", seaweedCR.Name) _ = 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 return
} }
@ -65,31 +69,41 @@ func (r *SeaweedReconciler) ensureFilerStatefulSet(seaweedCR *seaweedv1.Seaweed)
return ReconcileResult(err) return ReconcileResult(err)
} }
func (r *SeaweedReconciler) ensureFilerService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) { func (r *SeaweedReconciler) ensureFilerHeadlessService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
ctx := context.Background() return r.ensureService(seaweedCR, "-filer-headless", r.createFilerHeadlessService)
log := r.Log.WithValues("sw-filer-service", seaweedCR.Name) }
volumeServerService := &corev1.Service{} func (r *SeaweedReconciler) ensureFilerNodePortService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
err := r.Get(ctx, types.NamespacedName{Name: seaweedCR.Name + "-filer", Namespace: seaweedCR.Namespace}, volumeServerService) return r.ensureService(seaweedCR, "-filer", r.createFilerNodePortService)
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 labelsForFiler(name string) map[string]string { func labelsForFiler(name string) map[string]string {
return map[string]string{"app": "seaweedfs", "role": "filer", "name": name} 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)
}

View File

@ -8,12 +8,12 @@ import (
seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1" 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) labels := labelsForFiler(m.Name)
dep := &corev1.Service{ dep := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: m.Name + "-filer", Name: m.Name + "-filer-headless",
Namespace: m.Namespace, Namespace: m.Namespace,
Labels: labels, Labels: labels,
Annotations: map[string]string{ Annotations: map[string]string{
@ -57,3 +57,56 @@ func (r *SeaweedReconciler) createFilerService(m *seaweedv1.Seaweed) *corev1.Ser
} }
return dep 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
}