Skip to content

Commit e25f398

Browse files
committed
fix: resolve circular dependency and build errors
Fix multiple compilation issues from previous commits: 1. Circular dependency issue: - Move index registration from rule_version_types.go init() to index/rule_version.go using RegisterIndexers pattern - Follow established pattern used by Service and other resources 2. Resource interface implementation: - Implement full k8s runtime.Object interface in RuleVersion - Add metav1.TypeMeta and metav1.ObjectMeta fields - Add DeepCopyObject, String, and list type support - Match pattern from service_types.go 3. Index function fixes: - Use rv.Mesh instead of rv.GetMeta().GetMesh() - Use IndexCondition{Value, Operator} instead of IndexKey 4. Config nil safety: - Add RuleVersioning nil checks and initialization in Sanitize/PreProcess/PostProcess - Ensure tests pass when RuleVersioning is nil 5. Handler fix: - Update ensureVersioningEnabled to use RuleVersioning field All tests now pass and project builds successfully.
1 parent 7457a1f commit e25f398

5 files changed

Lines changed: 155 additions & 75 deletions

File tree

pkg/config/app/admin.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func (c *AdminConfig) Sanitize() {
8383
c.Observability.Sanitize()
8484
c.Diagnostics.Sanitize()
8585
c.Log.Sanitize()
86+
if c.RuleVersioning == nil {
87+
c.RuleVersioning = versioning.Default()
88+
}
8689
c.RuleVersioning.Sanitize()
8790
}
8891

@@ -95,6 +98,9 @@ func (c *AdminConfig) PreProcess() error {
9598
}
9699
return nil
97100
}
101+
if c.RuleVersioning == nil {
102+
c.RuleVersioning = versioning.Default()
103+
}
98104
return multierr.Combine(
99105
c.Engine.PreProcess(),
100106
discoveryPreProcess(),
@@ -116,6 +122,9 @@ func (c *AdminConfig) PostProcess() error {
116122
}
117123
return nil
118124
}
125+
if c.RuleVersioning == nil {
126+
c.RuleVersioning = versioning.Default()
127+
}
119128
return multierr.Combine(
120129
c.Engine.PostProcess(),
121130
discoveryPostProcess(),

pkg/console/handler/rule_version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func currentUser(c *gin.Context) string {
197197
}
198198

199199
func ensureVersioningEnabled(c *gin.Context, cs consolectx.Context) bool {
200-
if cs.RuleVersioning() != nil && cs.Config().Versioning != nil && cs.Config().Versioning.Enabled {
200+
if cs.RuleVersioning() != nil && cs.Config().RuleVersioning != nil && cs.Config().RuleVersioning.Enabled {
201201
return true
202202
}
203203
c.JSON(http.StatusServiceUnavailable, &model.CommonResp{

pkg/core/resource/apis/mesh/v1alpha1/rule_version_types.go

Lines changed: 113 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,75 +18,146 @@
1818
package v1alpha1
1919

2020
import (
21+
"encoding/json"
2122
"fmt"
2223

24+
"google.golang.org/protobuf/proto"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
k8sruntime "k8s.io/apimachinery/pkg/runtime"
27+
2328
meshproto "github.com/apache/dubbo-admin/api/mesh/v1alpha1"
29+
"github.com/apache/dubbo-admin/pkg/core/logger"
2430
coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model"
25-
"github.com/apache/dubbo-admin/pkg/core/store/index"
2631
)
2732

28-
const (
29-
RuleVersionKind coremodel.ResourceKind = "RuleVersion"
30-
)
33+
const RuleVersionKind coremodel.ResourceKind = "RuleVersion"
3134

32-
var _ coremodel.Resource = &RuleVersion{}
35+
func init() {
36+
coremodel.RegisterResourceSchema(RuleVersionKind, NewRuleVersion, NewRuleVersionList)
37+
}
3338

3439
type RuleVersion struct {
35-
Meta coremodel.ResourceMeta
36-
Spec *meshproto.RuleVersion
37-
}
40+
metav1.TypeMeta `json:",inline"`
3841

39-
func NewRuleVersion() *RuleVersion {
40-
return &RuleVersion{
41-
Spec: &meshproto.RuleVersion{},
42-
}
43-
}
42+
metav1.ObjectMeta `json:"metadata,omitempty"`
4443

45-
func (r *RuleVersion) GetMeta() coremodel.ResourceMeta {
46-
return r.Meta
47-
}
44+
// Mesh is the name of the dubbo mesh this resource belongs to.
45+
Mesh string `json:"mesh,omitempty"`
4846

49-
func (r *RuleVersion) SetMeta(meta coremodel.ResourceMeta) {
50-
r.Meta = meta
47+
// Spec is the specification of the RuleVersion resource.
48+
Spec *meshproto.RuleVersion `json:"spec,omitempty"`
5149
}
5250

53-
func (r *RuleVersion) GetSpec() coremodel.ResourceSpec {
54-
return r.Spec
51+
func (r *RuleVersion) ResourceKind() coremodel.ResourceKind {
52+
return RuleVersionKind
5553
}
5654

57-
func (r *RuleVersion) SetSpec(spec coremodel.ResourceSpec) error {
58-
value, ok := spec.(*meshproto.RuleVersion)
59-
if !ok {
60-
return fmt.Errorf("invalid spec type: %T", spec)
61-
}
62-
r.Spec = value
63-
return nil
64-
}
65-
66-
func (r *RuleVersion) Descriptor() coremodel.ResourceTypeDescriptor {
67-
return coremodel.ResourceTypeDescriptor{
68-
Kind: RuleVersionKind,
69-
}
55+
func (r *RuleVersion) ResourceMesh() string {
56+
return r.Mesh
7057
}
7158

7259
// ResourceKey format: /{mesh}/{name}
7360
// Name format: {parentKind}_{parentName}_v{versionNo}
7461
// Example: /default/ConditionRoute_my-service_v5
75-
//
76-
// Note: Strictly follows existing ResourceKey format, using underscores to avoid URL encoding issues
7762
func (r *RuleVersion) ResourceKey() string {
63+
if r.Spec == nil {
64+
return coremodel.BuildResourceKey(r.Mesh, r.Name)
65+
}
7866
name := fmt.Sprintf("%s_%s_v%d",
7967
r.Spec.GetParentRuleKind(),
8068
r.Spec.GetParentRuleName(),
8169
r.Spec.GetVersionNo(),
8270
)
83-
return coremodel.BuildResourceKey(r.Meta.GetMesh(), name)
71+
return coremodel.BuildResourceKey(r.Mesh, name)
8472
}
8573

86-
func init() {
87-
coremodel.RegisterResourceSchema(RuleVersionKind, &RuleVersion{},
88-
index.ByMesh(),
89-
index.RuleVersionByParentRule(),
90-
index.RuleVersionByContentHash(),
91-
)
74+
func (r *RuleVersion) ResourceMeta() metav1.ObjectMeta {
75+
return r.ObjectMeta
76+
}
77+
78+
func (r *RuleVersion) ResourceSpec() coremodel.ResourceSpec {
79+
return r.Spec
80+
}
81+
82+
func (r *RuleVersion) DeepCopyObject() k8sruntime.Object {
83+
out := &RuleVersion{
84+
TypeMeta: r.TypeMeta,
85+
Mesh: r.Mesh,
86+
}
87+
88+
r.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
89+
90+
if r.Spec != nil {
91+
spec, ok := proto.Clone(r.Spec).(*meshproto.RuleVersion)
92+
if !ok {
93+
logger.Warnf("failed to clone spec %v, spec is not conformed to %s", r.Spec, r.ResourceKind())
94+
return out
95+
}
96+
out.Spec = spec
97+
}
98+
99+
return out
100+
}
101+
102+
func (r *RuleVersion) String() string {
103+
jsonStr, err := json.Marshal(r)
104+
if err != nil {
105+
logger.Errorf("failed to encode RuleVersion: %s to json, err: %v", r.ResourceKey(), err)
106+
return ""
107+
}
108+
return string(jsonStr)
109+
}
110+
111+
func NewRuleVersion() coremodel.Resource {
112+
return &RuleVersion{
113+
TypeMeta: metav1.TypeMeta{
114+
Kind: string(RuleVersionKind),
115+
APIVersion: "v1alpha1",
116+
},
117+
Spec: &meshproto.RuleVersion{},
118+
}
119+
}
120+
121+
type RuleVersionList struct {
122+
metav1.TypeMeta `json:",inline"`
123+
metav1.ListMeta `json:"metadata,omitempty"`
124+
Items []*RuleVersion `json:"items"`
125+
}
126+
127+
func (r *RuleVersionList) DeepCopyObject() k8sruntime.Object {
128+
out := &RuleVersionList{
129+
TypeMeta: r.TypeMeta,
130+
}
131+
r.ListMeta.DeepCopyInto(&out.ListMeta)
132+
133+
if len(r.Items) == 0 {
134+
return out
135+
}
136+
out.Items = make([]*RuleVersion, len(r.Items))
137+
for i := range r.Items {
138+
out.Items[i] = r.Items[i].DeepCopyObject().(*RuleVersion)
139+
}
140+
return out
141+
}
142+
143+
func NewRuleVersionList() coremodel.ResourceList {
144+
return &RuleVersionList{
145+
TypeMeta: metav1.TypeMeta{
146+
Kind: string(RuleVersionKind),
147+
APIVersion: "v1alpha1",
148+
},
149+
Items: make([]*RuleVersion, 0),
150+
}
151+
}
152+
153+
func (r *RuleVersionList) SetItems(items []coremodel.Resource) {
154+
r.Items = make([]*RuleVersion, len(items))
155+
for i := range items {
156+
res, ok := items[i].(*RuleVersion)
157+
if !ok {
158+
logger.Errorf("unexpected resource type, expected: %s, get %s", RuleVersionKind, res.ResourceKind())
159+
continue
160+
}
161+
r.Items[i] = res
162+
}
92163
}

pkg/core/store/index/rule_version.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,55 +19,54 @@ package index
1919

2020
import (
2121
"fmt"
22+
"reflect"
2223

23-
v1alpha1 "github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1"
24+
"k8s.io/client-go/tools/cache"
25+
26+
"github.com/apache/dubbo-admin/pkg/common/bizerror"
27+
meshresource "github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1"
2428
)
2529

2630
const (
2731
RuleVersionParentRuleIndexName = "parent_rule"
2832
RuleVersionContentHashIndexName = "content_hash"
2933
)
3034

31-
// RuleVersionByParentRule defines an IndexDefinition for indexing by parent rule
32-
// Used during registration: to automatically establish indexes
33-
func RuleVersionByParentRule() IndexDefinition {
34-
return IndexDefinition{
35-
Name: RuleVersionParentRuleIndexName,
36-
KeyFunc: func(obj interface{}) ([]string, error) {
37-
rv, ok := obj.(*v1alpha1.RuleVersion)
38-
if !ok {
39-
return nil, fmt.Errorf("expected *RuleVersion, got %T", obj)
40-
}
41-
key := fmt.Sprintf("%s:%s:%s",
42-
rv.GetMeta().GetMesh(),
43-
rv.Spec.GetParentRuleKind(),
44-
rv.Spec.GetParentRuleName(),
45-
)
46-
return []string{key}, nil
47-
},
35+
func init() {
36+
RegisterIndexers(meshresource.RuleVersionKind, map[string]cache.IndexFunc{
37+
RuleVersionParentRuleIndexName: byParentRule,
38+
RuleVersionContentHashIndexName: byContentHash,
39+
})
40+
}
41+
42+
func byParentRule(obj interface{}) ([]string, error) {
43+
rv, ok := obj.(*meshresource.RuleVersion)
44+
if !ok {
45+
return nil, bizerror.NewAssertionError(string(meshresource.RuleVersionKind), reflect.TypeOf(obj).Name())
4846
}
47+
key := fmt.Sprintf("%s:%s:%s",
48+
rv.Mesh,
49+
rv.Spec.GetParentRuleKind(),
50+
rv.Spec.GetParentRuleName(),
51+
)
52+
return []string{key}, nil
4953
}
5054

51-
// RuleVersionByContentHash defines an IndexDefinition for indexing by content hash
52-
func RuleVersionByContentHash() IndexDefinition {
53-
return IndexDefinition{
54-
Name: RuleVersionContentHashIndexName,
55-
KeyFunc: func(obj interface{}) ([]string, error) {
56-
rv, ok := obj.(*v1alpha1.RuleVersion)
57-
if !ok {
58-
return nil, fmt.Errorf("expected *RuleVersion, got %T", obj)
59-
}
60-
return []string{rv.Spec.GetContentHash()}, nil
61-
},
55+
func byContentHash(obj interface{}) ([]string, error) {
56+
rv, ok := obj.(*meshresource.RuleVersion)
57+
if !ok {
58+
return nil, bizerror.NewAssertionError(string(meshresource.RuleVersionKind), reflect.TypeOf(obj).Name())
6259
}
60+
return []string{rv.Spec.GetContentHash()}, nil
6361
}
6462

6563
// ByParentRule creates a query condition (used during queries)
6664
// Used to query all versions of a specific rule
6765
func ByParentRule(mesh, parentKind, parentName string) IndexCondition {
6866
return IndexCondition{
6967
IndexName: RuleVersionParentRuleIndexName,
70-
IndexKey: fmt.Sprintf("%s:%s:%s", mesh, parentKind, parentName),
68+
Value: fmt.Sprintf("%s:%s:%s", mesh, parentKind, parentName),
69+
Operator: Equals,
7170
}
7271
}
7372

@@ -76,6 +75,7 @@ func ByParentRule(mesh, parentKind, parentName string) IndexCondition {
7675
func ByContentHash(hash string) IndexCondition {
7776
return IndexCondition{
7877
IndexName: RuleVersionContentHashIndexName,
79-
IndexKey: hash,
78+
Value: hash,
79+
Operator: Equals,
8080
}
8181
}

pkg/core/versioning/component.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (c *component) Init(ctx runtime.BuilderContext) error {
121121
return nil
122122
}
123123

124-
func (c *component) Start(rt runtime.Runtime, stop <-chan struct) error {
124+
func (c *component) Start(rt runtime.Runtime, stop <-chan struct{}) error {
125125
cfg := rt.Config().RuleVersioning
126126
if cfg == nil {
127127
cfg = versioningcfg.Default()

0 commit comments

Comments
 (0)