Skip to content

Commit

Permalink
Merge pull request #412 from heypnus/vpc/subnetport/api_migration
Browse files Browse the repository at this point in the history
Update the struct from SegmentPort to VpcSubnetPort for the SDK API upgrade
  • Loading branch information
heypnus authored Dec 1, 2023
2 parents 9823b7c + 748fa4c commit 8cdf0a2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pkg/nsx/services/mediator/mediator.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (serviceMediator *ServiceMediator) GetAvailableSubnet(subnetSet *v1alpha1.S
return serviceMediator.CreateOrUpdateSubnet(subnetSet, tags)
}

func (serviceMediator *ServiceMediator) GetPortsOfSubnet(nsxSubnetID string) (ports []model.SegmentPort) {
func (serviceMediator *ServiceMediator) GetPortsOfSubnet(nsxSubnetID string) (ports []model.VpcSubnetPort) {
subnetPortList := serviceMediator.SubnetPortStore.GetByIndex(common.IndexKeySubnetID, nsxSubnetID)
return subnetPortList
}
Expand Down
4 changes: 2 additions & 2 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.SegmentPort, error) {
func (service *SubnetPortService) buildSubnetPort(obj interface{}, nsxSubnetPath string, contextID string, labelTags *map[string]string) (*model.VpcSubnetPort, error) {
var objName, objNamespace, uid, appId string
switch o := obj.(type) {
case *v1alpha1.SubnetPort:
Expand Down Expand Up @@ -59,7 +59,7 @@ func (service *SubnetPortService) buildSubnetPort(obj interface{}, nsxSubnetPath
tags = append(tags, model.Tag{Scope: String(k), Tag: String(v)})
}
}
nsxSubnetPort := &model.SegmentPort{
nsxSubnetPort := &model.VpcSubnetPort{
DisplayName: String(nsxSubnetPortName),
Id: String(nsxSubnetPortID),
Attachment: &model.PortAttachment{
Expand Down
8 changes: 4 additions & 4 deletions pkg/nsx/services/subnetport/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type (
SubnetPort model.SegmentPort
SubnetPort model.VpcSubnetPort
)

type Comparable = common.Comparable
Expand Down Expand Up @@ -40,10 +40,10 @@ func (sp *SubnetPort) Value() data.DataValue {
return dataValue
}

func SubnetPortToComparable(sp *model.SegmentPort) Comparable {
func SubnetPortToComparable(sp *model.VpcSubnetPort) Comparable {
return (*SubnetPort)(sp)
}

func ComparableToSubnetPort(sp Comparable) *model.SegmentPort {
return (*model.SegmentPort)(sp.(*SubnetPort))
func ComparableToSubnetPort(sp Comparable) *model.VpcSubnetPort {
return (*model.VpcSubnetPort)(sp.(*SubnetPort))
}
30 changes: 15 additions & 15 deletions pkg/nsx/services/subnetport/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// keyFunc is used to get the key of a resource, usually, which is the ID of the resource
func keyFunc(obj interface{}) (string, error) {
switch v := obj.(type) {
case model.SegmentPort:
case model.VpcSubnetPort:
return *v.Id, nil
case types.UID:
return string(v), nil
Expand All @@ -36,7 +36,7 @@ func filterTag(tags []model.Tag, tagScope string) []string {
// index is used to filter out resources which are related to the CR
func subnetPortIndexByCRUID(obj interface{}) ([]string, error) {
switch o := obj.(type) {
case model.SegmentPort:
case model.VpcSubnetPort:
return filterTag(o.Tags, common.TagScopeSubnetPortCRUID), nil
default:
return nil, errors.New("subnetPortIndexByCRUID doesn't support unknown type")
Expand All @@ -45,7 +45,7 @@ func subnetPortIndexByCRUID(obj interface{}) ([]string, error) {

func subnetPortIndexByPodUID(obj interface{}) ([]string, error) {
switch o := obj.(type) {
case model.SegmentPort:
case model.VpcSubnetPort:
return filterTag(o.Tags, common.TagScopePodUID), nil
default:
return nil, errors.New("subnetPortIndexByCRUID doesn't support unknown type")
Expand All @@ -54,7 +54,7 @@ func subnetPortIndexByPodUID(obj interface{}) ([]string, error) {

func subnetPortIndexBySubnetID(obj interface{}) ([]string, error) {
switch o := obj.(type) {
case model.SegmentPort:
case model.VpcSubnetPort:
vpcInfo, err := common.ParseVPCResourcePath(*o.Path)
if err != nil {
return nil, err
Expand All @@ -75,7 +75,7 @@ func (vs *SubnetPortStore) Apply(i interface{}) error {
if i == nil {
return nil
}
subnetPort := i.(*model.SegmentPort)
subnetPort := i.(*model.VpcSubnetPort)
if subnetPort.MarkedForDelete != nil && *subnetPort.MarkedForDelete {
err := vs.Delete(*subnetPort)
log.V(1).Info("delete SubnetPort from store", "subnetport", subnetPort)
Expand All @@ -92,34 +92,34 @@ func (vs *SubnetPortStore) Apply(i interface{}) error {
return nil
}

func (subnetPortStore *SubnetPortStore) GetByKey(key string) *model.SegmentPort {
var subnetPort model.SegmentPort
func (subnetPortStore *SubnetPortStore) GetByKey(key string) *model.VpcSubnetPort {
var subnetPort model.VpcSubnetPort
obj := subnetPortStore.ResourceStore.GetByKey(key)
if obj != nil {
subnetPort = obj.(model.SegmentPort)
subnetPort = obj.(model.VpcSubnetPort)
}
return &subnetPort
}

func (subnetPortStore *SubnetPortStore) GetByIndex(key string, value string) []model.SegmentPort {
segmentPorts := make([]model.SegmentPort, 0)
func (subnetPortStore *SubnetPortStore) GetByIndex(key string, value string) []model.VpcSubnetPort {
subnetPorts := make([]model.VpcSubnetPort, 0)
objs := subnetPortStore.ResourceStore.GetByIndex(key, value)
for _, subnetPort := range objs {
segmentPorts = append(segmentPorts, subnetPort.(model.SegmentPort))
subnetPorts = append(subnetPorts, subnetPort.(model.VpcSubnetPort))
}
return segmentPorts
return subnetPorts
}

func (vs *SubnetPortStore) GetSubnetPortsByNamespace(ns string) []model.SegmentPort {
var ret []model.SegmentPort
func (vs *SubnetPortStore) GetSubnetPortsByNamespace(ns string) []model.VpcSubnetPort {
var ret []model.VpcSubnetPort
subnetPorts := vs.List()
if len(subnetPorts) == 0 {
log.V(1).Info("No subnet port found in SubnetPort store")
return ret
}

for _, subnetPort := range subnetPorts {
msubnetport := subnetPort.(model.SegmentPort)
msubnetport := subnetPort.(model.VpcSubnetPort)
tags := msubnetport.Tags
for _, tag := range tags {
// TODO: consider to create index for common.TagScopeNamespace like common.TagScopeSubnetPortCRUID, and leverage functions like getByIndex to perform searches.
Expand Down
11 changes: 4 additions & 7 deletions pkg/nsx/services/subnetport/subnetport.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func InitializeSubnetPort(service servicecommon.Service) (*SubnetPortService, er
servicecommon.TagScopePodUID: subnetPortIndexByPodUID,
servicecommon.IndexKeySubnetID: subnetPortIndexBySubnetID,
}),
BindingType: model.SegmentPortBindingType(),
BindingType: model.VpcSubnetPortBindingType(),
}}

go subnetPortService.InitializeResourceStore(&wg, fatalErrors, ResourceTypeSubnetPort, nil, subnetPortService.SubnetPortStore)
Expand Down Expand Up @@ -82,9 +82,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 := &model.VpcSubnetPort{}
var err error
nsxSubnetPort, err := service.buildSubnetPort(obj, nsxSubnetPath, contextID, tags)
if err != nil {
log.Error(err, "failed to build NSX subnet port", "nsxSubnetPort.Id", uid, "nsxSubnetPath", nsxSubnetPath, "contextID", contextID)
return nil, err
Expand All @@ -95,8 +93,7 @@ func (service *SubnetPortService) CreateOrUpdateSubnetPort(obj interface{}, nsxS
return nil, err
}
existingSubnetPort := service.SubnetPortStore.GetByKey(*nsxSubnetPort.Id)
//isChanged := servicecommon.CompareResource(SubnetPortToComparable(existingSubnetPort), SubnetPortToComparable(nsxSubnetPort))
isChanged := false
isChanged := servicecommon.CompareResource(SubnetPortToComparable(existingSubnetPort), SubnetPortToComparable(nsxSubnetPort))
if !isChanged {
log.Info("NSX subnet port not changed, skipping the update", "nsxSubnetPort.Id", nsxSubnetPort.Id, "nsxSubnetPath", nsxSubnetPath)
// We don't need to update it but still need to check realized state.
Expand Down Expand Up @@ -261,7 +258,7 @@ func (service *SubnetPortService) Cleanup() error {
subnetPorts := service.SubnetPortStore.List()
log.Info("cleanup subnetports", "count", len(subnetPorts))
for _, subnetPort := range subnetPorts {
subnetPortID := types.UID(*subnetPort.(model.SegmentPort).Id)
subnetPortID := types.UID(*subnetPort.(model.VpcSubnetPort).Id)
err := service.DeleteSubnetPort(subnetPortID)
if err != nil {
log.Error(err, "cleanup subnetport failed", "subnetPortID", subnetPortID)
Expand Down

0 comments on commit 8cdf0a2

Please sign in to comment.