-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Prevent circular dependencies between v2 resources and generate a mermaid diagram with their dependencies #19230
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
Changes from all commits
Commits
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
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
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,45 @@ | ||
| ```mermaid | ||
| flowchart TD | ||
| auth/v2beta1/computedtrafficpermissions --> auth/v2beta1/trafficpermissions | ||
| auth/v2beta1/computedtrafficpermissions --> auth/v2beta1/workloadidentity | ||
| catalog/v2beta1/failoverpolicy --> catalog/v2beta1/service | ||
| catalog/v2beta1/healthstatus | ||
| catalog/v2beta1/node --> catalog/v2beta1/healthstatus | ||
| catalog/v2beta1/service | ||
| catalog/v2beta1/serviceendpoints --> catalog/v2beta1/service | ||
| catalog/v2beta1/serviceendpoints --> catalog/v2beta1/workload | ||
| catalog/v2beta1/workload --> catalog/v2beta1/healthstatus | ||
| catalog/v2beta1/workload --> catalog/v2beta1/node | ||
| demo/v1/album | ||
| demo/v1/artist | ||
| demo/v1/concept | ||
| demo/v1/executive | ||
| demo/v1/recordlabel | ||
| demo/v2/album | ||
| demo/v2/artist | ||
| internal/v1/tombstone | ||
| mesh/v2beta1/computedexplicitdestinations --> catalog/v2beta1/service | ||
| mesh/v2beta1/computedexplicitdestinations --> catalog/v2beta1/workload | ||
| mesh/v2beta1/computedexplicitdestinations --> mesh/v2beta1/computedroutes | ||
| mesh/v2beta1/computedexplicitdestinations --> mesh/v2beta1/destinations | ||
| mesh/v2beta1/computedproxyconfiguration --> catalog/v2beta1/workload | ||
| mesh/v2beta1/computedproxyconfiguration --> mesh/v2beta1/proxyconfiguration | ||
| mesh/v2beta1/computedroutes --> catalog/v2beta1/failoverpolicy | ||
| mesh/v2beta1/computedroutes --> catalog/v2beta1/service | ||
| mesh/v2beta1/computedroutes --> mesh/v2beta1/destinationpolicy | ||
| mesh/v2beta1/computedroutes --> mesh/v2beta1/grpcroute | ||
| mesh/v2beta1/computedroutes --> mesh/v2beta1/httproute | ||
| mesh/v2beta1/computedroutes --> mesh/v2beta1/tcproute | ||
| mesh/v2beta1/destinationpolicy | ||
| mesh/v2beta1/destinations | ||
| mesh/v2beta1/grpcroute | ||
| mesh/v2beta1/httproute | ||
| mesh/v2beta1/proxyconfiguration | ||
| mesh/v2beta1/proxystatetemplate --> auth/v2beta1/computedtrafficpermissions | ||
| mesh/v2beta1/proxystatetemplate --> catalog/v2beta1/service | ||
| mesh/v2beta1/proxystatetemplate --> catalog/v2beta1/workload | ||
| mesh/v2beta1/proxystatetemplate --> mesh/v2beta1/computedexplicitdestinations | ||
| mesh/v2beta1/proxystatetemplate --> mesh/v2beta1/computedproxyconfiguration | ||
| mesh/v2beta1/proxystatetemplate --> mesh/v2beta1/computedroutes | ||
| mesh/v2beta1/tcproute | ||
| ``` |
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,100 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| package controller | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "github.com/hashicorp/go-multierror" | ||
|
|
||
| "github.com/hashicorp/consul/internal/resource" | ||
| "github.com/hashicorp/consul/proto-public/pbresource" | ||
| ) | ||
|
|
||
| func (m *Manager) ValidateDependencies(registrations []resource.Registration) error { | ||
| deps := m.CalculateDependencies(registrations) | ||
|
|
||
| return deps.validate() | ||
| } | ||
|
|
||
| type Dependencies map[string][]string | ||
|
|
||
| func (deps Dependencies) validate() error { | ||
| var merr error | ||
| seen := make(map[string]map[string]struct{}) | ||
|
|
||
| mkErr := func(src, dst string) error { | ||
| vals := []string{src, dst} | ||
| sort.Strings(vals) | ||
| return fmt.Errorf("circular dependency between %q and %q", vals[0], vals[1]) | ||
| } | ||
|
|
||
| for src, dsts := range deps { | ||
| seenDsts := seen[src] | ||
| if len(seenDsts) == 0 { | ||
| seen[src] = make(map[string]struct{}) | ||
| } | ||
|
|
||
| for _, dst := range dsts { | ||
| if _, ok := seenDsts[dst]; ok { | ||
| merr = multierror.Append(merr, mkErr(src, dst)) | ||
| } | ||
|
|
||
| if inverseDsts := seen[dst]; len(inverseDsts) > 0 { | ||
| if _, ok := inverseDsts[src]; ok { | ||
| merr = multierror.Append(merr, mkErr(src, dst)) | ||
| } | ||
| } | ||
| seen[src][dst] = struct{}{} | ||
| } | ||
| } | ||
|
|
||
| return merr | ||
| } | ||
|
|
||
| func (m *Manager) CalculateDependencies(registrations []resource.Registration) Dependencies { | ||
| typeToString := func(t *pbresource.Type) string { | ||
| return strings.ToLower(fmt.Sprintf("%s/%s/%s", t.Group, t.GroupVersion, t.Kind)) | ||
| } | ||
|
|
||
| out := make(map[string][]string) | ||
| for _, r := range registrations { | ||
| out[typeToString(r.Type)] = nil | ||
| } | ||
|
|
||
| for _, c := range m.controllers { | ||
| watches := make([]string, 0, len(c.watches)) | ||
| for _, w := range c.watches { | ||
| watches = append(watches, typeToString(w.watchedType)) | ||
| } | ||
|
|
||
| out[typeToString(c.managedType)] = watches | ||
| } | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| func (deps Dependencies) ToMermaid() string { | ||
| depStrings := make([]string, 0, len(deps)) | ||
|
|
||
| for src, dsts := range deps { | ||
| if len(dsts) == 0 { | ||
| depStrings = append(depStrings, fmt.Sprintf(" %s", src)) | ||
| continue | ||
| } | ||
|
|
||
| for _, dst := range dsts { | ||
| depStrings = append(depStrings, fmt.Sprintf(" %s --> %s", src, dst)) | ||
| } | ||
| } | ||
|
|
||
| sort.Slice(depStrings, func(a, b int) bool { | ||
| return depStrings[a] < depStrings[b] | ||
| }) | ||
| out := "flowchart TD\n" + strings.Join(depStrings, "\n") | ||
|
|
||
| return out | ||
| } |
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,66 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| package controller | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/consul/internal/testing/golden" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestDependenciesGolden(t *testing.T) { | ||
| deps := Dependencies{ | ||
| "t1": []string{"t2", "t3"}, | ||
| "t2": []string{"t4"}, | ||
| "t4": []string{"t1"}, | ||
| } | ||
| mermaid := deps.ToMermaid() | ||
| expected := golden.Get(t, mermaid, "dependencies.golden") | ||
| require.Equal(t, expected, mermaid) | ||
| } | ||
|
|
||
| func TestValidateDependencies(t *testing.T) { | ||
| type testCase struct { | ||
| dependencies Dependencies | ||
| expectErr string | ||
| } | ||
|
|
||
| run := func(t *testing.T, tc testCase) { | ||
| err := tc.dependencies.validate() | ||
| if len(tc.expectErr) > 0 { | ||
| require.Contains(t, err.Error(), tc.expectErr) | ||
| } else { | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| cases := map[string]testCase{ | ||
| "empty": { | ||
| dependencies: nil, | ||
| }, | ||
| "no circular dependencies": { | ||
| dependencies: Dependencies{ | ||
| "t1": []string{"t2", "t3"}, | ||
| "t2": []string{"t3"}, | ||
| "t3": []string{"t4"}, | ||
| "t4": nil, | ||
| }, | ||
| }, | ||
| "with circular dependency": { | ||
| dependencies: Dependencies{ | ||
| "t1": []string{"t2", "t3"}, | ||
| "t2": []string{"t1"}, | ||
| }, | ||
| expectErr: `circular dependency between "t1" and "t2"`, | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range cases { | ||
| t.Run(name, func(t *testing.T) { | ||
| run(t, tc) | ||
| }) | ||
| } | ||
| } |
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,5 @@ | ||
| flowchart TD | ||
| t1 --> t2 | ||
| t1 --> t3 | ||
| t2 --> t4 | ||
| t4 --> t1 |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Part of this I believe can call into
github.com/hashicorp/consul/internal/testing/goldenThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started looking into this and fixing the
flag redefined: updatethis causes is going to take quite a bit of work. For now, I'm going to leave this as is.