AlexStocks commented on code in PR #892:
URL: https://github.com/apache/dubbo-go-pixiu/pull/892#discussion_r2934588415
##########
controllers/internal/controller/gateway_controller.go:
##########
@@ -529,18 +531,80 @@ func (r *GatewayReconciler) ensureGatewayConfigMap(ctx
context.Context, gateway
isGatewayPolicy := policy.Spec.TargetRef.Kind ==
"Gateway" &&
string(policy.Spec.TargetRef.Name) ==
gateway.Name
+ r.Log.Info("processing cluster policy", "policy",
policy.Name, "isGatewayPolicy", isGatewayPolicy)
+
if isGatewayPolicy {
for j := range policy.Spec.ClusterRef {
clusterConfig :=
&policy.Spec.ClusterRef[j]
clusterConfigMap[clusterConfig.Name] =
clusterConfig
+ r.Log.Info("added cluster config",
"clusterName", clusterConfig.Name, "endpointCount",
len(clusterConfig.Endpoints))
}
} else {
for j := range policy.Spec.ServiceRef {
serviceConfig :=
&policy.Spec.ServiceRef[j]
serviceConfigMap[serviceConfig.Name] =
serviceConfig
+ r.Log.Info("added service config",
"serviceName", serviceConfig.Name, "endpointCount",
len(serviceConfig.Endpoints))
}
}
}
+
+ r.Log.Info("cluster policy maps", "clusterConfigCount",
len(clusterConfigMap), "serviceConfigCount", len(serviceConfigMap))
+
+ for _, cluster := range pixiuConfig.StaticResources.Clusters {
+ r.Log.Info("generated cluster", "name", cluster.Name,
"endpointCount", len(cluster.Endpoints))
+ }
+
+ for _, cluster := range pixiuConfig.StaticResources.Clusters {
Review Comment:
`range` 循环中 `cluster` 是值拷贝还是指针取决于 `Clusters` 的元素类型。如果 `Clusters` 是
`[]*converter.Cluster`(指针类型),这里没问题。但如果是 `[]converter.Cluster`(值类型),那么对
`cluster` 调用 `ApplyClusterConfig` 以及后续的 endpoint 恢复操作都不会反映到原始切片中,整个 policy
应用逻辑全部无效。
如果是值类型,需改为:
```go
for i := range pixiuConfig.StaticResources.Clusters {
cluster := &pixiuConfig.StaticResources.Clusters[i]
// ...
}
```
##########
controllers/internal/controller/gateway_controller.go:
##########
@@ -1159,3 +1259,116 @@ func isNumeric(s string) bool {
}
return len(s) > 0
}
+
+// resolveServiceClusterEndpoints resolves Service DNS names in
ServiceClusterConfig endpoints to actual IPs
+func (r *GatewayReconciler) resolveServiceClusterEndpoints(ctx
context.Context, namespace string, cluster *converter.Cluster, serviceConfig
*v1alpha1.ServiceClusterConfig) {
+ if len(serviceConfig.Endpoints) == 0 {
+ return
+ }
+
+ for i := range serviceConfig.Endpoints {
+ ep := &serviceConfig.Endpoints[i]
+ if !isIPAddress(ep.Address) {
+ serviceName, serviceNamespace :=
parseServiceAddress(ep.Address, namespace)
+ r.Log.Info("resolving service DNS", "address",
ep.Address, "serviceName", serviceName, "namespace", serviceNamespace)
+
+ endpoints, err := r.resolveServiceEndpoints(ctx,
serviceNamespace, serviceName, ep.Port)
+ if err == nil && len(endpoints) > 0 {
+ r.Log.Info("resolved service to endpoints",
"service", serviceName, "endpointCount", len(endpoints))
+ serviceConfig.Endpoints =
[]v1alpha1.EndpointConfig{}
Review Comment:
遍历 `serviceConfig.Endpoints` 时,碰到第一个非 IP 地址的 endpoint 就用
`serviceConfig.Endpoints = []v1alpha1.EndpointConfig{}` 替换整个切片并
`return`。如果有多个不同 DNS 名的 endpoint,后续的都被丢弃了。
建议遍历所有 endpoint,收集所有 resolved 结果后统一替换:
```go
var resolved []v1alpha1.EndpointConfig
for _, ep := range serviceConfig.Endpoints {
if isIPAddress(ep.Address) {
resolved = append(resolved, ep)
} else {
endpoints, err := lookupEndpoints(ep.Address)
// ... resolve and append all
}
}
serviceConfig.Endpoints = resolved
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]