Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
feat: update Gateway address if any IP is allocated
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Yang <reaver@flomesh.io>
  • Loading branch information
reaver-flomesh committed May 17, 2023
1 parent 86cbb87 commit c5d3a96
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion controllers/gateway/v1beta1/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ import (
"github.com/flomesh-io/fsm/pkg/config"
fctx "github.com/flomesh-io/fsm/pkg/context"
"github.com/flomesh-io/fsm/pkg/helm"
"github.com/flomesh-io/fsm/pkg/util"
ghodssyaml "github.com/ghodss/yaml"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/strvals"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
metautil "k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -202,12 +204,77 @@ func (r *gatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
activeGateways[gateway.Namespace] = activeGateway
}

// TODO: implement it
// 6. update addresses of Gateway status if any IP is allocated
if activeGateway != nil {
lbSvc := &corev1.Service{}
key := client.ObjectKey{
Namespace: activeGateway.Namespace,
Name: fmt.Sprintf("fsm-gateway-%s", activeGateway.Namespace),
}
if err := r.fctx.Get(ctx, key, lbSvc); err != nil {
return ctrl.Result{}, err
}

activeGateway.Status.Addresses = nil
if lbSvc.Spec.Type == corev1.ServiceTypeLoadBalancer {
if len(lbSvc.Status.LoadBalancer.Ingress) > 0 {
existingIPs := gatewayIPs(activeGateway)
expectedIPs := lbIPs(lbSvc)

sort.Strings(expectedIPs)
sort.Strings(existingIPs)

if util.StringsEqual(expectedIPs, existingIPs) {
return ctrl.Result{}, nil
}

for _, ing := range lbSvc.Status.LoadBalancer.Ingress {
activeGateway.Status.Addresses = append(activeGateway.Status.Addresses, gwv1beta1.GatewayAddress{
Type: addressTypePointer(gwv1beta1.IPAddressType),
Value: ing.IP,
})
}

if err := r.fctx.Status().Update(ctx, activeGateway); err != nil {
return ctrl.Result{Requeue: true}, err
}
} else {
return ctrl.Result{Requeue: true}, nil
}
}
}

return ctrl.Result{}, nil
}

func gatewayIPs(gateway *gwv1beta1.Gateway) []string {
var ips []string

for _, addr := range gateway.Status.Addresses {
if addr.Type == addressTypePointer(gwv1beta1.IPAddressType) && addr.Value != "" {
ips = append(ips, addr.Value)
}
}

return ips
}

func lbIPs(svc *corev1.Service) []string {
var ips []string

for _, ingress := range svc.Status.LoadBalancer.Ingress {
if ingress.IP != "" {
ips = append(ips, ingress.IP)
}
}

return ips
}

func addressTypePointer(addrType gwv1beta1.AddressType) *gwv1beta1.AddressType {
return &addrType
}

func (r *gatewayReconciler) findActiveGateway(ctx context.Context, gateway *gwv1beta1.Gateway) (*gwv1beta1.Gateway, ctrl.Result, error) {
gatewayList := &gwv1beta1.GatewayList{}
if err := r.fctx.List(ctx, gatewayList, client.InNamespace(gateway.Namespace)); err != nil {
Expand Down

0 comments on commit c5d3a96

Please sign in to comment.