From ae88df18f3d1477d3f96a1a2eecd7aea8d1c31ec Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Thu, 4 Jan 2024 15:34:48 +0800 Subject: [PATCH] fix:sync instance version not set --- pkg/controller/apis.go | 7 ++- pkg/inject/pkg/kube/inject/inject.go | 11 +++-- pkg/util/address/address.go | 20 ++++++-- .../bootstrap_template.yaml | 44 ++++++++++------- .../bootstrap_template_tls.yaml | 49 ++++++++++--------- 5 files changed, 81 insertions(+), 50 deletions(-) diff --git a/pkg/controller/apis.go b/pkg/controller/apis.go index 596a39eb..c9c1628a 100644 --- a/pkg/controller/apis.go +++ b/pkg/controller/apis.go @@ -96,6 +96,11 @@ func (p *PolarisController) addInstances(service *v1.Service, addresses []addres addr := addresses[i] metadata := mergeMetadataWithService(service, addr, p.config.PolarisController.ClusterName) + version := metadata[util.PolarisCustomVersion] + // 如果没有读取到用户显示设置的自定义版本号,则从 addr 中读取 + if version == "" { + version = addr.Version + } *healthy = *healthy && addr.Healthy tmpInstance := polarisapi.Instance{ @@ -105,7 +110,7 @@ func (p *PolarisController) addInstances(service *v1.Service, addresses []addres HealthCheck: &healthCheck, Host: addr.IP, Protocol: addr.Protocol, - Version: metadata[util.PolarisCustomVersion], + Version: version, Port: util.IntPtr(addr.Port), Weight: util.IntPtr(addr.Weight), Healthy: healthy, diff --git a/pkg/inject/pkg/kube/inject/inject.go b/pkg/inject/pkg/kube/inject/inject.go index 9cdc02f3..e35fb2fc 100644 --- a/pkg/inject/pkg/kube/inject/inject.go +++ b/pkg/inject/pkg/kube/inject/inject.go @@ -42,7 +42,6 @@ import ( "github.com/polarismesh/polaris-controller/common/log" "github.com/polarismesh/polaris-controller/pkg/inject/api/annotation" meshconfig "github.com/polarismesh/polaris-controller/pkg/inject/api/mesh/v1alpha1" - "github.com/polarismesh/polaris-controller/pkg/polarisapi" "github.com/polarismesh/polaris-controller/pkg/util" utils "github.com/polarismesh/polaris-controller/pkg/util" ) @@ -469,10 +468,7 @@ func InjectionData(sidecarTemplate, valuesConfig, version string, typeMetadata * injectAnnonations := map[string]string{} // 设置需要注入到 envoy 的 metadata // 强制开启 XDS On-Demand 能力 - envoyMetadata := map[string]string{ - "sidecar.polarismesh.cn/openOnDemand": "true", - "sidecar.polarismesh.cn/odcdsServerEndpoint": polarisapi.PolarisGrpc, - } + envoyMetadata := map[string]string{} // 这里负责将需要额外塞入的 annonation 数据进行返回 if len(metadata.Annotations) != 0 { tlsMode := util.MTLSModeNone @@ -484,6 +480,11 @@ func InjectionData(sidecarTemplate, valuesConfig, version string, typeMetadata * } injectAnnonations[util.PolarisTLSMode] = tlsMode envoyMetadata[util.PolarisTLSMode] = tlsMode + + // 按需加载能力需要显示开启 + if val, ok := metadata.Annotations["sidecar.polarismesh.cn/openOnDemand"]; ok { + envoyMetadata["sidecar.polarismesh.cn/openOnDemand"] = val + } } // 这里需要将 sidecar 所属的服务信息注入到 annonation 中,方便下发到 envoy 的 bootstrap.yaml 中 // 命名空间可以不太强要求用户设置,大部份场景都是保持和 kubernetes 部署所在的 namespace 保持一致的 diff --git a/pkg/util/address/address.go b/pkg/util/address/address.go index 013f5e25..256954fe 100644 --- a/pkg/util/address/address.go +++ b/pkg/util/address/address.go @@ -41,6 +41,7 @@ type Address struct { Isolate bool Metadata map[string]string Protocol string + Version string PolarisInstance model.Instance } @@ -68,6 +69,10 @@ func buildAddresses(endpoint *v1.EndpointAddress, subset *v1.EndpointSubset, has } pod, err := podLister.Pods(endpoint.TargetRef.Namespace).Get(endpoint.TargetRef.Name) + if err != nil { + log.SyncNamingScope().Error("list pod info fail", zap.String("namespace", endpoint.TargetRef.Namespace), + zap.String("pod-name", endpoint.TargetRef.Name), zap.Error(err)) + } instanceSet := make(InstanceSet) for _, port := range subset.Ports { @@ -92,16 +97,25 @@ func buildAddresses(endpoint *v1.EndpointAddress, subset *v1.EndpointSubset, has } instanceSet[ipPort] = address - if err != nil { continue } - - address.Metadata = pod.Labels + address.Metadata = noEmptyMap(pod.Labels) + // 默认从 POD 的 labels 中获取到 version 字段的值 + address.Version = address.Metadata["version"] } return instanceSet } +var _emptyPodLabels = map[string]string{} + +func noEmptyMap(m map[string]string) map[string]string { + if len(m) == 0 { + return _emptyPodLabels + } + return m +} + func GetAddressMapFromEndpoints(service *v1.Service, endpoint *v1.Endpoints, podLister corelisters.PodLister, indexPortMap util.IndexPortMap) InstanceSet { instanceSet := make(InstanceSet) diff --git a/sidecar/envoy-bootstrap-config-generator/bootstrap_template.yaml b/sidecar/envoy-bootstrap-config-generator/bootstrap_template.yaml index f56a8e6a..7b8a5b73 100644 --- a/sidecar/envoy-bootstrap-config-generator/bootstrap_template.yaml +++ b/sidecar/envoy-bootstrap-config-generator/bootstrap_template.yaml @@ -19,6 +19,23 @@ node: metadata: METADATA static_resources: clusters: + - name: polaris_xds_server + connect_timeout: 5s + typed_extension_protocol_options: + envoy.extensions.upstreams.http.v3.HttpProtocolOptions: + "@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions + explicit_http_config: + http2_protocol_options: {} + type: STRICT_DNS + load_assignment: + cluster_name: polaris_xds_server + endpoints: + - lb_endpoints: + - endpoint: + address: + socket_address: + address: POLARIS_SERVER_HOST + port_value: POLARIS_SERVER_PORT - name: polaris_ratelimit type: STATIC connect_timeout: 1s @@ -35,29 +52,18 @@ static_resources: path: /var/run/polaris/ratelimit/rls.sock dynamic_resources: lds_config: - ads: {} - resource_api_version: V3 - cds_config: - ads: {} - resource_api_version: V3 + api_config_source: + api_type: DELTA_GRPC + transport_api_version: V3 + grpc_services: + envoy_grpc: + cluster_name: polaris_xds_server ads_config: api_type: DELTA_GRPC transport_api_version: V3 grpc_services: - - google_grpc: - target_uri: POLARIS_SERVER_URL - stat_prefix: polarismesh - channel_args: - args: - grpc.http2.max_ping_strikes: - int_value: 0 - grpc.keepalive_time_ms: - int_value: 10000 - grpc.keepalive_timeout_ms: - int_value: 20000 - grpc.max_receive_message_length: - int_value: -1 - + envoy_grpc: + cluster_name: polaris_xds_server admin: access_log_path: /dev/stdout address: diff --git a/sidecar/envoy-bootstrap-config-generator/bootstrap_template_tls.yaml b/sidecar/envoy-bootstrap-config-generator/bootstrap_template_tls.yaml index 7644415a..eb86cb9e 100644 --- a/sidecar/envoy-bootstrap-config-generator/bootstrap_template_tls.yaml +++ b/sidecar/envoy-bootstrap-config-generator/bootstrap_template_tls.yaml @@ -19,8 +19,8 @@ node: metadata: METADATA static_resources: clusters: - - connectTimeout: 0.250s - name: sds-grpc + - name: sds-grpc + connectTimeout: 0.250s type: STATIC typed_extension_protocol_options: envoy.extensions.upstreams.http.v3.HttpProtocolOptions: @@ -35,6 +35,23 @@ static_resources: address: pipe: path: /var/run/polaris/mtls/sds.sock + - name: polaris_xds_server + connect_timeout: 5s + typed_extension_protocol_options: + envoy.extensions.upstreams.http.v3.HttpProtocolOptions: + "@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions + explicit_http_config: + http2_protocol_options: {} + type: STRICT_DNS + load_assignment: + cluster_name: polaris_xds_server + endpoints: + - lb_endpoints: + - endpoint: + address: + socket_address: + address: POLARIS_SERVER_HOST + port_value: POLARIS_SERVER_PORT - name: polaris_ratelimit type: STATIC connect_timeout: 1s @@ -49,32 +66,20 @@ static_resources: address: pipe: path: /var/run/polaris/ratelimit/rls.sock - dynamic_resources: lds_config: - ads: {} - resource_api_version: V3 - cds_config: - ads: {} - resource_api_version: V3 + api_config_source: + api_type: DELTA_GRPC + transport_api_version: V3 + grpc_services: + envoy_grpc: + cluster_name: polaris_xds_server ads_config: api_type: DELTA_GRPC transport_api_version: V3 grpc_services: - - google_grpc: - target_uri: POLARIS_SERVER_URL - stat_prefix: polarismesh - channel_args: - args: - grpc.http2.max_ping_strikes: - int_value: 0 - grpc.keepalive_time_ms: - int_value: 10000 - grpc.keepalive_timeout_ms: - int_value: 20000 - grpc.max_receive_message_length: - int_value: -1 - + envoy_grpc: + cluster_name: polaris_xds_server admin: access_log_path: /dev/stdout address: