@@ -39,12 +39,6 @@ func (r *L4LBReconciler) L4LBToL4LBMeta(l4lb *k8sv1alpha1.L4LB) (*k8sv1alpha1.L4
39
39
var timeout string
40
40
proto := "tcp"
41
41
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
-
48
42
l4lbMetaBackends := []k8sv1alpha1.L4LBMetaBackend {}
49
43
ipForTenant := ""
50
44
@@ -78,6 +72,22 @@ func (r *L4LBReconciler) L4LBToL4LBMeta(l4lb *k8sv1alpha1.L4LB) (*k8sv1alpha1.L4
78
72
tenantID = tenant .ID
79
73
}
80
74
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
+
81
91
if l4lb .Spec .State == "" || l4lb .Spec .State == "active" {
82
92
state = "enable"
83
93
} else {
@@ -396,5 +406,35 @@ func findTenantByIP(ip string) (int, error) {
396
406
}
397
407
}
398
408
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 )
400
440
}
0 commit comments