Skip to content

Commit

Permalink
fix(): Added support for AWS LB
Browse files Browse the repository at this point in the history
Signed-off-by: Bharath Horatti <[email protected]>
  • Loading branch information
bharath-avesha committed Oct 17, 2023
1 parent 9a68235 commit 3a45746
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
23 changes: 23 additions & 0 deletions controllers/slice/slice_gw_edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/kubeslice/slicegw-edge/pkg/edgeservice"
kubeslicev1beta1 "github.com/kubeslice/worker-operator/api/v1beta1"
"github.com/kubeslice/worker-operator/controllers"
"github.com/kubeslice/worker-operator/pkg/cluster"
"github.com/kubeslice/worker-operator/pkg/gatewayedge"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -105,9 +106,31 @@ func serviceForSliceGatewayEdge(sliceName, svcName string, portmap *map[string]i
return svc
}

func getClusterProviderID(ctx context.Context, c client.Client) (string, error) {
cl := cluster.NewCluster(c, "local-cluster")
clusterInfo, err := cl.GetClusterInfo(ctx)
if err != nil {
return "", err
}

return clusterInfo.ClusterProperty.GeoLocation.CloudProvider, nil
}

func (r *SliceReconciler) createSliceGatewayEdgeService(ctx context.Context, slice *kubeslicev1beta1.Slice, portmap *map[string]int32) error {
log := r.Log.WithValues("slice", slice.Name)
svc := serviceForSliceGatewayEdge(slice.Name, "svc-"+slice.Name+"-gw-edge", portmap)

// Note: Special treatment for AWS EKS clusters. The LB is not provisioned unless we add AWS specific annotations
// to the service. This is needed only for EKS.
if clusterProvider, _ := getClusterProviderID(ctx, r.Client); clusterProvider == "aws" {
if svc.ObjectMeta.Annotations == nil {
svc.ObjectMeta.Annotations = make(map[string]string)
}
svc.ObjectMeta.Annotations["service.beta.kubernetes.io/aws-load-balancer-type"] = "external"
svc.ObjectMeta.Annotations["service.beta.kubernetes.io/aws-load-balancer-nlb-target-type"] = "ip"
svc.ObjectMeta.Annotations["service.beta.kubernetes.io/aws-load-balancer-scheme"] = "internet-facing"
}

ctrl.SetControllerReference(slice, svc, r.Scheme)

err := r.Create(ctx, svc)
Expand Down
24 changes: 20 additions & 4 deletions controllers/slicegateway/slicegateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import (
"errors"
"fmt"
"math"
"net"
"os"
"strconv"
"sync"
"time"

"github.com/go-logr/logr"
"github.com/kubeslice/worker-operator/controllers"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -884,7 +886,7 @@ func (r *SliceGwReconciler) createEndpointForGatewayServer(slicegateway *kubesli
},
Subsets: []corev1.EndpointSubset{
{
Addresses: getAddrSlice(endpointIPs),
Addresses: getAddrSlice(endpointIPs, r.Log),
},
},
}
Expand Down Expand Up @@ -947,7 +949,7 @@ func (r *SliceGwReconciler) reconcileGatewayEndpoint(ctx context.Context, sliceG

if !checkEndpointSubset(currentEndpointFound, endpointIPs, true) {
log.Info("Updating the Endpoint, since sliceGatewayRemoteNodeIp has changed", "from endpointFound", currentEndpointFound.Addresses[0].IP)
endpointFound.Subsets[0].Addresses = getAddrSlice(endpointIPs)
endpointFound.Subsets[0].Addresses = getAddrSlice(endpointIPs, r.Log)
toUpdate = true
}
// When "toUpdate" is set to true we update the endpoints addresses
Expand Down Expand Up @@ -1021,10 +1023,24 @@ func (r *SliceGwReconciler) restartGatewayPods(ctx context.Context, sliceGWName

}

func getAddrSlice(endpointIPs []string) []corev1.EndpointAddress {
func getAddrSlice(endpointIPs []string, log logr.Logger) []corev1.EndpointAddress {
endpointSlice := make([]corev1.EndpointAddress, 0)
for _, ip := range endpointIPs {
endpointSlice = append(endpointSlice, corev1.EndpointAddress{IP: ip})
if net.ParseIP(ip) == nil {
host := ip
resolver := net.Resolver{}
resolvedIps, err := resolver.LookupHost(context.Background(), host)
if err != nil {
log.Error(err, "Failed to resolve name", "hostname", host)
continue
}
log.Info("Resolved hostname", "IPs", resolvedIps)
for _, resolvedIp := range resolvedIps {
endpointSlice = append(endpointSlice, corev1.EndpointAddress{IP: resolvedIp})
}
} else {
endpointSlice = append(endpointSlice, corev1.EndpointAddress{IP: ip})
}
}
return endpointSlice
}
Expand Down

0 comments on commit 3a45746

Please sign in to comment.