Alanxtl commented on code in PR #1046: URL: https://github.com/apache/dubbo-go-pixiu/pull/1046#discussion_r4291114401
########## admin/logic/route_binding.go: ########## @@ -0,0 +1,1367 @@ +/* + * 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 logic + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net/url" + "reflect" + "sort" + "strconv" + "strings" + "time" +) + +import ( + clientv3 "go.etcd.io/etcd/client/v3" +) + +import ( + adminconfig "github.com/apache/dubbo-go-pixiu/admin/config" + commonyaml "github.com/apache/dubbo-go-pixiu/pkg/common/yaml" + legacyconfig "github.com/apache/dubbo-go-pixiu/pkg/config" + "github.com/apache/dubbo-go-pixiu/pkg/config/schema" +) + +const ( + routeBindingPathSegment = "route-bindings" + routeBindingRevisionSegment = "route-bindings-revision" + routeBindingRuntimeResourceDir = "resources" + maxRouteBindingIDRetries = 16 +) + +var ( + ErrRouteBindingNotFound = errors.New("admin route binding not found") + ErrRouteBindingAlreadyExists = errors.New("admin route binding already exists") + ErrRouteBindingNameImmutable = errors.New("admin route binding metadata.name is immutable") + ErrRouteBindingConflict = errors.New("admin route binding was modified concurrently") + ErrRouteBindingPublishConflict = errors.New("admin route binding publish conflict") + ErrRouteBindingRuntimeConflict = errors.New("admin route binding conflicts with an existing runtime route") +) + +// routeBindingKV is the small part of clientv3.KV needed by the route store. +// Keeping this boundary narrow makes the planning and transaction logic easy +// to exercise without coupling it to the gost wrapper. +type routeBindingKV interface { + Get(context.Context, string, ...clientv3.OpOption) (*clientv3.GetResponse, error) + Txn(context.Context) clientv3.Txn +} + +// RouteBinding is the Admin-facing response shape. ResourceID and MethodID +// are stable runtime identities used by Pixiu's legacy etcd watcher; they are +// not part of the user-editable AdminRouteBinding object. +type RouteBinding struct { + Object schema.AdminObject `json:"object"` + ResourceID int `json:"resourceId"` + MethodID int `json:"methodId"` + Revision int64 `json:"revision"` + PublishStatus *RouteBindingPublishStatus `json:"publishStatus,omitempty"` +} + +// RouteBindingPublishResult describes one atomic publish transaction. +type RouteBindingPublishResult struct { + Name string `json:"name"` + Revision int64 `json:"revision"` + DraftRevision int64 `json:"draftRevision"` + PublishedRevision int64 `json:"publishedRevision"` + PublishedCount int `json:"publishedCount"` + DeletedCount int `json:"deletedCount"` +} + +// RouteBindingPublishStatus exposes one route's draft and published state. +// Revisions are the etcd key mod revisions for that route, not a global +// configuration revision. +type RouteBindingPublishStatus struct { + Name string `json:"name"` + DraftRevision int64 `json:"draftRevision"` + PublishedRevision int64 `json:"publishedRevision"` + DraftExists bool `json:"draftExists"` + PublishedExists bool `json:"publishedExists"` + Dirty bool `json:"dirty"` +} + +type RouteBindingDiffChange struct { + Path string `json:"path"` + Before any `json:"before"` + After any `json:"after"` +} + +// RouteBindingDiff compares one route's draft object with its published +// object. A nil side represents a route that only exists in the other scope. +type RouteBindingDiff struct { + Name string `json:"name"` + Draft *RouteBinding `json:"draft,omitempty"` + Published *RouteBinding `json:"published,omitempty"` + Changes []RouteBindingDiffChange `json:"changes"` +} + +type routeBindingRecord struct { + Object schema.AdminObject `json:"object"` + ResourceID int `json:"resourceId"` + MethodID int `json:"methodId"` +} + +type routeBindingEntry struct { + record routeBindingRecord + key string + revision int64 +} + +// RouteBindingStore owns the AdminRouteBinding persistence and publish +// boundary. Published runtime data is deliberately written in the existing +// Resource/Method layout so the current Pixiu watcher can consume it. +type RouteBindingStore struct { + kv routeBindingKV + root string + registry *schema.Registry +} + +// NewRouteBindingStore creates a store over an etcd v3 client. +func NewRouteBindingStore(kv routeBindingKV, root string, registry *schema.Registry) (*RouteBindingStore, error) { + if kv == nil { + return nil, errors.New("route binding etcd client is nil") + } + root = strings.TrimRight(strings.TrimSpace(root), "/") + if root == "" { + return nil, errors.New("route binding etcd root path is empty") + } + if registry == nil { + var err error + registry, err = schema.NewBuiltinRegistry() + if err != nil { + return nil, fmt.Errorf("create route binding schema registry: %w", err) + } + } + return &RouteBindingStore{kv: kv, root: root, registry: registry}, nil +} + +// NewAdminRouteBindingStore creates the production store from the Admin +// process' configured etcd client and root path. +func NewAdminRouteBindingStore() (*RouteBindingStore, error) { + if adminconfig.Bootstrap == nil { + return nil, errors.New("admin bootstrap is nil") + } + if adminconfig.Client == nil { + return nil, errors.New("admin etcd client is nil") + } + rawClient := adminconfig.Client.GetRawClient() + if rawClient == nil { + return nil, errors.New("admin raw etcd client is nil") + } + registry, err := schema.DefaultRegistry() + if err != nil { + return nil, fmt.Errorf("get Admin route binding schema registry: %w", err) + } + return NewRouteBindingStore(rawClient, adminconfig.Bootstrap.GetPath(), registry) +} + +func routeBindingContext(ctx context.Context) context.Context { + if ctx == nil { + return context.Background() + } + return ctx +} + +// Registry returns the schema registry used by this store. +func (s *RouteBindingStore) Registry() *schema.Registry { + return s.registry +} + +// List returns high-level route bindings from either the draft or published +// namespace. Empty namespaces are returned as an empty slice. +func (s *RouteBindingStore) List(ctx context.Context, unpublished bool) ([]RouteBinding, error) { + ctx = routeBindingContext(ctx) + entries, err := s.listEntries(ctx, s.bindingPrefix(unpublished)) + if err != nil { + return nil, err + } + var statuses map[string]RouteBindingPublishStatus + if unpublished { + publishedEntries, err := s.listEntries(ctx, s.bindingPrefix(false)) + if err != nil { + return nil, err + } + statuses = routeBindingStatuses(entries, publishedEntries) + } + result := make([]RouteBinding, 0, len(entries)) + for _, entry := range entries { + view := routeBindingView(entry) + if status, exists := statuses[entry.record.Object.Metadata.Name]; exists { + view.PublishStatus = &status + } + result = append(result, view) + } + return result, nil +} + +func routeBindingStatuses(draftEntries, publishedEntries []routeBindingEntry) map[string]RouteBindingPublishStatus { + publishedByName := make(map[string]routeBindingEntry, len(publishedEntries)) + for _, entry := range publishedEntries { + publishedByName[entry.record.Object.Metadata.Name] = entry + } + + statuses := make(map[string]RouteBindingPublishStatus, len(draftEntries)) + for _, draft := range draftEntries { + name := draft.record.Object.Metadata.Name + published, publishedExists := publishedByName[name] + status := RouteBindingPublishStatus{ + Name: name, + DraftRevision: draft.revision, + DraftExists: true, + PublishedExists: publishedExists, + PublishedRevision: published.revision, + Dirty: !publishedExists || !reflect.DeepEqual(draft.record.Object, published.record.Object), + } + statuses[name] = status + } + return statuses +} + +// Get returns one high-level route binding from either the draft or published +// namespace. +func (s *RouteBindingStore) Get(ctx context.Context, name string, unpublished bool) (RouteBinding, error) { + ctx = routeBindingContext(ctx) + name, err := normalizeRouteBindingName(name) + if err != nil { + return RouteBinding{}, err + } + entry, exists, _, err := s.getEntry(ctx, s.bindingPrefix(unpublished), name) + if err != nil { + return RouteBinding{}, err + } + if !exists { + return RouteBinding{}, fmt.Errorf("%w: %s", ErrRouteBindingNotFound, name) + } + return routeBindingView(entry), nil +} + +// Normalize validates an object and applies all registered schema defaults. +// It is useful to the controller's validate and preview endpoints and does +// not require an etcd write. +func (s *RouteBindingStore) Normalize(object schema.AdminObject) (schema.AdminObject, error) { + normalized, err := s.registry.Normalize(object) + if err != nil { + return normalized, err + } + name, err := normalizeRouteBindingName(normalized.Metadata.Name) + if err != nil { + return normalized, err + } + normalized.Metadata.Name = name + return normalized, nil +} + +// Preview validates and compiles an Admin object without writing it. +func (s *RouteBindingStore) Preview(object schema.AdminObject) (schema.AdminObject, []byte, error) { + normalized, err := s.Normalize(object) + if err != nil { + return normalized, nil, err + } + compiled, err := schema.CompileAdminRouteBinding(s.registry, normalized) + if err != nil { + return normalized, nil, err + } + preview, err := compiled.PreviewYAML() + if err != nil { + return normalized, nil, err + } + return normalized, preview, nil +} + +// SaveDraft validates and atomically stores one AdminRouteBinding in the +// draft namespace. POST callers use create=true; PUT callers use create=false. +// expectedRevision is the draft key's mod revision and may be zero to allow +// an unconditional write. +func (s *RouteBindingStore) SaveDraft(ctx context.Context, object schema.AdminObject, create bool, expectedRevision int64) (RouteBinding, error) { + ctx = routeBindingContext(ctx) + if err := validateSaveDraftRequest(create, expectedRevision); err != nil { + return RouteBinding{}, err + } + normalized, err := s.Normalize(object) + if err != nil { + return RouteBinding{}, err + } + name := normalized.Metadata.Name + draftPrefix := s.bindingPrefix(true) + draftEntry, draftExists, draftRevision, err := s.getEntry(ctx, draftPrefix, name) + if err != nil { + return RouteBinding{}, err + } + if err := validateDraftSaveState(name, create, expectedRevision, draftExists, draftRevision); err != nil { + return RouteBinding{}, err + } + + resourceID, methodID, err := s.resolveRuntimeIdentity(ctx, name, draftEntry, draftExists) + if err != nil { + return RouteBinding{}, err + } + + record := routeBindingRecord{ + Object: normalized, + ResourceID: resourceID, + MethodID: methodID, + } + value, err := json.Marshal(record) + if err != nil { + return RouteBinding{}, fmt.Errorf("encode route binding %q: %w", name, err) + } + if err := s.commitDraft(ctx, name, draftPrefix, value, create, expectedRevision); err != nil { + return RouteBinding{}, err + } + + saved, exists, revision, err := s.getEntry(ctx, draftPrefix, name) + if err != nil { + return RouteBinding{}, err + } + if !exists { + return RouteBinding{}, fmt.Errorf("route binding %q disappeared after save", name) + } + saved.revision = revision + return routeBindingView(saved), nil +} + +// UpdateDraft updates one existing route binding draft without allowing its +// metadata.name to change. The original name is the stable identity supplied +// by the caller, while the object name is user-editable in the YAML editor. +func (s *RouteBindingStore) UpdateDraft(ctx context.Context, originalName string, object schema.AdminObject, expectedRevision int64) (RouteBinding, error) { + originalName, err := normalizeRouteBindingName(originalName) + if err != nil { + return RouteBinding{}, err + } + normalized, err := s.Normalize(object) + if err != nil { + return RouteBinding{}, err + } + if normalized.Metadata.Name != originalName { + return RouteBinding{}, fmt.Errorf("%w: %q cannot be changed to %q", ErrRouteBindingNameImmutable, originalName, normalized.Metadata.Name) + } + return s.SaveDraft(ctx, normalized, false, expectedRevision) +} + +func validateSaveDraftRequest(create bool, expectedRevision int64) error { + if expectedRevision < 0 { + return errors.New("expected revision must not be negative") + } + if create && expectedRevision > 0 { + return errors.New("create route binding does not accept an expected revision") + } + return nil +} + +func validateDraftSaveState(name string, create bool, expectedRevision int64, exists bool, revision int64) error { + if create && exists { + return fmt.Errorf("%w: %s", ErrRouteBindingAlreadyExists, name) + } + if expectedRevision > 0 && (!exists || revision != expectedRevision) { + return fmt.Errorf("%w: %s", ErrRouteBindingConflict, name) + } + return nil +} + +func (s *RouteBindingStore) resolveRuntimeIdentity(ctx context.Context, name string, draft routeBindingEntry, draftExists bool) (int, int, error) { + resourceID, methodID := draft.record.ResourceID, draft.record.MethodID + if !draftExists { + published, exists, _, err := s.getEntry(ctx, s.bindingPrefix(false), name) + if err != nil { + return 0, 0, err + } + if exists { + resourceID, methodID = published.record.ResourceID, published.record.MethodID + } + } + if resourceID <= 0 { + var err error + resourceID, err = s.allocateRuntimeID(ctx) + if err != nil { + return 0, 0, err + } + } + if methodID <= 0 { + // One AdminRouteBinding currently compiles to exactly one Method. Method + // IDs are scoped below the resource, so sharing the stable route ID is + // safe and avoids a second allocation transaction. + methodID = resourceID + } + return resourceID, methodID, nil +} + +func (s *RouteBindingStore) commitDraft(ctx context.Context, name, draftPrefix string, value []byte, create bool, expectedRevision int64) error { + comparisons := make([]clientv3.Cmp, 0, 1) + if create { + comparisons = append(comparisons, clientv3.Compare(clientv3.CreateRevision(s.bindingKey(draftPrefix, name)), "=", 0)) + } else if expectedRevision > 0 { + comparisons = append(comparisons, clientv3.Compare(clientv3.ModRevision(s.bindingKey(draftPrefix, name)), "=", expectedRevision)) + } + operations := []clientv3.Op{ + clientv3.OpPut(s.bindingKey(draftPrefix, name), string(value)), + } + transaction := s.kv.Txn(ctx) + if len(comparisons) > 0 { + transaction = transaction.If(comparisons...) + } + response, err := transaction.Then(operations...).Commit() + if err != nil { + return fmt.Errorf("save route binding %q: %w", name, err) + } + if response.Succeeded { + return nil + } + if create { + return fmt.Errorf("%w: %s", ErrRouteBindingAlreadyExists, name) + } + return fmt.Errorf("%w: %s", ErrRouteBindingConflict, name) +} + +// DeleteAndPublish removes one route draft and its published runtime state in +// the same etcd transaction. This keeps the delete flow atomic for callers: +// there is no intermediate state where the draft is gone but the route still +// remains active in Pixiu. +func (s *RouteBindingStore) DeleteAndPublish(ctx context.Context, name string, expectedRevision int64) (RouteBindingPublishResult, error) { + ctx = routeBindingContext(ctx) + if expectedRevision < 0 { + return RouteBindingPublishResult{}, errors.New("expected revision must not be negative") + } + name, err := normalizeRouteBindingName(name) + if err != nil { + return RouteBindingPublishResult{}, err + } + state, err := s.readPublishState(ctx, name) + if err != nil { + return RouteBindingPublishResult{}, err + } + if expectedRevision > 0 && (!state.draftExists || state.draftRevision != expectedRevision) { + return RouteBindingPublishResult{}, fmt.Errorf("%w: %s", ErrRouteBindingConflict, name) + } + comparisons, err := s.buildPublishComparisons(ctx, state) + if err != nil { + return RouteBindingPublishResult{}, err + } + operations := make([]clientv3.Op, 0, 4) + if state.draftExists { + operations = append(operations, clientv3.OpDelete(s.bindingKey(state.draftPrefix, name))) + } + deletedCount := 0 + if state.publishedExists { + if state.published.record.ResourceID <= 0 || state.published.record.MethodID <= 0 { + return RouteBindingPublishResult{}, fmt.Errorf("published route binding %q has invalid runtime identity", name) + } + operations = append(operations, + clientv3.OpDelete(s.runtimeResourceKey(state.published.record.ResourceID), clientv3.WithPrefix()), Review Comment: WithPrefix() 使用 /resources/1 作为前缀,会同时删除 /resources/10、/resources/11 等路由及其 Method。其他路由的发布记录仍然保留,页面可能显示“已发布”,实际配置却已丢失。应分别删除 Resource 精确键和带 / 的子键前缀。 ########## admin/logic/route_binding.go: ########## @@ -0,0 +1,1367 @@ +/* + * 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 logic + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net/url" + "reflect" + "sort" + "strconv" + "strings" + "time" +) + +import ( + clientv3 "go.etcd.io/etcd/client/v3" +) + +import ( + adminconfig "github.com/apache/dubbo-go-pixiu/admin/config" + commonyaml "github.com/apache/dubbo-go-pixiu/pkg/common/yaml" + legacyconfig "github.com/apache/dubbo-go-pixiu/pkg/config" + "github.com/apache/dubbo-go-pixiu/pkg/config/schema" +) + +const ( + routeBindingPathSegment = "route-bindings" + routeBindingRevisionSegment = "route-bindings-revision" + routeBindingRuntimeResourceDir = "resources" + maxRouteBindingIDRetries = 16 +) + +var ( + ErrRouteBindingNotFound = errors.New("admin route binding not found") + ErrRouteBindingAlreadyExists = errors.New("admin route binding already exists") + ErrRouteBindingNameImmutable = errors.New("admin route binding metadata.name is immutable") + ErrRouteBindingConflict = errors.New("admin route binding was modified concurrently") + ErrRouteBindingPublishConflict = errors.New("admin route binding publish conflict") + ErrRouteBindingRuntimeConflict = errors.New("admin route binding conflicts with an existing runtime route") +) + +// routeBindingKV is the small part of clientv3.KV needed by the route store. +// Keeping this boundary narrow makes the planning and transaction logic easy +// to exercise without coupling it to the gost wrapper. +type routeBindingKV interface { + Get(context.Context, string, ...clientv3.OpOption) (*clientv3.GetResponse, error) + Txn(context.Context) clientv3.Txn +} + +// RouteBinding is the Admin-facing response shape. ResourceID and MethodID +// are stable runtime identities used by Pixiu's legacy etcd watcher; they are +// not part of the user-editable AdminRouteBinding object. +type RouteBinding struct { + Object schema.AdminObject `json:"object"` + ResourceID int `json:"resourceId"` + MethodID int `json:"methodId"` + Revision int64 `json:"revision"` + PublishStatus *RouteBindingPublishStatus `json:"publishStatus,omitempty"` +} + +// RouteBindingPublishResult describes one atomic publish transaction. +type RouteBindingPublishResult struct { + Name string `json:"name"` + Revision int64 `json:"revision"` + DraftRevision int64 `json:"draftRevision"` + PublishedRevision int64 `json:"publishedRevision"` + PublishedCount int `json:"publishedCount"` + DeletedCount int `json:"deletedCount"` +} + +// RouteBindingPublishStatus exposes one route's draft and published state. +// Revisions are the etcd key mod revisions for that route, not a global +// configuration revision. +type RouteBindingPublishStatus struct { + Name string `json:"name"` + DraftRevision int64 `json:"draftRevision"` + PublishedRevision int64 `json:"publishedRevision"` + DraftExists bool `json:"draftExists"` + PublishedExists bool `json:"publishedExists"` + Dirty bool `json:"dirty"` +} + +type RouteBindingDiffChange struct { + Path string `json:"path"` + Before any `json:"before"` + After any `json:"after"` +} + +// RouteBindingDiff compares one route's draft object with its published +// object. A nil side represents a route that only exists in the other scope. +type RouteBindingDiff struct { + Name string `json:"name"` + Draft *RouteBinding `json:"draft,omitempty"` + Published *RouteBinding `json:"published,omitempty"` + Changes []RouteBindingDiffChange `json:"changes"` +} + +type routeBindingRecord struct { + Object schema.AdminObject `json:"object"` + ResourceID int `json:"resourceId"` + MethodID int `json:"methodId"` +} + +type routeBindingEntry struct { + record routeBindingRecord + key string + revision int64 +} + +// RouteBindingStore owns the AdminRouteBinding persistence and publish +// boundary. Published runtime data is deliberately written in the existing +// Resource/Method layout so the current Pixiu watcher can consume it. +type RouteBindingStore struct { + kv routeBindingKV + root string + registry *schema.Registry +} + +// NewRouteBindingStore creates a store over an etcd v3 client. +func NewRouteBindingStore(kv routeBindingKV, root string, registry *schema.Registry) (*RouteBindingStore, error) { + if kv == nil { + return nil, errors.New("route binding etcd client is nil") + } + root = strings.TrimRight(strings.TrimSpace(root), "/") + if root == "" { + return nil, errors.New("route binding etcd root path is empty") + } + if registry == nil { + var err error + registry, err = schema.NewBuiltinRegistry() + if err != nil { + return nil, fmt.Errorf("create route binding schema registry: %w", err) + } + } + return &RouteBindingStore{kv: kv, root: root, registry: registry}, nil +} + +// NewAdminRouteBindingStore creates the production store from the Admin +// process' configured etcd client and root path. +func NewAdminRouteBindingStore() (*RouteBindingStore, error) { + if adminconfig.Bootstrap == nil { + return nil, errors.New("admin bootstrap is nil") + } + if adminconfig.Client == nil { + return nil, errors.New("admin etcd client is nil") + } + rawClient := adminconfig.Client.GetRawClient() + if rawClient == nil { + return nil, errors.New("admin raw etcd client is nil") + } + registry, err := schema.DefaultRegistry() + if err != nil { + return nil, fmt.Errorf("get Admin route binding schema registry: %w", err) + } + return NewRouteBindingStore(rawClient, adminconfig.Bootstrap.GetPath(), registry) +} + +func routeBindingContext(ctx context.Context) context.Context { + if ctx == nil { + return context.Background() + } + return ctx +} + +// Registry returns the schema registry used by this store. +func (s *RouteBindingStore) Registry() *schema.Registry { + return s.registry +} + +// List returns high-level route bindings from either the draft or published +// namespace. Empty namespaces are returned as an empty slice. +func (s *RouteBindingStore) List(ctx context.Context, unpublished bool) ([]RouteBinding, error) { + ctx = routeBindingContext(ctx) + entries, err := s.listEntries(ctx, s.bindingPrefix(unpublished)) + if err != nil { + return nil, err + } + var statuses map[string]RouteBindingPublishStatus + if unpublished { + publishedEntries, err := s.listEntries(ctx, s.bindingPrefix(false)) + if err != nil { + return nil, err + } + statuses = routeBindingStatuses(entries, publishedEntries) + } + result := make([]RouteBinding, 0, len(entries)) + for _, entry := range entries { + view := routeBindingView(entry) + if status, exists := statuses[entry.record.Object.Metadata.Name]; exists { + view.PublishStatus = &status + } + result = append(result, view) + } + return result, nil +} + +func routeBindingStatuses(draftEntries, publishedEntries []routeBindingEntry) map[string]RouteBindingPublishStatus { + publishedByName := make(map[string]routeBindingEntry, len(publishedEntries)) + for _, entry := range publishedEntries { + publishedByName[entry.record.Object.Metadata.Name] = entry + } + + statuses := make(map[string]RouteBindingPublishStatus, len(draftEntries)) + for _, draft := range draftEntries { + name := draft.record.Object.Metadata.Name + published, publishedExists := publishedByName[name] + status := RouteBindingPublishStatus{ + Name: name, + DraftRevision: draft.revision, + DraftExists: true, + PublishedExists: publishedExists, + PublishedRevision: published.revision, + Dirty: !publishedExists || !reflect.DeepEqual(draft.record.Object, published.record.Object), + } + statuses[name] = status + } + return statuses +} + +// Get returns one high-level route binding from either the draft or published +// namespace. +func (s *RouteBindingStore) Get(ctx context.Context, name string, unpublished bool) (RouteBinding, error) { + ctx = routeBindingContext(ctx) + name, err := normalizeRouteBindingName(name) + if err != nil { + return RouteBinding{}, err + } + entry, exists, _, err := s.getEntry(ctx, s.bindingPrefix(unpublished), name) + if err != nil { + return RouteBinding{}, err + } + if !exists { + return RouteBinding{}, fmt.Errorf("%w: %s", ErrRouteBindingNotFound, name) + } + return routeBindingView(entry), nil +} + +// Normalize validates an object and applies all registered schema defaults. +// It is useful to the controller's validate and preview endpoints and does +// not require an etcd write. +func (s *RouteBindingStore) Normalize(object schema.AdminObject) (schema.AdminObject, error) { + normalized, err := s.registry.Normalize(object) + if err != nil { + return normalized, err + } + name, err := normalizeRouteBindingName(normalized.Metadata.Name) + if err != nil { + return normalized, err + } + normalized.Metadata.Name = name + return normalized, nil +} + +// Preview validates and compiles an Admin object without writing it. +func (s *RouteBindingStore) Preview(object schema.AdminObject) (schema.AdminObject, []byte, error) { + normalized, err := s.Normalize(object) + if err != nil { + return normalized, nil, err + } + compiled, err := schema.CompileAdminRouteBinding(s.registry, normalized) + if err != nil { + return normalized, nil, err + } + preview, err := compiled.PreviewYAML() + if err != nil { + return normalized, nil, err + } + return normalized, preview, nil +} + +// SaveDraft validates and atomically stores one AdminRouteBinding in the +// draft namespace. POST callers use create=true; PUT callers use create=false. +// expectedRevision is the draft key's mod revision and may be zero to allow +// an unconditional write. +func (s *RouteBindingStore) SaveDraft(ctx context.Context, object schema.AdminObject, create bool, expectedRevision int64) (RouteBinding, error) { + ctx = routeBindingContext(ctx) + if err := validateSaveDraftRequest(create, expectedRevision); err != nil { + return RouteBinding{}, err + } + normalized, err := s.Normalize(object) + if err != nil { + return RouteBinding{}, err + } + name := normalized.Metadata.Name + draftPrefix := s.bindingPrefix(true) + draftEntry, draftExists, draftRevision, err := s.getEntry(ctx, draftPrefix, name) + if err != nil { + return RouteBinding{}, err + } + if err := validateDraftSaveState(name, create, expectedRevision, draftExists, draftRevision); err != nil { + return RouteBinding{}, err + } + + resourceID, methodID, err := s.resolveRuntimeIdentity(ctx, name, draftEntry, draftExists) + if err != nil { + return RouteBinding{}, err + } + + record := routeBindingRecord{ + Object: normalized, + ResourceID: resourceID, + MethodID: methodID, + } + value, err := json.Marshal(record) + if err != nil { + return RouteBinding{}, fmt.Errorf("encode route binding %q: %w", name, err) + } + if err := s.commitDraft(ctx, name, draftPrefix, value, create, expectedRevision); err != nil { + return RouteBinding{}, err + } + + saved, exists, revision, err := s.getEntry(ctx, draftPrefix, name) + if err != nil { + return RouteBinding{}, err + } + if !exists { + return RouteBinding{}, fmt.Errorf("route binding %q disappeared after save", name) + } + saved.revision = revision + return routeBindingView(saved), nil +} + +// UpdateDraft updates one existing route binding draft without allowing its +// metadata.name to change. The original name is the stable identity supplied +// by the caller, while the object name is user-editable in the YAML editor. +func (s *RouteBindingStore) UpdateDraft(ctx context.Context, originalName string, object schema.AdminObject, expectedRevision int64) (RouteBinding, error) { + originalName, err := normalizeRouteBindingName(originalName) + if err != nil { + return RouteBinding{}, err + } + normalized, err := s.Normalize(object) + if err != nil { + return RouteBinding{}, err + } + if normalized.Metadata.Name != originalName { + return RouteBinding{}, fmt.Errorf("%w: %q cannot be changed to %q", ErrRouteBindingNameImmutable, originalName, normalized.Metadata.Name) + } + return s.SaveDraft(ctx, normalized, false, expectedRevision) +} + +func validateSaveDraftRequest(create bool, expectedRevision int64) error { + if expectedRevision < 0 { + return errors.New("expected revision must not be negative") + } + if create && expectedRevision > 0 { + return errors.New("create route binding does not accept an expected revision") + } + return nil +} + +func validateDraftSaveState(name string, create bool, expectedRevision int64, exists bool, revision int64) error { + if create && exists { + return fmt.Errorf("%w: %s", ErrRouteBindingAlreadyExists, name) + } + if expectedRevision > 0 && (!exists || revision != expectedRevision) { + return fmt.Errorf("%w: %s", ErrRouteBindingConflict, name) + } + return nil +} + +func (s *RouteBindingStore) resolveRuntimeIdentity(ctx context.Context, name string, draft routeBindingEntry, draftExists bool) (int, int, error) { + resourceID, methodID := draft.record.ResourceID, draft.record.MethodID + if !draftExists { + published, exists, _, err := s.getEntry(ctx, s.bindingPrefix(false), name) + if err != nil { + return 0, 0, err + } + if exists { + resourceID, methodID = published.record.ResourceID, published.record.MethodID + } + } + if resourceID <= 0 { + var err error + resourceID, err = s.allocateRuntimeID(ctx) + if err != nil { + return 0, 0, err + } + } + if methodID <= 0 { + // One AdminRouteBinding currently compiles to exactly one Method. Method + // IDs are scoped below the resource, so sharing the stable route ID is + // safe and avoids a second allocation transaction. + methodID = resourceID + } + return resourceID, methodID, nil +} + +func (s *RouteBindingStore) commitDraft(ctx context.Context, name, draftPrefix string, value []byte, create bool, expectedRevision int64) error { + comparisons := make([]clientv3.Cmp, 0, 1) + if create { + comparisons = append(comparisons, clientv3.Compare(clientv3.CreateRevision(s.bindingKey(draftPrefix, name)), "=", 0)) + } else if expectedRevision > 0 { + comparisons = append(comparisons, clientv3.Compare(clientv3.ModRevision(s.bindingKey(draftPrefix, name)), "=", expectedRevision)) + } + operations := []clientv3.Op{ + clientv3.OpPut(s.bindingKey(draftPrefix, name), string(value)), + } + transaction := s.kv.Txn(ctx) + if len(comparisons) > 0 { + transaction = transaction.If(comparisons...) + } + response, err := transaction.Then(operations...).Commit() + if err != nil { + return fmt.Errorf("save route binding %q: %w", name, err) + } + if response.Succeeded { + return nil + } + if create { + return fmt.Errorf("%w: %s", ErrRouteBindingAlreadyExists, name) + } + return fmt.Errorf("%w: %s", ErrRouteBindingConflict, name) +} + +// DeleteAndPublish removes one route draft and its published runtime state in +// the same etcd transaction. This keeps the delete flow atomic for callers: +// there is no intermediate state where the draft is gone but the route still +// remains active in Pixiu. +func (s *RouteBindingStore) DeleteAndPublish(ctx context.Context, name string, expectedRevision int64) (RouteBindingPublishResult, error) { + ctx = routeBindingContext(ctx) + if expectedRevision < 0 { + return RouteBindingPublishResult{}, errors.New("expected revision must not be negative") + } + name, err := normalizeRouteBindingName(name) + if err != nil { + return RouteBindingPublishResult{}, err + } + state, err := s.readPublishState(ctx, name) + if err != nil { + return RouteBindingPublishResult{}, err + } + if expectedRevision > 0 && (!state.draftExists || state.draftRevision != expectedRevision) { + return RouteBindingPublishResult{}, fmt.Errorf("%w: %s", ErrRouteBindingConflict, name) + } + comparisons, err := s.buildPublishComparisons(ctx, state) + if err != nil { + return RouteBindingPublishResult{}, err + } + operations := make([]clientv3.Op, 0, 4) + if state.draftExists { + operations = append(operations, clientv3.OpDelete(s.bindingKey(state.draftPrefix, name))) + } + deletedCount := 0 + if state.publishedExists { + if state.published.record.ResourceID <= 0 || state.published.record.MethodID <= 0 { + return RouteBindingPublishResult{}, fmt.Errorf("published route binding %q has invalid runtime identity", name) + } + operations = append(operations, + clientv3.OpDelete(s.runtimeResourceKey(state.published.record.ResourceID), clientv3.WithPrefix()), + clientv3.OpDelete(s.bindingKey(state.publishedPrefix, name)), + ) + deletedCount = 1 + } + operations = append(operations, + clientv3.OpPut(s.publishedRevisionKey(), strconv.FormatInt(time.Now().UnixNano(), 10))) + response, err := s.kv.Txn(ctx).If(comparisons...).Then(operations...).Commit() + if err != nil { + return RouteBindingPublishResult{}, fmt.Errorf("delete route binding %q: %w", name, err) + } + if !response.Succeeded { + return RouteBindingPublishResult{}, fmt.Errorf("%w: %s", ErrRouteBindingPublishConflict, name) + } + return RouteBindingPublishResult{ + Name: name, + Revision: response.Header.Revision, + DraftRevision: state.draftRevision, + PublishedRevision: 0, + DeletedCount: deletedCount, + }, nil +} + +// Publish publishes exactly one route binding. The generated legacy resource +// and method, the published high-level binding, and the internal generation +// guard are written or deleted in one etcd transaction. Other routes' draft +// and published values are left untouched. +func (s *RouteBindingStore) Publish(ctx context.Context, name string, expectedDraftRevision int64) (RouteBindingPublishResult, error) { + ctx = routeBindingContext(ctx) + if expectedDraftRevision < 0 { + return RouteBindingPublishResult{}, errors.New("expected draft revision must not be negative") + } + name, err := normalizeRouteBindingName(name) + if err != nil { + return RouteBindingPublishResult{}, err + } + state, err := s.readPublishState(ctx, name) + if err != nil { + return RouteBindingPublishResult{}, err + } + if expectedDraftRevision > 0 && (!state.draftExists || state.draftRevision != expectedDraftRevision) { + return RouteBindingPublishResult{}, fmt.Errorf("%w: %s", ErrRouteBindingPublishConflict, name) + } + publishedEntries, err := s.listEntries(ctx, state.publishedPrefix) + if err != nil { + return RouteBindingPublishResult{}, err + } + candidate, err := s.compilePublishCandidate(name, state.draft, state.draftExists, publishedEntries) + if err != nil { + return RouteBindingPublishResult{}, err + } + if err := s.checkRuntimeRouteConflict(ctx, candidate, state); err != nil { + return RouteBindingPublishResult{}, err + } + comparisons, err := s.buildPublishComparisons(ctx, state) + if err != nil { + return RouteBindingPublishResult{}, err + } + operations, publishedCount, deletedCount, err := s.buildSinglePublishOperations(state, candidate) + if err != nil { + return RouteBindingPublishResult{}, err + } + operations = append(operations, + clientv3.OpPut(s.publishedRevisionKey(), strconv.FormatInt(time.Now().UnixNano(), 10))) + response, err := s.kv.Txn(ctx).If(comparisons...).Then(operations...).Commit() + if err != nil { + return RouteBindingPublishResult{}, fmt.Errorf("publish route binding %q: %w", name, err) + } + if !response.Succeeded { + return RouteBindingPublishResult{}, fmt.Errorf("%w: %s", ErrRouteBindingPublishConflict, name) + } + publishedRevision, err := s.publishedRouteRevision(ctx, state.publishedPrefix, name, candidate) + if err != nil { + return RouteBindingPublishResult{}, err + } + return RouteBindingPublishResult{ + Name: name, + Revision: response.Header.Revision, + DraftRevision: state.draftRevision, + PublishedRevision: publishedRevision, + PublishedCount: publishedCount, + DeletedCount: deletedCount, + }, nil +} + +type routeBindingPublishState struct { + draftPrefix string + publishedPrefix string + draft routeBindingEntry + draftExists bool + draftRevision int64 + published routeBindingEntry + publishedExists bool + publishedRevision int64 +} + +func (s *RouteBindingStore) readPublishState(ctx context.Context, name string) (routeBindingPublishState, error) { + state := routeBindingPublishState{ + draftPrefix: s.bindingPrefix(true), + publishedPrefix: s.bindingPrefix(false), + } + var err error + state.draft, state.draftExists, state.draftRevision, err = s.getEntry(ctx, state.draftPrefix, name) + if err != nil { + return routeBindingPublishState{}, err + } + state.published, state.publishedExists, state.publishedRevision, err = s.getEntry(ctx, state.publishedPrefix, name) + if err != nil { + return routeBindingPublishState{}, err + } + if !state.draftExists && !state.publishedExists { + return routeBindingPublishState{}, fmt.Errorf("%w: %s", ErrRouteBindingNotFound, name) + } + return state, nil +} + +func (s *RouteBindingStore) compilePublishCandidate(name string, draft routeBindingEntry, draftExists bool, published []routeBindingEntry) (*compiledRouteEntry, error) { + if !draftExists { + return nil, nil + } + entries := make([]routeBindingEntry, 0, len(published)+1) + for _, entry := range published { + if entry.record.Object.Metadata.Name != name { + entries = append(entries, entry) + } + } + entries = append(entries, draft) + compiled, err := s.compileSnapshot(entries) + if err != nil { + return nil, err + } + for index := range compiled { + if compiled[index].object.Metadata.Name == name { + return &compiled[index], nil + } + } + return nil, fmt.Errorf("route binding %q was not compiled", name) +} + +func (s *RouteBindingStore) buildPublishComparisons(ctx context.Context, state routeBindingPublishState) ([]clientv3.Cmp, error) { + comparisons := make([]clientv3.Cmp, 0, 2) + if state.draftExists { + // Always compare the value observed above, even when the caller omitted + // expectedDraftRevision. This closes the read/modify/write race. + comparisons = append(comparisons, + clientv3.Compare(clientv3.ModRevision(s.bindingKey(state.draftPrefix, state.draft.record.Object.Metadata.Name)), "=", state.draftRevision)) + if state.publishedExists { + comparisons = append(comparisons, + clientv3.Compare(clientv3.ModRevision(s.bindingKey(state.publishedPrefix, state.published.record.Object.Metadata.Name)), "=", state.publishedRevision)) + } else { + comparisons = append(comparisons, + clientv3.Compare(clientv3.CreateRevision(s.bindingKey(state.publishedPrefix, state.draft.record.Object.Metadata.Name)), "=", 0)) + } + } else { + comparisons = append(comparisons, + clientv3.Compare(clientv3.ModRevision(s.bindingKey(state.publishedPrefix, state.published.record.Object.Metadata.Name)), "=", state.publishedRevision)) + } + publishedMarkerRevision, publishedMarkerExists, err := s.markerRevision(ctx, s.publishedRevisionKey()) + if err != nil { + return nil, err + } + if publishedMarkerExists { + comparisons = append(comparisons, + clientv3.Compare(clientv3.ModRevision(s.publishedRevisionKey()), "=", publishedMarkerRevision)) + } else { + comparisons = append(comparisons, + clientv3.Compare(clientv3.CreateRevision(s.publishedRevisionKey()), "=", 0)) + } + return comparisons, nil +} + +func (s *RouteBindingStore) buildSinglePublishOperations(state routeBindingPublishState, candidate *compiledRouteEntry) ([]clientv3.Op, int, int, error) { + if candidate == nil { + return []clientv3.Op{ + clientv3.OpDelete(s.runtimeResourceKey(state.published.record.ResourceID), clientv3.WithPrefix()), + clientv3.OpDelete(s.bindingKey(state.publishedPrefix, state.published.record.Object.Metadata.Name)), + }, 0, 1, nil + } + compiled := candidate.legacy + operations := make([]clientv3.Op, 0, 4) + if state.publishedExists && state.published.record.ResourceID != compiled.Resource.ID { + operations = append(operations, + clientv3.OpDelete(s.runtimeResourceKey(state.published.record.ResourceID), clientv3.WithPrefix())) + } else if state.publishedExists && state.published.record.MethodID != compiled.Method.ID { + operations = append(operations, + clientv3.OpDelete(s.runtimeMethodKey(state.published.record.ResourceID, state.published.record.MethodID))) + } + resourceValue, methodValue, err := marshalCompiledRuntimeRoute(compiled) + if err != nil { + return nil, 0, 0, fmt.Errorf("encode route binding %q for runtime: %w", candidate.object.Metadata.Name, err) + } + recordValue, err := json.Marshal(routeBindingRecord{ + Object: candidate.object, + ResourceID: compiled.Resource.ID, + MethodID: compiled.Method.ID, + }) + if err != nil { + return nil, 0, 0, fmt.Errorf("encode published route binding %q: %w", candidate.object.Metadata.Name, err) + } + operations = append(operations, + clientv3.OpPut(s.runtimeResourceKey(compiled.Resource.ID), string(resourceValue)), + clientv3.OpPut(s.runtimeMethodKey(compiled.Resource.ID, compiled.Method.ID), string(methodValue)), + clientv3.OpPut(s.bindingKey(state.publishedPrefix, candidate.object.Metadata.Name), string(recordValue))) + return operations, 1, 0, nil +} + +func (s *RouteBindingStore) publishedRouteRevision(ctx context.Context, prefix, name string, candidate *compiledRouteEntry) (int64, error) { + if candidate == nil { + return 0, nil + } + _, exists, revision, err := s.getEntry(ctx, prefix, name) + if err != nil { + return 0, err + } + if !exists { + return 0, fmt.Errorf("route binding %q disappeared after publish", name) + } + return revision, nil +} + +// checkRuntimeRouteConflict protects the breaking migration boundary between +// the legacy Resource/Method keys and AdminRouteBinding. Existing runtime +// method keys are still consumed by Pixiu's watcher, even when they are not +// visible through the new Admin API. Publishing a new route with the same +// HTTP method and path would otherwise commit successfully and leave the +// watcher with duplicate runtime routes. +func (s *RouteBindingStore) checkRuntimeRouteConflict(ctx context.Context, candidate *compiledRouteEntry, state routeBindingPublishState) error { + if candidate == nil { + return nil + } + prefix := s.runtimeResourcePrefix() + response, err := s.kv.Get(ctx, prefix, clientv3.WithPrefix()) + if err != nil { + return fmt.Errorf("list existing runtime resources and methods: %w", err) + } + resources, err := runtimeResourcesFromResponse(prefix, response) + if err != nil { + return err + } + resourcePaths, runtimeRoutes := indexRuntimeResourceRoutes(resources, len(response.Kvs)) + methodRoutes, err := runtimeMethodRoutesFromResponse(prefix, response, resourcePaths, state) + if err != nil { + return err + } + runtimeRoutes = append(runtimeRoutes, methodRoutes...) + return findRuntimeRouteConflict(candidate, runtimeRoutes) +} + +func runtimeResourcesFromResponse(prefix string, response *clientv3.GetResponse) (map[int]legacyconfig.Resource, error) { + resources := make(map[int]legacyconfig.Resource) + for _, kv := range response.Kvs { + resourceID, ok := runtimeResourceID(prefix, string(kv.Key)) + if !ok { + continue + } + var resource legacyconfig.Resource + if err := commonyaml.UnmarshalYML(kv.Value, &resource); err != nil { + return nil, fmt.Errorf("decode existing runtime resource %q: %w", string(kv.Key), err) + } + resources[resourceID] = resource + } + return resources, nil +} + +func indexRuntimeResourceRoutes(resources map[int]legacyconfig.Resource, capacity int) (map[int]string, []runtimeRouteBinding) { + resourcePaths := make(map[int]string) + runtimeRoutes := make([]runtimeRouteBinding, 0, capacity) + for resourceID, resource := range resources { + collectRuntimeResourceRoutes(resource, "", resourceID, resourcePaths, &runtimeRoutes) + } + return resourcePaths, runtimeRoutes +} + +func runtimeMethodRoutesFromResponse( + prefix string, + response *clientv3.GetResponse, + resourcePaths map[int]string, + state routeBindingPublishState, +) ([]runtimeRouteBinding, error) { + routes := make([]runtimeRouteBinding, 0, len(response.Kvs)) + for _, kv := range response.Kvs { + route, ok, err := runtimeMethodRouteFromKV(prefix, string(kv.Key), kv.Value, resourcePaths, state) + if err != nil { + return nil, err + } + if ok { + routes = append(routes, route) + } + } + return routes, nil +} + +func runtimeMethodRouteFromKV( + prefix string, + key string, + value []byte, + resourcePaths map[int]string, + state routeBindingPublishState, +) (runtimeRouteBinding, bool, error) { + resourceID, methodID, ok, err := runtimeMethodIDs(prefix, key) + if err != nil || !ok { + return runtimeRouteBinding{}, false, err + } + // A republish of the same published AdminRouteBinding owns this runtime + // identity; a new route must not bypass the check by reusing orphaned IDs. + if state.publishedExists && resourceID == state.published.record.ResourceID && methodID == state.published.record.MethodID { + return runtimeRouteBinding{}, false, nil + } + var method legacyconfig.Method + if err := commonyaml.UnmarshalYML(value, &method); err != nil { + return runtimeRouteBinding{}, false, fmt.Errorf("decode existing runtime method %q: %w", key, err) + } + path := strings.TrimSpace(method.ResourcePath) + if path == "" { + path = resourcePaths[resourceID] + } + if path == "" { + return runtimeRouteBinding{}, false, nil + } + return runtimeRouteBinding{path: path, httpVerb: method.HTTPVerb, key: key}, true, nil +} + +func findRuntimeRouteConflict(candidate *compiledRouteEntry, runtimeRoutes []runtimeRouteBinding) error { + candidatePath := strings.ToLower(candidate.legacy.Resource.Path) + candidateVerb := strings.TrimSpace(candidate.legacy.Method.HTTPVerb) + for _, route := range runtimeRoutes { + if strings.ToLower(route.path) == candidatePath && strings.EqualFold(route.httpVerb, candidateVerb) { + return fmt.Errorf("%w: %s %s already exists at %q", ErrRouteBindingRuntimeConflict, candidateVerb, candidate.legacy.Resource.Path, route.key) + } + } + return nil +} + +type runtimeRouteBinding struct { + path string + httpVerb string + key string +} + +func collectRuntimeResourceRoutes(resource legacyconfig.Resource, parentPath string, keyResourceID int, resourcePaths map[int]string, routes *[]runtimeRouteBinding) { + groupPath := parentPath + if groupPath == "/" { + groupPath = "" + } + if !strings.HasPrefix(resource.Path, "/") { + return + } + fullPath := groupPath + resource.Path + if keyResourceID > 0 { + resourcePaths[keyResourceID] = fullPath + } + if resource.ID > 0 { + resourcePaths[resource.ID] = fullPath + } + resourceKey := fmt.Sprintf("resource %d", keyResourceID) + for _, method := range resource.Methods { + *routes = append(*routes, runtimeRouteBinding{path: fullPath, httpVerb: method.HTTPVerb, key: resourceKey}) + } + for _, nested := range resource.Resources { + // Match addAPIFromResource: nested resources are visited with their + // parent's declared path, and the router lowercases paths at insertion. + collectRuntimeResourceRoutes(nested, resource.Path, nested.ID, resourcePaths, routes) + } +} + +// Status returns draft and published revisions for one route. The revisions +// are independent of other route bindings in the same config namespace. +func (s *RouteBindingStore) Status(ctx context.Context, name string) (RouteBindingPublishStatus, error) { + ctx = routeBindingContext(ctx) + name, err := normalizeRouteBindingName(name) + if err != nil { + return RouteBindingPublishStatus{}, err + } + draft, draftExists, draftRevision, err := s.getEntry(ctx, s.bindingPrefix(true), name) + if err != nil { + return RouteBindingPublishStatus{}, err + } + published, publishedExists, publishedRevision, err := s.getEntry(ctx, s.bindingPrefix(false), name) + if err != nil { + return RouteBindingPublishStatus{}, err + } + if !draftExists && !publishedExists { + return RouteBindingPublishStatus{}, fmt.Errorf("%w: %s", ErrRouteBindingNotFound, name) + } + dirty := draftExists != publishedExists + if draftExists && publishedExists { + dirty = !reflect.DeepEqual(draft.record.Object, published.record.Object) + } + return RouteBindingPublishStatus{ + Name: name, + DraftRevision: draftRevision, + PublishedRevision: publishedRevision, + DraftExists: draftExists, + PublishedExists: publishedExists, + Dirty: dirty, + }, nil +} + +// Diff compares one route's draft and published objects. It does not read or +// mutate any other route's draft state. +func (s *RouteBindingStore) Diff(ctx context.Context, name string) (RouteBindingDiff, error) { + ctx = routeBindingContext(ctx) + name, err := normalizeRouteBindingName(name) + if err != nil { + return RouteBindingDiff{}, err + } + draft, draftExists, _, err := s.getEntry(ctx, s.bindingPrefix(true), name) + if err != nil { + return RouteBindingDiff{}, err + } + published, publishedExists, _, err := s.getEntry(ctx, s.bindingPrefix(false), name) + if err != nil { + return RouteBindingDiff{}, err + } + if !draftExists && !publishedExists { + return RouteBindingDiff{}, fmt.Errorf("%w: %s", ErrRouteBindingNotFound, name) + } + + var draftView, publishedView *RouteBinding + if draftExists { + view := routeBindingView(draft) + draftView = &view + } + if publishedExists { + view := routeBindingView(published) + publishedView = &view + } + changes, err := diffRouteBindingObjects( + func() schema.AdminObject { + if publishedExists { + return published.record.Object + } + return schema.AdminObject{} + }(), + func() schema.AdminObject { + if draftExists { + return draft.record.Object + } + return schema.AdminObject{} + }(), + publishedExists, + draftExists, + ) + if err != nil { + return RouteBindingDiff{}, err + } + return RouteBindingDiff{ + Name: name, + Draft: draftView, + Published: publishedView, + Changes: changes, + }, nil +} + +func diffRouteBindingObjects(before, after schema.AdminObject, beforeExists, afterExists bool) ([]RouteBindingDiffChange, error) { + beforeValues := make(map[string]any) + afterValues := make(map[string]any) + if beforeExists { + if err := flattenRouteBindingValue("", before, beforeValues); err != nil { + return nil, err + } + } + if afterExists { + if err := flattenRouteBindingValue("", after, afterValues); err != nil { + return nil, err + } + } + keys := make(map[string]struct{}, len(beforeValues)+len(afterValues)) + for key := range beforeValues { + keys[key] = struct{}{} + } + for key := range afterValues { + keys[key] = struct{}{} + } + paths := make([]string, 0, len(keys)) + for key := range keys { + paths = append(paths, key) + } + sort.Strings(paths) + changes := make([]RouteBindingDiffChange, 0, len(paths)) + for _, path := range paths { + beforeValue, beforeOK := beforeValues[path] + afterValue, afterOK := afterValues[path] + if beforeOK && afterOK && reflect.DeepEqual(beforeValue, afterValue) { + continue + } + changes = append(changes, RouteBindingDiffChange{ + Path: path, + Before: valueOrNil(beforeValue, beforeOK), + After: valueOrNil(afterValue, afterOK), + }) + } + return changes, nil +} + +func flattenRouteBindingValue(path string, value any, result map[string]any) error { + switch typed := value.(type) { + case schema.AdminObject: + return flattenRouteBindingValue(path, map[string]any{ + "kind": typed.Kind, + "metadata": typed.Metadata, + "spec": typed.Spec, + }, result) + case schema.ObjectMetadata: + return flattenRouteBindingValue(path, map[string]any{"name": typed.Name}, result) + case map[string]any: + if len(typed) == 0 { + result[path] = typed + return nil + } + keys := make([]string, 0, len(typed)) + for key := range typed { + keys = append(keys, key) + } + sort.Strings(keys) + for _, key := range keys { + childPath := key + if path != "" { + childPath = path + "." + key + } + if err := flattenRouteBindingValue(childPath, typed[key], result); err != nil { + return err + } + } + return nil + case []any: + if len(typed) == 0 { + result[path] = typed + return nil + } + for index, item := range typed { + childPath := fmt.Sprintf("%s[%d]", path, index) + if err := flattenRouteBindingValue(childPath, item, result); err != nil { + return err + } + } + return nil + default: + result[path] = typed + return nil + } +} + +func valueOrNil(value any, exists bool) any { + if !exists { + return nil + } + return value +} + +type compiledRouteEntry struct { + entry routeBindingEntry + object schema.AdminObject + legacy schema.CompiledRoute +} + +func (s *RouteBindingStore) compileSnapshot(entries []routeBindingEntry) ([]compiledRouteEntry, error) { + result := make([]compiledRouteEntry, 0, len(entries)) + paths := make(map[string]string, len(entries)) + resourceIDs := make(map[int]string, len(entries)) + for _, entry := range entries { + if entry.record.ResourceID <= 0 || entry.record.MethodID <= 0 { + return nil, fmt.Errorf("route binding %q has invalid runtime identity", entry.record.Object.Metadata.Name) + } + normalized, err := s.Normalize(entry.record.Object) + if err != nil { + return nil, fmt.Errorf("route binding %q: %w", entry.record.Object.Metadata.Name, err) + } + legacy, err := schema.CompileAdminRouteBinding(s.registry, normalized) + if err != nil { + return nil, fmt.Errorf("route binding %q: %w", entry.record.Object.Metadata.Name, err) + } + legacy.Resource.ID = entry.record.ResourceID + legacy.Method.ID = entry.record.MethodID + routeKey := legacy.Resource.Path + "\x00" + legacy.Method.HTTPVerb Review Comment: 新模型允许 GET /users 和 POST /users 分配独立 Resource。但现有 watcher 的 ResourceDelete → RemoveAPIByPath → DeleteNode 会删除该路径的全部 HTTP 方法。因此,删除其中一条路由会连带移除另一条仍处于发布状态的路由。需要调整 Resource 组织方式或运行时删除逻辑。 ########## admin/web/src/features/config/RouteBindingEditor.tsx: ########## @@ -0,0 +1,607 @@ +/* + * 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. + */ + +import { useCallback, useEffect, useMemo, useRef, useState } from 'react' +import { Locale, translateText } from '../../i18n' +import { routeBindingApi } from '../../services/route-binding-api' +import { ApiError } from '../../services/http' +import type { + AdminRouteBindingObject, + RouteBinding, + RouteBindingDiff, + RouteBindingObjectSchema, + RouteBindingParam, + RouteBindingPublishStatus, + RouteBindingValidationIssue, +} from '../../types/api' +import { + RouteEditorContent, + RouteEditorContract, + RouteEditorHeader, + RouteEditorNotice, + RouteEditorTabs, +} from './RouteBindingEditorSections' +import type { BusyAction, EditorTab, Notice } from './RouteBindingEditorSections' +import { + createDefaultRouteBinding, + hasLegacyPublishPreference, + normaliseRouteBindingObject, + parseRouteBindingYaml, + routeEditorOptions, + stringifyRouteBindingYaml, +} from './RouteBindingEditorModel' + +type RouteBindingEditorProps = { + readonly locale: Locale + readonly mode: 'create' | 'edit' + readonly binding?: RouteBinding | null + readonly loading: boolean + readonly published: boolean + readonly publishStatus?: RouteBindingPublishStatus | null + readonly onBack: () => void + readonly onSaved: () => void | Promise<void> +} + +function createEmptyObject(): AdminRouteBindingObject { + return { + kind: 'AdminRouteBinding', + metadata: { name: '' }, + spec: { + entry: { protocol: '', path: '', method: '' }, + target: { + protocol: '', + application: '', + interface: '', + method: '', + version: '', + group: '', + cluster: '', + }, + params: [], + enabled: false, + extensions: {}, + }, + } +} + +function errorMessage(error: unknown, fallback: string) { + return error instanceof Error ? error.message : fallback +} + +function routeBindingHydrationKey( + binding: RouteBinding | null | undefined, + mode: RouteBindingEditorProps['mode'], +) { + if (binding) return `${binding.object.metadata.name}:${binding.revision}` + if (mode === 'create') return 'new-route' + return '' +} + +function errorIssues(error: unknown) { + return error instanceof ApiError ? error.issues : [] +} + +function issueFor(issues: RouteBindingValidationIssue[], path: string) { + return issues.find((issue) => issue.path === path)?.message || '' +} + +function currentStatusLabel( + isEnglish: boolean, + dirty: boolean, + published: boolean, + translate: (value: string) => string, +) { + if (dirty) return isEnglish ? 'Unsaved changes' : '有未保存改动' + if (published) return translate('已发布') + return translate('草稿') +} + +function lifecycleStatusLabel(isEnglish: boolean, enabled: boolean) { + if (enabled) return isEnglish ? 'Draft enabled' : '草稿启用' + return isEnglish ? 'Draft disabled' : '草稿停用' +} + +function publishSuccessMessage(isEnglish: boolean, deleted: boolean) { + if (isEnglish) { + return deleted + ? 'The route was removed atomically from etcd. Pixiu runtime loading is not acknowledged here.' + : 'The route was committed atomically to etcd. Pixiu runtime loading is not acknowledged here.' + } + return deleted + ? '当前路由已通过 etcd 事务原子删除;此处不会确认 Pixiu 运行时是否已完成加载。' + : '当前路由已通过 etcd 事务原子写入;此处不会确认 Pixiu 运行时是否已完成加载。' +} + +export function RouteBindingEditor({ + locale, + mode: initialMode, + binding, + loading, + published: initialPublished, + publishStatus: initialPublishStatus, + onBack, + onSaved, +}: RouteBindingEditorProps) { + const isEnglish = locale === 'en-US' + const tx = (value: string) => translateText(locale, value) + const [mode, setMode] = useState(initialMode) + const [object, setObject] = useState<AdminRouteBindingObject>(createEmptyObject) + const [revision, setRevision] = useState(binding?.revision || 0) + const [published, setPublished] = useState(initialPublished) + const [publishStatus, setPublishStatus] = useState<RouteBindingPublishStatus | null>( + initialPublishStatus || null, + ) + const [activeTab, setActiveTab] = useState<EditorTab>('form') + const [busy, setBusy] = useState<BusyAction>('') + const [dirty, setDirty] = useState(initialMode === 'create') + const [issues, setIssues] = useState<RouteBindingValidationIssue[]>([]) + const [notice, setNotice] = useState<Notice | null>(null) + const [previewYaml, setPreviewYaml] = useState('') + const [routeYaml, setRouteYaml] = useState('') + const [yamlError, setYamlError] = useState('') + const [yamlDefaultsApplied, setYamlDefaultsApplied] = useState(false) + const [schema, setSchema] = useState<RouteBindingObjectSchema | null>(null) + const [schemaLoading, setSchemaLoading] = useState(true) + const [schemaError, setSchemaError] = useState('') + const [diffData, setDiffData] = useState<RouteBindingDiff | null>(null) + const hydratedBindingKey = useRef('') + + const loadSchema = useCallback(async () => { + setSchemaLoading(true) + setSchemaError('') + try { + setSchema(await routeBindingApi.schema()) + } catch (error: unknown) { + setSchema(null) + setSchemaError( + errorMessage( + error, + isEnglish ? 'Could not load the route schema.' : '无法加载路由 Schema。', + ), + ) + } finally { + setSchemaLoading(false) + } + }, [isEnglish]) + + useEffect(() => { + void loadSchema() + }, [loadSchema]) + + useEffect(() => { + if (loading || schemaLoading || !schema) return + const bindingKey = routeBindingHydrationKey(binding, initialMode) + if (!bindingKey) return + if (hydratedBindingKey.current === bindingKey) return + let normalized: AdminRouteBindingObject + const removedLegacyPublish = binding ? hasLegacyPublishPreference(binding.object) : false + try { + normalized = binding + ? normaliseRouteBindingObject(binding.object, schema, true) + : createDefaultRouteBinding(schema) + } catch (error: unknown) { + const message = errorMessage( + error, + isEnglish ? 'Route data does not match the schema.' : '路由数据不符合 Schema。', + ) + if (binding) { + hydratedBindingKey.current = bindingKey + setObject(createDefaultRouteBinding(schema)) + setRouteYaml(stringifyRouteBindingYaml(binding.object)) + setYamlError(message) + setDirty(true) + setActiveTab('yaml') + setNotice({ tone: 'error', text: message }) + } else { + setSchemaError(message) + } + return + } + hydratedBindingKey.current = bindingKey + setMode(initialMode) + setObject(normalized) + setRevision(binding?.revision || 0) + setPublished(initialPublished) + setPublishStatus(initialPublishStatus || null) + setDirty(initialMode === 'create' || removedLegacyPublish) + setIssues([]) + setNotice( + removedLegacyPublish + ? { + tone: 'success', + text: isEnglish + ? 'An obsolete no-op spec.publish field was removed. Save this draft to persist the cleanup.' + : '已移除旧的无效 spec.publish 字段;保存草稿后才会持久化这次清理。', + } + : null, + ) + setPreviewYaml('') + setRouteYaml(stringifyRouteBindingYaml(normalized)) + setYamlError('') + setYamlDefaultsApplied(false) + setDiffData(null) + setActiveTab('form') + }, [ + binding, + initialMode, + initialPublishStatus, + initialPublished, + isEnglish, + loading, + schema, + schemaLoading, + ]) + + const entry = object.spec.entry + const signature = useMemo(() => JSON.stringify(object), [object]) + const routeLabel = object.metadata.name || (isEnglish ? 'New API route' : '新建 API 路由') + const currentStatus = currentStatusLabel(isEnglish, dirty, published, tx) + const lifecycleStatus = lifecycleStatusLabel(isEnglish, object.spec.enabled) + const options = useMemo(() => (schema ? routeEditorOptions(schema) : undefined), [schema]) + + const editorBlocked = schemaLoading || !schema || Boolean(schemaError) + + const updateObject = (next: AdminRouteBindingObject) => { + setObject(next) + setRouteYaml(stringifyRouteBindingYaml(next)) + setYamlError('') + setYamlDefaultsApplied(false) + setDirty(true) + setIssues([]) + setNotice(null) + setPreviewYaml('') + setDiffData(null) + } + + const updateYaml = (value: string) => { + setRouteYaml(value) + setDirty(true) + setIssues([]) + setNotice(null) + setPreviewYaml('') + setDiffData(null) + + if (!schema) { + setYamlError(isEnglish ? 'Route schema is not loaded.' : '路由 Schema 尚未加载。') + return + } + const parsed = parseRouteBindingYaml(value, schema) + if (!parsed.object) { + setYamlError(parsed.error) + setYamlDefaultsApplied(false) + return + } + if (mode === 'edit' && parsed.object.metadata.name !== object.metadata.name) { + setYamlError( + isEnglish ? 'metadata.name cannot change while editing.' : '编辑时不能修改 metadata.name。', + ) + return + } + setYamlError('') + setYamlDefaultsApplied(parsed.defaultsApplied) + setObject(parsed.object) + } + + const applyYamlToForm = () => { + if (!schema) { + setYamlError(isEnglish ? 'Route schema is not loaded.' : '路由 Schema 尚未加载。') + return + } + const parsed = parseRouteBindingYaml(routeYaml, schema) + if (!parsed.object) { + setYamlError(parsed.error) + return + } + if (mode === 'edit' && parsed.object.metadata.name !== object.metadata.name) { + setYamlError( + isEnglish ? 'metadata.name cannot change while editing.' : '编辑时不能修改 metadata.name。', + ) + return + } + setObject(parsed.object) + setRouteYaml(stringifyRouteBindingYaml(parsed.object)) + setYamlError('') + setYamlDefaultsApplied(false) + setIssues([]) + setNotice({ + tone: 'success', + text: isEnglish ? 'YAML is synced to the form.' : 'YAML 已同步到表单。', + }) + } + + const updateEntry = (key: 'path' | 'method', value: string) => + updateObject({ + ...object, + spec: { ...object.spec, entry: { ...object.spec.entry, [key]: value } }, + }) + + const updateTarget = ( + key: 'application' | 'interface' | 'method' | 'version' | 'group' | 'cluster', + value: string, + ) => + updateObject({ + ...object, + spec: { ...object.spec, target: { ...object.spec.target, [key]: value } }, + }) + + const updateParam = (index: number, patch: Partial<RouteBindingParam>) => { + const params = object.spec.params.map((param, paramIndex) => + paramIndex === index ? { ...param, ...patch } : param, + ) + updateObject({ ...object, spec: { ...object.spec, params } }) + } + + const addParam = () => { + const nextParam: RouteBindingParam = { + from: '', + to: object.spec.params.length, + type: options?.paramTypes[0] || '', + } + updateObject({ + ...object, + spec: { ...object.spec, params: [...object.spec.params, nextParam] }, + }) + } + + const removeParam = (index: number) => + updateObject({ + ...object, + spec: { + ...object.spec, + params: object.spec.params.filter((_, paramIndex) => paramIndex !== index), + }, + }) + + const setRouteEnabled = (enabled: boolean) => + updateObject({ ...object, spec: { ...object.spec, enabled } }) + + const validate = async () => { + if (yamlError || editorBlocked || !schema) return + setBusy('validate') + setNotice(null) + try { + const result = await routeBindingApi.validate(object) + const normalized = normaliseRouteBindingObject(result.object, schema) + const changed = JSON.stringify(normalized) !== signature + setObject(normalized) + setRouteYaml(stringifyRouteBindingYaml(normalized)) + setYamlError('') + setYamlDefaultsApplied(false) + setDirty((current) => current || changed) + setIssues([]) + setNotice({ + tone: 'success', + text: isEnglish + ? 'Configuration is valid. Schema defaults are applied.' + : '配置校验通过,默认值已补齐。', + }) + } catch (error: unknown) { + setIssues(errorIssues(error)) + setNotice({ tone: 'error', text: errorMessage(error, tx('配置校验失败')) }) + } finally { + setBusy('') + } + } + + const saveDraft = async (): Promise<RouteBinding | null> => { + if (yamlError || editorBlocked || !schema) return null + if (!object.metadata.name.trim()) { + const missingName = [ + { path: 'metadata.name', code: 'required', message: tx('请输入路由名称') }, + ] + setIssues(missingName) + setNotice({ tone: 'error', text: tx('请先补充必填字段') }) + setActiveTab('form') + return null + } + setBusy('save') + setNotice(null) + try { + const saved = + mode === 'create' + ? await routeBindingApi.create(object) + : await routeBindingApi.update( + binding?.object.metadata.name || object.metadata.name, + object, + revision, + ) + const normalized = normaliseRouteBindingObject(saved.object, schema) + setObject(normalized) + setRouteYaml(stringifyRouteBindingYaml(normalized)) + setYamlError('') + setYamlDefaultsApplied(false) + setMode('edit') + setRevision(saved.revision || 0) + setDirty(false) + setIssues([]) + setPreviewYaml('') + setDiffData(null) + setPublished(false) + setNotice({ tone: 'success', text: isEnglish ? 'Draft saved.' : '草稿已保存。' }) + try { + const nextStatus = await routeBindingApi.status(normalized.metadata.name) + setPublishStatus(nextStatus) + setPublished(Boolean(nextStatus.publishedExists && !nextStatus.dirty)) + } catch { + // The draft was saved successfully. A status refresh is best effort. + } + await onSaved() + return saved + } catch (error: unknown) { + setIssues(errorIssues(error)) + setNotice({ tone: 'error', text: errorMessage(error, tx('保存草稿失败')) }) + setActiveTab('form') + return null + } finally { + setBusy('') + } + } + + const publish = async () => { + if (yamlError || editorBlocked || !schema) return + if (dirty || mode === 'create' || revision === 0) { + const saved = await saveDraft() + if (!saved) return + } + setBusy('publish') + setNotice(null) + try { + const routeName = object.metadata.name + const status = await routeBindingApi.status(routeName) + const result = await routeBindingApi.publish(routeName, status.draftRevision) Review Comment: 发布前重新获取 status.draftRevision,覆盖了当前编辑器持有的版本。如果另一位用户修改了草稿,当前用户点击发布就会直接发布对方的内容,绕过过期版本检查。应传入当前加载或刚保存成功的 revision,发生冲突时要求刷新。 ########## admin/logic/route_binding.go: ########## @@ -0,0 +1,1367 @@ +/* + * 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 logic + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net/url" + "reflect" + "sort" + "strconv" + "strings" + "time" +) + +import ( + clientv3 "go.etcd.io/etcd/client/v3" +) + +import ( + adminconfig "github.com/apache/dubbo-go-pixiu/admin/config" + commonyaml "github.com/apache/dubbo-go-pixiu/pkg/common/yaml" + legacyconfig "github.com/apache/dubbo-go-pixiu/pkg/config" + "github.com/apache/dubbo-go-pixiu/pkg/config/schema" +) + +const ( + routeBindingPathSegment = "route-bindings" + routeBindingRevisionSegment = "route-bindings-revision" + routeBindingRuntimeResourceDir = "resources" + maxRouteBindingIDRetries = 16 +) + +var ( + ErrRouteBindingNotFound = errors.New("admin route binding not found") + ErrRouteBindingAlreadyExists = errors.New("admin route binding already exists") + ErrRouteBindingNameImmutable = errors.New("admin route binding metadata.name is immutable") + ErrRouteBindingConflict = errors.New("admin route binding was modified concurrently") + ErrRouteBindingPublishConflict = errors.New("admin route binding publish conflict") + ErrRouteBindingRuntimeConflict = errors.New("admin route binding conflicts with an existing runtime route") +) + +// routeBindingKV is the small part of clientv3.KV needed by the route store. +// Keeping this boundary narrow makes the planning and transaction logic easy +// to exercise without coupling it to the gost wrapper. +type routeBindingKV interface { + Get(context.Context, string, ...clientv3.OpOption) (*clientv3.GetResponse, error) + Txn(context.Context) clientv3.Txn +} + +// RouteBinding is the Admin-facing response shape. ResourceID and MethodID +// are stable runtime identities used by Pixiu's legacy etcd watcher; they are +// not part of the user-editable AdminRouteBinding object. +type RouteBinding struct { + Object schema.AdminObject `json:"object"` + ResourceID int `json:"resourceId"` + MethodID int `json:"methodId"` + Revision int64 `json:"revision"` + PublishStatus *RouteBindingPublishStatus `json:"publishStatus,omitempty"` +} + +// RouteBindingPublishResult describes one atomic publish transaction. +type RouteBindingPublishResult struct { + Name string `json:"name"` + Revision int64 `json:"revision"` + DraftRevision int64 `json:"draftRevision"` + PublishedRevision int64 `json:"publishedRevision"` + PublishedCount int `json:"publishedCount"` + DeletedCount int `json:"deletedCount"` +} + +// RouteBindingPublishStatus exposes one route's draft and published state. +// Revisions are the etcd key mod revisions for that route, not a global +// configuration revision. +type RouteBindingPublishStatus struct { + Name string `json:"name"` + DraftRevision int64 `json:"draftRevision"` + PublishedRevision int64 `json:"publishedRevision"` + DraftExists bool `json:"draftExists"` + PublishedExists bool `json:"publishedExists"` + Dirty bool `json:"dirty"` +} + +type RouteBindingDiffChange struct { + Path string `json:"path"` + Before any `json:"before"` + After any `json:"after"` +} + +// RouteBindingDiff compares one route's draft object with its published +// object. A nil side represents a route that only exists in the other scope. +type RouteBindingDiff struct { + Name string `json:"name"` + Draft *RouteBinding `json:"draft,omitempty"` + Published *RouteBinding `json:"published,omitempty"` + Changes []RouteBindingDiffChange `json:"changes"` +} + +type routeBindingRecord struct { + Object schema.AdminObject `json:"object"` + ResourceID int `json:"resourceId"` + MethodID int `json:"methodId"` +} + +type routeBindingEntry struct { + record routeBindingRecord + key string + revision int64 +} + +// RouteBindingStore owns the AdminRouteBinding persistence and publish +// boundary. Published runtime data is deliberately written in the existing +// Resource/Method layout so the current Pixiu watcher can consume it. +type RouteBindingStore struct { + kv routeBindingKV + root string + registry *schema.Registry +} + +// NewRouteBindingStore creates a store over an etcd v3 client. +func NewRouteBindingStore(kv routeBindingKV, root string, registry *schema.Registry) (*RouteBindingStore, error) { + if kv == nil { + return nil, errors.New("route binding etcd client is nil") + } + root = strings.TrimRight(strings.TrimSpace(root), "/") + if root == "" { + return nil, errors.New("route binding etcd root path is empty") + } + if registry == nil { + var err error + registry, err = schema.NewBuiltinRegistry() + if err != nil { + return nil, fmt.Errorf("create route binding schema registry: %w", err) + } + } + return &RouteBindingStore{kv: kv, root: root, registry: registry}, nil +} + +// NewAdminRouteBindingStore creates the production store from the Admin +// process' configured etcd client and root path. +func NewAdminRouteBindingStore() (*RouteBindingStore, error) { + if adminconfig.Bootstrap == nil { + return nil, errors.New("admin bootstrap is nil") + } + if adminconfig.Client == nil { + return nil, errors.New("admin etcd client is nil") + } + rawClient := adminconfig.Client.GetRawClient() + if rawClient == nil { + return nil, errors.New("admin raw etcd client is nil") + } + registry, err := schema.DefaultRegistry() + if err != nil { + return nil, fmt.Errorf("get Admin route binding schema registry: %w", err) + } + return NewRouteBindingStore(rawClient, adminconfig.Bootstrap.GetPath(), registry) +} + +func routeBindingContext(ctx context.Context) context.Context { + if ctx == nil { + return context.Background() + } + return ctx +} + +// Registry returns the schema registry used by this store. +func (s *RouteBindingStore) Registry() *schema.Registry { + return s.registry +} + +// List returns high-level route bindings from either the draft or published +// namespace. Empty namespaces are returned as an empty slice. +func (s *RouteBindingStore) List(ctx context.Context, unpublished bool) ([]RouteBinding, error) { + ctx = routeBindingContext(ctx) + entries, err := s.listEntries(ctx, s.bindingPrefix(unpublished)) + if err != nil { + return nil, err + } + var statuses map[string]RouteBindingPublishStatus + if unpublished { + publishedEntries, err := s.listEntries(ctx, s.bindingPrefix(false)) + if err != nil { + return nil, err + } + statuses = routeBindingStatuses(entries, publishedEntries) + } + result := make([]RouteBinding, 0, len(entries)) + for _, entry := range entries { + view := routeBindingView(entry) + if status, exists := statuses[entry.record.Object.Metadata.Name]; exists { + view.PublishStatus = &status + } + result = append(result, view) + } + return result, nil +} + +func routeBindingStatuses(draftEntries, publishedEntries []routeBindingEntry) map[string]RouteBindingPublishStatus { + publishedByName := make(map[string]routeBindingEntry, len(publishedEntries)) + for _, entry := range publishedEntries { + publishedByName[entry.record.Object.Metadata.Name] = entry + } + + statuses := make(map[string]RouteBindingPublishStatus, len(draftEntries)) + for _, draft := range draftEntries { + name := draft.record.Object.Metadata.Name + published, publishedExists := publishedByName[name] + status := RouteBindingPublishStatus{ + Name: name, + DraftRevision: draft.revision, + DraftExists: true, + PublishedExists: publishedExists, + PublishedRevision: published.revision, + Dirty: !publishedExists || !reflect.DeepEqual(draft.record.Object, published.record.Object), + } + statuses[name] = status + } + return statuses +} + +// Get returns one high-level route binding from either the draft or published +// namespace. +func (s *RouteBindingStore) Get(ctx context.Context, name string, unpublished bool) (RouteBinding, error) { + ctx = routeBindingContext(ctx) + name, err := normalizeRouteBindingName(name) + if err != nil { + return RouteBinding{}, err + } + entry, exists, _, err := s.getEntry(ctx, s.bindingPrefix(unpublished), name) + if err != nil { + return RouteBinding{}, err + } + if !exists { + return RouteBinding{}, fmt.Errorf("%w: %s", ErrRouteBindingNotFound, name) + } + return routeBindingView(entry), nil +} + +// Normalize validates an object and applies all registered schema defaults. +// It is useful to the controller's validate and preview endpoints and does +// not require an etcd write. +func (s *RouteBindingStore) Normalize(object schema.AdminObject) (schema.AdminObject, error) { + normalized, err := s.registry.Normalize(object) + if err != nil { + return normalized, err + } + name, err := normalizeRouteBindingName(normalized.Metadata.Name) + if err != nil { + return normalized, err + } + normalized.Metadata.Name = name + return normalized, nil +} + +// Preview validates and compiles an Admin object without writing it. +func (s *RouteBindingStore) Preview(object schema.AdminObject) (schema.AdminObject, []byte, error) { + normalized, err := s.Normalize(object) + if err != nil { + return normalized, nil, err + } + compiled, err := schema.CompileAdminRouteBinding(s.registry, normalized) + if err != nil { + return normalized, nil, err + } + preview, err := compiled.PreviewYAML() + if err != nil { + return normalized, nil, err + } + return normalized, preview, nil +} + +// SaveDraft validates and atomically stores one AdminRouteBinding in the +// draft namespace. POST callers use create=true; PUT callers use create=false. +// expectedRevision is the draft key's mod revision and may be zero to allow +// an unconditional write. +func (s *RouteBindingStore) SaveDraft(ctx context.Context, object schema.AdminObject, create bool, expectedRevision int64) (RouteBinding, error) { + ctx = routeBindingContext(ctx) + if err := validateSaveDraftRequest(create, expectedRevision); err != nil { + return RouteBinding{}, err + } + normalized, err := s.Normalize(object) + if err != nil { + return RouteBinding{}, err + } + name := normalized.Metadata.Name + draftPrefix := s.bindingPrefix(true) + draftEntry, draftExists, draftRevision, err := s.getEntry(ctx, draftPrefix, name) + if err != nil { + return RouteBinding{}, err + } + if err := validateDraftSaveState(name, create, expectedRevision, draftExists, draftRevision); err != nil { + return RouteBinding{}, err + } + + resourceID, methodID, err := s.resolveRuntimeIdentity(ctx, name, draftEntry, draftExists) + if err != nil { + return RouteBinding{}, err + } + + record := routeBindingRecord{ + Object: normalized, + ResourceID: resourceID, + MethodID: methodID, + } + value, err := json.Marshal(record) + if err != nil { + return RouteBinding{}, fmt.Errorf("encode route binding %q: %w", name, err) + } + if err := s.commitDraft(ctx, name, draftPrefix, value, create, expectedRevision); err != nil { + return RouteBinding{}, err + } + + saved, exists, revision, err := s.getEntry(ctx, draftPrefix, name) + if err != nil { + return RouteBinding{}, err + } + if !exists { + return RouteBinding{}, fmt.Errorf("route binding %q disappeared after save", name) + } + saved.revision = revision + return routeBindingView(saved), nil +} + +// UpdateDraft updates one existing route binding draft without allowing its +// metadata.name to change. The original name is the stable identity supplied +// by the caller, while the object name is user-editable in the YAML editor. +func (s *RouteBindingStore) UpdateDraft(ctx context.Context, originalName string, object schema.AdminObject, expectedRevision int64) (RouteBinding, error) { + originalName, err := normalizeRouteBindingName(originalName) + if err != nil { + return RouteBinding{}, err + } + normalized, err := s.Normalize(object) + if err != nil { + return RouteBinding{}, err + } + if normalized.Metadata.Name != originalName { + return RouteBinding{}, fmt.Errorf("%w: %q cannot be changed to %q", ErrRouteBindingNameImmutable, originalName, normalized.Metadata.Name) + } + return s.SaveDraft(ctx, normalized, false, expectedRevision) +} + +func validateSaveDraftRequest(create bool, expectedRevision int64) error { + if expectedRevision < 0 { + return errors.New("expected revision must not be negative") + } + if create && expectedRevision > 0 { + return errors.New("create route binding does not accept an expected revision") + } + return nil +} + +func validateDraftSaveState(name string, create bool, expectedRevision int64, exists bool, revision int64) error { + if create && exists { + return fmt.Errorf("%w: %s", ErrRouteBindingAlreadyExists, name) + } + if expectedRevision > 0 && (!exists || revision != expectedRevision) { + return fmt.Errorf("%w: %s", ErrRouteBindingConflict, name) + } + return nil +} + +func (s *RouteBindingStore) resolveRuntimeIdentity(ctx context.Context, name string, draft routeBindingEntry, draftExists bool) (int, int, error) { + resourceID, methodID := draft.record.ResourceID, draft.record.MethodID + if !draftExists { + published, exists, _, err := s.getEntry(ctx, s.bindingPrefix(false), name) + if err != nil { + return 0, 0, err + } + if exists { + resourceID, methodID = published.record.ResourceID, published.record.MethodID + } + } + if resourceID <= 0 { + var err error + resourceID, err = s.allocateRuntimeID(ctx) + if err != nil { + return 0, 0, err + } + } + if methodID <= 0 { + // One AdminRouteBinding currently compiles to exactly one Method. Method + // IDs are scoped below the resource, so sharing the stable route ID is + // safe and avoids a second allocation transaction. + methodID = resourceID + } + return resourceID, methodID, nil +} + +func (s *RouteBindingStore) commitDraft(ctx context.Context, name, draftPrefix string, value []byte, create bool, expectedRevision int64) error { + comparisons := make([]clientv3.Cmp, 0, 1) + if create { + comparisons = append(comparisons, clientv3.Compare(clientv3.CreateRevision(s.bindingKey(draftPrefix, name)), "=", 0)) + } else if expectedRevision > 0 { + comparisons = append(comparisons, clientv3.Compare(clientv3.ModRevision(s.bindingKey(draftPrefix, name)), "=", expectedRevision)) + } + operations := []clientv3.Op{ + clientv3.OpPut(s.bindingKey(draftPrefix, name), string(value)), + } + transaction := s.kv.Txn(ctx) + if len(comparisons) > 0 { + transaction = transaction.If(comparisons...) + } + response, err := transaction.Then(operations...).Commit() + if err != nil { + return fmt.Errorf("save route binding %q: %w", name, err) + } + if response.Succeeded { + return nil + } + if create { + return fmt.Errorf("%w: %s", ErrRouteBindingAlreadyExists, name) + } + return fmt.Errorf("%w: %s", ErrRouteBindingConflict, name) +} + +// DeleteAndPublish removes one route draft and its published runtime state in +// the same etcd transaction. This keeps the delete flow atomic for callers: +// there is no intermediate state where the draft is gone but the route still +// remains active in Pixiu. +func (s *RouteBindingStore) DeleteAndPublish(ctx context.Context, name string, expectedRevision int64) (RouteBindingPublishResult, error) { + ctx = routeBindingContext(ctx) + if expectedRevision < 0 { + return RouteBindingPublishResult{}, errors.New("expected revision must not be negative") + } + name, err := normalizeRouteBindingName(name) + if err != nil { + return RouteBindingPublishResult{}, err + } + state, err := s.readPublishState(ctx, name) + if err != nil { + return RouteBindingPublishResult{}, err + } + if expectedRevision > 0 && (!state.draftExists || state.draftRevision != expectedRevision) { + return RouteBindingPublishResult{}, fmt.Errorf("%w: %s", ErrRouteBindingConflict, name) + } + comparisons, err := s.buildPublishComparisons(ctx, state) + if err != nil { + return RouteBindingPublishResult{}, err + } + operations := make([]clientv3.Op, 0, 4) + if state.draftExists { + operations = append(operations, clientv3.OpDelete(s.bindingKey(state.draftPrefix, name))) + } + deletedCount := 0 + if state.publishedExists { + if state.published.record.ResourceID <= 0 || state.published.record.MethodID <= 0 { + return RouteBindingPublishResult{}, fmt.Errorf("published route binding %q has invalid runtime identity", name) + } + operations = append(operations, + clientv3.OpDelete(s.runtimeResourceKey(state.published.record.ResourceID), clientv3.WithPrefix()), + clientv3.OpDelete(s.bindingKey(state.publishedPrefix, name)), + ) + deletedCount = 1 + } + operations = append(operations, + clientv3.OpPut(s.publishedRevisionKey(), strconv.FormatInt(time.Now().UnixNano(), 10))) + response, err := s.kv.Txn(ctx).If(comparisons...).Then(operations...).Commit() + if err != nil { + return RouteBindingPublishResult{}, fmt.Errorf("delete route binding %q: %w", name, err) + } + if !response.Succeeded { + return RouteBindingPublishResult{}, fmt.Errorf("%w: %s", ErrRouteBindingPublishConflict, name) + } + return RouteBindingPublishResult{ + Name: name, + Revision: response.Header.Revision, + DraftRevision: state.draftRevision, + PublishedRevision: 0, + DeletedCount: deletedCount, + }, nil +} + +// Publish publishes exactly one route binding. The generated legacy resource +// and method, the published high-level binding, and the internal generation +// guard are written or deleted in one etcd transaction. Other routes' draft +// and published values are left untouched. +func (s *RouteBindingStore) Publish(ctx context.Context, name string, expectedDraftRevision int64) (RouteBindingPublishResult, error) { + ctx = routeBindingContext(ctx) + if expectedDraftRevision < 0 { + return RouteBindingPublishResult{}, errors.New("expected draft revision must not be negative") + } + name, err := normalizeRouteBindingName(name) + if err != nil { + return RouteBindingPublishResult{}, err + } + state, err := s.readPublishState(ctx, name) + if err != nil { + return RouteBindingPublishResult{}, err + } + if expectedDraftRevision > 0 && (!state.draftExists || state.draftRevision != expectedDraftRevision) { + return RouteBindingPublishResult{}, fmt.Errorf("%w: %s", ErrRouteBindingPublishConflict, name) + } + publishedEntries, err := s.listEntries(ctx, state.publishedPrefix) + if err != nil { + return RouteBindingPublishResult{}, err + } + candidate, err := s.compilePublishCandidate(name, state.draft, state.draftExists, publishedEntries) + if err != nil { + return RouteBindingPublishResult{}, err + } + if err := s.checkRuntimeRouteConflict(ctx, candidate, state); err != nil { + return RouteBindingPublishResult{}, err + } + comparisons, err := s.buildPublishComparisons(ctx, state) + if err != nil { + return RouteBindingPublishResult{}, err + } + operations, publishedCount, deletedCount, err := s.buildSinglePublishOperations(state, candidate) + if err != nil { + return RouteBindingPublishResult{}, err + } + operations = append(operations, + clientv3.OpPut(s.publishedRevisionKey(), strconv.FormatInt(time.Now().UnixNano(), 10))) + response, err := s.kv.Txn(ctx).If(comparisons...).Then(operations...).Commit() + if err != nil { + return RouteBindingPublishResult{}, fmt.Errorf("publish route binding %q: %w", name, err) + } + if !response.Succeeded { + return RouteBindingPublishResult{}, fmt.Errorf("%w: %s", ErrRouteBindingPublishConflict, name) + } + publishedRevision, err := s.publishedRouteRevision(ctx, state.publishedPrefix, name, candidate) + if err != nil { + return RouteBindingPublishResult{}, err + } + return RouteBindingPublishResult{ + Name: name, + Revision: response.Header.Revision, + DraftRevision: state.draftRevision, + PublishedRevision: publishedRevision, + PublishedCount: publishedCount, + DeletedCount: deletedCount, + }, nil +} + +type routeBindingPublishState struct { + draftPrefix string + publishedPrefix string + draft routeBindingEntry + draftExists bool + draftRevision int64 + published routeBindingEntry + publishedExists bool + publishedRevision int64 +} + +func (s *RouteBindingStore) readPublishState(ctx context.Context, name string) (routeBindingPublishState, error) { + state := routeBindingPublishState{ + draftPrefix: s.bindingPrefix(true), + publishedPrefix: s.bindingPrefix(false), + } + var err error + state.draft, state.draftExists, state.draftRevision, err = s.getEntry(ctx, state.draftPrefix, name) + if err != nil { + return routeBindingPublishState{}, err + } + state.published, state.publishedExists, state.publishedRevision, err = s.getEntry(ctx, state.publishedPrefix, name) + if err != nil { + return routeBindingPublishState{}, err + } + if !state.draftExists && !state.publishedExists { + return routeBindingPublishState{}, fmt.Errorf("%w: %s", ErrRouteBindingNotFound, name) + } + return state, nil +} + +func (s *RouteBindingStore) compilePublishCandidate(name string, draft routeBindingEntry, draftExists bool, published []routeBindingEntry) (*compiledRouteEntry, error) { + if !draftExists { + return nil, nil + } + entries := make([]routeBindingEntry, 0, len(published)+1) + for _, entry := range published { + if entry.record.Object.Metadata.Name != name { + entries = append(entries, entry) + } + } + entries = append(entries, draft) + compiled, err := s.compileSnapshot(entries) + if err != nil { + return nil, err + } + for index := range compiled { + if compiled[index].object.Metadata.Name == name { + return &compiled[index], nil + } + } + return nil, fmt.Errorf("route binding %q was not compiled", name) +} + +func (s *RouteBindingStore) buildPublishComparisons(ctx context.Context, state routeBindingPublishState) ([]clientv3.Cmp, error) { + comparisons := make([]clientv3.Cmp, 0, 2) + if state.draftExists { + // Always compare the value observed above, even when the caller omitted + // expectedDraftRevision. This closes the read/modify/write race. + comparisons = append(comparisons, + clientv3.Compare(clientv3.ModRevision(s.bindingKey(state.draftPrefix, state.draft.record.Object.Metadata.Name)), "=", state.draftRevision)) + if state.publishedExists { + comparisons = append(comparisons, + clientv3.Compare(clientv3.ModRevision(s.bindingKey(state.publishedPrefix, state.published.record.Object.Metadata.Name)), "=", state.publishedRevision)) + } else { + comparisons = append(comparisons, + clientv3.Compare(clientv3.CreateRevision(s.bindingKey(state.publishedPrefix, state.draft.record.Object.Metadata.Name)), "=", 0)) + } + } else { + comparisons = append(comparisons, + clientv3.Compare(clientv3.ModRevision(s.bindingKey(state.publishedPrefix, state.published.record.Object.Metadata.Name)), "=", state.publishedRevision)) + } + publishedMarkerRevision, publishedMarkerExists, err := s.markerRevision(ctx, s.publishedRevisionKey()) Review Comment: 请求 A 完成快照读取和冲突检查后,请求 B 可以先发布同路径、同方法的路由。随后 A 读取到 B 更新后的 marker,事务比较仍会成功,导致两条冲突配置同时发布。marker 应在读取待校验快照之前取得,并在提交时比较。 -- 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]
