-
Notifications
You must be signed in to change notification settings - Fork 86
feat(omnicontrol): add resource topology aggregation backend #387
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
Open
sarthakkjha
wants to merge
2
commits into
karmada-io:main
Choose a base branch
from
sarthakkjha:omnicontrol
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,33 @@ | ||
| # OmniControl | ||
|
|
||
| Backend package for unified resource topology visualization in Karmada Dashboard. | ||
|
|
||
| ## Purpose | ||
|
|
||
| Aggregates the propagation path of a resource: | ||
| ``` | ||
| ResourceTemplate → PropagationPolicy → ResourceBinding → Work → Member Cluster | ||
| ``` | ||
|
|
||
| This enables users to trace exactly where a resource is distributed and identify propagation failures. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```go | ||
| topology, err := omnicontrol.GetDeploymentTopology(ctx, k8sClient, karmadaClient, "default", "nginx") | ||
| if err != nil { | ||
| // handle error | ||
| } | ||
|
|
||
| if topology.Policy != nil { | ||
| fmt.Println("Policy:", topology.Policy.Name) | ||
| } | ||
| if topology.Binding != nil { | ||
| fmt.Println("Binding:", topology.Binding.Name) | ||
| } | ||
| fmt.Println("Clusters:", len(topology.ClusterStatuses)) | ||
| ``` | ||
|
|
||
| ## Status | ||
|
|
||
| **PoC** - Initial implementation for Deployments. Future work includes support for all resource types and API endpoint integration. |
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,104 @@ | ||
| /* | ||
| Copyright 2024 The Karmada Authors. | ||
|
|
||
| Licensed 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 omnicontrol | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| karmadaworkv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1" | ||
| karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| client "k8s.io/client-go/kubernetes" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| // GetDeploymentTopology returns the propagation topology for a Deployment. | ||
| func GetDeploymentTopology(ctx context.Context, k8sClient client.Interface, karmadaClient karmadaclientset.Interface, namespace, name string) (*ResourceTopology, error) { | ||
| klog.V(4).InfoS("Building topology", "namespace", namespace, "name", name) | ||
|
|
||
| deployment, err := k8sClient.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{}) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get deployment %s/%s: %w", namespace, name, err) | ||
| } | ||
|
|
||
| unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(deployment) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to convert deployment: %w", err) | ||
| } | ||
|
|
||
| topology := &ResourceTopology{ | ||
| Resource: &unstructured.Unstructured{Object: unstructuredMap}, | ||
| } | ||
|
|
||
| // Karmada binding naming: "<name>-<kind>" | ||
| bindingName := name + "-deployment" | ||
| rb, err := karmadaClient.WorkV1alpha2().ResourceBindings(namespace).Get(ctx, bindingName, metav1.GetOptions{}) | ||
| if err != nil { | ||
| klog.V(4).InfoS("ResourceBinding not found", "name", bindingName) | ||
| return topology, nil | ||
| } | ||
| topology.Binding = rb | ||
|
|
||
| // Get policy from binding labels | ||
| policyName := rb.Labels["propagationpolicy.karmada.io/name"] | ||
| policyNS := rb.Labels["propagationpolicy.karmada.io/namespace"] | ||
| if policyNS == "" { | ||
| policyNS = namespace | ||
| } | ||
| if policyName != "" { | ||
| policy, err := karmadaClient.PolicyV1alpha1().PropagationPolicies(policyNS).Get(ctx, policyName, metav1.GetOptions{}) | ||
| if err == nil { | ||
| topology.Policy = policy | ||
| } else { | ||
| klog.V(4).InfoS("Could not retrieve PropagationPolicy", "namespace", policyNS, "name", policyName, "error", err) | ||
| } | ||
| } | ||
|
|
||
| // Get Work objects from execution namespaces | ||
| for _, status := range rb.Status.AggregatedStatus { | ||
| execNS := "karmada-es-" + status.ClusterName | ||
| works, err := karmadaClient.WorkV1alpha1().Works(execNS).List(ctx, metav1.ListOptions{ | ||
| LabelSelector: fmt.Sprintf("resourcebinding.karmada.io/uid=%s", rb.UID), | ||
| }) | ||
| if err != nil { | ||
| klog.Warningf("Failed to list works in execution namespace %s: %v", execNS, err) | ||
| continue | ||
| } | ||
|
|
||
| for i := range works.Items { | ||
| topology.ClusterStatuses = append(topology.ClusterStatuses, ClusterStatus{ | ||
| ClusterName: status.ClusterName, | ||
| SyncStatus: deriveSyncStatus(&works.Items[i]), | ||
| Work: &works.Items[i], | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| return topology, nil | ||
| } | ||
|
|
||
| func deriveSyncStatus(work *karmadaworkv1alpha1.Work) string { | ||
| for _, cond := range work.Status.Conditions { | ||
| if cond.Type == karmadaworkv1alpha1.WorkApplied { | ||
| return string(cond.Status) | ||
| } | ||
| } | ||
| return "Unknown" | ||
| } | ||
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,93 @@ | ||
| /* | ||
| Copyright 2024 The Karmada Authors. | ||
|
|
||
| Licensed 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 omnicontrol | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| karmadapolicyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" | ||
| karmadaworkv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" | ||
| karmadafake "github.com/karmada-io/karmada/pkg/generated/clientset/versioned/fake" | ||
| appsv1 "k8s.io/api/apps/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes/fake" | ||
| ) | ||
|
|
||
| func TestGetDeploymentTopology_NotFound(t *testing.T) { | ||
| k8sClient := fake.NewSimpleClientset() | ||
| karmadaClient := karmadafake.NewSimpleClientset() | ||
|
|
||
| _, err := GetDeploymentTopology(context.Background(), k8sClient, karmadaClient, "default", "nonexistent") | ||
| if err == nil { | ||
| t.Error("expected error when deployment does not exist, got nil") | ||
| } | ||
| } | ||
sarthakkjha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func TestGetDeploymentTopology_NoBinding(t *testing.T) { | ||
| deployment := &appsv1.Deployment{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "nginx", Namespace: "default"}, | ||
| } | ||
| k8sClient := fake.NewSimpleClientset(deployment) | ||
| karmadaClient := karmadafake.NewSimpleClientset() | ||
|
|
||
| topology, err := GetDeploymentTopology(context.Background(), k8sClient, karmadaClient, "default", "nginx") | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if topology.Binding != nil { | ||
| t.Error("expected no binding, got one") | ||
| } | ||
| if topology.Resource == nil { | ||
| t.Error("expected resource to be populated") | ||
| } | ||
| } | ||
|
|
||
| func TestGetDeploymentTopology_FullTopology(t *testing.T) { | ||
| deployment := &appsv1.Deployment{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "nginx", Namespace: "default"}, | ||
| } | ||
| policy := &karmadapolicyv1alpha1.PropagationPolicy{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "nginx-policy", Namespace: "default"}, | ||
| } | ||
| binding := &karmadaworkv1alpha2.ResourceBinding{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "nginx-deployment", | ||
| Namespace: "default", | ||
| Labels: map[string]string{ | ||
| "propagationpolicy.karmada.io/name": "nginx-policy", | ||
| "propagationpolicy.karmada.io/namespace": "default", | ||
| }, | ||
| }, | ||
| } | ||
| k8sClient := fake.NewSimpleClientset(deployment) | ||
| karmadaClient := karmadafake.NewSimpleClientset(binding, policy) | ||
|
|
||
| topology, err := GetDeploymentTopology(context.Background(), k8sClient, karmadaClient, "default", "nginx") | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if topology.Resource == nil { | ||
| t.Error("expected resource") | ||
| } | ||
| if topology.Binding == nil { | ||
| t.Error("expected binding") | ||
| } | ||
| if topology.Policy == nil { | ||
| t.Error("expected policy") | ||
| } | ||
| } | ||
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,39 @@ | ||
| /* | ||
| Copyright 2024 The Karmada Authors. | ||
|
|
||
| Licensed 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 omnicontrol | ||
|
|
||
| import ( | ||
| karmadapolicyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" | ||
| karmadaworkv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1" | ||
| karmadaworkv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| ) | ||
|
|
||
| // ClusterStatus holds the sync state of a resource in a member cluster. | ||
| type ClusterStatus struct { | ||
| ClusterName string `json:"clusterName"` | ||
| SyncStatus string `json:"syncStatus"` | ||
| Work *karmadaworkv1alpha1.Work `json:"work,omitempty"` | ||
| } | ||
|
|
||
| // ResourceTopology aggregates a resource's propagation path: Template → Policy → Binding → Work. | ||
| type ResourceTopology struct { | ||
| Resource *unstructured.Unstructured `json:"resource"` | ||
| Policy *karmadapolicyv1alpha1.PropagationPolicy `json:"policy,omitempty"` | ||
| Binding *karmadaworkv1alpha2.ResourceBinding `json:"binding,omitempty"` | ||
| ClusterStatuses []ClusterStatus `json:"clusterStatuses,omitempty"` | ||
| } |
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.