From 79fd88a78e7a4c3059a98edd6c9c586c0942547a Mon Sep 17 00:00:00 2001 From: Ondra Machacek Date: Wed, 27 Nov 2024 12:18:30 +0100 Subject: [PATCH] Fetch Vlan IDs Signed-off-by: Ondra Machacek --- .../provider/container/vsphere/collector.go | 6 ++++-- .../provider/container/vsphere/model.go | 15 +++++++++++++++ pkg/controller/provider/model/vsphere/model.go | 2 ++ pkg/controller/provider/web/vsphere/network.go | 2 ++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/pkg/controller/provider/container/vsphere/collector.go b/pkg/controller/provider/container/vsphere/collector.go index f7df8e7e5..6a25e2ebc 100644 --- a/pkg/controller/provider/container/vsphere/collector.go +++ b/pkg/controller/provider/container/vsphere/collector.go @@ -85,8 +85,9 @@ const ( // Network fTag = "tag" // PortGroup - fDVSwitch = "config.distributedVirtualSwitch" - fKey = "key" + fDVSwitch = "config.distributedVirtualSwitch" + fDVSwitchVlan = "config.defaultPortConfig" + fKey = "key" // DV Switch fDVSwitchHost = "config.host" // Datastore @@ -676,6 +677,7 @@ func (r *Collector) propertySpec() []types.PropertySpec { PathSet: []string{ fName, fDVSwitch, + fDVSwitchVlan, fTag, fKey, }, diff --git a/pkg/controller/provider/container/vsphere/model.go b/pkg/controller/provider/container/vsphere/model.go index 1b3a7c5bf..c402e6808 100644 --- a/pkg/controller/provider/container/vsphere/model.go +++ b/pkg/controller/provider/container/vsphere/model.go @@ -1,6 +1,7 @@ package vsphere import ( + "fmt" "net/url" "sort" "strconv" @@ -306,6 +307,7 @@ func (v *HostAdapter) Apply(u types.ObjectUpdate) { Key: portGroup.Key, Name: portGroup.Spec.Name, Switch: portGroup.Vswitch, + VlanId: portGroup.Spec.VlanId, }) } } @@ -394,6 +396,19 @@ func (v *NetworkAdapter) Apply(u types.ObjectUpdate) { } case fDVSwitch: v.model.DVSwitch = v.Ref(p.Val) + case fDVSwitchVlan: + if portSettings, cast := p.Val.(types.VMwareDVSPortSetting); cast { + if vlanIdSpec, cast := portSettings.Vlan.(*types.VmwareDistributedVirtualSwitchVlanIdSpec); cast && int(vlanIdSpec.VlanId) > 0 { + v.model.VlanId = strconv.Itoa(int(vlanIdSpec.VlanId)) + } + if vlanIdSpec, cast := portSettings.Vlan.(*types.VmwareDistributedVirtualSwitchTrunkVlanSpec); cast { + refList := []string{} + for _, val := range vlanIdSpec.VlanId { + refList = append(refList, fmt.Sprintf("%d-%d", val.Start, val.End)) + } + v.model.VlanId = strings.Join(refList, ",") + } + } } } } diff --git a/pkg/controller/provider/model/vsphere/model.go b/pkg/controller/provider/model/vsphere/model.go index 46db9be92..17418263e 100644 --- a/pkg/controller/provider/model/vsphere/model.go +++ b/pkg/controller/provider/model/vsphere/model.go @@ -199,6 +199,7 @@ type PortGroup struct { Key string `json:"key"` Name string `json:"name"` Switch string `json:"vSwitch"` + VlanId int32 `json:"vlanId"` } type Switch struct { @@ -214,6 +215,7 @@ type Network struct { DVSwitch Ref `sql:""` Key string `sql:""` Host []DVSHost `sql:""` + VlanId string `sql:""` } type DVSHost struct { diff --git a/pkg/controller/provider/web/vsphere/network.go b/pkg/controller/provider/web/vsphere/network.go index 00b234d01..e5967e2d8 100644 --- a/pkg/controller/provider/web/vsphere/network.go +++ b/pkg/controller/provider/web/vsphere/network.go @@ -176,6 +176,7 @@ type Network struct { Resource Variant string `json:"variant"` DVSwitch *model.Ref `json:"dvSwitch,omitempty"` + VlanId string `json:"vlanId"` Host []model.DVSHost `json:"host"` Tag string `json:"tag,omitempty"` Key string `json:"key,omitempty"` @@ -191,6 +192,7 @@ func (r *Network) With(m *model.Network) { case model.NetDvPortGroup: r.DVSwitch = &m.DVSwitch r.Key = m.Key + r.VlanId = m.VlanId case model.NetDvSwitch: r.Host = m.Host }