Skip to content

Commit

Permalink
Implement image fetcher feature
Browse files Browse the repository at this point in the history
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.
Refer design doc
https://confluence.eng.vmware.com/display/NSBU/Design%3A+Image+Fetcher+binds+to+workload+network

Signed-off-by: Xie Zheng <[email protected]>
  • Loading branch information
zhengxiexie committed Feb 4, 2024
1 parent 924cc34 commit 4265f0f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/pod/pod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,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)
Expand Down
14 changes: 13 additions & 1 deletion pkg/controllers/subnetport/subnetport_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,19 @@ func (r *SubnetPortReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return common.ResultRequeue, err
}
log.Info("got labels from virtualmachine for subnetport", "subnetPort.UID", subnetPort.UID, "virtualmachine name", subnetPort.Spec.AttachmentRef.Name, "labels", labels)
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 id of the VM the pod runs on
contextID := subnetPort.Annotations[servicecommon.AnnotationNodeID]

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)
Expand Down
2 changes: 2 additions & 0 deletions pkg/nsx/services/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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"
AnnotationNodeID string = "nsx.vmware.com/node-id"
LabelDefaultVMSubnetSet string = "VirtualMachine"
LabelDefaultPodSubnetSet string = "Pod"
DefaultPodSubnetSet string = "pod-default"
Expand Down
19 changes: 15 additions & 4 deletions pkg/nsx/services/subnetport/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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{
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/nsx/services/subnetport/subnetport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
14 changes: 4 additions & 10 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
Expand All @@ -469,9 +468,12 @@ 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))})
tags = append(tags, model.Tag{Scope: String(common.TagScopeNamespaceUID), 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)})
Expand All @@ -488,14 +490,6 @@ func BuildBasicTags(cluster string, obj interface{}, namespaceID types.UID) []mo
log.Info("unknown obj type", "obj", obj)
}

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))})
}
}
return tags
}

Expand Down

0 comments on commit 4265f0f

Please sign in to comment.