AsperforMias commented on code in PR #1552: URL: https://github.com/apache/dubbo-admin/pull/1552#discussion_r4112248940
########## 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` and `runtime` are plain bools, and neither has `omitempty`. When the YAML omits them they decode as false, and `EncodeRule` writes `enabled: false` and `runtime: false` back out. Java `AbstractRouterRule` defaults both to true and only overrides them when the key is present. `AffinityStateRouter` returns the invokers unchanged on `!enabled`, so affinity does not run. Dubbo-Go affinity goes the other way: the `default:"true"` tag is never applied, so a missing key stays false and the rule stays off. Filling in true or false changes one of the two runtimes. An absent key has to stay absent. Only an explicit false should be written. The current tests only build structs that already set `Enabled`. Add a case where the original document has neither key and `encode(decode())` still omits them. ########## 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 agrees on both sides. Java `ScriptRule` and Dubbo-Go both treat a missing `enabled` as on. Go uses `cfg.Enabled == nil || *cfg.Enabled`. A missing key becomes false here, and publishing again disables the script on both Java and Go. An explicit `enabled: false` still has to be written out. ########## 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: A non-empty document is accepted without `ValidateRule`. An out-of-range ratio, a bad scope, a type other than javascript, or an empty script all come back with a nil error. `EncodeRule` still rejects a normal write. An empty body can stay a tombstone and skip validation. Anything else should return the validation error. `To*` already turns YAML errors into nil, and a validation failure should do the same. The script decode return has the same gap. -- 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]
