Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update terminal ingress #4448

Closed
wants to merge 11 commits into from
90 changes: 76 additions & 14 deletions controllers/go.work.sum

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions controllers/terminal/api/v1/terminal_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import (
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// +kubebuilder:validation:Enum=nginx
type IngressType string
type GatewayType string

const (
Nginx IngressType = "nginx"
Nginx GatewayType = "nginx"
Gateway GatewayType = "envoy"
)

// TerminalSpec defines the desired state of Terminal
Expand All @@ -49,7 +50,7 @@ type TerminalSpec struct {
APIServer string `json:"apiServer"`
//+kubebuilder:validation:Optional
//+kubebuilder:default=nginx
IngressType IngressType `json:"ingressType"`
GatewayType GatewayType `json:"gatewayType"`
}

// TerminalStatus defines the observed state of Terminal
Expand Down
62 changes: 62 additions & 0 deletions controllers/terminal/controllers/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ import (

networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pointer is deprecated and why import to same package?

"k8s.io/utils/ptr"

terminalv1 "github.com/labring/sealos/controllers/terminal/api/v1"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
)

const (
Expand Down Expand Up @@ -90,3 +93,62 @@ func (r *TerminalReconciler) createNginxIngress(terminal *terminalv1.Terminal, h
}
return ingress
}

func (r *TerminalReconciler) createGateway(terminal *terminalv1.Terminal, host string) *gatewayv1.HTTPRoute {

objectMeta := metav1.ObjectMeta{
Name: terminal.Name,
Namespace: terminal.Namespace,
}
commonRoute := gatewayv1.CommonRouteSpec{
ParentRefs: []gatewayv1.ParentReference{
{
Group: (*gatewayv1.Group)(ptr.To("gateway.networking.k8s.io")),
Kind: (*gatewayv1.Kind)(ptr.To("Gateway")),
Name: "eg",
SectionName: (*gatewayv1.SectionName)(ptr.To("https")),
Namespace: (*gatewayv1.Namespace)(ptr.To("sealos-system")),
},
{
Group: (*gatewayv1.Group)(ptr.To("gateway.networking.k8s.io")),
Kind: (*gatewayv1.Kind)(ptr.To("Gateway")),
Name: "eg",
SectionName: (*gatewayv1.SectionName)(ptr.To("http")),
Namespace: (*gatewayv1.Namespace)(ptr.To("sealos-system")),
},
},
}
hostname := gatewayv1.Hostname(host)
rule := gatewayv1.HTTPRouteRule{
BackendRefs: []gatewayv1.HTTPBackendRef{
{
BackendRef: gatewayv1.BackendRef{
BackendObjectReference: gatewayv1.BackendObjectReference{
Group: (*gatewayv1.Group)(ptr.To("")),
Kind: (*gatewayv1.Kind)(ptr.To("Service")),
Name: gatewayv1.ObjectName(terminal.Name),
Port: (*gatewayv1.PortNumber)(pointer.Int32Ptr(3000)),
},
Weight: pointer.Int32Ptr(1),
},
},
},
Matches: []gatewayv1.HTTPRouteMatch{
{
Path: &gatewayv1.HTTPPathMatch{
Type: (*gatewayv1.PathMatchType)(ptr.To("PathPrefix")),
Value: ptr.To("/"),
},
},
},
}
// httpRoute :=
return &gatewayv1.HTTPRoute{
ObjectMeta: objectMeta,
Spec: gatewayv1.HTTPRouteSpec{
CommonRouteSpec: commonRoute,
Hostnames: []gatewayv1.Hostname{hostname},
Rules: []gatewayv1.HTTPRouteRule{rule},
},
}
}
32 changes: 31 additions & 1 deletion controllers/terminal/controllers/terminal_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"

terminalv1 "github.com/labring/sealos/controllers/terminal/api/v1"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
)

const (
Expand Down Expand Up @@ -156,13 +157,42 @@ func (r *TerminalReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
func (r *TerminalReconciler) syncIngress(ctx context.Context, terminal *terminalv1.Terminal, hostname string) error {
var err error
host := hostname + "." + r.terminalDomain
switch terminal.Spec.IngressType {
switch terminal.Spec.GatewayType {
case terminalv1.Nginx:
err = r.syncNginxIngress(ctx, terminal, host)
case terminalv1.Gateway:
err = r.syncGateway(ctx, terminal, host)
}
return err
}

func (r *TerminalReconciler) syncGateway(ctx context.Context, terminal *terminalv1.Terminal, host string) error {
httproute := &gatewayv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Name: terminal.Name,
Namespace: terminal.Namespace,
},
}
if _, err := controllerutil.CreateOrUpdate(ctx, r.Client, httproute, func() error {
expectHttproute := r.createGateway(terminal, host)
httproute.ObjectMeta.Labels = expectHttproute.ObjectMeta.Labels
httproute.ObjectMeta.Annotations = expectHttproute.ObjectMeta.Annotations
httproute.Spec.Rules = expectHttproute.Spec.Rules
httproute.Spec.Hostnames = expectHttproute.Spec.Hostnames
httproute.Spec.CommonRouteSpec = expectHttproute.Spec.CommonRouteSpec
return controllerutil.SetControllerReference(terminal, httproute, r.Scheme)
}); err != nil {
return err
}

domain := Protocol + host + r.getPort()
if terminal.Status.Domain != domain {
terminal.Status.Domain = domain
return r.Status().Update(ctx, terminal)
}
return nil
}

func (r *TerminalReconciler) syncNginxIngress(ctx context.Context, terminal *terminalv1.Terminal, host string) error {
ingress := &networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Expand Down
1 change: 1 addition & 0 deletions controllers/terminal/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@ require (
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
sigs.k8s.io/gateway-api v1.0.0
)
2 changes: 2 additions & 0 deletions controllers/terminal/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1161,3 +1161,5 @@ sigs.k8s.io/structured-merge-diff/v4 v4.2.3/go.mod h1:qjx8mGObPmV2aSZepjQjbmb2ih
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
sigs.k8s.io/gateway-api v1.0.0 h1:iPTStSv41+d9p0xFydll6d7f7MOBGuqXM6p2/zVYMAs=
sigs.k8s.io/gateway-api v1.0.0/go.mod h1:4cUgr0Lnp5FZ0Cdq8FdRwCvpiWws7LVhLHGIudLlf4c=
Loading