add filer and s3 ingress

This commit is contained in:
Chris Lu 2020-11-04 14:30:15 -08:00
parent 31c843cae4
commit 46806156b6
7 changed files with 135 additions and 52 deletions

View File

@ -98,9 +98,6 @@ type SeaweedSpec struct {
StatefulSetUpdateStrategy appsv1.StatefulSetUpdateStrategyType `json:"statefulSetUpdateStrategy,omitempty"`
VolumeServerDiskCount int32 `json:"volumeServerDiskCount,omitempty"`
// Ingresses
Hosts []string `json:"hosts,omitempty"`
}
// SeaweedStatus defines the observed state of Seaweed
@ -151,6 +148,9 @@ type VolumeSpec struct {
IdleTimeout *int32 `json:"idleTimeout,omitempty"`
MaxVolumeCounts *int32 `json:"maxVolumeCounts,omitempty"`
MinFreeSpacePercent *int32 `json:"minFreeSpacePercent,omitempty"`
// Ingresses
HostSuffix *string `json:"hostSuffix,omitempty"`
}
// FilerSpec is the spec for filers
@ -169,6 +169,9 @@ type FilerSpec struct {
// Filer-specific settings
MaxMB *int32 `json:"maxMB,omitempty"`
// Ingresses
HostSuffix *string `json:"hostSuffix,omitempty"`
}
// ComponentSpec is the base spec of each component, the fields should always accessed by the Basic<Component>Spec() method to respect the cluster-level properties

View File

@ -128,6 +128,11 @@ func (in *FilerSpec) DeepCopyInto(out *FilerSpec) {
*out = new(int32)
**out = **in
}
if in.HostSuffix != nil {
in, out := &in.HostSuffix, &out.HostSuffix
*out = new(string)
**out = **in
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new FilerSpec.
@ -315,11 +320,6 @@ func (in *SeaweedSpec) DeepCopyInto(out *SeaweedSpec) {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.Hosts != nil {
in, out := &in.Hosts, &out.Hosts
*out = make([]string, len(*in))
copy(*out, *in)
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SeaweedSpec.
@ -424,6 +424,11 @@ func (in *VolumeSpec) DeepCopyInto(out *VolumeSpec) {
*out = new(int32)
**out = **in
}
if in.HostSuffix != nil {
in, out := &in.HostSuffix, &out.HostSuffix
*out = new(string)
**out = **in
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new VolumeSpec.

View File

@ -1343,6 +1343,9 @@ spec:
description: Whether Hostnetwork of the component is enabled. Override
the cluster-level setting if present
type: boolean
hostSuffix:
description: Ingresses
type: string
imagePullPolicy:
description: ImagePullPolicy of the component. Override the cluster-level
imagePullPolicy if present
@ -1492,11 +1495,6 @@ spec:
hostNetwork:
description: Whether Hostnetwork is enabled for pods
type: boolean
hosts:
description: Ingresses
items:
type: string
type: array
image:
description: Image
type: string
@ -3190,6 +3188,9 @@ spec:
description: Whether Hostnetwork of the component is enabled. Override
the cluster-level setting if present
type: boolean
hostSuffix:
description: Ingresses
type: string
idleTimeout:
format: int32
type: integer

View File

@ -0,0 +1,63 @@
package controllers
import (
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
ctrl "sigs.k8s.io/controller-runtime"
seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1"
)
func (r *SeaweedReconciler) createFilerIngress(m *seaweedv1.Seaweed) *extensionsv1beta1.Ingress {
labels := labelsForIngress(m.Name)
dep := &extensionsv1beta1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: m.Name + "-ingress",
Namespace: m.Namespace,
Labels: labels,
},
Spec: extensionsv1beta1.IngressSpec{
// TLS: ingressSpec.TLS,
Rules: []extensionsv1beta1.IngressRule{
{
Host: "filer." + *m.Spec.Filer.HostSuffix,
IngressRuleValue: extensionsv1beta1.IngressRuleValue{
HTTP: &extensionsv1beta1.HTTPIngressRuleValue{
Paths: []extensionsv1beta1.HTTPIngressPath{
{
Path: "/",
Backend: extensionsv1beta1.IngressBackend{
ServiceName: m.Name + "-filer",
ServicePort: intstr.FromInt(seaweedv1.FilerHTTPPort),
},
},
},
},
},
},
{
Host: "s3." + *m.Spec.Filer.HostSuffix,
IngressRuleValue: extensionsv1beta1.IngressRuleValue{
HTTP: &extensionsv1beta1.HTTPIngressRuleValue{
Paths: []extensionsv1beta1.HTTPIngressPath{
{
Path: "/",
Backend: extensionsv1beta1.IngressBackend{
ServiceName: m.Name + "-s3",
ServicePort: intstr.FromInt(seaweedv1.FilerS3Port),
},
},
},
},
},
},
},
},
}
// Set master instance as the owner and controller
ctrl.SetControllerReference(m, dep, r.Scheme)
return dep
}

View File

@ -0,0 +1,46 @@
package controllers
import (
"context"
"github.com/seaweedfs/seaweedfs-operator/controllers/label"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1"
)
func (r *SeaweedReconciler) ensureSeaweedIngress(seaweedCR *seaweedv1.Seaweed) (done bool, result ctrl.Result, err error) {
_ = context.Background()
_ = r.Log.WithValues("seaweed", seaweedCR.Name)
if seaweedCR.Spec.Filer.HostSuffix != nil && len(*seaweedCR.Spec.Filer.HostSuffix) != 0 {
if done, result, err = r.ensureFilerIngress(seaweedCR); done {
return
}
}
return
}
func (r *SeaweedReconciler) ensureFilerIngress(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
log := r.Log.WithValues("sw-master-service", seaweedCR.Name)
ingressService := r.createFilerIngress(seaweedCR)
if err := controllerutil.SetControllerReference(seaweedCR, ingressService, r.Scheme); err != nil {
return ReconcileResult(err)
}
_, err := r.CreateOrUpdateIngress(ingressService)
log.Info("Get master service " + ingressService.Name)
return ReconcileResult(err)
}
func labelsForIngress(name string) map[string]string {
return map[string]string{
label.ManagedByLabelKey: "seaweedfs-operator",
label.NameLabelKey: "seaweedfs",
label.ComponentLabelKey: "ingress",
label.InstanceLabelKey: name,
}
}

View File

@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
@ -14,49 +13,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// svcName is the backend service name
func createIngress(seaweedCR *seaweedv1.Seaweed, svcName string, port int) *extensionsv1beta1.Ingress {
ingressLabel := map[string]string{"app": "seaweedfs", "role": "ingress", "name": svcName}
ingress := &extensionsv1beta1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: svcName + "-ingress",
Namespace: seaweedCR.Namespace,
Labels: ingressLabel,
},
Spec: extensionsv1beta1.IngressSpec{
Rules: []extensionsv1beta1.IngressRule{},
},
}
for _, host := range seaweedCR.Spec.Hosts {
rule := extensionsv1beta1.IngressRule{
Host: host,
IngressRuleValue: extensionsv1beta1.IngressRuleValue{
HTTP: &extensionsv1beta1.HTTPIngressRuleValue{
Paths: []extensionsv1beta1.HTTPIngressPath{
{
Path: "/",
Backend: extensionsv1beta1.IngressBackend{
ServiceName: svcName,
ServicePort: intstr.FromInt(port),
},
},
},
},
},
}
ingress.Spec.Rules = append(ingress.Spec.Rules, rule)
}
return ingress
}
// the following is adapted from tidb-operator/pkg/controller/generic_control.go
const (

View File

@ -68,6 +68,10 @@ func (r *SeaweedReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
return result, err
}
if done, result, err = r.ensureSeaweedIngress(seaweedCR); done {
return result, err
}
return ctrl.Result{}, nil
}