-
Notifications
You must be signed in to change notification settings - Fork 24
Add Inventory Controller watchers and VM tag management for VKS IDPS support #1399
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
Draft
yuntanghsu
wants to merge
1
commit into
vmware-tanzu:main
Choose a base branch
from
yuntanghsu:yuntanghsu/idps
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.
Draft
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 |
|---|---|---|
|
|
@@ -45,6 +45,8 @@ var ( | |
| watchIngress, | ||
| watchNode, | ||
| watchNetworkPolicy, | ||
| watchNSXServiceAccount, | ||
| watchVirtualMachine, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
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,126 @@ | ||
| package inventory | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| vmv1alpha1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1" | ||
| "k8s.io/apimachinery/pkg/labels" | ||
| "k8s.io/client-go/tools/cache" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| nsxvmwarecomv1alpha1 "github.com/vmware-tanzu/nsx-operator/pkg/apis/legacy/v1alpha1" | ||
| "github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/inventory" | ||
| ) | ||
|
|
||
| func watchNSXServiceAccount(c *InventoryController, mgr ctrl.Manager) error { | ||
| nsxSAInformer, err := mgr.GetCache().GetInformer(context.Background(), &nsxvmwarecomv1alpha1.NSXServiceAccount{}) | ||
| if err != nil { | ||
| log.Error(err, "Failed to create NSXServiceAccount informer") | ||
| return err | ||
| } | ||
|
|
||
| _, err = nsxSAInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ | ||
| AddFunc: func(obj interface{}) { | ||
| c.handleNSXServiceAccount(obj) | ||
| }, | ||
| UpdateFunc: func(oldObj, newObj interface{}) { | ||
| c.handleNSXServiceAccount(newObj) | ||
| }, | ||
| DeleteFunc: func(obj interface{}) { | ||
| c.handleNSXServiceAccountDelete(obj) | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| log.Error(err, "Failed to add NSXServiceAccount event handler") | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c *InventoryController) handleNSXServiceAccount(obj interface{}) { | ||
| var nsxSA *nsxvmwarecomv1alpha1.NSXServiceAccount | ||
| switch v := obj.(type) { | ||
| case *nsxvmwarecomv1alpha1.NSXServiceAccount: | ||
| nsxSA = v | ||
| case cache.DeletedFinalStateUnknown: | ||
| var ok bool | ||
| nsxSA, ok = v.Obj.(*nsxvmwarecomv1alpha1.NSXServiceAccount) | ||
| if !ok { | ||
| err := fmt.Errorf("obj is not valid *NSXServiceAccount") | ||
| log.Error(err, "DeletedFinalStateUnknown Obj is not *NSXServiceAccount") | ||
| return | ||
| } | ||
| } | ||
|
|
||
| if nsxSA.Status.Phase != nsxvmwarecomv1alpha1.NSXServiceAccountPhaseRealized { | ||
| log.Debug("Skip NSXServiceAccount not yet realized", "namespace", nsxSA.Namespace, "name", nsxSA.Name) | ||
| return | ||
| } | ||
|
|
||
| c.enqueueVMsForCluster(nsxSA) | ||
| } | ||
|
|
||
| func (c *InventoryController) handleNSXServiceAccountDelete(obj interface{}) { | ||
| var nsxSA *nsxvmwarecomv1alpha1.NSXServiceAccount | ||
| switch v := obj.(type) { | ||
| case *nsxvmwarecomv1alpha1.NSXServiceAccount: | ||
| nsxSA = v | ||
| case cache.DeletedFinalStateUnknown: | ||
| var ok bool | ||
| nsxSA, ok = v.Obj.(*nsxvmwarecomv1alpha1.NSXServiceAccount) | ||
| if !ok { | ||
| err := fmt.Errorf("obj is not valid *NSXServiceAccount") | ||
| log.Error(err, "DeletedFinalStateUnknown Obj is not *NSXServiceAccount") | ||
| return | ||
| } | ||
| } | ||
|
|
||
| log.Info("NSXServiceAccount deleted, enqueuing VMs for tag removal", "namespace", nsxSA.Namespace, "name", nsxSA.Name) | ||
| c.enqueueVMsForCluster(nsxSA) | ||
| } | ||
|
|
||
| // enqueueVMsForCluster lists VirtualMachines belonging to the NSXServiceAccount's | ||
| // CAPI cluster and enqueues them to the inventory queue for VM tag processing. | ||
| func (c *InventoryController) enqueueVMsForCluster(nsxSA *nsxvmwarecomv1alpha1.NSXServiceAccount) { | ||
| clusterName := getClusterNameFromSA(nsxSA) | ||
| if clusterName == "" { | ||
| log.Info("NSXServiceAccount has no Cluster OwnerReference, skipping VM enqueue", | ||
| "namespace", nsxSA.Namespace, "name", nsxSA.Name) | ||
| return | ||
| } | ||
|
|
||
| vmList := &vmv1alpha1.VirtualMachineList{} | ||
| if err := c.Client.List(context.Background(), vmList, &client.ListOptions{ | ||
| Namespace: nsxSA.Namespace, | ||
| LabelSelector: labels.SelectorFromSet(labels.Set{inventory.CAPIClusterNameLabel: clusterName}), | ||
| }); err != nil { | ||
| log.Error(err, "Failed to list VirtualMachines for cluster", | ||
| "namespace", nsxSA.Namespace, "cluster", clusterName) | ||
| return | ||
| } | ||
|
|
||
| for i := range vmList.Items { | ||
| vm := &vmList.Items[i] | ||
| log.Debug("Enqueuing VM from NSXServiceAccount event", | ||
| "namespace", vm.Namespace, "name", vm.Name, "cluster", clusterName) | ||
| key, _ := keyFunc(vm) | ||
| c.inventoryObjectQueue.Add(inventory.InventoryKey{ | ||
| InventoryType: inventory.InventoryVirtualMachine, | ||
| ExternalId: vm.Status.InstanceUUID, | ||
| Key: key, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // getClusterNameFromSA extracts the CAPI Cluster name from the NSXServiceAccount's OwnerReferences. | ||
| func getClusterNameFromSA(nsxSA *nsxvmwarecomv1alpha1.NSXServiceAccount) string { | ||
| for _, ref := range nsxSA.OwnerReferences { | ||
| if ref.Kind == "Cluster" && strings.Contains(ref.APIVersion, "cluster.x-k8s.io") { | ||
| return ref.Name | ||
| } | ||
| } | ||
| return "" | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
With the exception of the above realization check,
handleNSXServiceAccountandhandleNSXServiceAccountDeleteare exactly the same.I think it should be ok to have this check even in the deletion handler, so maybe could we just remove
handleNSXServiceAccountDelete?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.
Could the NSXServiceAccount somehow become unrealized? I separated them because if the NSXServiceAccount becomes unrealized, the update and delete functions will skip it, and the VM's tag won't be removed.