Skip to content

Commit 47a68dc

Browse files
LBwatcher: site detection (#30)
* feat: l4lb: find site for specified backend IP if it is not defined in CR * fix: l4lb: error message Co-authored-by: Artashes Balabekyan <[email protected]>
1 parent 8cf227c commit 47a68dc

File tree

8 files changed

+53
-15
lines changed

8 files changed

+53
-15
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
[![](https://github.com/netrisai/netris-operator/workflows/Create%20release/badge.svg)](https://github.com/netrisai/netris-operator/actions)
66
[![Artifact HUB](https://img.shields.io/endpoint?url=https://artifacthub.io/badge/repository/netrisai)](https://artifacthub.io/packages/helm/netrisai/netris-operator)
77
[![Go Report Card](https://goreportcard.com/badge/github.com/netrisai/netris-operator)](https://goreportcard.com/report/github.com/netrisai/netris-operator)
8-
[![Language](https://img.shields.io/badge/Language-Go-blue.svg)](https://golang.org/)
98
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
109

1110

api/v1alpha1/l4lb_types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type L4LBSpec struct {
3232

3333
Check L4LBCheck `json:"check,omitempty"`
3434
OwnerTenant string `json:"ownerTenant,omitempty"`
35-
Site string `json:"site"`
35+
Site string `json:"site,omitempty"`
3636

3737
// +kubebuilder:validation:Enum=tcp;udp
3838
Protocol string `json:"protocol,omitempty"`

config/crd/bases/k8s.netris.ai_l4lbs.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ spec:
114114
required:
115115
- backend
116116
- frontend
117-
- site
118117
type: object
119118
status:
120119
description: L4LBStatus defines the observed state of L4LB

controllers/l4lb_translations.go

+47-7
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ func (r *L4LBReconciler) L4LBToL4LBMeta(l4lb *k8sv1alpha1.L4LB) (*k8sv1alpha1.L4
3939
var timeout string
4040
proto := "tcp"
4141

42-
if site, ok := NStorage.SitesStorage.FindByName(l4lb.Spec.Site); ok {
43-
siteID = site.ID
44-
} else {
45-
return nil, fmt.Errorf("'%s' site not found", l4lb.Spec.Site)
46-
}
47-
4842
l4lbMetaBackends := []k8sv1alpha1.L4LBMetaBackend{}
4943
ipForTenant := ""
5044

@@ -78,6 +72,22 @@ func (r *L4LBReconciler) L4LBToL4LBMeta(l4lb *k8sv1alpha1.L4LB) (*k8sv1alpha1.L4
7872
tenantID = tenant.ID
7973
}
8074

75+
if l4lb.Spec.Site == "" {
76+
siteid, err := findSiteByIP(ipForTenant)
77+
if err != nil {
78+
return nil, err
79+
}
80+
siteID = siteid
81+
}
82+
83+
if siteID == 0 {
84+
if site, ok := NStorage.SitesStorage.FindByName(l4lb.Spec.Site); ok {
85+
siteID = site.ID
86+
} else {
87+
return nil, fmt.Errorf("'%s' site not found", l4lb.Spec.Site)
88+
}
89+
}
90+
8191
if l4lb.Spec.State == "" || l4lb.Spec.State == "active" {
8292
state = "enable"
8393
} else {
@@ -396,5 +406,35 @@ func findTenantByIP(ip string) (int, error) {
396406
}
397407
}
398408

399-
return tenantID, fmt.Errorf("There is no subnets for specified IP address %s", ip)
409+
return tenantID, fmt.Errorf("There are no subnets for specified IP address %s", ip)
410+
}
411+
412+
func findSiteByIP(ip string) (int, error) {
413+
siteID := 0
414+
subnets, err := Cred.GetSubnets()
415+
if err != nil {
416+
return siteID, err
417+
}
418+
419+
subnetChilds := []api.APISubnetChild{}
420+
for _, subnet := range subnets {
421+
subnetChilds = append(subnetChilds, subnet.Children...)
422+
}
423+
424+
for _, subnet := range subnetChilds {
425+
ipAddr := net.ParseIP(ip)
426+
_, ipNet, err := net.ParseCIDR(fmt.Sprintf("%s/%d", subnet.Prefix, subnet.Length))
427+
if err != nil {
428+
return siteID, err
429+
}
430+
if ipNet.Contains(ipAddr) {
431+
sID, _ := strconv.Atoi(subnet.SiteID)
432+
if err != nil {
433+
return siteID, err
434+
}
435+
return sID, nil
436+
}
437+
}
438+
439+
return siteID, fmt.Errorf("There are no sites for specified IP address %s", ip)
400440
}

controllers/l4lbmeta_controller.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,10 @@ func (u *uniReconciler) updateL4LBIfNeccesarry(l4lbCR *v1alpha1.L4LB, l4lbMeta v
243243
l4lbCR.Spec.Frontend.IP = l4lbMeta.Spec.IP
244244
shouldUpdateCR = true
245245
}
246-
if l4lbCR.Spec.OwnerTenant == "" {
246+
if l4lbCR.Spec.OwnerTenant == "" || l4lbCR.Spec.Site == "" {
247247
if updatedL4LB, ok := NStorage.L4LBStorage.FindByID(l4lbMeta.Spec.ID); ok {
248248
l4lbCR.Spec.OwnerTenant = updatedL4LB.TenantName
249+
l4lbCR.Spec.Site = updatedL4LB.SiteName
249250
if l4lbCR.Spec.Frontend.IP == "" {
250251
l4lbCR.Spec.Frontend.IP = updatedL4LB.IP
251252
}

deploy/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ All resources are included in a single YAML manifest file:
1111
1) Install the CustomResourceDefinitions and netris-operator itself:
1212

1313
```
14-
kubectl apply -f https://github.com/netrisai/netris-operator/releases/download/v0.3.9/netris-operator.yaml
14+
kubectl apply -f https://github.com/netrisai/netris-operator/releases/download/v0.4.0/netris-operator.yaml
1515
```
1616

1717

deploy/charts/netris-operator/Chart.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ type: application
1515
# This is the chart version. This version number should be incremented each time you make changes
1616
# to the chart and its templates, including the app version.
1717
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18-
version: 0.3.1
18+
version: 0.3.2
1919

2020
# This is the version number of the application being deployed. This version number should be
2121
# incremented each time you make changes to the application. Versions are not expected to
2222
# follow Semantic Versioning. They should reflect the version the application is using.
23-
appVersion: v0.3.9
23+
appVersion: v0.4.0
2424
home: https://github.com/netrisai/netris-operator
2525
icon: https://www.netris.ai/wp-content/uploads/2020/05/logo-600.png # [todo] Change url to permalink
2626
keywords:

deploy/charts/netris-operator/crds/k8s.netris.ai_l4lbs.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ spec:
114114
required:
115115
- backend
116116
- frontend
117-
- site
118117
type: object
119118
status:
120119
description: L4LBStatus defines the observed state of L4LB

0 commit comments

Comments
 (0)