Skip to content

Commit

Permalink
LBwacher changed generation logic (#40)
Browse files Browse the repository at this point in the history
* fix: l4-watcher: change lb generation and ip assignment logic

* fix: lb-watcher: stop using  iprole annotation

* fix: bgp: set status N/A when trasport type is vnet

* Preparation for new release

Co-authored-by: Artashes Balabekyan <[email protected]>
  • Loading branch information
pogossian and artashesbalabekyan authored Sep 6, 2021
1 parent 49c0c66 commit 631db98
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 86 deletions.
10 changes: 0 additions & 10 deletions api/v1alpha1/l4lb_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,3 @@ func (l *L4LB) SetServiceIngressIPs(s string) {
anns["serviceingressips"] = s
l.SetAnnotations(anns)
}

func (l *L4LB) IPRole() string {
return l.GetAnnotations()["resource.k8s.netris.ai/iprole"]
}

func (l *L4LB) SetIPRole(s string) {
anns := l.GetAnnotations()
anns["resource.k8s.netris.ai/iprole"] = s
l.SetAnnotations(anns)
}
12 changes: 10 additions & 2 deletions controllers/bgpmeta_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ func (r *BGPMetaReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
bgpCR.Status.BGPStatus = bgp.BgpState
prefixCount, _ := strconv.Atoi(bgp.BgpPrefixes)
bgpCR.Status.BGPPrefixes = prefixCount
bgpCR.Status.PortState = bgp.PortStatus
portStatus := bgp.PortStatus
if bgpCR.Spec.Transport.Type == "vnet" {
portStatus = "N/A"
}
bgpCR.Status.PortState = portStatus
bgpCR.Status.TerminateOnSwitch = bgp.TermSwName
if bgp.Vlan > 1 {
bgpCR.Status.VLANID = strconv.Itoa(bgp.Vlan)
Expand Down Expand Up @@ -140,7 +144,11 @@ func (r *BGPMetaReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
bgpCR.Status.BGPStatus = apiBGP.BgpState
prefixCount, _ := strconv.Atoi(apiBGP.BgpPrefixes)
bgpCR.Status.BGPPrefixes = prefixCount
bgpCR.Status.PortState = apiBGP.PortStatus
portStatus := apiBGP.PortStatus
if bgpCR.Spec.Transport.Type == "vnet" {
portStatus = "N/A"
}
bgpCR.Status.PortState = portStatus
bgpCR.Status.TerminateOnSwitch = apiBGP.TermSwName
if apiBGP.Vlan > 1 {
bgpCR.Status.VLANID = strconv.Itoa(apiBGP.Vlan)
Expand Down
4 changes: 2 additions & 2 deletions deploy/charts/netris-operator/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.3.8
version: 0.3.9

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
appVersion: v0.4.6
appVersion: v0.4.7
home: https://github.com/netrisai/netris-operator
icon: https://www.netris.ai/wp-content/uploads/2020/05/logo-600.png # [todo] Change url to permalink
keywords:
Expand Down
96 changes: 25 additions & 71 deletions lbwatcher/lb_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ func (w *Watcher) loadBalancerProcess(clientset *kubernetes.Clientset, cl client
debugLogger.Info("Generating load balancers from k8s...")
var errors []error = nil
lbTimeout := "2000"
serviceLBs, err := w.generateLoadBalancers(clientset, lbTimeout)
if err != nil {
logger.Error(err, "")
}

l4lbs, err := getL4LBs(cl)
if err != nil {
Expand All @@ -129,11 +125,16 @@ func (w *Watcher) loadBalancerProcess(clientset *kubernetes.Clientset, cl client

ipAuto := make(map[string]string)
for _, lb := range filteerdL4LBs {
if uid := lb.GetServiceUID(); uid != "" && lb.IPRole() == "main" {
if uid := lb.GetServiceUID(); uid != "" {
ipAuto[uid] = lb.Spec.Frontend.IP
}
}

serviceLBs, err := w.generateLoadBalancers(clientset, ipAuto, lbTimeout)
if err != nil {
logger.Error(err, "")
}

lbsToCreate, lbsToUpdate, lbsToDelete, ingressIPsMap := compareLoadBalancers(filteerdL4LBs, serviceLBs)

js, _ := json.Marshal(lbsToCreate)
Expand Down Expand Up @@ -207,30 +208,9 @@ func deleteL4LB(cl client.Client, lb k8sv1alpha1.L4LB) error {
func updateL4LBs(cl client.Client, lbs []k8sv1alpha1.L4LB, ipAuto map[string]string) []error {
var errors []error
for _, lb := range lbs {
if ip, ok := ipAuto[lb.GetServiceUID()]; ok && lb.Spec.Frontend.IP == "" {
if ip == "" {
break
} else {
lb.Spec.Frontend.IP = ip
lb.SetIPRole("child")
err := updateL4LB(cl, lb)
if err != nil {
errors = append(errors, fmt.Errorf("{updateL4LB} %s", err))
}
}
} else {
if lb.Spec.Frontend.IP == "" {
lb.SetIPRole("main")
} else {
lb.SetIPRole("standard")
}
err := updateL4LB(cl, lb)
if err != nil {
errors = append(errors, fmt.Errorf("{updateL4LB} %s", err))
}
if lb.Spec.Frontend.IP == "" {
break
}
err := updateL4LB(cl, lb)
if err != nil {
errors = append(errors, fmt.Errorf("{updateL4LB} %s", err))
}
}
return errors
Expand All @@ -245,30 +225,9 @@ func createL4LB(cl client.Client, lb *k8sv1alpha1.L4LB) error {
func createL4LBs(cl client.Client, lbs []*k8sv1alpha1.L4LB, ipAuto map[string]string) []error {
var errors []error
for _, lb := range lbs {
if ip, ok := ipAuto[lb.GetServiceUID()]; ok && lb.Spec.Frontend.IP == "" {
if ip == "" {
break
} else {
lb.Spec.Frontend.IP = ip
lb.SetIPRole("child")
err := createL4LB(cl, lb)
if err != nil {
errors = append(errors, fmt.Errorf("{createL4LB} %s", err))
}
}
} else {
if lb.Spec.Frontend.IP == "" {
lb.SetIPRole("main")
} else {
lb.SetIPRole("standard")
}
err := createL4LB(cl, lb)
if err != nil {
errors = append(errors, fmt.Errorf("{createL4LB} %s", err))
}
if lb.Spec.Frontend.IP == "" {
break
}
err := createL4LB(cl, lb)
if err != nil {
errors = append(errors, fmt.Errorf("{createL4LB} %s", err))
}
}
return errors
Expand Down Expand Up @@ -310,8 +269,6 @@ func compareLoadBalancers(LBs []k8sv1alpha1.L4LB, serviceLBs []*k8sv1alpha1.L4LB
}
}

autoIPs := make(map[string]string)

if len(serviceLBsMap) > 0 {
for _, serviceLB := range serviceLBsMap {
if lb, ok := LBsMap[serviceLB.Name]; ok {
Expand All @@ -326,19 +283,8 @@ func compareLoadBalancers(LBs []k8sv1alpha1.L4LB, serviceLBs []*k8sv1alpha1.L4LB

lbIngressMap[serviceLB.GetServiceUID()][lb.Spec.Frontend.IP] = 1

if serviceLB.Spec.Frontend.IP != "" || (lb.IPRole() != "child" && lb.IPRole() != "main") {
if serviceLB.Spec.Frontend.IP != lb.Spec.Frontend.IP {
lb.Spec.Frontend.IP = serviceLB.Spec.Frontend.IP
update = true
}
}

if lb.IPRole() == "main" && lb.Spec.Frontend.IP != "" {
autoIPs[lb.GetServiceUID()] = lb.Spec.Frontend.IP
}

if ip, ok := autoIPs[lb.GetServiceUID()]; ok && lb.IPRole() == "child" && ip != lb.Spec.Frontend.IP && !update {
lb.Spec.Frontend.IP = ip
if serviceLB.Spec.Frontend.IP != lb.Spec.Frontend.IP {
lb.Spec.Frontend.IP = serviceLB.Spec.Frontend.IP
update = true
}

Expand Down Expand Up @@ -409,7 +355,7 @@ func getL4LBs(cl client.Client) (*k8sv1alpha1.L4LBList, error) {
return l4lb, nil
}

func (w *Watcher) generateLoadBalancers(clientset *kubernetes.Clientset, lbTimeout string) ([]*k8sv1alpha1.L4LB, error) {
func (w *Watcher) generateLoadBalancers(clientset *kubernetes.Clientset, autoIPs map[string]string, lbTimeout string) ([]*k8sv1alpha1.L4LB, error) {
lbList := []*k8sv1alpha1.L4LB{}
serviceList, err := getServices(clientset, "")
if err != nil {
Expand Down Expand Up @@ -476,7 +422,15 @@ func (w *Watcher) generateLoadBalancers(clientset *kubernetes.Clientset, lbTimeo
}

if len(lbIPs) > 0 && len(hostIPS) > 0 {
for _, lbIP := range lbIPs {
for i, lbIP := range lbIPs {
frontendIP := lbIP.IP
if lbIP.IP == "" {
if ip, ok := autoIPs[string(svc.GetUID())]; ok && ip != "" {
frontendIP = ip
} else if i > 0 {
break
}
}
backends := []k8sv1alpha1.L4LBBackend{}
for _, hostIP := range hostIPS {
backend := fmt.Sprintf("%s:%d", hostIP, lbIP.NodePort)
Expand All @@ -498,7 +452,7 @@ func (w *Watcher) generateLoadBalancers(clientset *kubernetes.Clientset, lbTimeo
Protocol: strings.ToLower(lbIP.Protocol),
Frontend: k8sv1alpha1.L4LBFrontend{
Port: lbIP.Port,
IP: lbIP.IP,
IP: frontendIP,
},
State: "active",
Check: k8sv1alpha1.L4LBCheck{
Expand Down
2 changes: 1 addition & 1 deletion samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Name | Default |Values | Des

# Calico Integration

Calico nodes exchange routing information over BGP to enable reachability for Calico networked workloads. Netris can also integrate with you Calico CNI. It will create BGP peers with your cluster's nodes, then will disable Calico Node to Node mesh. For more details, get familiar with [calico docs](https://docs.projectcalico.org/networking/bgp).
Calico nodes exchange routing information over BGP to enable reachability for Calico networked workloads. Netris can also integrate with your Calico CNI. It will create BGP peers with your cluster's nodes, then will disable Calico Node to Node mesh. For more details, get familiar with [calico docs](https://docs.projectcalico.org/networking/bgp).

Add this annotation to enable Netris-Calico Integration.
```
Expand Down

0 comments on commit 631db98

Please sign in to comment.