From 6c5f7e2eb510f63a69dfe7071e939b3988e6bfb7 Mon Sep 17 00:00:00 2001 From: Xie Zheng Date: Wed, 31 Jan 2024 18:00:54 +0800 Subject: [PATCH] Implement image fetcher feature Sync nsx.vmware.com/node-id annotation of subnetport with nsx-t. Sync wcp.vmware.com/image-fetcher: "true" and namespace_uid label of subnetport with tags of nsx-t. Signed-off-by: Xie Zheng --- cmd/main.go | 2 +- pkg/controllers/pod/pod_controller.go | 2 +- .../subnetport/subnetport_controller.go | 30 ++++++++++++++++--- pkg/nsx/services/common/types.go | 2 ++ pkg/nsx/services/subnetport/builder.go | 19 +++++++++--- pkg/nsx/services/subnetport/subnetport.go | 4 +-- pkg/util/utils.go | 13 ++++---- 7 files changed, 52 insertions(+), 20 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 026d11b29..170603db0 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -234,7 +234,7 @@ func main() { node.StartNodeController(mgr, nodeService) staticroutecontroller.StartStaticRouteController(mgr, staticRouteService) - subnetport.StartSubnetPortController(mgr, subnetPortService, subnetService, vpcService) + subnetport.StartSubnetPortController(mgr, subnetPortService, subnetService, vpcService, nodeService) pod.StartPodController(mgr, subnetPortService, subnetService, vpcService, nodeService) StartIPPoolController(mgr, ipPoolService, vpcService) networkpolicycontroller.StartNetworkPolicyController(mgr, commonService, vpcService) diff --git a/pkg/controllers/pod/pod_controller.go b/pkg/controllers/pod/pod_controller.go index daad9ca7d..773fcdd76 100644 --- a/pkg/controllers/pod/pod_controller.go +++ b/pkg/controllers/pod/pod_controller.go @@ -92,7 +92,7 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R return common.ResultRequeue, err } contextID := *node.Id - nsxSubnetPortState, err := r.SubnetPortService.CreateOrUpdateSubnetPort(pod, nsxSubnetPath, contextID, &pod.ObjectMeta.Labels) + nsxSubnetPortState, err := r.SubnetPortService.CreateOrUpdateSubnetPort(pod, nsxSubnetPath, contextID, &pod.ObjectMeta.Labels, false) if err != nil { log.Error(err, "failed to create or update NSX subnet port, would retry exponentially", "pod.Name", req.NamespacedName, "pod.UID", pod.UID) updateFail(r, &ctx, pod, &err) diff --git a/pkg/controllers/subnetport/subnetport_controller.go b/pkg/controllers/subnetport/subnetport_controller.go index 2ee9871d1..4a041264e 100644 --- a/pkg/controllers/subnetport/subnetport_controller.go +++ b/pkg/controllers/subnetport/subnetport_controller.go @@ -54,6 +54,7 @@ type SubnetPortReconciler struct { SubnetService servicecommon.SubnetServiceProvider VPCService servicecommon.VPCServiceProvider Recorder record.EventRecorder + NodeServiceReader servicecommon.NodeServiceReader } // +kubebuilder:rbac:groups=nsx.vmware.com,resources=subnetports,verbs=get;list;watch;create;update;patch;delete @@ -99,7 +100,27 @@ func (r *SubnetPortReconciler) Reconcile(ctx context.Context, req ctrl.Request) log.Error(err, "failed to get labels from virtualmachine", "subnetPort.Name", subnetPort.Name, "subnetPort.UID", subnetPort.UID) return common.ResultRequeue, err } - nsxSubnetPortState, err := r.SubnetPortService.CreateOrUpdateSubnetPort(subnetPort, nsxSubnetPath, "", labels) + + isVmSubnetPort := true + if subnetPort.Labels[servicecommon.LabelImageFetcher] == "true" { + isVmSubnetPort = false + if labels == nil { + labels = &map[string]string{} + } + (*labels)[servicecommon.LabelImageFetcher] = "true" + } + // specified by user, or default to the node name of the VM the pod runs on + hostname := subnetPort.Annotations[servicecommon.AnnotationHostName] + contextID := "" + if hostname != "" { + nodes := r.NodeServiceReader.GetNodeByName(hostname) + if len(nodes) == 0 { + return common.ResultRequeue, fmt.Errorf("node %s not found", hostname) + } + contextID = *nodes[0].Id + } + + nsxSubnetPortState, err := r.SubnetPortService.CreateOrUpdateSubnetPort(subnetPort, nsxSubnetPath, contextID, labels, isVmSubnetPort) if err != nil { log.Error(err, "failed to create or update NSX subnet port, would retry exponentially", "subnetport", req.NamespacedName) updateFail(r, &ctx, subnetPort, &err) @@ -162,8 +183,8 @@ func (r *SubnetPortReconciler) SetupWithManager(mgr ctrl.Manager) error { MaxConcurrentReconciles: common.NumReconcile(), }). Watches(&vmv1alpha1.VirtualMachine{}, - handler.EnqueueRequestsFromMapFunc(r.vmMapFunc), - builder.WithPredicates(predicate.LabelChangedPredicate{})). + handler.EnqueueRequestsFromMapFunc(r.vmMapFunc), + builder.WithPredicates(predicate.LabelChangedPredicate{})). Complete(r) // TODO: watch the virtualmachine event and update the labels on NSX subnet port. } @@ -199,7 +220,7 @@ func (r *SubnetPortReconciler) vmMapFunc(_ context.Context, vm client.Object) [] return requests } -func StartSubnetPortController(mgr ctrl.Manager, subnetPortService *subnetport.SubnetPortService, subnetService *subnet.SubnetService, vpcService *vpc.VPCService) { +func StartSubnetPortController(mgr ctrl.Manager, subnetPortService *subnetport.SubnetPortService, subnetService *subnet.SubnetService, vpcService *vpc.VPCService, nodeService servicecommon.NodeServiceReader) { subnetPortReconciler := SubnetPortReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), @@ -207,6 +228,7 @@ func StartSubnetPortController(mgr ctrl.Manager, subnetPortService *subnetport.S SubnetPortService: subnetPortService, VPCService: vpcService, Recorder: mgr.GetEventRecorderFor("subnetport-controller"), + NodeServiceReader: nodeService, } if err := subnetPortReconciler.Start(mgr); err != nil { log.Error(err, "failed to create controller", "controller", "SubnetPort") diff --git a/pkg/nsx/services/common/types.go b/pkg/nsx/services/common/types.go index 74b4bc5be..8251c7a29 100644 --- a/pkg/nsx/services/common/types.go +++ b/pkg/nsx/services/common/types.go @@ -59,6 +59,8 @@ const ( TagScopeVMNamespaceUID string = "nsx-op/vm_namespace_uid" TagScopeVMNamespace string = "nsx-op/vm_namespace" LabelDefaultSubnetSet string = "nsxoperator.vmware.com/default-subnetset-for" + LabelImageFetcher string = "wcp.vmware.com/image-fetcher" + AnnotationHostName string = "kubernetes.io/hostname" LabelDefaultVMSubnetSet string = "VirtualMachine" LabelDefaultPodSubnetSet string = "Pod" DefaultPodSubnetSet string = "pod-default" diff --git a/pkg/nsx/services/subnetport/builder.go b/pkg/nsx/services/subnetport/builder.go index 0372b1ae3..bdb0250ea 100644 --- a/pkg/nsx/services/subnetport/builder.go +++ b/pkg/nsx/services/subnetport/builder.go @@ -20,7 +20,7 @@ var ( String = common.String ) -func (service *SubnetPortService) buildSubnetPort(obj interface{}, nsxSubnetPath string, contextID string, labelTags *map[string]string) (*model.VpcSubnetPort, error) { +func (service *SubnetPortService) buildSubnetPort(obj interface{}, nsxSubnetPath string, contextID string, labelTags *map[string]string, isVmSubnetPort bool) (*model.VpcSubnetPort, error) { var objName, objNamespace, uid, appId string switch o := obj.(type) { case *v1alpha1.SubnetPort: @@ -54,9 +54,20 @@ func (service *SubnetPortService) buildSubnetPort(obj interface{}, nsxSubnetPath } namespace_uid := namespace.UID tags := util.BuildBasicTags(getCluster(service), obj, namespace_uid) + var tagsFiltered []model.Tag + for _, tag := range tags { + if isVmSubnetPort && *tag.Scope == common.TagScopeNamespaceUID { + continue + } + if !isVmSubnetPort && *tag.Scope == common.TagScopeVMNamespaceUID { + continue + } + tagsFiltered = append(tagsFiltered, tag) + } + if labelTags != nil { for k, v := range *labelTags { - tags = append(tags, model.Tag{Scope: String(k), Tag: String(v)}) + tagsFiltered = append(tagsFiltered, model.Tag{Scope: String(k), Tag: String(v)}) } } nsxSubnetPort := &model.VpcSubnetPort{ @@ -68,14 +79,14 @@ func (service *SubnetPortService) buildSubnetPort(obj interface{}, nsxSubnetPath TrafficTag: common.Int64(0), Type_: String("STATIC"), }, - Tags: tags, + Tags: tagsFiltered, Path: &nsxSubnetPortPath, ParentPath: &nsxSubnetPath, } if appId != "" { nsxSubnetPort.Attachment.AppId = &appId - nsxSubnetPort.Attachment.ContextId = &contextID } + nsxSubnetPort.Attachment.ContextId = &contextID return nsxSubnetPort, nil } diff --git a/pkg/nsx/services/subnetport/subnetport.go b/pkg/nsx/services/subnetport/subnetport.go index 6bc9e15db..fe32adfc2 100644 --- a/pkg/nsx/services/subnetport/subnetport.go +++ b/pkg/nsx/services/subnetport/subnetport.go @@ -74,7 +74,7 @@ func InitializeSubnetPort(service servicecommon.Service) (*SubnetPortService, er return subnetPortService, nil } -func (service *SubnetPortService) CreateOrUpdateSubnetPort(obj interface{}, nsxSubnetPath string, contextID string, tags *map[string]string) (*model.SegmentPortState, error) { +func (service *SubnetPortService) CreateOrUpdateSubnetPort(obj interface{}, nsxSubnetPath string, contextID string, tags *map[string]string, isVmSubnetPort bool) (*model.SegmentPortState, error) { var uid string switch o := obj.(type) { case *v1alpha1.SubnetPort: @@ -83,7 +83,7 @@ func (service *SubnetPortService) CreateOrUpdateSubnetPort(obj interface{}, nsxS uid = string(o.UID) } log.Info("creating or updating subnetport", "nsxSubnetPort.Id", uid, "nsxSubnetPath", nsxSubnetPath) - nsxSubnetPort, err := service.buildSubnetPort(obj, nsxSubnetPath, contextID, tags) + nsxSubnetPort, err := service.buildSubnetPort(obj, nsxSubnetPath, contextID, tags, isVmSubnetPort) if err != nil { log.Error(err, "failed to build NSX subnet port", "nsxSubnetPort.Id", uid, "nsxSubnetPath", nsxSubnetPath, "contextID", contextID) return nil, err diff --git a/pkg/util/utils.go b/pkg/util/utils.go index 098fd42da..c4ca2d52d 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -447,7 +447,6 @@ func BuildBasicTags(cluster string, obj interface{}, namespaceID types.UID) []mo Tag: String(strings.Join(common.TagValueVersion, ".")), }, } - isVmSubnetPort := false switch i := obj.(type) { case *v1alpha1.StaticRoute: tags = append(tags, model.Tag{Scope: String(common.TagScopeNamespace), Tag: String(i.ObjectMeta.Namespace)}) @@ -465,9 +464,11 @@ func BuildBasicTags(cluster string, obj interface{}, namespaceID types.UID) []mo tags = append(tags, model.Tag{Scope: String(common.TagScopeSubnetSetCRUID), Tag: String(string(i.UID))}) case *v1alpha1.SubnetPort: tags = append(tags, model.Tag{Scope: String(common.TagScopeVMNamespace), Tag: String(i.ObjectMeta.Namespace)}) - isVmSubnetPort = true tags = append(tags, model.Tag{Scope: String(common.TagScopeSubnetPortCRName), Tag: String(i.ObjectMeta.Name)}) tags = append(tags, model.Tag{Scope: String(common.TagScopeSubnetPortCRUID), Tag: String(string(i.UID))}) + if len(namespaceID) > 0 { + tags = append(tags, model.Tag{Scope: String(common.TagScopeVMNamespaceUID), Tag: String(string(namespaceID))}) + } case *v1.Pod: tags = append(tags, model.Tag{Scope: String(common.TagScopeNamespace), Tag: String(i.ObjectMeta.Namespace)}) tags = append(tags, model.Tag{Scope: String(common.TagScopePodName), Tag: String(i.ObjectMeta.Name)}) @@ -485,13 +486,9 @@ func BuildBasicTags(cluster string, obj interface{}, namespaceID types.UID) []mo } if len(namespaceID) > 0 { - if isVmSubnetPort == true { - // In the NSX subnet port created for VM, the namespace uid tag is TagScopeVMNamespaceUID instead of TagScopeNamespaceUID. - tags = append(tags, model.Tag{Scope: String(common.TagScopeVMNamespaceUID), Tag: String(string(namespaceID))}) - } else { - tags = append(tags, model.Tag{Scope: String(common.TagScopeNamespaceUID), Tag: String(string(namespaceID))}) - } + tags = append(tags, model.Tag{Scope: String(common.TagScopeNamespaceUID), Tag: String(string(namespaceID))}) } + return tags }