feat: Map Nacos instance weights to Istio WorkloadEntry weights in watchers (#3342)

Co-authored-by: EricaLiu <30773688+Erica177@users.noreply.github.com>
This commit is contained in:
aias00
2026-01-23 15:56:58 +08:00
committed by GitHub
parent a2eb599eff
commit 255f0bde76
4 changed files with 393 additions and 0 deletions

View File

@@ -15,6 +15,7 @@
package nacos
import (
"math"
"strconv"
"strings"
"sync"
@@ -352,10 +353,19 @@ func (w *watcher) generateServiceEntry(host string, services []model.SubscribeSe
portList = append(portList, port)
}
}
// Calculate weight from Nacos instance
// Nacos weight is float64, need to convert to uint32 for Istio
// Use math.Round to preserve fractional weights (e.g., 0.5, 1.5)
// If weight is 0 or negative, use default weight 1
weight := uint32(1)
if service.Weight > 0 {
weight = uint32(math.Round(service.Weight))
}
endpoint := v1alpha3.WorkloadEntry{
Address: service.Ip,
Ports: map[string]uint32{port.Protocol: port.Number},
Labels: service.Metadata,
Weight: weight,
}
endpoints = append(endpoints, &endpoint)
}