Skip to content

Commit

Permalink
Support for in-cluster lb
Browse files Browse the repository at this point in the history
  • Loading branch information
TrekkieCoder committed Jul 20, 2023
1 parent 6baf93a commit d7753cf
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 9 deletions.
83 changes: 74 additions & 9 deletions pkg/agent/manager/loadbalancer/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,22 @@ func (m *Manager) makeLoxiLBCIStatusModel(instance string, client *api.LoxiClien
}, nil
}

func (m *Manager) makeLoxiLBBGPGlobalModel(localAS int, selfID string) (api.BGPGlobalConfig, error) {

return api.BGPGlobalConfig{
LocalAs: int64(localAS),
RouterID: selfID,
}, nil
}

func (m *Manager) makeLoxiLBBGNeighModel(remoteAS int, IPString string) (api.BGPNeigh, error) {

return api.BGPNeigh{
RemoteAs: int64(remoteAS),
IPAddress: IPString,
}, nil
}

func (m *Manager) addIngress(service *corev1.Service, newIP net.IP) {
service.Status.LoadBalancer.Ingress =
append(service.Status.LoadBalancer.Ingress, corev1.LoadBalancerIngress{IP: newIP.String()})
Expand Down Expand Up @@ -1069,15 +1085,13 @@ loop:
}
}
case aliveClient := <-loxiAliveCh:
isSuccess := false
if m.networkConfig.SetRoles && m.ElectionRunOnce {
cisModel, err := m.makeLoxiLBCIStatusModel("default", aliveClient)
if err == nil {
for retry := 0; retry < 5; retry++ {
klog.Infof("set-role...count %d", retry)
klog.Infof("set-role - count %d", retry)
if err := aliveClient.CIStatus().Create(context.Background(), &cisModel); err == nil {
klog.Infof("set-role success")
isSuccess = true
break
} else {
time.Sleep(1 * time.Second)
Expand All @@ -1086,7 +1100,6 @@ loop:
}
}

isSuccess = false
if m.networkConfig.SetBGP != 0 {
var bgpPeers []string
for _, lc := range m.LoxiClients {
Expand All @@ -1099,20 +1112,72 @@ loop:
bgpPeers = append(bgpPeers, lpc.Host)
}
}
klog.Infof("Set BGP Peer for %v : %v", aliveClient.Host, bgpPeers)
klog.Infof("Set BGP Peer(s) for %v : %v", aliveClient.Host, bgpPeers)
bgpGlobalCfg, _ := m.makeLoxiLBBGPGlobalModel(int(m.networkConfig.SetBGP), aliveClient.Host)
for retry := 0; retry < 2; retry++ {
if err := aliveClient.BGP().CreateGlobalConfig(context.Background(), &bgpGlobalCfg); err == nil {
klog.Infof("set-bgp-global success")
break
} else {
klog.Infof("set-bgp-global cfg - failed count(%d)", retry)
time.Sleep(1 * time.Second)
}
}

for _, bgpPeer := range bgpPeers {
bgpNeighCfg, _ := m.makeLoxiLBBGNeighModel(int(m.networkConfig.SetBGP), bgpPeer)
for retry := 0; retry < 2; retry++ {

if err := aliveClient.BGP().CreateNeigh(context.Background(), &bgpNeighCfg); err == nil {
klog.Infof("set-bgp-neigh(%s) success", bgpPeer)
break
} else {
klog.Infof("set-bgp-neigh(%s) cfg - failed count(%d)", bgpPeer, retry)
time.Sleep(1 * time.Second)
}
}
}

for _, bgpPeerURL := range m.networkConfig.ExtBGPPeers {
bgpPeer := strings.Split(bgpPeerURL, ":")
if len(bgpPeer) > 2 {
continue
}

bgpRemoteIP := net.ParseIP(bgpPeer[0])
if bgpRemoteIP == nil {
continue
}

asid, err := strconv.ParseInt(bgpPeer[1], 10, 0)
if err != nil || asid == 0 {
continue
}

bgpNeighCfg, _ := m.makeLoxiLBBGNeighModel(int(asid), bgpRemoteIP.String())
for retry := 0; retry < 2; retry++ {

if err := aliveClient.BGP().CreateNeigh(context.Background(), &bgpNeighCfg); err == nil {
klog.Infof("set-ebgp-neigh(%s:%v) cfg success", bgpRemoteIP.String(), asid)
break
} else {
klog.Infof("set-ebgp-neigh(%s:%v) cfg - failed count(%d)", bgpRemoteIP.String(), asid, retry)
time.Sleep(1 * time.Second)
}
}
}
}

isSuccess = false
isSuccess := false
for _, value := range m.lbCache {
for _, lbModel := range value.LbModelList {
klog.Infof("reinstallLoxiLbRules: lbModel: %v", lbModel)
for retry := 0; retry < 5; retry++ {
klog.Infof("retry reinstall LB rule...count %d", retry)
if err := aliveClient.LoadBalancer().Create(context.Background(), &lbModel); err == nil {
klog.Infof("reinstall success")
klog.Infof("reinstallLoxiLbRules: lbModel: %v success", lbModel)
isSuccess = true
break
} else {
klog.Infof("reinstallLoxiLbRules: lbModel: %v retry(%d)", lbModel, retry)
time.Sleep(1 * time.Second)
}
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func (l *LoxiClient) CIStatus() *CiStatusAPI {
return newCiStatusAPI(l.GetRESTClient())
}

func (l *LoxiClient) BGP() *BGPAPI {

Check failure on line 96 in pkg/api/client.go

View workflow job for this annotation

GitHub Actions / Run-Preflight

undefined: BGPAPI
return newBGPAPI(l.GetRESTClient())

Check failure on line 97 in pkg/api/client.go

View workflow job for this annotation

GitHub Actions / Run-Preflight

undefined: newBGPAPI
}

func (l *LoxiClient) HealthCheck() *HealthCheckAPI {
return newHealthCheckAPI(l.GetRESTClient())
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -24,6 +25,10 @@ type LoxiRequest struct {
client *RESTClient
}

type LoxiResult struct {
Result string `json:"result"`
}

func NewLoxiRequest(method, resource string, client *RESTClient) *LoxiRequest {
return &LoxiRequest{
method: method,
Expand Down Expand Up @@ -96,6 +101,7 @@ func (l *LoxiRequest) SubResource(subresources ...string) *LoxiRequest {
}

func (l *LoxiRequest) Do(ctx context.Context) *LoxiResponse {
result := LoxiResult{}
if l.err != nil {
return &LoxiResponse{err: l.err}
}
Expand All @@ -122,6 +128,16 @@ func (l *LoxiRequest) Do(ctx context.Context) *LoxiResponse {
return &LoxiResponse{err: err}
}

if resp.StatusCode == http.StatusOK {
if err := json.Unmarshal(respByte, &result); err != nil {
return &LoxiResponse{err: err}
}

if result.Result != "Success" && !strings.Contains(result.Result, "exists") {
return &LoxiResponse{err: errors.New(result.Result)}
}
}

return &LoxiResponse{
statusCode: resp.StatusCode,
body: respByte,
Expand Down

0 comments on commit d7753cf

Please sign in to comment.