-
Notifications
You must be signed in to change notification settings - Fork 9
feat: adding device sync to Nautobot Operator #2098
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,31 @@ spec: | |
| - configMapSelector | ||
| type: object | ||
| type: array | ||
| deviceRef: | ||
| items: | ||
| description: ConfigMapRef defines a reference to a specific ConfigMap | ||
| properties: | ||
| configMapSelector: | ||
| description: The name of the ConfigMap resource being referred | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The description says this selector "is" a name, but then on line 132 we have a separate key field — so it's unclear what the outer description is even referring to. This still looks confusing to me. |
||
| to | ||
| properties: | ||
| key: | ||
| description: The key in the ConfigMap data | ||
| type: string | ||
| name: | ||
| description: The name of the ConfigMap | ||
| minLength: 1 | ||
| type: string | ||
| namespace: | ||
| description: The namespace where the ConfigMap resides | ||
| type: string | ||
| required: | ||
| - name | ||
| type: object | ||
| required: | ||
| - configMapSelector | ||
| type: object | ||
| type: array | ||
| deviceTypeRef: | ||
| items: | ||
| description: ConfigMapRef defines a reference to a specific ConfigMap | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,31 @@ spec: | |
| - configMapSelector | ||
| type: object | ||
| type: array | ||
| deviceRef: | ||
| items: | ||
| description: ConfigMapRef defines a reference to a specific ConfigMap | ||
| properties: | ||
| configMapSelector: | ||
| description: The name of the ConfigMap resource being referred | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same confusion as I described in /config/crd/bases/sync.rax.io_nautobots.yaml |
||
| to | ||
| properties: | ||
| key: | ||
| description: The key in the ConfigMap data | ||
| type: string | ||
| name: | ||
| description: The name of the ConfigMap | ||
| minLength: 1 | ||
| type: string | ||
| namespace: | ||
| description: The namespace where the ConfigMap resides | ||
| type: string | ||
| required: | ||
| - name | ||
| type: object | ||
| required: | ||
| - configMapSelector | ||
| type: object | ||
| type: array | ||
| deviceTypeRef: | ||
| items: | ||
| description: ConfigMapRef defines a reference to a specific ConfigMap | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sort" | ||
| ) | ||
|
|
||
| // ResourceNode defines a sync resource with explicit dependency declarations. | ||
| // The topological sort uses DependsOn to determine execution order automatically. | ||
| type ResourceNode struct { | ||
| Name string | ||
| DependsOn []string | ||
| } | ||
|
|
||
| // topologicalSort using Kahn's algorithm here | ||
| // a deterministic execution order that respects all declared dependencies. | ||
| // | ||
| // Returns an error if: | ||
| // - A cycle is detected (not a DAG) | ||
| // - A node references a dependency that doesn't exist in the input | ||
| // - Duplicate node names are provided | ||
| func topologicalSort(nodes []ResourceNode) ([]string, error) { | ||
| if len(nodes) == 0 { | ||
| return nil, nil | ||
| } | ||
| nodeSet := make(map[string]struct{}, len(nodes)) | ||
| for _, node := range nodes { | ||
| if _, exists := nodeSet[node.Name]; exists { | ||
| return nil, fmt.Errorf("duplicate node name: %q", node.Name) | ||
| } | ||
| nodeSet[node.Name] = struct{}{} | ||
| } | ||
|
|
||
| for _, node := range nodes { | ||
| for _, dep := range node.DependsOn { | ||
| if _, exists := nodeSet[dep]; !exists { | ||
| return nil, fmt.Errorf("node %q depends on %q which does not exist", node.Name, dep) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| inDegree := make(map[string]int, len(nodes)) | ||
| dependents := make(map[string][]string, len(nodes)) | ||
|
|
||
| for _, node := range nodes { | ||
| inDegree[node.Name] = len(node.DependsOn) | ||
| for _, dep := range node.DependsOn { | ||
| dependents[dep] = append(dependents[dep], node.Name) | ||
| } | ||
| } | ||
|
|
||
| var queue []string | ||
| for _, node := range nodes { | ||
| if inDegree[node.Name] == 0 { | ||
| queue = append(queue, node.Name) | ||
| } | ||
| } | ||
| sort.Strings(queue) | ||
|
|
||
| result := make([]string, 0, len(nodes)) | ||
|
|
||
| for len(queue) > 0 { | ||
| current := queue[0] | ||
| queue = queue[1:] | ||
| result = append(result, current) | ||
|
|
||
| children := dependents[current] | ||
| sort.Strings(children) | ||
| for _, child := range children { | ||
| inDegree[child]-- | ||
| if inDegree[child] == 0 { | ||
| queue = append(queue, child) | ||
| } | ||
| } | ||
| sort.Strings(queue) | ||
| } | ||
|
|
||
| if len(result) != len(nodes) { | ||
| var cycleNodes []string | ||
| for name, degree := range inDegree { | ||
| if degree > 0 { | ||
| cycleNodes = append(cycleNodes, name) | ||
| } | ||
| } | ||
| sort.Strings(cycleNodes) | ||
| return nil, fmt.Errorf("dependency cycle detected involving nodes: %v", cycleNodes) | ||
| } | ||
|
|
||
| return result, nil | ||
| } |
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.
Nit: can we improve the description here? It sounds slightly unintuitive. I can't make sens eof this sentence, 'ie' reference to a specific ConfigMap' of what?