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

feat: support gateway ssl #507

Merged
merged 2 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/controllers/gateway/apisix/ssl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package apisix

type SSLClient struct {
client *Cluster
}

func NewSSLClient(c *Cluster) *SSLClient {
return &SSLClient{client: c}
}

// Put create or update ssl
func (s *SSLClient) Put(id string, data map[string]interface{}) error {
url := s.client.baseURL + "/ssl/" + id
return s.client.Put(url, "ssl", data)
}

// Delete delete ssl
func (s *SSLClient) Delete(id string) error {
url := s.client.baseURL + "/ssl/" + id
return s.client.Delete(url, "ssl")
}
1 change: 1 addition & 0 deletions core/controllers/gateway/apisix/ssl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package apisix
68 changes: 66 additions & 2 deletions core/controllers/gateway/controllers/domain_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@ package controllers

import (
"context"

"errors"
"github.com/labring/laf/core/controllers/gateway/apisix"
"github.com/labring/laf/core/pkg/common"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"

gatewayv1 "github.com/labring/laf/core/controllers/gateway/api/v1"
corev1 "k8s.io/api/core/v1"
)

const sslFinalizer = "ssl.gateway.laf.dev"

// DomainReconciler reconciles a Domain object
type DomainReconciler struct {
client.Client
Expand Down Expand Up @@ -58,9 +64,67 @@ type DomainReconciler struct {
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile
func (r *DomainReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)
var domain gatewayv1.Domain
if err := r.Get(ctx, req.NamespacedName, &domain); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if !domain.DeletionTimestamp.IsZero() {
return r.delete(ctx, &domain)
}

return r.apply(ctx, &domain)
}

func (r *DomainReconciler) apply(ctx context.Context, domain *gatewayv1.Domain) (ctrl.Result, error) {

if domain.Spec.CertConfigRef == "" {
return ctrl.Result{}, nil
}

var secret corev1.Secret
if err := r.Get(ctx, client.ObjectKey{Namespace: common.GetSystemNamespace(), Name: domain.Spec.CertConfigRef}, &secret); err != nil {
return ctrl.Result{}, err
}
if secret.Data["tls.crt"] == nil || secret.Data["tls.key"] == nil {
return ctrl.Result{}, errors.New("secret tls.crt or tls.key is nil")
}
data := map[string]interface{}{
"cert": string(secret.Data["tls.crt"]),
"key": string(secret.Data["tls.key"]),
"snis": []string{"*." + domain.Spec.Domain},
}
cluster := apisix.NewCluster(domain.Spec.Cluster.Url, domain.Spec.Cluster.Key)
sslClient := apisix.NewSSLClient(cluster)
err := sslClient.Put(domain.Name, data)
if err != nil {
return ctrl.Result{}, err
}

// add finalizer if not present
if controllerutil.AddFinalizer(domain, routeFinalizer) {
if err := r.Update(ctx, domain); err != nil {
return ctrl.Result{}, err
}
}

return ctrl.Result{}, nil
}

func (r *DomainReconciler) delete(ctx context.Context, domain *gatewayv1.Domain) (ctrl.Result, error) {
_log := log.FromContext(ctx)

cluster := apisix.NewCluster(domain.Spec.Cluster.Url, domain.Spec.Cluster.Key)
sslClient := apisix.NewSSLClient(cluster)
sslClient.Delete(domain.Name)

// TODO(user): your logic here
// remove the finalizer
if controllerutil.RemoveFinalizer(domain, routeFinalizer) {
if err := r.Update(ctx, domain); err != nil {
return ctrl.Result{}, err
}
}

_log.Info("ssl deleted: " + domain.Name)
return ctrl.Result{}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions core/controllers/gateway/controllers/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ func (r *GatewayReconciler) applyApp(ctx context.Context, gateway *gatewayv1.Gat
DomainName: appDomain.Name,
DomainNamespace: appDomain.Namespace,
Backend: gatewayv1.Backend{
ServiceName: gateway.Spec.AppId,
ServicePort: 80,
ServiceName: gateway.Spec.AppId + "." + gateway.Namespace,
ServicePort: 8000,
},
},
}
Expand Down