Skip to content

Commit

Permalink
fix:sync instance version not set
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun committed Jan 4, 2024
1 parent f806d33 commit ae88df1
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 50 deletions.
7 changes: 6 additions & 1 deletion pkg/controller/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions pkg/inject/pkg/kube/inject/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -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 保持一致的
Expand Down
20 changes: 17 additions & 3 deletions pkg/util/address/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Address struct {
Isolate bool
Metadata map[string]string
Protocol string
Version string
PolarisInstance model.Instance
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
44 changes: 25 additions & 19 deletions sidecar/envoy-bootstrap-config-generator/bootstrap_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit ae88df1

Please sign in to comment.