AsperforMias commented on code in PR #1552: URL: https://github.com/apache/dubbo-admin/pull/1552#discussion_r4112248948
########## pkg/core/resource/apis/mesh/v1alpha1/router_rule_codec.go: ########## @@ -0,0 +1,239 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package v1alpha1 + +import ( + "fmt" + "strings" + + "sigs.k8s.io/yaml" + + meshproto "github.com/apache/dubbo-admin/api/mesh/v1alpha1" + "github.com/apache/dubbo-admin/pkg/common/bizerror" + "github.com/apache/dubbo-admin/pkg/common/constants" + coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model" +) + +// The protobuf field is named affinity for historical/internal reasons. The +// public Dubbo 3 contract is affinityAware, so never marshal the protobuf +// message directly for these resources. +type affinityRouteYAML struct { + ConfigVersion string `json:"configVersion"` + Scope string `json:"scope"` + Key string `json:"key"` + Runtime bool `json:"runtime"` + Enabled bool `json:"enabled"` + AffinityAware *affinityAwareYAML `json:"affinityAware"` +} + +type affinityAwareYAML struct { + Key string `json:"key"` + Ratio int32 `json:"ratio"` +} + +type scriptRouteYAML struct { + ConfigVersion string `json:"configVersion"` + Scope string `json:"scope"` + Key string `json:"key"` + Enabled bool `json:"enabled"` Review Comment: script 这边两边是一致的。Java `ScriptRule` 和 Dubbo-Go 都把缺省 `enabled` 当成开启,Go 是 `cfg.Enabled == nil || *cfg.Enabled`。这里缺键会落成 false,再发布一次,Java 和 Go 都会停掉这条脚本。显式 `enabled: false` 还是要能写出去。 ########## pkg/core/resource/apis/mesh/v1alpha1/router_rule_codec.go: ########## @@ -0,0 +1,239 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package v1alpha1 + +import ( + "fmt" + "strings" + + "sigs.k8s.io/yaml" + + meshproto "github.com/apache/dubbo-admin/api/mesh/v1alpha1" + "github.com/apache/dubbo-admin/pkg/common/bizerror" + "github.com/apache/dubbo-admin/pkg/common/constants" + coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model" +) + +// The protobuf field is named affinity for historical/internal reasons. The +// public Dubbo 3 contract is affinityAware, so never marshal the protobuf +// message directly for these resources. +type affinityRouteYAML struct { + ConfigVersion string `json:"configVersion"` + Scope string `json:"scope"` + Key string `json:"key"` + Runtime bool `json:"runtime"` + Enabled bool `json:"enabled"` Review Comment: `enabled` 和 `runtime` 是普通 bool,又没有 `omitempty`。YAML 里没有这两个键时会解成 false,`EncodeRule` 再写回去就变成 `enabled: false`、`runtime: false`。 Java `AbstractRouterRule` 这两个字段默认是 true,键不存在不会覆盖。`AffinityStateRouter` 在 `!enabled` 时直接把 invoker 原样返回,不做亲和过滤。Dubbo-Go 的 affinity 又是反的:结构体上的 `default:"true"` 没有生效,缺省就是 false,规则不启用。 缺省不能填成 true,也不能填成 false。键不在就保持不在,只有显式 false 才写出来。现有测试只覆盖了结构体里已经带了 `Enabled` 的情况,补一条原文没有这两个键、`encode(decode())` 之后仍然没有的用例。 ########## pkg/core/resource/apis/mesh/v1alpha1/router_rule_codec.go: ########## @@ -0,0 +1,239 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package v1alpha1 + +import ( + "fmt" + "strings" + + "sigs.k8s.io/yaml" + + meshproto "github.com/apache/dubbo-admin/api/mesh/v1alpha1" + "github.com/apache/dubbo-admin/pkg/common/bizerror" + "github.com/apache/dubbo-admin/pkg/common/constants" + coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model" +) + +// The protobuf field is named affinity for historical/internal reasons. The +// public Dubbo 3 contract is affinityAware, so never marshal the protobuf +// message directly for these resources. +type affinityRouteYAML struct { + ConfigVersion string `json:"configVersion"` + Scope string `json:"scope"` + Key string `json:"key"` + Runtime bool `json:"runtime"` + Enabled bool `json:"enabled"` + AffinityAware *affinityAwareYAML `json:"affinityAware"` +} + +type affinityAwareYAML struct { + Key string `json:"key"` + Ratio int32 `json:"ratio"` +} + +type scriptRouteYAML struct { + ConfigVersion string `json:"configVersion"` + Scope string `json:"scope"` + Key string `json:"key"` + Enabled bool `json:"enabled"` + Type string `json:"type"` + Script string `json:"script"` +} + +// EncodeRule serializes a rule resource using the public Dubbo YAML contract. +// Existing rule kinds fall back to their historical protobuf/YAML encoding; +// Affinity and Script use explicit codecs because their public field names and +// boolean defaults differ from the internal protobuf shape. +func EncodeRule(r coremodel.Resource) ([]byte, error) { + if err := ValidateRule(r); err != nil { + return nil, err + } + var value any + switch typed := r.(type) { + case *AffinityRouteResource: + var aware *affinityAwareYAML + if typed.Spec.Affinity != nil { + aware = &affinityAwareYAML{Key: typed.Spec.Affinity.Key, Ratio: typed.Spec.Affinity.Ratio} + } + value = affinityRouteYAML{ + ConfigVersion: typed.Spec.ConfigVersion, + Scope: typed.Spec.Scope, + Key: typed.Spec.Key, + Runtime: typed.Spec.Runtime, + Enabled: typed.Spec.Enabled, + AffinityAware: aware, + } + case *ScriptRouteResource: + value = scriptRouteYAML{ + ConfigVersion: typed.Spec.ConfigVersion, + Scope: typed.Spec.Scope, + Key: typed.Spec.Key, + Enabled: typed.Spec.Enabled, + Type: typed.Spec.Type, + Script: typed.Spec.Script, + } + default: + if r == nil || r.ResourceSpec() == nil { + return nil, invalidRule("rule spec is required") + } + value = r.ResourceSpec() + } + data, err := yaml.Marshal(value) + if err != nil { + return nil, bizerror.Wrap(err, bizerror.YamlError, "failed to marshal rule") + } + return data, nil +} + +// DecodeRule parses a public rule YAML document into a typed resource. It is +// used by both registry subscribers and governor read-back paths. +func DecodeRule(kind coremodel.ResourceKind, mesh, name, data string) (coremodel.Resource, error) { + if strings.TrimSpace(data) == "" { + switch kind { + case AffinityRouteKind: + return NewAffinityRouteResourceWithAttributes(name, mesh), nil + case ScriptRouteKind: + return NewScriptRouteResourceWithAttributes(name, mesh), nil + } + } + switch kind { + case AffinityRouteKind: + external := affinityRouteYAML{} + if err := yaml.Unmarshal([]byte(data), &external); err != nil { + return nil, ruleYAMLError(name, err) + } + res := NewAffinityRouteResourceWithAttributes(name, mesh) + if external.AffinityAware != nil { + res.Spec.Affinity = &meshproto.AffinityAware{ + Key: external.AffinityAware.Key, + Ratio: external.AffinityAware.Ratio, + } + } + res.Spec.ConfigVersion = external.ConfigVersion + res.Spec.Scope = external.Scope + res.Spec.Key = external.Key + res.Spec.Runtime = external.Runtime + res.Spec.Enabled = external.Enabled + return res, nil Review Comment: 非空文档解成功之后没有跑 `ValidateRule`。ratio 越界、scope 不对、type 不是 javascript、空脚本,都会以 nil error 返回。`EncodeRule` 能拦住正规写入;空 body 的 tombstone 可以继续不校验,非空的应该把校验错误返回出去。`To*` 已经会把 YAML 错误变成 nil,校验失败同样处理就行。 script 的解码返回也一样。 -- 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]
