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

PR: support for change in deployment endpoints #10

Merged
merged 1 commit into from
Oct 4, 2024
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module loxilb.io/loxilb-ingress-manager
go 1.23.0

require (
github.com/loxilb-io/kube-loxilb v0.9.6-0.20240724081844-310d8829b72f
github.com/loxilb-io/kube-loxilb v0.9.7-0.20241004024038-4b8ffe3b0ef9
k8s.io/api v0.30.3
k8s.io/apimachinery v0.30.3
k8s.io/client-go v0.30.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/loxilb-io/kube-loxilb v0.9.6-0.20240724081844-310d8829b72f h1:epj7PKKn49bPJXC4/ZseppGumBje5YSwd6l/zSR1q1I=
github.com/loxilb-io/kube-loxilb v0.9.6-0.20240724081844-310d8829b72f/go.mod h1:ShMJ6ylhEeWRGSnqoAWxuJ44K2I7zJAUH0s3gkJELRA=
github.com/loxilb-io/kube-loxilb v0.9.7-0.20241004024038-4b8ffe3b0ef9 h1:yw4M9mAM0IDg2+yA3DqwrN3HuLfWGMquKcnr7u8UmXg=
github.com/loxilb-io/kube-loxilb v0.9.7-0.20241004024038-4b8ffe3b0ef9/go.mod h1:+hSZ4/dswNl/RVQFbIefYqCbrh0XA7m6d9yY6QF9Lx0=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
Expand Down
91 changes: 81 additions & 10 deletions managers/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package managers
import (
"context"
"fmt"
"time"

corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
Expand Down Expand Up @@ -47,15 +48,40 @@ type LoxilbIngressReconciler struct {
func (r *LoxilbIngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)

ruleName := fmt.Sprintf("%s_%s", req.Namespace, req.Name)
ruleNameHTTPS := fmt.Sprintf("%s_%s_https", req.Namespace, req.Name)

currLBList, err := r.LoxiClient.LoadBalancer().List(ctx)
if err != nil {
logger.Info("Failed to get existing loxilb-ingress rules")
return ctrl.Result{}, err
}

exist := false
existHTTPS := false
for _, lbItem := range currLBList.Item {
if lbItem.Service.Name == ruleName {
exist = true
} else if lbItem.Service.Name == ruleNameHTTPS {
existHTTPS = true
}
}

ingress := &netv1.Ingress{}
err := r.Client.Get(ctx, req.NamespacedName, ingress)
err = r.Client.Get(ctx, req.NamespacedName, ingress)
if err != nil {
// Ingress is deleted.
if errors.IsNotFound(err) {
logger.Info("This resource is deleted", "Ingress", req.NamespacedName)
ruleName := fmt.Sprintf("%s_%s", req.Namespace, req.Name)
if err := r.LoxiClient.LoadBalancer().DeleteByName(ctx, ruleName); err != nil {
logger.Error(err, "failed to delete loxilb-ingress rule "+ruleName)
if exist {
if err := r.LoxiClient.LoadBalancer().DeleteByName(ctx, ruleName); err != nil {
logger.Error(err, "failed to delete loxilb-ingress rule "+ruleName)
}
}
if existHTTPS {
if err := r.LoxiClient.LoadBalancer().DeleteByName(ctx, ruleNameHTTPS); err != nil {
logger.Error(err, "failed to delete loxilb-ingress rule "+ruleNameHTTPS)
}
}
return ctrl.Result{}, nil
}
Expand All @@ -67,13 +93,54 @@ func (r *LoxilbIngressReconciler) Reconcile(ctx context.Context, req ctrl.Reques
// when ingress is added, install rule to loxilb-ingress
models, err := r.createLoxiModelList(ctx, ingress)
if err != nil {
if exist {
if err := r.LoxiClient.LoadBalancer().DeleteByName(ctx, ruleName); err == nil {
logger.Info("deleted loxilb-ingress rule ", ruleName, "no endpoints")
}
}
if existHTTPS {
if err := r.LoxiClient.LoadBalancer().DeleteByName(ctx, ruleNameHTTPS); err == nil {
logger.Info("deleted loxilb-ingress rule ", ruleNameHTTPS, "no endpoints")
}
}
logger.Error(err, "Failed to set ingress. failed to create loxilb loadbalancer model", "[]loxiapi.LoadBalancerModel", models)
return ctrl.Result{}, err
}

logger.Info("createLoxiModelList return models:", "[]loxiapi.LoadBalancerModel", models)

var applyModels []loxiapi.LoadBalancerModel
nextModel:
for _, model := range models {
for _, lbItem := range currLBList.Item {
if lbItem.Service.Name == model.Service.Name && len(lbItem.Endpoints) == len(model.Endpoints) {
match := true
for _, mep := range model.Endpoints {
epMatch := false
for _, ep := range lbItem.Endpoints {
if mep.EndpointIP == ep.EndpointIP && mep.TargetPort == ep.TargetPort {
epMatch = true
break
}
}
if !epMatch {
match = false
break
}
}
if match {
continue nextModel
}
}
}
applyModels = append(applyModels, model)
}

if len(applyModels) <= 0 {
return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
}

logger.Info("createLoxiModelList return models:", "[]loxiapi.LoadBalancerModel", applyModels)

for _, model := range applyModels {
err = r.LoxiClient.LoadBalancer().Create(ctx, &model)
if err != nil {
if err.Error() != "lbrule-exists error" {
Expand All @@ -88,7 +155,7 @@ func (r *LoxilbIngressReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

logger.Info("This resource is created", "ingress", ingress)
return ctrl.Result{}, nil
return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
}

func (r *LoxilbIngressReconciler) createLoxiLoadBalancerService(ns, name, externalIP string, security int32, host string) loxiapi.LoadBalancerService {
Expand Down Expand Up @@ -141,7 +208,7 @@ func (r *LoxilbIngressReconciler) createLoxiLoadBalancerEndpoints(ctx context.Co
return loxilbEpList, nil
}

func (r *LoxilbIngressReconciler) checkTlsHost(host string, TLS []netv1.IngressTLS) bool {
func (r *LoxilbIngressReconciler) checkTLSHost(host string, TLS []netv1.IngressTLS) bool {
for _, tls := range TLS {
for _, tlsHost := range tls.Hosts {
if host == tlsHost {
Expand Down Expand Up @@ -170,11 +237,15 @@ func (r *LoxilbIngressReconciler) createLoxiModelList(ctx context.Context, ingre
ns := r.getBackendServiceNamespace(ingress, name)
port := path.Backend.Service.Port.Number
security := int32(0)
if r.checkTlsHost(rule.Host, ingress.Spec.TLS) {
if r.checkTLSHost(rule.Host, ingress.Spec.TLS) {
security = 1
}

loxisvc := r.createLoxiLoadBalancerService(ingress.Namespace, ingress.Name, r.LoxiClient.Host, security, rule.Host)
lbName := ingress.Name
if security == 1 {
lbName += "_https"
}
loxisvc := r.createLoxiLoadBalancerService(ingress.Namespace, lbName, r.LoxiClient.Host, security, rule.Host)
loxiep, err := r.createLoxiLoadBalancerEndpoints(ctx, ns, name, port)
if err != nil {
return models, err
Expand Down
Loading