-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Basic resource type registry #16622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Basic resource type registry #16622
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4ce5933
first pass
analogue ac890a1
Finish up tests
analogue 66818c7
Guard access to the registry with locks
analogue c5b3faa
Use string as registry map key instead of unstable pb struct
analogue 83ff27e
Add TODO for protobuf message type
analogue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package resource | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sync" | ||
|
|
||
| "github.com/hashicorp/consul/proto-public/pbresource" | ||
| ) | ||
|
|
||
| type Registry interface { | ||
| // Register the given resource type and its hooks. | ||
| Register(reg Registration) | ||
|
|
||
| // Resolve the given resource type and its hooks. | ||
| Resolve(typ *pbresource.Type) (reg Registration, ok bool) | ||
| } | ||
|
|
||
| type Registration struct { | ||
| // Type is the GVK of the resource type. | ||
| Type *pbresource.Type | ||
|
|
||
| // In the future, we'll add hooks, the controller etc. here. | ||
| // TODO: https://github.com/hashicorp/consul/pull/16622#discussion_r1134515909 | ||
| } | ||
|
|
||
| // Hashable key for a resource type | ||
| type TypeKey string | ||
|
|
||
| // Resource type registry | ||
| type TypeRegistry struct { | ||
| // registrations keyed by GVK | ||
| registrations map[string]Registration | ||
| lock sync.RWMutex | ||
| } | ||
|
|
||
| func NewRegistry() Registry { | ||
| return &TypeRegistry{ | ||
| registrations: make(map[string]Registration), | ||
| } | ||
| } | ||
|
|
||
| func (r *TypeRegistry) Register(registration Registration) { | ||
| r.lock.Lock() | ||
| defer r.lock.Unlock() | ||
|
|
||
| typ := registration.Type | ||
| if typ.Group == "" || typ.GroupVersion == "" || typ.Kind == "" { | ||
| panic("type field(s) cannot be empty") | ||
| } | ||
|
|
||
| key := ToGVK(registration.Type) | ||
| if _, ok := r.registrations[key]; ok { | ||
| panic(fmt.Sprintf("resource type %s already registered", key)) | ||
| } | ||
|
|
||
| r.registrations[key] = registration | ||
| } | ||
|
|
||
| func (r *TypeRegistry) Resolve(typ *pbresource.Type) (reg Registration, ok bool) { | ||
| r.lock.RLock() | ||
| defer r.lock.RUnlock() | ||
|
|
||
| if registration, ok := r.registrations[ToGVK(typ)]; ok { | ||
| return registration, true | ||
| } | ||
| return Registration{}, false | ||
| } | ||
|
|
||
| func ToGVK(resourceType *pbresource.Type) string { | ||
| return fmt.Sprintf("%s/%s/%s", resourceType.Group, resourceType.GroupVersion, resourceType.Kind) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package resource | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/consul/proto-public/pbresource" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestRegister(t *testing.T) { | ||
| r := NewRegistry() | ||
|
|
||
| serviceType := &pbresource.Type{ | ||
| Group: "mesh", | ||
| GroupVersion: "v1", | ||
| Kind: "service", | ||
| } | ||
|
|
||
| // register | ||
| serviceRegistration := Registration{Type: serviceType} | ||
| r.Register(serviceRegistration) | ||
|
|
||
| // register existing should panic | ||
| assertRegisterPanics(t, r.Register, serviceRegistration, "resource type mesh/v1/service already registered") | ||
|
|
||
| // register empty Group should panic | ||
| assertRegisterPanics(t, r.Register, Registration{ | ||
| Type: &pbresource.Type{ | ||
| Group: "", | ||
| GroupVersion: "v1", | ||
| Kind: "service", | ||
| }, | ||
| }, "type field(s) cannot be empty") | ||
|
|
||
| // register empty GroupVersion should panic | ||
| assertRegisterPanics(t, r.Register, Registration{ | ||
| Type: &pbresource.Type{ | ||
| Group: "mesh", | ||
| GroupVersion: "", | ||
| Kind: "service", | ||
| }, | ||
| }, "type field(s) cannot be empty") | ||
|
|
||
| // register empty Kind should panic | ||
| assertRegisterPanics(t, r.Register, Registration{ | ||
| Type: &pbresource.Type{ | ||
| Group: "mesh", | ||
| GroupVersion: "v1", | ||
| Kind: "", | ||
| }, | ||
| }, "type field(s) cannot be empty") | ||
| } | ||
|
|
||
| func assertRegisterPanics(t *testing.T, registerFn func(reg Registration), registration Registration, panicString string) { | ||
| defer func() { | ||
| if r := recover(); r == nil { | ||
| t.Errorf("expected panic, but none occurred") | ||
| } else { | ||
| errstr, ok := r.(string) | ||
| if !ok { | ||
| t.Errorf("unexpected error type returned from panic") | ||
| } else if errstr != panicString { | ||
| t.Errorf("expected %s error message but got: %s", panicString, errstr) | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| registerFn(registration) | ||
| } | ||
|
|
||
| func TestResolve(t *testing.T) { | ||
| r := NewRegistry() | ||
|
|
||
| serviceType := &pbresource.Type{ | ||
| Group: "mesh", | ||
| GroupVersion: "v1", | ||
| Kind: "service", | ||
| } | ||
|
|
||
| // not found | ||
| _, ok := r.Resolve(serviceType) | ||
| assert.False(t, ok) | ||
|
|
||
| // found | ||
| r.Register(Registration{Type: serviceType}) | ||
| registration, ok := r.Resolve(serviceType) | ||
| assert.True(t, ok) | ||
| assert.Equal(t, registration.Type, serviceType) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.