diff --git a/server/controller/common/resource_type.go b/server/controller/common/resource_type.go index 02aca1b201a..2102311e23a 100644 --- a/server/controller/common/resource_type.go +++ b/server/controller/common/resource_type.go @@ -70,6 +70,7 @@ const ( RESOURCE_TYPE_IP_EN = "ip" RESOURCE_TYPE_ALL_IP_EN = "all_ip" RESOURCE_TYPE_LB_RULE_EN = "lb_rule" + RESOURCE_TYPE_AGENT_EN = "agent" RESOURCE_TYPE_CUSTOM_SERVICE_EN = "biz_service" diff --git a/server/controller/recorder/cache/diffbase/az.go b/server/controller/recorder/cache/diffbase/az.go index 6477c21c653..a6129fd4354 100644 --- a/server/controller/recorder/cache/diffbase/az.go +++ b/server/controller/recorder/cache/diffbase/az.go @@ -17,39 +17,35 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddAZ(dbItem *metadbmodel.AZ, seq int) { - b.AZs[dbItem.Lcuuid] = &AZ{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - Label: dbItem.Label, - RegionLcuuid: dbItem.Region, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_AZ_EN, b.AZs[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type AZ struct { + ResourceBase + Name string + Label string + RegionLcuuid string } -func (b *DataSet) DeleteAZ(lcuuid string) { - delete(b.AZs, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_AZ_EN, lcuuid), b.metadata.LogPrefixes) +func (a *AZ) reset(dbItem *metadbmodel.AZ, tool *tool.Tool) { + a.Name = dbItem.Name + a.Label = dbItem.Label + a.RegionLcuuid = dbItem.Region } -type AZ struct { - DiffBase - Name string `json:"name"` - Label string `json:"label"` - RegionLcuuid string `json:"region_lcuuid"` +func NewAZCollection(t *tool.Tool) *AZCollection { + c := new(AZCollection) + c.collection = newCollectionBuilder[*AZ](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_AZ_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.AZ { return new(metadbmodel.AZ) }). + withCacheItemFactory(func() *AZ { return new(AZ) }). + build() + return c } -func (a *AZ) Update(cloudItem *cloudmodel.AZ) { - a.Name = cloudItem.Name - a.Label = cloudItem.Label - a.RegionLcuuid = cloudItem.RegionLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_AZ_EN, a)) +type AZCollection struct { + collection[*AZ, *metadbmodel.AZ] } diff --git a/server/controller/recorder/cache/diffbase/cen.go b/server/controller/recorder/cache/diffbase/cen.go index a31bec6faae..817dab6dd25 100644 --- a/server/controller/recorder/cache/diffbase/cen.go +++ b/server/controller/recorder/cache/diffbase/cen.go @@ -24,16 +24,16 @@ import ( rcommon "github.com/deepflowio/deepflow/server/controller/recorder/common" ) -func (b *DataSet) AddCEN(dbItem *metadbmodel.CEN, seq int, toolDataSet *tool.DataSet) { +func (b *DataSet) AddCEN(dbItem *metadbmodel.CEN, seq int, tool *tool.Tool) { vpcLcuuids := []string{} for _, vpcID := range rcommon.StringToIntSlice(dbItem.VPCIDs) { - vpcLcuuid, exists := toolDataSet.GetVPCLcuuidByID(vpcID) - if exists { + vpcLcuuid := tool.VPC().GetByID(vpcID).Lcuuid() + if vpcLcuuid != "" { vpcLcuuids = append(vpcLcuuids, vpcLcuuid) } } b.CENs[dbItem.Lcuuid] = &CEN{ - DiffBase: DiffBase{ + ResourceBase: ResourceBase{ Sequence: seq, Lcuuid: dbItem.Lcuuid, }, @@ -48,14 +48,38 @@ func (b *DataSet) DeleteCEN(lcuuid string) { log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_CEN_EN, lcuuid), b.metadata.LogPrefixes) } +func (c *CEN) Update(cloudItem *cloudmodel.CEN) { + c.Name = cloudItem.Name + c.VPCLcuuids = cloudItem.VPCLcuuids + log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_CEN_EN, c)) +} + type CEN struct { - DiffBase + ResourceBase Name string `json:"name"` VPCLcuuids []string `json:"vpc_lcuuids"` } -func (c *CEN) Update(cloudItem *cloudmodel.CEN) { - c.Name = cloudItem.Name - c.VPCLcuuids = cloudItem.VPCLcuuids - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_CEN_EN, c)) +func (c *CEN) reset(dbItem *metadbmodel.CEN, tool *tool.Tool) { + vpcLcuuids := []string{} + for _, vpcID := range rcommon.StringToIntSlice(dbItem.VPCIDs) { + vpcLcuuid := tool.VPC().GetByID(vpcID).Lcuuid() + if vpcLcuuid != "" { + vpcLcuuids = append(vpcLcuuids, vpcLcuuid) + } + } + c.Name = dbItem.Name + c.VPCLcuuids = vpcLcuuids +} + +type CENCollection struct { + collectionComponent[*CEN, CEN, *metadbmodel.CEN, metadbmodel.CEN] +} + +func NewCENCollection(t *tool.Tool) *CENCollection { + c := new(CENCollection) + c.withResourceType(ctrlrcommon.RESOURCE_TYPE_CEN_EN). + withTool(t). + init() + return c } diff --git a/server/controller/recorder/cache/diffbase/collection.go b/server/controller/recorder/cache/diffbase/collection.go new file mode 100644 index 00000000000..829c055fe9f --- /dev/null +++ b/server/controller/recorder/cache/diffbase/collection.go @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package diffbase + +import ( + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" +) + +type ResourceBase struct { + Sequence int `json:"sequence"` + Lcuuid string `json:"lcuuid"` +} + +func (d ResourceBase) GetSequence() int { + return d.Sequence +} + +func (d ResourceBase) GetLcuuid() string { + return d.Lcuuid +} + +func (d *ResourceBase) SetSequence(sequence int) { + d.Sequence = sequence +} + +func (d *ResourceBase) init(sequence int, lcuuid string) { + d.Sequence = sequence + d.Lcuuid = lcuuid +} + +// CacheItem defines cache object must implement the interface +type CacheItem[D DBItem] interface { + GetLcuuid() string + GetSequence() int + init(sequence int, lcuuid string) + reset(dbItem D, tool *tool.Tool) // reset the item with dbItem +} + +// DBItem defines database object must implement the interface +type DBItem interface { + GetID() int + GetLcuuid() string +} + +// collectionBuilder use builder pattern to build collection +type collectionBuilder[T CacheItem[D], D DBItem] struct { + collection[T, D] +} + +func newCollectionBuilder[T CacheItem[D], D DBItem]() *collectionBuilder[T, D] { + return &collectionBuilder[T, D]{} +} + +func (b *collectionBuilder[T, D]) withResourceType(resourceType string) *collectionBuilder[T, D] { + b.resourceType = resourceType + return b +} + +func (b *collectionBuilder[T, D]) withTool(tool *tool.Tool) *collectionBuilder[T, D] { + b.tool = tool + return b +} + +func (b *collectionBuilder[T, D]) withDBItemFactory(factory func() D) *collectionBuilder[T, D] { + b.dbItemFactory = factory + return b +} + +func (b *collectionBuilder[T, D]) withCacheItemFactory(factory func() T) *collectionBuilder[T, D] { + b.cacheItemFactory = factory + return b +} + +func (b *collectionBuilder[T, D]) build() collection[T, D] { + return collection[T, D]{ + resourceType: b.resourceType, + tool: b.tool, + dbItemFactory: b.dbItemFactory, + cacheItemFactory: b.cacheItemFactory, + + lcuuidToItem: make(map[string]T), + } +} + +type collection[T CacheItem[D], D DBItem] struct { + resourceType string + tool *tool.Tool + + dbItemFactory func() D + cacheItemFactory func() T + + lcuuidToItem map[string]T +} + +func (c *collection[T, D]) GetByLcuuid(lcuuid string) T { + empty := c.cacheItemFactory() + if lcuuid == "" { + return empty + } + if item, ok := c.lcuuidToItem[lcuuid]; ok { + return item + } + return empty +} + +func (c *collection[T, D]) GetAll() map[string]T { + return c.lcuuidToItem +} + +func (c *collection[T, D]) Add(dbItem D, seq int) { + item := c.cacheItemFactory() + item.init(seq, dbItem.GetLcuuid()) + item.reset(dbItem, c.tool) + c.lcuuidToItem[dbItem.GetLcuuid()] = item + c.tool.GetLogFunc()(addDiffBase(c.resourceType, dbItem.GetLcuuid()), c.tool.Metadata().LogPrefixes) +} + +func (c *collection[T, D]) Update(dbItem D, seq int) { + if existingItem, ok := c.lcuuidToItem[dbItem.GetLcuuid()]; ok { + existingItem.reset(dbItem, c.tool) + c.tool.GetLogFunc()(updateDiffBase(c.resourceType, dbItem.GetLcuuid()), c.tool.Metadata().LogPrefixes) + return + } + // if cache item not exists, add it + log.Errorf("%s cache item not found (lcuuid: %s), add it", c.resourceType, dbItem.GetLcuuid(), c.tool.Metadata().LogPrefixes) + c.Add(dbItem, seq) +} + +func (c *collection[T, D]) Delete(dbItem D) { + delete(c.lcuuidToItem, dbItem.GetLcuuid()) + c.tool.GetLogFunc()(deleteDiffBase(c.resourceType, dbItem.GetLcuuid()), c.tool.Metadata().LogPrefixes) +} diff --git a/server/controller/recorder/cache/diffbase/config_map.go b/server/controller/recorder/cache/diffbase/config_map.go index 9d188a13f98..78d52e1254a 100644 --- a/server/controller/recorder/cache/diffbase/config_map.go +++ b/server/controller/recorder/cache/diffbase/config_map.go @@ -17,54 +17,42 @@ package diffbase import ( - "sigs.k8s.io/yaml" - - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddConfigMap(dbItem *metadbmodel.ConfigMap, seq int) { - b.ConfigMaps[dbItem.Lcuuid] = &ConfigMap{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - Data: string(dbItem.Data), - DataHash: dbItem.DataHash, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_CONFIG_MAP_EN, b.ConfigMaps[dbItem.Lcuuid].ToLoggable()), b.metadata.LogPrefixes) +type ConfigMap struct { + ResourceBase + Name string + Data string + DataHash string } -func (b *DataSet) DeleteConfigMap(lcuuid string) { - delete(b.ConfigMaps, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_CONFIG_MAP_EN, lcuuid), b.metadata.LogPrefixes) +func (a *ConfigMap) reset(dbItem *metadbmodel.ConfigMap, tool *tool.Tool) { + a.Name = dbItem.Name + a.Data = string(dbItem.Data) + a.DataHash = dbItem.DataHash } -type ConfigMap struct { - DiffBase - Name string `json:"name"` - Data string `json:"data"` - DataHash string `json:"data_hash"` -} - -// ToLoggable converts the ConfigMap to a loggable format, excluding the Data field. -func (v ConfigMap) ToLoggable() interface{} { - copied := v +// ToLoggable converts ConfigMap to a loggable format, excluding sensitive fields +func (a ConfigMap) ToLoggable() interface{} { + copied := a copied.Data = "**HIDDEN**" return copied } -func (v *ConfigMap) Update(cloudItem *cloudmodel.ConfigMap, toolDataSet *tool.DataSet) { - v.Name = cloudItem.Name - yamlData, err := yaml.JSONToYAML([]byte(cloudItem.Data)) - if err != nil { - log.Errorf("failed to convert JSON data: %v to YAML: %s", cloudItem.Data, toolDataSet.GetMetadata().LogPrefixes) - return - } - v.Data = string(yamlData) - v.DataHash = cloudItem.DataHash - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_CONFIG_MAP_EN, v.ToLoggable())) +func NewConfigMapCollection(t *tool.Tool) *ConfigMapCollection { + c := new(ConfigMapCollection) + c.collection = newCollectionBuilder[*ConfigMap](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_CONFIG_MAP_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.ConfigMap { return new(metadbmodel.ConfigMap) }). + withCacheItemFactory(func() *ConfigMap { return new(ConfigMap) }). + build() + return c +} + +type ConfigMapCollection struct { + collection[*ConfigMap, *metadbmodel.ConfigMap] } diff --git a/server/controller/recorder/cache/diffbase/constraint.go b/server/controller/recorder/cache/diffbase/constraint.go new file mode 100644 index 00000000000..b4b81dc0812 --- /dev/null +++ b/server/controller/recorder/cache/diffbase/constraint.go @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package diffbase + +import ( + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" +) + +type CollectionConstriant interface { + *RegionCollection | *AZCollection | *SubDomainCollection | *HostCollection | *VMCollection | + *VPCCollection | *NetworkCollection | *SubnetCollection | *VRouterCollection | *RoutingTableCollection | + *DHCPPortCollection | *VInterfaceCollection | *WANIPCollection | *LANIPCollection | *FloatingIPCollection | + *NATGatewayCollection | *NATRuleCollection | *NATVMConnectionCollection | *LBCollection | + *LBListenerCollection | *LBTargetServerCollection | *LBVMConnectionCollection | *CENCollection | + *PeerConnectionCollection | *RDSInstanceCollection | *RedisInstanceCollection | *PodClusterCollection | + *PodNodeCollection | *VMPodNodeConnectionCollection | *PodNamespaceCollection | *PodIngressCollection | + *PodIngressRuleCollection | *PodIngressRuleBackendCollection | *PodServiceCollection | + *PodServicePortCollection | *PodGroupCollection | *ConfigMapCollection | *PodGroupConfigMapConnectionCollection | + *PodGroupPortCollection | *PodReplicaSetCollection | *PodCollection | *ProcessCollection | *VIPCollection + + GetResourceType() string + + Add(seq int, dbData interface{}) + Update(dbData interface{}) + Delete(lcuuid string) +} + +type DiffBaseConstraintPointer[ + DT DiffBaseConstraint, + MPT metadbmodel.AssetResourceConstraintPtr[MT], + MT metadbmodel.AssetResourceConstraint, +] interface { + *DT + + GetSequence() int + ResetSequence(seq int) + GetLcuuid() string + + init(sql int, lcuuid string) + reset(dbItem MPT, tool *tool.Tool) +} + +type DiffBaseConstraint interface { + Region | AZ | SubDomain | Host | VM | + VPC | Network | Subnet | VRouter | RoutingTable | + DHCPPort | VInterface | WANIP | LANIP | FloatingIP | + NATGateway | NATRule | NATVMConnection | LB | + LBListener | LBTargetServer | LBVMConnection | CEN | + PeerConnection | RDSInstance | RedisInstance | PodCluster | + PodNode | VMPodNodeConnection | PodNamespace | PodIngress | + PodIngressRule | PodIngressRuleBackend | PodService | + PodServicePort | PodGroup | ConfigMap | PodGroupConfigMapConnection | + PodGroupPort | PodReplicaSet | Pod | Process | VIP + + GetSequence() int + GetLcuuid() string +} diff --git a/server/controller/recorder/cache/diffbase/dhcp_port.go b/server/controller/recorder/cache/diffbase/dhcp_port.go index 00ed699ccc4..83ace7cea49 100644 --- a/server/controller/recorder/cache/diffbase/dhcp_port.go +++ b/server/controller/recorder/cache/diffbase/dhcp_port.go @@ -17,44 +17,37 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddDHCPPort(dbItem *metadbmodel.DHCPPort, seq int, toolDataSet *tool.DataSet) { - vpcLcuuid, _ := toolDataSet.GetVPCLcuuidByID(dbItem.VPCID) - b.DHCPPorts[dbItem.Lcuuid] = &DHCPPort{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - RegionLcuuid: dbItem.Region, - AZLcuuid: dbItem.AZ, - VPCLcuuid: vpcLcuuid, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_DHCP_PORT_EN, b.DHCPPorts[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type DHCPPort struct { + ResourceBase + Name string + RegionLcuuid string + AZLcuuid string + VPCLcuuid string } -func (b *DataSet) DeleteDHCPPort(lcuuid string) { - delete(b.DHCPPorts, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_DHCP_PORT_EN, lcuuid), b.metadata.LogPrefixes) +func (a *DHCPPort) reset(dbItem *metadbmodel.DHCPPort, tool *tool.Tool) { + a.Name = dbItem.Name + a.RegionLcuuid = dbItem.Region + a.AZLcuuid = dbItem.AZ + a.VPCLcuuid = tool.VPC().GetByID(dbItem.VPCID).Lcuuid() } -type DHCPPort struct { - DiffBase - Name string `json:"name"` - RegionLcuuid string `json:"region_lcuuid"` - AZLcuuid string `json:"az_lcuuid"` - VPCLcuuid string `json:"vpc_lcuuid"` +func NewDHCPPortCollection(t *tool.Tool) *DHCPPortCollection { + c := new(DHCPPortCollection) + c.collection = newCollectionBuilder[*DHCPPort](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_DHCP_PORT_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.DHCPPort { return new(metadbmodel.DHCPPort) }). + withCacheItemFactory(func() *DHCPPort { return new(DHCPPort) }). + build() + return c } -func (d *DHCPPort) Update(cloudItem *cloudmodel.DHCPPort) { - d.Name = cloudItem.Name - d.RegionLcuuid = cloudItem.RegionLcuuid - d.AZLcuuid = cloudItem.AZLcuuid - d.VPCLcuuid = cloudItem.VPCLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_DHCP_PORT_EN, d)) +type DHCPPortCollection struct { + collection[*DHCPPort, *metadbmodel.DHCPPort] } diff --git a/server/controller/recorder/cache/diffbase/diff_base.go b/server/controller/recorder/cache/diffbase/diff_base.go index 0ae261fafe2..82c84f66ae4 100644 --- a/server/controller/recorder/cache/diffbase/diff_base.go +++ b/server/controller/recorder/cache/diffbase/diff_base.go @@ -121,20 +121,3 @@ func NewDataSet(md *rcommon.Metadata) *DataSet { VIP: make(map[string]*VIP), } } - -type DiffBase struct { - Sequence int `json:"sequence"` - Lcuuid string `json:"lcuuid"` -} - -func (d *DiffBase) GetSequence() int { - return d.Sequence -} - -func (d *DiffBase) SetSequence(sequence int) { - d.Sequence = sequence -} - -func (d *DiffBase) GetLcuuid() string { - return d.Lcuuid -} diff --git a/server/controller/recorder/cache/diffbase/diffbase.go b/server/controller/recorder/cache/diffbase/diffbase.go new file mode 100644 index 00000000000..6a4c4282fea --- /dev/null +++ b/server/controller/recorder/cache/diffbase/diffbase.go @@ -0,0 +1,214 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package diffbase + +import ( + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" + common "github.com/deepflowio/deepflow/server/controller/recorder/common" +) + +// FIXME +// AI key words to add a new struct: analyze this file, add new field podReplicaSet below podGroupPort in struct, +// and add methods or initialization for this field. + +func NewDiffBases(t *tool.Tool) *DiffBases { + d := &DiffBases{} + + // Clouds + d.subDomain = NewSubDomainCollection(t) + d.region = NewRegionCollection(t) + d.az = NewAZCollection(t) + + // Computes + d.host = NewHostCollection(t) + d.vm = NewVMCollection(t) + + // Networks + d.vpc = NewVPCCollection(t) + d.network = NewNetworkCollection(t) + d.subnet = NewSubnetCollection(t) + d.vRouter = NewVRouterCollection(t) + d.routingTable = NewRoutingTableCollection(t) + d.dhcpPort = NewDHCPPortCollection(t) + d.vInterface = NewVInterfaceCollection(t) + d.lanIP = NewLANIPCollection(t) + d.wanIP = NewWANIPCollection(t) + d.floatingIP = NewFloatingIPCollection(t) + d.vip = NewVIPCollection(t) + + // Network services + d.natGateway = NewNATGatewayCollection(t) + d.natRule = NewNATRuleCollection(t) + d.natVMConnection = NewNATVMConnectionCollection(t) + d.lb = NewLBCollection(t) + d.lbListener = NewLBListenerCollection(t) + d.lbTargetServer = NewLBTargetServerCollection(t) + d.lbVMConnection = NewLBVMConnectionCollection(t) + d.cen = NewCENCollection(t) + d.peerConnection = NewPeerConnectionCollection(t) + + // Storage services + d.rdsInstance = NewRDSInstanceCollection(t) + d.redisInstance = NewRedisInstanceCollection(t) + + // Kubernetes + d.podCluster = NewPodClusterCollection(t) + d.podNode = NewPodNodeCollection(t) + d.podNamespace = NewPodNamespaceCollection(t) + d.podIngress = NewPodIngressCollection(t) + d.podIngressRule = NewPodIngressRuleCollection(t) + d.podService = NewPodServiceCollection(t) + d.podGroup = NewPodGroupCollection(t) + d.pod = NewPodCollection(t) + d.podGroupPort = NewPodGroupPortCollection(t) + d.podReplicaSet = NewPodReplicaSetCollection(t) + d.podServicePort = NewPodServicePortCollection(t) + d.podIngressRuleBackend = NewPodIngressRuleBackendCollection(t) + d.vmPodNodeConnection = NewVMPodNodeConnectionCollection(t) + d.configMap = NewConfigMapCollection(t) + d.podGroupConfigMapConnection = NewPodGroupConfigMapConnectionCollection(t) + + // Processes + d.process = NewProcessCollection(t) + + return d +} + +type DiffBases struct { + metadata *common.Metadata + LogController + + // Clouds + subDomain *SubDomainCollection + region *RegionCollection + az *AZCollection + + // Computes + host *HostCollection + vm *VMCollection + + // Networks + vpc *VPCCollection + network *NetworkCollection + subnet *SubnetCollection + vRouter *VRouterCollection + routingTable *RoutingTableCollection + dhcpPort *DHCPPortCollection + vInterface *VInterfaceCollection + lanIP *LANIPCollection + wanIP *WANIPCollection + floatingIP *FloatingIPCollection + vip *VIPCollection + + // Network services + natGateway *NATGatewayCollection + natRule *NATRuleCollection + natVMConnection *NATVMConnectionCollection + lb *LBCollection + lbListener *LBListenerCollection + lbTargetServer *LBTargetServerCollection + lbVMConnection *LBVMConnectionCollection + cen *CENCollection + peerConnection *PeerConnectionCollection + + // Storage services + rdsInstance *RDSInstanceCollection + redisInstance *RedisInstanceCollection + + // Kubernetes + podCluster *PodClusterCollection + podNode *PodNodeCollection + vmPodNodeConnection *VMPodNodeConnectionCollection + podNamespace *PodNamespaceCollection + podIngress *PodIngressCollection + podIngressRule *PodIngressRuleCollection + podIngressRuleBackend *PodIngressRuleBackendCollection + podService *PodServiceCollection + podServicePort *PodServicePortCollection + podGroup *PodGroupCollection + podGroupPort *PodGroupPortCollection + podReplicaSet *PodReplicaSetCollection + pod *PodCollection + configMap *ConfigMapCollection + podGroupConfigMapConnection *PodGroupConfigMapConnectionCollection + + // Processes + process *ProcessCollection +} + +// Clouds +func (d DiffBases) SubDomain() *SubDomainCollection { return d.subDomain } +func (d DiffBases) Region() *RegionCollection { return d.region } +func (d DiffBases) AZ() *AZCollection { return d.az } + +// Computes +func (d DiffBases) Host() *HostCollection { return d.host } +func (d DiffBases) VM() *VMCollection { return d.vm } + +// Networks +func (d DiffBases) VPC() *VPCCollection { return d.vpc } +func (d DiffBases) Network() *NetworkCollection { return d.network } +func (d DiffBases) Subnet() *SubnetCollection { return d.subnet } +func (d DiffBases) VRouter() *VRouterCollection { return d.vRouter } +func (d DiffBases) RoutingTable() *RoutingTableCollection { return d.routingTable } +func (d DiffBases) DHCPPort() *DHCPPortCollection { return d.dhcpPort } +func (d DiffBases) VInterface() *VInterfaceCollection { return d.vInterface } +func (d DiffBases) LANIP() *LANIPCollection { return d.lanIP } +func (d DiffBases) WANIP() *WANIPCollection { return d.wanIP } +func (d DiffBases) FloatingIP() *FloatingIPCollection { return d.floatingIP } +func (d DiffBases) VIP() *VIPCollection { return d.vip } + +// Network services +func (d DiffBases) NATGateway() *NATGatewayCollection { return d.natGateway } +func (d DiffBases) NATRule() *NATRuleCollection { return d.natRule } +func (d DiffBases) NATVMConnection() *NATVMConnectionCollection { return d.natVMConnection } +func (d DiffBases) LB() *LBCollection { return d.lb } +func (d DiffBases) LBListener() *LBListenerCollection { return d.lbListener } +func (d DiffBases) LBTargetServer() *LBTargetServerCollection { return d.lbTargetServer } +func (d DiffBases) LBVMConnection() *LBVMConnectionCollection { return d.lbVMConnection } +func (d DiffBases) CEN() *CENCollection { return d.cen } +func (d DiffBases) PeerConnection() *PeerConnectionCollection { return d.peerConnection } + +// Storage services +func (d DiffBases) RDSInstance() *RDSInstanceCollection { return d.rdsInstance } +func (d DiffBases) RedisInstance() *RedisInstanceCollection { return d.redisInstance } + +// Kubernetes +func (d DiffBases) PodCluster() *PodClusterCollection { return d.podCluster } +func (d DiffBases) PodNode() *PodNodeCollection { return d.podNode } +func (d DiffBases) PodNamespace() *PodNamespaceCollection { return d.podNamespace } +func (d DiffBases) PodIngress() *PodIngressCollection { return d.podIngress } +func (d DiffBases) PodIngressRule() *PodIngressRuleCollection { return d.podIngressRule } +func (d DiffBases) PodService() *PodServiceCollection { return d.podService } +func (d DiffBases) PodGroup() *PodGroupCollection { return d.podGroup } +func (d DiffBases) Pod() *PodCollection { return d.pod } +func (d DiffBases) PodGroupPort() *PodGroupPortCollection { return d.podGroupPort } +func (d DiffBases) PodReplicaSet() *PodReplicaSetCollection { return d.podReplicaSet } +func (d DiffBases) PodServicePort() *PodServicePortCollection { return d.podServicePort } +func (d DiffBases) ConfigMap() *ConfigMapCollection { return d.configMap } +func (d DiffBases) PodIngressRuleBackend() *PodIngressRuleBackendCollection { + return d.podIngressRuleBackend +} +func (d DiffBases) VMPodNodeConnection() *VMPodNodeConnectionCollection { + return d.vmPodNodeConnection +} +func (d DiffBases) PodGroupConfigMapConnection() *PodGroupConfigMapConnectionCollection { + return d.podGroupConfigMapConnection +} + +// Processes +func (d DiffBases) Process() *ProcessCollection { return d.process } diff --git a/server/controller/recorder/cache/diffbase/floating_ip.go b/server/controller/recorder/cache/diffbase/floating_ip.go index c0e15ac2c2c..914459ec059 100644 --- a/server/controller/recorder/cache/diffbase/floating_ip.go +++ b/server/controller/recorder/cache/diffbase/floating_ip.go @@ -17,38 +17,33 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddFloatingIP(dbItem *metadbmodel.FloatingIP, seq int, toolDataSet *tool.DataSet) { - vpcLcuuid, _ := toolDataSet.GetVPCLcuuidByID(dbItem.VPCID) - b.FloatingIPs[dbItem.Lcuuid] = &FloatingIP{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - RegionLcuuid: dbItem.Region, - VPCLcuuid: vpcLcuuid, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_FLOATING_IP_EN, b.FloatingIPs[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type FloatingIP struct { + ResourceBase + RegionLcuuid string + VPCLcuuid string } -func (b *DataSet) DeleteFloatingIP(lcuuid string) { - delete(b.FloatingIPs, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_FLOATING_IP_EN, lcuuid), b.metadata.LogPrefixes) +func (a *FloatingIP) reset(dbItem *metadbmodel.FloatingIP, tool *tool.Tool) { + a.RegionLcuuid = dbItem.Region + a.VPCLcuuid = tool.VPC().GetByID(dbItem.VPCID).Lcuuid() } -type FloatingIP struct { - DiffBase - RegionLcuuid string `json:"region_lcuuid"` - VPCLcuuid string `json:"vpc_lcuuid"` +func NewFloatingIPCollection(t *tool.Tool) *FloatingIPCollection { + c := new(FloatingIPCollection) + c.collection = newCollectionBuilder[*FloatingIP](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_FLOATING_IP_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.FloatingIP { return new(metadbmodel.FloatingIP) }). + withCacheItemFactory(func() *FloatingIP { return new(FloatingIP) }). + build() + return c } -func (f *FloatingIP) Update(cloudItem *cloudmodel.FloatingIP) { - f.RegionLcuuid = cloudItem.RegionLcuuid - f.VPCLcuuid = cloudItem.VPCLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_FLOATING_IP_EN, f)) +type FloatingIPCollection struct { + collection[*FloatingIP, *metadbmodel.FloatingIP] } diff --git a/server/controller/recorder/cache/diffbase/host.go b/server/controller/recorder/cache/diffbase/host.go index ea1b4ccd1e0..6e1658e22b9 100644 --- a/server/controller/recorder/cache/diffbase/host.go +++ b/server/controller/recorder/cache/diffbase/host.go @@ -17,57 +17,47 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddHost(dbItem *metadbmodel.Host, seq int) { - b.Hosts[dbItem.Lcuuid] = &Host{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - RegionLcuuid: dbItem.Region, - AZLcuuid: dbItem.AZ, - IP: dbItem.IP, - Hostname: dbItem.Hostname, - HType: dbItem.HType, - VCPUNum: dbItem.VCPUNum, - MemTotal: dbItem.MemTotal, - ExtraInfo: dbItem.ExtraInfo, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_HOST_EN, b.Hosts[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type Host struct { + ResourceBase + Name string + IP string + Hostname string + HType int + VCPUNum int + MemTotal int + ExtraInfo string + RegionLcuuid string + AZLcuuid string } -func (b *DataSet) DeleteHost(lcuuid string) { - delete(b.Hosts, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_HOST_EN, lcuuid), b.metadata.LogPrefixes) +func (a *Host) reset(dbItem *metadbmodel.Host, tool *tool.Tool) { + a.Name = dbItem.Name + a.IP = dbItem.IP + a.Hostname = dbItem.Hostname + a.HType = dbItem.HType + a.VCPUNum = dbItem.VCPUNum + a.MemTotal = dbItem.MemTotal + a.ExtraInfo = dbItem.ExtraInfo + a.RegionLcuuid = dbItem.Region + a.AZLcuuid = dbItem.AZ } -type Host struct { - DiffBase - Name string `json:"name"` - IP string `json:"ip"` - Hostname string `json:"hostname"` - HType int `json:"htype"` - VCPUNum int `json:"vcpu_num"` - MemTotal int `json:"mem_total"` - ExtraInfo string `json:"extra_info"` - RegionLcuuid string `json:"region_lcuuid"` - AZLcuuid string `json:"az_lcuuid"` +func NewHostCollection(t *tool.Tool) *HostCollection { + c := new(HostCollection) + c.collection = newCollectionBuilder[*Host](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_HOST_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.Host { return new(metadbmodel.Host) }). + withCacheItemFactory(func() *Host { return new(Host) }). + build() + return c } -func (h *Host) Update(cloudItem *cloudmodel.Host) { - h.Name = cloudItem.Name - h.IP = cloudItem.IP - h.Hostname = cloudItem.Hostname - h.HType = cloudItem.HType - h.VCPUNum = cloudItem.VCPUNum - h.MemTotal = cloudItem.MemTotal - h.ExtraInfo = cloudItem.ExtraInfo - h.RegionLcuuid = cloudItem.RegionLcuuid - h.AZLcuuid = cloudItem.AZLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_HOST_EN, h)) +type HostCollection struct { + collection[*Host, *metadbmodel.Host] } diff --git a/server/controller/recorder/cache/diffbase/lan_ip.go b/server/controller/recorder/cache/diffbase/lan_ip.go index 901ec7ece4e..6158033e50f 100644 --- a/server/controller/recorder/cache/diffbase/lan_ip.go +++ b/server/controller/recorder/cache/diffbase/lan_ip.go @@ -17,39 +17,31 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddLANIP(dbItem *metadbmodel.LANIP, seq int, toolDataSet *tool.DataSet) { - // ip subnet id is not used in the current version, so it is commented out to avoid updating the subnet id too frequently, - // which may cause recorder performance issues. - // subnetLcuuid, _ := toolDataSet.GetSubnetLcuuidByID(dbItem.SubnetID) - b.LANIPs[dbItem.Lcuuid] = &LANIP{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - SubDomainLcuuid: dbItem.SubDomain, - // SubnetLcuuid: subnetLcuuid, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_LAN_IP_EN, b.LANIPs[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type LANIP struct { + ResourceBase + SubDomainLcuuid string } -func (b *DataSet) DeleteLANIP(lcuuid string) { - delete(b.LANIPs, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_LAN_IP_EN, lcuuid), b.metadata.LogPrefixes) +func (a *LANIP) reset(dbItem *metadbmodel.LANIP, tool *tool.Tool) { + a.SubDomainLcuuid = dbItem.SubDomain } -type LANIP struct { - DiffBase - SubDomainLcuuid string `json:"sub_domain_lcuuid"` - SubnetLcuuid string `json:"subnet_lcuuid"` +func NewLANIPCollection(t *tool.Tool) *LANIPCollection { + c := new(LANIPCollection) + c.collection = newCollectionBuilder[*LANIP](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_LAN_IP_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.LANIP { return new(metadbmodel.LANIP) }). + withCacheItemFactory(func() *LANIP { return new(LANIP) }). + build() + return c } -func (l *LANIP) Update(cloudItem *cloudmodel.IP) { - l.SubnetLcuuid = cloudItem.SubnetLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_LAN_IP_EN, l)) +type LANIPCollection struct { + collection[*LANIP, *metadbmodel.LANIP] } diff --git a/server/controller/recorder/cache/diffbase/lb.go b/server/controller/recorder/cache/diffbase/lb.go index 10a5e8e0615..9522ff1df22 100644 --- a/server/controller/recorder/cache/diffbase/lb.go +++ b/server/controller/recorder/cache/diffbase/lb.go @@ -17,44 +17,39 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddLB(dbItem *metadbmodel.LB, seq int) { - b.LBs[dbItem.Lcuuid] = &LB{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - Model: dbItem.Model, - VIP: dbItem.VIP, - RegionLcuuid: dbItem.Region, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_LB_EN, b.LBs[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type LB struct { + ResourceBase + Name string + Model int + VIP string + VPCLcuuid string + RegionLcuuid string } -func (b *DataSet) DeleteLB(lcuuid string) { - delete(b.LBs, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_LB_EN, lcuuid), b.metadata.LogPrefixes) +func (a *LB) reset(dbItem *metadbmodel.LB, tool *tool.Tool) { + a.Name = dbItem.Name + a.Model = dbItem.Model + a.VIP = dbItem.VIP + a.VPCLcuuid = tool.VPC().GetByID(dbItem.VPCID).Lcuuid() + a.RegionLcuuid = dbItem.Region } -type LB struct { - DiffBase - Name string `json:"name"` - Model int `json:"model"` - VIP string `json:"vip"` - VPCLcuuid string `json:"vpc_lcuuid"` - RegionLcuuid string `json:"region_lcuuid"` +func NewLBCollection(t *tool.Tool) *LBCollection { + c := new(LBCollection) + c.collection = newCollectionBuilder[*LB](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_LB_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.LB { return new(metadbmodel.LB) }). + withCacheItemFactory(func() *LB { return new(LB) }). + build() + return c } -func (l *LB) Update(cloudItem *cloudmodel.LB) { - l.Name = cloudItem.Name - l.Model = cloudItem.Model - l.VIP = cloudItem.VIP - l.VPCLcuuid = cloudItem.VPCLcuuid - l.RegionLcuuid = cloudItem.RegionLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_LB_EN, l)) +type LBCollection struct { + collection[*LB, *metadbmodel.LB] } diff --git a/server/controller/recorder/cache/diffbase/lb_listener.go b/server/controller/recorder/cache/diffbase/lb_listener.go index aece1ee552a..3874e650431 100644 --- a/server/controller/recorder/cache/diffbase/lb_listener.go +++ b/server/controller/recorder/cache/diffbase/lb_listener.go @@ -17,45 +17,39 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddLBListener(dbItem *metadbmodel.LBListener, seq int) { - b.LBListeners[dbItem.Lcuuid] = &LBListener{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - IPs: dbItem.IPs, - SNATIPs: dbItem.SNATIPs, - Port: dbItem.Port, - Protocol: dbItem.Protocol, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_LB_LISTENER_EN, b.LBListeners[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type LBListener struct { + ResourceBase + Name string + IPs string + SNATIPs string + Port int + Protocol string } -func (b *DataSet) DeleteLBListener(lcuuid string) { - delete(b.LBListeners, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_LB_LISTENER_EN, lcuuid), b.metadata.LogPrefixes) +func (a *LBListener) reset(dbItem *metadbmodel.LBListener, tool *tool.Tool) { + a.Name = dbItem.Name + a.IPs = dbItem.IPs + a.SNATIPs = dbItem.SNATIPs + a.Port = dbItem.Port + a.Protocol = dbItem.Protocol } -type LBListener struct { - DiffBase - Name string `json:"name"` - IPs string `json:"ips"` - SNATIPs string `json:"snat_ips"` - Port int `json:"port"` - Protocol string `json:"protocal"` +func NewLBListenerCollection(t *tool.Tool) *LBListenerCollection { + c := new(LBListenerCollection) + c.collection = newCollectionBuilder[*LBListener](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_LB_LISTENER_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.LBListener { return new(metadbmodel.LBListener) }). + withCacheItemFactory(func() *LBListener { return new(LBListener) }). + build() + return c } -func (l *LBListener) Update(cloudItem *cloudmodel.LBListener) { - l.Name = cloudItem.Name - l.IPs = cloudItem.IPs - l.SNATIPs = cloudItem.SNATIPs - l.Port = cloudItem.Port - l.Protocol = cloudItem.Protocol - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_LB_LISTENER_EN, l)) +type LBListenerCollection struct { + collection[*LBListener, *metadbmodel.LBListener] } diff --git a/server/controller/recorder/cache/diffbase/lb_target_server.go b/server/controller/recorder/cache/diffbase/lb_target_server.go index 88f7c533e7c..bd75808b42f 100644 --- a/server/controller/recorder/cache/diffbase/lb_target_server.go +++ b/server/controller/recorder/cache/diffbase/lb_target_server.go @@ -17,39 +17,35 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddLBTargetServer(dbItem *metadbmodel.LBTargetServer, seq int) { - b.LBTargetServers[dbItem.Lcuuid] = &LBTargetServer{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - IP: dbItem.IP, - Port: dbItem.Port, - Protocol: dbItem.Protocol, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_LB_TARGET_SERVER_EN, b.LBTargetServers[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type LBTargetServer struct { + ResourceBase + IP string + Port int + Protocol string } -func (b *DataSet) DeleteLBTargetServer(lcuuid string) { - delete(b.LBTargetServers, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_LB_TARGET_SERVER_EN, lcuuid), b.metadata.LogPrefixes) +func (a *LBTargetServer) reset(dbItem *metadbmodel.LBTargetServer, tool *tool.Tool) { + a.IP = dbItem.IP + a.Port = dbItem.Port + a.Protocol = dbItem.Protocol } -type LBTargetServer struct { - DiffBase - IP string `json:"ip"` - Port int `json:"port"` - Protocol string `json:"protocal"` +func NewLBTargetServerCollection(t *tool.Tool) *LBTargetServerCollection { + c := new(LBTargetServerCollection) + c.collection = newCollectionBuilder[*LBTargetServer](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_LB_TARGET_SERVER_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.LBTargetServer { return new(metadbmodel.LBTargetServer) }). + withCacheItemFactory(func() *LBTargetServer { return new(LBTargetServer) }). + build() + return c } -func (l *LBTargetServer) Update(cloudItem *cloudmodel.LBTargetServer) { - l.IP = cloudItem.IP - l.Port = cloudItem.Port - l.Protocol = cloudItem.Protocol - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_LB_TARGET_SERVER_EN, l)) +type LBTargetServerCollection struct { + collection[*LBTargetServer, *metadbmodel.LBTargetServer] } diff --git a/server/controller/recorder/cache/diffbase/lb_vm_connection.go b/server/controller/recorder/cache/diffbase/lb_vm_connection.go index faf145adf78..785a0749302 100644 --- a/server/controller/recorder/cache/diffbase/lb_vm_connection.go +++ b/server/controller/recorder/cache/diffbase/lb_vm_connection.go @@ -19,23 +19,27 @@ package diffbase import ( ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddLBVMConnection(dbItem *metadbmodel.LBVMConnection, seq int) { - b.LBVMConnections[dbItem.Lcuuid] = &LBVMConnection{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_LB_VM_CONNECTION_EN, b.LBVMConnections[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type LBVMConnection struct { + ResourceBase } -func (b *DataSet) DeleteLBVMConnection(lcuuid string) { - delete(b.LBVMConnections, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_LB_VM_CONNECTION_EN, lcuuid), b.metadata.LogPrefixes) +func (a *LBVMConnection) reset(dbItem *metadbmodel.LBVMConnection, tool *tool.Tool) { } -type LBVMConnection struct { - DiffBase +func NewLBVMConnectionCollection(t *tool.Tool) *LBVMConnectionCollection { + c := new(LBVMConnectionCollection) + c.collection = newCollectionBuilder[*LBVMConnection](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_LB_VM_CONNECTION_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.LBVMConnection { return new(metadbmodel.LBVMConnection) }). + withCacheItemFactory(func() *LBVMConnection { return new(LBVMConnection) }). + build() + return c +} + +type LBVMConnectionCollection struct { + collection[*LBVMConnection, *metadbmodel.LBVMConnection] } diff --git a/server/controller/recorder/cache/diffbase/nat_gateway.go b/server/controller/recorder/cache/diffbase/nat_gateway.go index b2d4888a662..780db9b8b0e 100644 --- a/server/controller/recorder/cache/diffbase/nat_gateway.go +++ b/server/controller/recorder/cache/diffbase/nat_gateway.go @@ -17,39 +17,35 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddNATGateway(dbItem *metadbmodel.NATGateway, seq int) { - b.NATGateways[dbItem.Lcuuid] = &NATGateway{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - FloatingIPs: dbItem.FloatingIPs, - RegionLcuuid: dbItem.Region, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_NAT_GATEWAY_EN, b.NATGateways[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type NATGateway struct { + ResourceBase + Name string + FloatingIPs string + RegionLcuuid string } -func (b *DataSet) DeleteNATGateway(lcuuid string) { - delete(b.NATGateways, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_NAT_GATEWAY_EN, lcuuid), b.metadata.LogPrefixes) +func (a *NATGateway) reset(dbItem *metadbmodel.NATGateway, tool *tool.Tool) { + a.Name = dbItem.Name + a.FloatingIPs = dbItem.FloatingIPs + a.RegionLcuuid = dbItem.Region } -type NATGateway struct { - DiffBase - Name string `json:"name"` - FloatingIPs string `json:"floating_ips"` - RegionLcuuid string `json:"region_lcuuid"` +func NewNATGatewayCollection(t *tool.Tool) *NATGatewayCollection { + c := new(NATGatewayCollection) + c.collection = newCollectionBuilder[*NATGateway](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_NAT_GATEWAY_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.NATGateway { return new(metadbmodel.NATGateway) }). + withCacheItemFactory(func() *NATGateway { return new(NATGateway) }). + build() + return c } -func (n *NATGateway) Update(cloudItem *cloudmodel.NATGateway) { - n.Name = cloudItem.Name - n.FloatingIPs = cloudItem.FloatingIPs - n.RegionLcuuid = cloudItem.RegionLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_NAT_GATEWAY_EN, n)) +type NATGatewayCollection struct { + collection[*NATGateway, *metadbmodel.NATGateway] } diff --git a/server/controller/recorder/cache/diffbase/nat_rule.go b/server/controller/recorder/cache/diffbase/nat_rule.go index f3a5a9ce620..f67b8bbc535 100644 --- a/server/controller/recorder/cache/diffbase/nat_rule.go +++ b/server/controller/recorder/cache/diffbase/nat_rule.go @@ -19,23 +19,27 @@ package diffbase import ( ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddNATRule(dbItem *metadbmodel.NATRule, seq int) { - b.NATRules[dbItem.Lcuuid] = &NATRule{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_NAT_RULE_EN, b.NATRules[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type NATRule struct { + ResourceBase } -func (b *DataSet) DeleteNATRule(lcuuid string) { - delete(b.NATRules, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_NAT_RULE_EN, lcuuid), b.metadata.LogPrefixes) +func (a *NATRule) reset(dbItem *metadbmodel.NATRule, tool *tool.Tool) { } -type NATRule struct { - DiffBase +func NewNATRuleCollection(t *tool.Tool) *NATRuleCollection { + c := new(NATRuleCollection) + c.collection = newCollectionBuilder[*NATRule](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_NAT_RULE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.NATRule { return new(metadbmodel.NATRule) }). + withCacheItemFactory(func() *NATRule { return new(NATRule) }). + build() + return c +} + +type NATRuleCollection struct { + collection[*NATRule, *metadbmodel.NATRule] } diff --git a/server/controller/recorder/cache/diffbase/nat_vm_connection.go b/server/controller/recorder/cache/diffbase/nat_vm_connection.go index 611afd921b4..72ea5b2158a 100644 --- a/server/controller/recorder/cache/diffbase/nat_vm_connection.go +++ b/server/controller/recorder/cache/diffbase/nat_vm_connection.go @@ -19,23 +19,27 @@ package diffbase import ( ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddNATVMConnection(dbItem *metadbmodel.NATVMConnection, seq int) { - b.NATVMConnections[dbItem.Lcuuid] = &NATVMConnection{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_NAT_VM_CONNECTION_EN, b.NATVMConnections[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type NATVMConnection struct { + ResourceBase } -func (b *DataSet) DeleteNATVMConnection(lcuuid string) { - delete(b.NATVMConnections, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_NAT_VM_CONNECTION_EN, lcuuid), b.metadata.LogPrefixes) +func (a *NATVMConnection) reset(dbItem *metadbmodel.NATVMConnection, tool *tool.Tool) { } -type NATVMConnection struct { - DiffBase +func NewNATVMConnectionCollection(t *tool.Tool) *NATVMConnectionCollection { + c := new(NATVMConnectionCollection) + c.collection = newCollectionBuilder[*NATVMConnection](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_NAT_VM_CONNECTION_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.NATVMConnection { return new(metadbmodel.NATVMConnection) }). + withCacheItemFactory(func() *NATVMConnection { return new(NATVMConnection) }). + build() + return c +} + +type NATVMConnectionCollection struct { + collection[*NATVMConnection, *metadbmodel.NATVMConnection] } diff --git a/server/controller/recorder/cache/diffbase/network.go b/server/controller/recorder/cache/diffbase/network.go index 52d108ab1bc..68bc345a7c2 100644 --- a/server/controller/recorder/cache/diffbase/network.go +++ b/server/controller/recorder/cache/diffbase/network.go @@ -17,58 +17,47 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddNetwork(dbItem *metadbmodel.Network, seq int, toolDataSet *tool.DataSet) { - vpcLcuuid, _ := toolDataSet.GetVPCLcuuidByID(dbItem.VPCID) - b.Networks[dbItem.Lcuuid] = &Network{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - Label: dbItem.Label, - TunnelID: dbItem.TunnelID, - NetType: dbItem.NetType, - SegmentationID: dbItem.SegmentationID, - VPCLcuuid: vpcLcuuid, - RegionLcuuid: dbItem.Region, - AZLcuuid: dbItem.AZ, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_NETWORK_EN, b.Networks[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type Network struct { + ResourceBase + Name string + Label string + TunnelID int + NetType int + SegmentationID int + VPCLcuuid string + RegionLcuuid string + AZLcuuid string + SubDomainLcuuid string } -func (b *DataSet) DeleteNetwork(lcuuid string) { - delete(b.Networks, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_NETWORK_EN, lcuuid), b.metadata.LogPrefixes) +func (a *Network) reset(dbItem *metadbmodel.Network, tool *tool.Tool) { + a.Name = dbItem.Name + a.Label = dbItem.Label + a.TunnelID = dbItem.TunnelID + a.NetType = dbItem.NetType + a.SegmentationID = dbItem.SegmentationID + a.VPCLcuuid = tool.VPC().GetByID(dbItem.VPCID).Lcuuid() + a.RegionLcuuid = dbItem.Region + a.AZLcuuid = dbItem.AZ + a.SubDomainLcuuid = dbItem.SubDomain } -type Network struct { - DiffBase - Name string `json:"name"` - Label string `json:"label"` - TunnelID int `json:"tunnel_id"` - NetType int `json:"net_type"` - SegmentationID int `json:"segmentation_id"` - VPCLcuuid string `json:"vpc_lcuuid"` - RegionLcuuid string `json:"region_lcuuid"` - AZLcuuid string `json:"az_lcuuid"` - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func NewNetworkCollection(t *tool.Tool) *NetworkCollection { + c := new(NetworkCollection) + c.collection = newCollectionBuilder[*Network](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_NETWORK_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.Network { return new(metadbmodel.Network) }). + withCacheItemFactory(func() *Network { return new(Network) }). + build() + return c } -func (n *Network) Update(cloudItem *cloudmodel.Network) { - n.Name = cloudItem.Name - n.Label = cloudItem.Label - n.TunnelID = cloudItem.TunnelID - n.NetType = cloudItem.NetType - n.SegmentationID = cloudItem.SegmentationID - n.VPCLcuuid = cloudItem.VPCLcuuid - n.RegionLcuuid = cloudItem.RegionLcuuid - n.AZLcuuid = cloudItem.AZLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_NETWORK_EN, n)) +type NetworkCollection struct { + collection[*Network, *metadbmodel.Network] } diff --git a/server/controller/recorder/cache/diffbase/peer_connection.go b/server/controller/recorder/cache/diffbase/peer_connection.go index 4df723b4691..0eb8ee34018 100644 --- a/server/controller/recorder/cache/diffbase/peer_connection.go +++ b/server/controller/recorder/cache/diffbase/peer_connection.go @@ -17,34 +17,31 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddPeerConnection(dbItem *metadbmodel.PeerConnection, seq int, toolDataSet *tool.DataSet) { - b.PeerConnections[dbItem.Lcuuid] = &PeerConnection{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_PEER_CONNECTION_EN, b.PeerConnections[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type PeerConnection struct { + ResourceBase + Name string } -func (b *DataSet) DeletePeerConnection(lcuuid string) { - delete(b.PeerConnections, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_PEER_CONNECTION_EN, lcuuid), b.metadata.LogPrefixes) +func (a *PeerConnection) reset(dbItem *metadbmodel.PeerConnection, tool *tool.Tool) { + a.Name = dbItem.Name } -type PeerConnection struct { - DiffBase - Name string `json:"name"` +func NewPeerConnectionCollection(t *tool.Tool) *PeerConnectionCollection { + c := new(PeerConnectionCollection) + c.collection = newCollectionBuilder[*PeerConnection](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_PEER_CONNECTION_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PeerConnection { return new(metadbmodel.PeerConnection) }). + withCacheItemFactory(func() *PeerConnection { return new(PeerConnection) }). + build() + return c } -func (p *PeerConnection) Update(cloudItem *cloudmodel.PeerConnection) { - p.Name = cloudItem.Name - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_PEER_CONNECTION_EN, p)) +type PeerConnectionCollection struct { + collection[*PeerConnection, *metadbmodel.PeerConnection] } diff --git a/server/controller/recorder/cache/diffbase/pod.go b/server/controller/recorder/cache/diffbase/pod.go index 4e924ef76ad..8aec96086e4 100644 --- a/server/controller/recorder/cache/diffbase/pod.go +++ b/server/controller/recorder/cache/diffbase/pod.go @@ -17,94 +17,59 @@ package diffbase import ( - "time" - - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddPod(dbItem *metadbmodel.Pod, seq int, toolDataSet *tool.DataSet) { - var podNodeLcuuid string - if dbItem.PodNodeID != 0 { - podNodeLcuuid, _ = toolDataSet.GetPodNodeLcuuidByID(dbItem.PodNodeID) - } - var podReplicaSetLcuuid string - if dbItem.PodReplicaSetID != 0 { - podReplicaSetLcuuid, _ = toolDataSet.GetPodReplicaSetLcuuidByID(dbItem.PodReplicaSetID) - } - var podGroupLcuuid string - if dbItem.PodGroupID != 0 { - podGroupLcuuid, _ = toolDataSet.GetPodGroupLcuuidByID(dbItem.PodGroupID) - } - var podServiceLcuuid string - if dbItem.PodServiceID != 0 { - podServiceLcuuid, _ = toolDataSet.GetPodServiceLcuuidByID(dbItem.PodServiceID) - } - vpcLcuuid, _ := toolDataSet.GetVPCLcuuidByID(dbItem.VPCID) - b.Pods[dbItem.Lcuuid] = &Pod{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - Label: dbItem.Label, - Annotation: dbItem.Annotation, - ENV: dbItem.ENV, - ContainerIDs: dbItem.ContainerIDs, - State: dbItem.State, - CreatedAt: dbItem.CreatedAt, - PodNodeLcuuid: podNodeLcuuid, - PodReplicaSetLcuuid: podReplicaSetLcuuid, - PodGroupLcuuid: podGroupLcuuid, - PodServiceLcuuid: podServiceLcuuid, - VPCLcuuid: vpcLcuuid, - RegionLcuuid: dbItem.Region, - AZLcuuid: dbItem.AZ, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_EN, b.Pods[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type Pod struct { + ResourceBase + Name string + Label string + Annotation string + ENV string + ContainerIDs string + State int + CreatedAt time.Time + PodNodeLcuuid string + PodReplicaSetLcuuid string + PodGroupLcuuid string + PodServiceLcuuid string + VPCLcuuid string + RegionLcuuid string + AZLcuuid string + SubDomainLcuuid string } -func (b *DataSet) DeletePod(lcuuid string) { - delete(b.Pods, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_EN, lcuuid), b.metadata.LogPrefixes) +func (a *Pod) reset(dbItem *metadbmodel.Pod, tool *tool.Tool) { + a.Name = dbItem.Name + a.Label = dbItem.Label + a.Annotation = dbItem.Annotation + a.ENV = dbItem.ENV + a.ContainerIDs = dbItem.ContainerIDs + a.State = dbItem.State + a.CreatedAt = dbItem.CreatedAt + a.PodNodeLcuuid = tool.PodNode().GetByID(dbItem.PodNodeID).Lcuuid() + a.PodReplicaSetLcuuid = tool.PodReplicaSet().GetByID(dbItem.PodReplicaSetID).Lcuuid() + a.PodGroupLcuuid = tool.PodGroup().GetByID(dbItem.PodGroupID).Lcuuid() + a.PodServiceLcuuid = tool.PodService().GetByID(dbItem.PodServiceID).Lcuuid() + a.VPCLcuuid = tool.VPC().GetByID(dbItem.VPCID).Lcuuid() + a.RegionLcuuid = dbItem.Region + a.AZLcuuid = dbItem.AZ + a.SubDomainLcuuid = dbItem.SubDomain } -type Pod struct { - DiffBase - Name string `json:"name"` - Label string `json:"label"` - Annotation string `json:"annotation"` - ENV string `json:"env"` - ContainerIDs string `json:"container_ids"` - State int `json:"state"` - CreatedAt time.Time `json:"created_at"` - PodNodeLcuuid string `json:"pod_node_lcuuid"` - PodReplicaSetLcuuid string `json:"pod_replica_set_lcuuid"` - PodGroupLcuuid string `json:"pod_group_lcuuid"` - PodServiceLcuuid string `json:"pod_service_lcuuid"` - VPCLcuuid string `json:"vpc_lcuuid"` - RegionLcuuid string `json:"region_lcuuid"` - AZLcuuid string `json:"az_lcuuid"` - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func NewPodCollection(t *tool.Tool) *PodCollection { + c := new(PodCollection) + c.collection = newCollectionBuilder[*Pod](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.Pod { return new(metadbmodel.Pod) }). + withCacheItemFactory(func() *Pod { return new(Pod) }). + build() + return c } -func (p *Pod) Update(cloudItem *cloudmodel.Pod) { - p.Name = cloudItem.Name - p.Label = cloudItem.Label - p.ENV = cloudItem.ENV - p.Annotation = cloudItem.Annotation - p.ContainerIDs = cloudItem.ContainerIDs - p.State = cloudItem.State - p.CreatedAt = cloudItem.CreatedAt - p.PodNodeLcuuid = cloudItem.PodNodeLcuuid - p.PodReplicaSetLcuuid = cloudItem.PodReplicaSetLcuuid - p.PodGroupLcuuid = cloudItem.PodGroupLcuuid - p.PodServiceLcuuid = cloudItem.PodServiceLcuuid - p.VPCLcuuid = cloudItem.VPCLcuuid - p.RegionLcuuid = cloudItem.RegionLcuuid - p.AZLcuuid = cloudItem.AZLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_EN, p)) +type PodCollection struct { + collection[*Pod, *metadbmodel.Pod] } diff --git a/server/controller/recorder/cache/diffbase/pod_cluster.go b/server/controller/recorder/cache/diffbase/pod_cluster.go index d10dcd2a180..79f32cf9d4a 100644 --- a/server/controller/recorder/cache/diffbase/pod_cluster.go +++ b/server/controller/recorder/cache/diffbase/pod_cluster.go @@ -17,44 +17,39 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddPodCluster(dbItem *metadbmodel.PodCluster, seq int) { - b.PodClusters[dbItem.Lcuuid] = &PodCluster{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - ClusterName: dbItem.ClusterName, - RegionLcuuid: dbItem.Region, - AZLcuuid: dbItem.AZ, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_CLUSTER_EN, b.PodClusters[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type PodCluster struct { + ResourceBase + Name string + ClusterName string + RegionLcuuid string + AZLcuuid string + SubDomainLcuuid string } -func (b *DataSet) DeletePodCluster(lcuuid string) { - delete(b.PodClusters, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_CLUSTER_EN, lcuuid), b.metadata.LogPrefixes) +func (a *PodCluster) reset(dbItem *metadbmodel.PodCluster, tool *tool.Tool) { + a.Name = dbItem.Name + a.ClusterName = dbItem.ClusterName + a.RegionLcuuid = dbItem.Region + a.AZLcuuid = dbItem.AZ + a.SubDomainLcuuid = dbItem.SubDomain } -type PodCluster struct { - DiffBase - Name string `json:"name"` - ClusterName string `json:"cluster_name"` - RegionLcuuid string `json:"region_lcuuid"` - AZLcuuid string `json:"az_lcuuid"` - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func NewPodClusterCollection(t *tool.Tool) *PodClusterCollection { + c := new(PodClusterCollection) + c.collection = newCollectionBuilder[*PodCluster](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_CLUSTER_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodCluster { return new(metadbmodel.PodCluster) }). + withCacheItemFactory(func() *PodCluster { return new(PodCluster) }). + build() + return c } -func (p *PodCluster) Update(cloudItem *cloudmodel.PodCluster) { - p.Name = cloudItem.Name - p.ClusterName = cloudItem.ClusterName - p.RegionLcuuid = cloudItem.RegionLcuuid - p.AZLcuuid = cloudItem.AZLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_CLUSTER_EN, p)) +type PodClusterCollection struct { + collection[*PodCluster, *metadbmodel.PodCluster] } diff --git a/server/controller/recorder/cache/diffbase/pod_group.go b/server/controller/recorder/cache/diffbase/pod_group.go index 9064b8c66cf..25acbe54688 100644 --- a/server/controller/recorder/cache/diffbase/pod_group.go +++ b/server/controller/recorder/cache/diffbase/pod_group.go @@ -17,89 +17,61 @@ package diffbase import ( - "sigs.k8s.io/yaml" - - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddPodGroup(dbItem *metadbmodel.PodGroup, seq int) { - b.PodGroups[dbItem.Lcuuid] = &PodGroup{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - Label: dbItem.Label, - NetworkMode: dbItem.NetworkMode, - PodNum: dbItem.PodNum, - Type: dbItem.Type, - Metadata: string(dbItem.Metadata), - MetadataHash: dbItem.MetadataHash, - Spec: string(dbItem.Spec), - SpecHash: dbItem.SpecHash, - RegionLcuuid: dbItem.Region, - AZLcuuid: dbItem.AZ, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_GROUP_EN, b.PodGroups[dbItem.Lcuuid].ToLoggable()), b.metadata.LogPrefixes) -} - -func (b *DataSet) DeletePodGroup(lcuuid string) { - delete(b.PodGroups, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_GROUP_EN, lcuuid), b.metadata.LogPrefixes) +type PodGroup struct { + ResourceBase + Name string + Label string + PodNum int + Type int + Metadata string + MetadataHash string + Spec string + SpecHash string + RegionLcuuid string + AZLcuuid string + SubDomainLcuuid string + NetworkMode int } -type PodGroup struct { - DiffBase - Name string `json:"name"` - Label string `json:"label"` - PodNum int `json:"pod_num"` - Type int `json:"type"` - NetworkMode int `json:"network_mode"` - Metadata string `json:"metadata"` - MetadataHash string `json:"metadata_hash"` - Spec string `json:"spec"` - SpecHash string `json:"spec_hash"` - RegionLcuuid string `json:"region_lcuuid"` - AZLcuuid string `json:"az_lcuuid"` - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func (a *PodGroup) reset(dbItem *metadbmodel.PodGroup, tool *tool.Tool) { + a.Name = dbItem.Name + a.Label = dbItem.Label + a.PodNum = dbItem.PodNum + a.Type = dbItem.Type + a.Metadata = string(dbItem.Metadata) + a.MetadataHash = dbItem.MetadataHash + a.Spec = string(dbItem.Spec) + a.SpecHash = dbItem.SpecHash + a.RegionLcuuid = dbItem.Region + a.AZLcuuid = dbItem.AZ + a.SubDomainLcuuid = dbItem.SubDomain + a.NetworkMode = dbItem.NetworkMode } -// ToLoggable converts PodGroup to a loggable format, excluding fields Spec and Metadata -func (p PodGroup) ToLoggable() interface{} { - copied := p +// ToLoggable converts PodGroup to a loggable format, excluding sensitive fields +func (a PodGroup) ToLoggable() interface{} { + copied := a copied.Metadata = "**HIDDEN**" copied.Spec = "**HIDDEN**" return copied } -func (p *PodGroup) Update(cloudItem *cloudmodel.PodGroup, toolDataSet *tool.DataSet) { - p.Name = cloudItem.Name - p.Label = cloudItem.Label - p.NetworkMode = cloudItem.NetworkMode - p.PodNum = cloudItem.PodNum - p.Type = cloudItem.Type - - yamlMetadata, err := yaml.JSONToYAML([]byte(cloudItem.Metadata)) - if err != nil { - log.Errorf("failed to convert JSON metadata: %v to YAML: %s", cloudItem.Metadata, toolDataSet.GetMetadata().LogPrefixes) - return - } - p.Metadata = string(yamlMetadata) - p.MetadataHash = cloudItem.MetadataHash - - yamlSpec, err := yaml.JSONToYAML([]byte(cloudItem.Spec)) - if err != nil { - log.Errorf("failed to convert JSON spec: %v to YAML: %s", cloudItem.Spec, toolDataSet.GetMetadata().LogPrefixes) - return - } - p.Spec = string(yamlSpec) - p.SpecHash = cloudItem.SpecHash +func NewPodGroupCollection(t *tool.Tool) *PodGroupCollection { + c := new(PodGroupCollection) + c.collection = newCollectionBuilder[*PodGroup](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_GROUP_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodGroup { return new(metadbmodel.PodGroup) }). + withCacheItemFactory(func() *PodGroup { return new(PodGroup) }). + build() + return c +} - p.RegionLcuuid = cloudItem.RegionLcuuid - p.AZLcuuid = cloudItem.AZLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_GROUP_EN, p.ToLoggable())) +type PodGroupCollection struct { + collection[*PodGroup, *metadbmodel.PodGroup] } diff --git a/server/controller/recorder/cache/diffbase/pod_group_config_map_connection.go b/server/controller/recorder/cache/diffbase/pod_group_config_map_connection.go index 31c9ce4530e..3f37e17f087 100644 --- a/server/controller/recorder/cache/diffbase/pod_group_config_map_connection.go +++ b/server/controller/recorder/cache/diffbase/pod_group_config_map_connection.go @@ -19,23 +19,27 @@ package diffbase import ( ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddPodGroupConfigMapConnection(dbItem *metadbmodel.PodGroupConfigMapConnection, seq int) { - b.PodGroupConfigMapConnections[dbItem.Lcuuid] = &PodGroupConfigMapConnection{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_GROUP_CONFIG_MAP_CONNECTION_EN, b.PodGroupConfigMapConnections[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type PodGroupConfigMapConnection struct { + ResourceBase } -func (b *DataSet) DeletePodGroupConfigMapConnection(lcuuid string) { - delete(b.PodGroupConfigMapConnections, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_GROUP_CONFIG_MAP_CONNECTION_EN, lcuuid), b.metadata.LogPrefixes) +func (a *PodGroupConfigMapConnection) reset(dbItem *metadbmodel.PodGroupConfigMapConnection, tool *tool.Tool) { } -type PodGroupConfigMapConnection struct { - DiffBase +func NewPodGroupConfigMapConnectionCollection(t *tool.Tool) *PodGroupConfigMapConnectionCollection { + c := new(PodGroupConfigMapConnectionCollection) + c.collection = newCollectionBuilder[*PodGroupConfigMapConnection](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_GROUP_CONFIG_MAP_CONNECTION_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodGroupConfigMapConnection { return new(metadbmodel.PodGroupConfigMapConnection) }). + withCacheItemFactory(func() *PodGroupConfigMapConnection { return new(PodGroupConfigMapConnection) }). + build() + return c +} + +type PodGroupConfigMapConnectionCollection struct { + collection[*PodGroupConfigMapConnection, *metadbmodel.PodGroupConfigMapConnection] } diff --git a/server/controller/recorder/cache/diffbase/pod_group_port.go b/server/controller/recorder/cache/diffbase/pod_group_port.go index 78f245db933..a89a4431194 100644 --- a/server/controller/recorder/cache/diffbase/pod_group_port.go +++ b/server/controller/recorder/cache/diffbase/pod_group_port.go @@ -17,35 +17,33 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddPodGroupPort(dbItem *metadbmodel.PodGroupPort, seq int) { - b.PodGroupPorts[dbItem.Lcuuid] = &PodGroupPort{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_GROUP_PORT_EN, b.PodGroupPorts[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type PodGroupPort struct { + ResourceBase + Name string + SubDomainLcuuid string } -func (b *DataSet) DeletePodGroupPort(lcuuid string) { - delete(b.PodGroupPorts, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_GROUP_PORT_EN, lcuuid), b.metadata.LogPrefixes) +func (a *PodGroupPort) reset(dbItem *metadbmodel.PodGroupPort, tool *tool.Tool) { + a.Name = dbItem.Name + a.SubDomainLcuuid = dbItem.SubDomain } -type PodGroupPort struct { - DiffBase - Name string `json:"name"` - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func NewPodGroupPortCollection(t *tool.Tool) *PodGroupPortCollection { + c := new(PodGroupPortCollection) + c.collection = newCollectionBuilder[*PodGroupPort](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_GROUP_PORT_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodGroupPort { return new(metadbmodel.PodGroupPort) }). + withCacheItemFactory(func() *PodGroupPort { return new(PodGroupPort) }). + build() + return c } -func (p *PodGroupPort) Update(cloudItem *cloudmodel.PodGroupPort) { - p.Name = cloudItem.Name - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_GROUP_PORT_EN, p)) +type PodGroupPortCollection struct { + collection[*PodGroupPort, *metadbmodel.PodGroupPort] } diff --git a/server/controller/recorder/cache/diffbase/pod_ingress.go b/server/controller/recorder/cache/diffbase/pod_ingress.go index 2a28d1893d3..992f9bed4d3 100644 --- a/server/controller/recorder/cache/diffbase/pod_ingress.go +++ b/server/controller/recorder/cache/diffbase/pod_ingress.go @@ -17,41 +17,37 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddPodIngress(dbItem *metadbmodel.PodIngress, seq int) { - b.PodIngresses[dbItem.Lcuuid] = &PodIngress{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - RegionLcuuid: dbItem.Region, - AZLcuuid: dbItem.AZ, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_INGRESS_EN, b.PodIngresses[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type PodIngress struct { + ResourceBase + Name string + RegionLcuuid string + AZLcuuid string + SubDomainLcuuid string } -func (b *DataSet) DeletePodIngress(lcuuid string) { - delete(b.PodIngresses, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_INGRESS_EN, lcuuid), b.metadata.LogPrefixes) +func (a *PodIngress) reset(dbItem *metadbmodel.PodIngress, tool *tool.Tool) { + a.Name = dbItem.Name + a.RegionLcuuid = dbItem.Region + a.AZLcuuid = dbItem.AZ + a.SubDomainLcuuid = dbItem.SubDomain } -type PodIngress struct { - DiffBase - Name string `json:"name"` - RegionLcuuid string `json:"region_lcuuid"` - AZLcuuid string `json:"az_lcuuid"` - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func NewPodIngressCollection(t *tool.Tool) *PodIngressCollection { + c := new(PodIngressCollection) + c.collection = newCollectionBuilder[*PodIngress](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_INGRESS_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodIngress { return new(metadbmodel.PodIngress) }). + withCacheItemFactory(func() *PodIngress { return new(PodIngress) }). + build() + return c } -func (p *PodIngress) Update(cloudItem *cloudmodel.PodIngress) { - p.Name = cloudItem.Name - p.RegionLcuuid = cloudItem.RegionLcuuid - p.AZLcuuid = cloudItem.AZLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_INGRESS_EN, p)) +type PodIngressCollection struct { + collection[*PodIngress, *metadbmodel.PodIngress] } diff --git a/server/controller/recorder/cache/diffbase/pod_ingress_rule.go b/server/controller/recorder/cache/diffbase/pod_ingress_rule.go index 3435d3aee10..27552f925f0 100644 --- a/server/controller/recorder/cache/diffbase/pod_ingress_rule.go +++ b/server/controller/recorder/cache/diffbase/pod_ingress_rule.go @@ -19,25 +19,29 @@ package diffbase import ( ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddPodIngressRule(dbItem *metadbmodel.PodIngressRule, seq int) { - b.PodIngressRules[dbItem.Lcuuid] = &PodIngressRule{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_INGRESS_RULE_EN, b.PodIngressRules[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type PodIngressRule struct { + ResourceBase + SubDomainLcuuid string } -func (b *DataSet) DeletePodIngressRule(lcuuid string) { - delete(b.PodIngressRules, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_INGRESS_RULE_EN, lcuuid), b.metadata.LogPrefixes) +func (a *PodIngressRule) reset(dbItem *metadbmodel.PodIngressRule, tool *tool.Tool) { + a.SubDomainLcuuid = dbItem.SubDomain } -type PodIngressRule struct { - DiffBase - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func NewPodIngressRuleCollection(t *tool.Tool) *PodIngressRuleCollection { + c := new(PodIngressRuleCollection) + c.collection = newCollectionBuilder[*PodIngressRule](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_INGRESS_RULE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodIngressRule { return new(metadbmodel.PodIngressRule) }). + withCacheItemFactory(func() *PodIngressRule { return new(PodIngressRule) }). + build() + return c +} + +type PodIngressRuleCollection struct { + collection[*PodIngressRule, *metadbmodel.PodIngressRule] } diff --git a/server/controller/recorder/cache/diffbase/pod_ingress_rule_backend.go b/server/controller/recorder/cache/diffbase/pod_ingress_rule_backend.go index 79e045fba14..ea8e9138dc2 100644 --- a/server/controller/recorder/cache/diffbase/pod_ingress_rule_backend.go +++ b/server/controller/recorder/cache/diffbase/pod_ingress_rule_backend.go @@ -19,25 +19,29 @@ package diffbase import ( ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddPodIngressRuleBackend(dbItem *metadbmodel.PodIngressRuleBackend, seq int) { - b.PodIngressRuleBackends[dbItem.Lcuuid] = &PodIngressRuleBackend{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_INGRESS_RULE_BACKEND_EN, b.PodIngressRuleBackends[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type PodIngressRuleBackend struct { + ResourceBase + SubDomainLcuuid string } -func (b *DataSet) DeletePodIngressRuleBackend(lcuuid string) { - delete(b.PodIngressRuleBackends, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_INGRESS_RULE_BACKEND_EN, lcuuid), b.metadata.LogPrefixes) +func (a *PodIngressRuleBackend) reset(dbItem *metadbmodel.PodIngressRuleBackend, tool *tool.Tool) { + a.SubDomainLcuuid = dbItem.SubDomain } -type PodIngressRuleBackend struct { - DiffBase - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func NewPodIngressRuleBackendCollection(t *tool.Tool) *PodIngressRuleBackendCollection { + c := new(PodIngressRuleBackendCollection) + c.collection = newCollectionBuilder[*PodIngressRuleBackend](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_INGRESS_RULE_BACKEND_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodIngressRuleBackend { return new(metadbmodel.PodIngressRuleBackend) }). + withCacheItemFactory(func() *PodIngressRuleBackend { return new(PodIngressRuleBackend) }). + build() + return c +} + +type PodIngressRuleBackendCollection struct { + collection[*PodIngressRuleBackend, *metadbmodel.PodIngressRuleBackend] } diff --git a/server/controller/recorder/cache/diffbase/pod_namespace.go b/server/controller/recorder/cache/diffbase/pod_namespace.go index af811a8f8c6..50d1764b2c5 100644 --- a/server/controller/recorder/cache/diffbase/pod_namespace.go +++ b/server/controller/recorder/cache/diffbase/pod_namespace.go @@ -17,41 +17,37 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddPodNamespace(dbItem *metadbmodel.PodNamespace, seq int) { - b.PodNamespaces[dbItem.Lcuuid] = &PodNamespace{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - RegionLcuuid: dbItem.Region, - AZLcuuid: dbItem.AZ, - SubDomainLcuuid: dbItem.SubDomain, - LearnedCloudTags: dbItem.LearnedCloudTags, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_NAMESPACE_EN, b.PodNamespaces[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type PodNamespace struct { + ResourceBase + RegionLcuuid string + AZLcuuid string + SubDomainLcuuid string + LearnedCloudTags map[string]string } -func (b *DataSet) DeletePodNamespace(lcuuid string) { - delete(b.PodNamespaces, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_NAMESPACE_EN, lcuuid), b.metadata.LogPrefixes) +func (a *PodNamespace) reset(dbItem *metadbmodel.PodNamespace, tool *tool.Tool) { + a.RegionLcuuid = dbItem.Region + a.AZLcuuid = dbItem.AZ + a.SubDomainLcuuid = dbItem.SubDomain + a.LearnedCloudTags = dbItem.LearnedCloudTags } -type PodNamespace struct { - DiffBase - RegionLcuuid string `json:"region_lcuuid"` - AZLcuuid string `json:"az_lcuuid"` - SubDomainLcuuid string `json:"sub_domain_lcuuid"` - LearnedCloudTags map[string]string `json:"learned_cloud_tags"` +func NewPodNamespaceCollection(t *tool.Tool) *PodNamespaceCollection { + c := new(PodNamespaceCollection) + c.collection = newCollectionBuilder[*PodNamespace](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_NAMESPACE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodNamespace { return new(metadbmodel.PodNamespace) }). + withCacheItemFactory(func() *PodNamespace { return new(PodNamespace) }). + build() + return c } -func (p *PodNamespace) Update(cloudItem *cloudmodel.PodNamespace) { - p.RegionLcuuid = cloudItem.RegionLcuuid - p.AZLcuuid = cloudItem.AZLcuuid - p.LearnedCloudTags = cloudItem.CloudTags - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_NAMESPACE_EN, p)) +type PodNamespaceCollection struct { + collection[*PodNamespace, *metadbmodel.PodNamespace] } diff --git a/server/controller/recorder/cache/diffbase/pod_node.go b/server/controller/recorder/cache/diffbase/pod_node.go index 59b9cf625e9..69ec4409d2c 100644 --- a/server/controller/recorder/cache/diffbase/pod_node.go +++ b/server/controller/recorder/cache/diffbase/pod_node.go @@ -17,56 +17,47 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddPodNode(dbItem *metadbmodel.PodNode, seq int) { - b.PodNodes[dbItem.Lcuuid] = &PodNode{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Type: dbItem.Type, - State: dbItem.State, - Hostname: dbItem.Hostname, - IP: dbItem.IP, - VCPUNum: dbItem.VCPUNum, - MemTotal: dbItem.MemTotal, - RegionLcuuid: dbItem.Region, - AZLcuuid: dbItem.AZ, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_NODE_EN, b.PodNodes[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type PodNode struct { + ResourceBase + Type int + State int + Hostname string + IP string + VCPUNum int + MemTotal int + RegionLcuuid string + AZLcuuid string + SubDomainLcuuid string } -func (b *DataSet) DeletePodNode(lcuuid string) { - delete(b.PodNodes, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_NODE_EN, lcuuid), b.metadata.LogPrefixes) +func (a *PodNode) reset(dbItem *metadbmodel.PodNode, tool *tool.Tool) { + a.Type = dbItem.Type + a.State = dbItem.State + a.Hostname = dbItem.Hostname + a.IP = dbItem.IP + a.VCPUNum = dbItem.VCPUNum + a.MemTotal = dbItem.MemTotal + a.RegionLcuuid = dbItem.Region + a.AZLcuuid = dbItem.AZ + a.SubDomainLcuuid = dbItem.SubDomain } -type PodNode struct { - DiffBase - Type int `json:"type"` - State int `json:"state"` - Hostname string `json:"hostname"` - IP string `json:"ip"` - VCPUNum int `json:"vcpu_num"` - MemTotal int `json:"mem_total"` - RegionLcuuid string `json:"region_lcuuid"` - AZLcuuid string `json:"az_lcuuid"` - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func NewPodNodeCollection(t *tool.Tool) *PodNodeCollection { + c := new(PodNodeCollection) + c.collection = newCollectionBuilder[*PodNode](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_NODE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodNode { return new(metadbmodel.PodNode) }). + withCacheItemFactory(func() *PodNode { return new(PodNode) }). + build() + return c } -func (p *PodNode) Update(cloudItem *cloudmodel.PodNode) { - p.Type = cloudItem.Type - p.State = cloudItem.State - p.Hostname = cloudItem.Hostname - p.IP = cloudItem.IP - p.VCPUNum = cloudItem.VCPUNum - p.MemTotal = cloudItem.MemTotal - p.RegionLcuuid = cloudItem.RegionLcuuid - p.AZLcuuid = cloudItem.AZLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_NODE_EN, p)) +type PodNodeCollection struct { + collection[*PodNode, *metadbmodel.PodNode] } diff --git a/server/controller/recorder/cache/diffbase/pod_replica_set.go b/server/controller/recorder/cache/diffbase/pod_replica_set.go index 990e842b022..9b3ac32306e 100644 --- a/server/controller/recorder/cache/diffbase/pod_replica_set.go +++ b/server/controller/recorder/cache/diffbase/pod_replica_set.go @@ -17,47 +17,41 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddPodReplicaSet(dbItem *metadbmodel.PodReplicaSet, seq int) { - b.PodReplicaSets[dbItem.Lcuuid] = &PodReplicaSet{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - Label: dbItem.Label, - PodNum: dbItem.PodNum, - RegionLcuuid: dbItem.Region, - AZLcuuid: dbItem.AZ, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_REPLICA_SET_EN, b.PodReplicaSets[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type PodReplicaSet struct { + ResourceBase + Name string + Label string + PodNum int + RegionLcuuid string + AZLcuuid string + SubDomainLcuuid string } -func (b *DataSet) DeletePodReplicaSet(lcuuid string) { - delete(b.PodReplicaSets, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_REPLICA_SET_EN, lcuuid), b.metadata.LogPrefixes) +func (a *PodReplicaSet) reset(dbItem *metadbmodel.PodReplicaSet, tool *tool.Tool) { + a.Name = dbItem.Name + a.Label = dbItem.Label + a.PodNum = dbItem.PodNum + a.RegionLcuuid = dbItem.Region + a.AZLcuuid = dbItem.AZ + a.SubDomainLcuuid = dbItem.SubDomain } -type PodReplicaSet struct { - DiffBase - Name string `json:"name"` - Label string `json:"label"` - PodNum int `json:"pod_num"` - RegionLcuuid string `json:"region_lcuuid"` - AZLcuuid string `json:"az_lcuuid"` - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func NewPodReplicaSetCollection(t *tool.Tool) *PodReplicaSetCollection { + c := new(PodReplicaSetCollection) + c.collection = newCollectionBuilder[*PodReplicaSet](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_REPLICA_SET_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodReplicaSet { return new(metadbmodel.PodReplicaSet) }). + withCacheItemFactory(func() *PodReplicaSet { return new(PodReplicaSet) }). + build() + return c } -func (p *PodReplicaSet) Update(cloudItem *cloudmodel.PodReplicaSet) { - p.Name = cloudItem.Name - p.Label = cloudItem.Label - p.PodNum = cloudItem.PodNum - p.RegionLcuuid = cloudItem.RegionLcuuid - p.AZLcuuid = cloudItem.AZLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_REPLICA_SET_EN, p)) +type PodReplicaSetCollection struct { + collection[*PodReplicaSet, *metadbmodel.PodReplicaSet] } diff --git a/server/controller/recorder/cache/diffbase/pod_service.go b/server/controller/recorder/cache/diffbase/pod_service.go index 2acad53de11..9b4fa8d3802 100644 --- a/server/controller/recorder/cache/diffbase/pod_service.go +++ b/server/controller/recorder/cache/diffbase/pod_service.go @@ -17,99 +17,65 @@ package diffbase import ( - "sigs.k8s.io/yaml" - - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddPodService(dbItem *metadbmodel.PodService, seq int, toolDataSet *tool.DataSet) { - var podIngressLcuuid string - if dbItem.PodIngressID != 0 { - podIngressLcuuid, _ = toolDataSet.GetPodIngressLcuuidByID(dbItem.PodIngressID) - } - b.PodServices[dbItem.Lcuuid] = &PodService{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - Label: dbItem.Label, - Annotation: dbItem.Annotation, - Selector: dbItem.Selector, - ExternalIP: dbItem.ExternalIP, - ServiceClusterIP: dbItem.ServiceClusterIP, - Metadata: string(dbItem.Metadata), - MetadataHash: dbItem.MetadataHash, - Spec: string(dbItem.Spec), - SpecHash: dbItem.SpecHash, - PodIngressLcuuid: podIngressLcuuid, - RegionLcuuid: dbItem.Region, - AZLcuuid: dbItem.AZ, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_SERVICE_EN, b.PodServices[dbItem.Lcuuid].ToLoggable()), b.metadata.LogPrefixes) -} - -func (b *DataSet) DeletePodService(lcuuid string) { - delete(b.PodServices, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_SERVICE_EN, lcuuid), b.metadata.LogPrefixes) +type PodService struct { + ResourceBase + Name string + Label string + Annotation string + Selector string + ExternalIP string + ServiceClusterIP string + Metadata string + MetadataHash string + Spec string + SpecHash string + PodIngressLcuuid string + RegionLcuuid string + AZLcuuid string + SubDomainLcuuid string } -type PodService struct { - DiffBase - Name string `json:"name"` - Label string `json:"label"` - Annotation string `json:"annotation"` - Selector string `json:"selector"` - ExternalIP string `json:"external_ip"` - ServiceClusterIP string `json:"service_cluster_ip"` - Metadata string `json:"metadata"` - MetadataHash string `json:"metadata_hash"` - Spec string `json:"spec"` - SpecHash string `json:"spec_hash"` - PodIngressLcuuid string `json:"pod_ingress_lcuuid"` - RegionLcuuid string `json:"region_lcuuid"` - AZLcuuid string `json:"az_lcuuid"` - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func (a *PodService) reset(dbItem *metadbmodel.PodService, tool *tool.Tool) { + a.Name = dbItem.Name + a.Label = dbItem.Label + a.Annotation = dbItem.Annotation + a.Selector = dbItem.Selector + a.ExternalIP = dbItem.ExternalIP + a.ServiceClusterIP = dbItem.ServiceClusterIP + a.Metadata = string(dbItem.Metadata) + a.MetadataHash = dbItem.MetadataHash + a.Spec = string(dbItem.Spec) + a.SpecHash = dbItem.SpecHash + a.PodIngressLcuuid = tool.PodIngress().GetByID(dbItem.PodIngressID).Lcuuid() + a.RegionLcuuid = dbItem.Region + a.AZLcuuid = dbItem.AZ + a.SubDomainLcuuid = dbItem.SubDomain } -// ToLoggable converts PodService to a loggable format, excluding fields Spec and Metadata -func (p PodService) ToLoggable() interface{} { - copied := p +// ToLoggable converts PodService to a loggable format, excluding sensitive fields +func (a PodService) ToLoggable() interface{} { + copied := a copied.Metadata = "**HIDDEN**" copied.Spec = "**HIDDEN**" return copied } -func (p *PodService) Update(cloudItem *cloudmodel.PodService, toolDataSet *tool.DataSet) { - p.Name = cloudItem.Name - p.Label = cloudItem.Label - p.Annotation = cloudItem.Annotation - p.Selector = cloudItem.Selector - p.ExternalIP = cloudItem.ExternalIP - p.ServiceClusterIP = cloudItem.ServiceClusterIP - - yamlMetadata, err := yaml.JSONToYAML([]byte(cloudItem.Metadata)) - if err != nil { - log.Errorf("failed to convert JSON metadata: %v to YAML: %s", cloudItem.Metadata, toolDataSet.GetMetadata().LogPrefixes) - return - } - p.Metadata = string(yamlMetadata) - p.MetadataHash = cloudItem.MetadataHash - - yamlSpec, err := yaml.JSONToYAML([]byte(cloudItem.Spec)) - if err != nil { - log.Errorf("failed to convert JSON spec: %v to YAML: %s", cloudItem.Spec, toolDataSet.GetMetadata().LogPrefixes) - return - } - p.Spec = string(yamlSpec) - p.SpecHash = cloudItem.SpecHash +func NewPodServiceCollection(t *tool.Tool) *PodServiceCollection { + c := new(PodServiceCollection) + c.collection = newCollectionBuilder[*PodService](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_SERVICE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodService { return new(metadbmodel.PodService) }). + withCacheItemFactory(func() *PodService { return new(PodService) }). + build() + return c +} - p.PodIngressLcuuid = cloudItem.PodIngressLcuuid - p.RegionLcuuid = cloudItem.RegionLcuuid - p.AZLcuuid = cloudItem.AZLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_SERVICE_EN, p.ToLoggable())) +type PodServiceCollection struct { + collection[*PodService, *metadbmodel.PodService] } diff --git a/server/controller/recorder/cache/diffbase/pod_service_port.go b/server/controller/recorder/cache/diffbase/pod_service_port.go index 5ae60c8979f..257da750996 100644 --- a/server/controller/recorder/cache/diffbase/pod_service_port.go +++ b/server/controller/recorder/cache/diffbase/pod_service_port.go @@ -17,35 +17,33 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddPodServicePort(dbItem *metadbmodel.PodServicePort, seq int) { - b.PodServicePorts[dbItem.Lcuuid] = &PodServicePort{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_SERVICE_PORT_EN, b.PodServicePorts[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type PodServicePort struct { + ResourceBase + Name string + SubDomainLcuuid string } -func (b *DataSet) DeletePodServicePort(lcuuid string) { - delete(b.PodServicePorts, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_SERVICE_PORT_EN, lcuuid), b.metadata.LogPrefixes) +func (a *PodServicePort) reset(dbItem *metadbmodel.PodServicePort, tool *tool.Tool) { + a.Name = dbItem.Name + a.SubDomainLcuuid = dbItem.SubDomain } -type PodServicePort struct { - DiffBase - Name string `json:"name"` - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func NewPodServicePortCollection(t *tool.Tool) *PodServicePortCollection { + c := new(PodServicePortCollection) + c.collection = newCollectionBuilder[*PodServicePort](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_SERVICE_PORT_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodServicePort { return new(metadbmodel.PodServicePort) }). + withCacheItemFactory(func() *PodServicePort { return new(PodServicePort) }). + build() + return c } -func (p *PodServicePort) Update(cloudItem *cloudmodel.PodServicePort) { - p.Name = cloudItem.Name - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_POD_SERVICE_PORT_EN, p)) +type PodServicePortCollection struct { + collection[*PodServicePort, *metadbmodel.PodServicePort] } diff --git a/server/controller/recorder/cache/diffbase/pod_service_test.go b/server/controller/recorder/cache/diffbase/pod_service_test.go index 7b6bb8a7625..b5d73a79bc8 100644 --- a/server/controller/recorder/cache/diffbase/pod_service_test.go +++ b/server/controller/recorder/cache/diffbase/pod_service_test.go @@ -17,7 +17,7 @@ func TestPodServiceExternalIPSync(t *testing.T) { } // 创建 mock 工具数据集 - toolDataSet := &tool.DataSet{} + tool := &tool.Tool{} // 测试场景1:AddPodService 应该正确设置 ExternalIP dbItem := &metadbmodel.PodService{ @@ -29,7 +29,7 @@ func TestPodServiceExternalIPSync(t *testing.T) { ServiceClusterIP: "10.0.0.1", } - dataset.AddPodService(dbItem, 1, toolDataSet) + dataset.AddPodService(dbItem, 1, tool) // 验证 ExternalIP 被正确设置 podService := dataset.PodServices["test-lcuuid-1"] @@ -47,7 +47,7 @@ func TestPodServiceExternalIPSync(t *testing.T) { Spec: "{}", } - podService.Update(cloudItem, toolDataSet) + podService.Update(cloudItem, tool) // 验证 ExternalIP 被正确更新 if podService.ExternalIP != "192.168.1.200" { @@ -56,7 +56,7 @@ func TestPodServiceExternalIPSync(t *testing.T) { // 测试场景3:空字符串 ExternalIP cloudItem.ExternalIP = "" - podService.Update(cloudItem, toolDataSet) + podService.Update(cloudItem, tool) if podService.ExternalIP != "" { t.Errorf("Expected ExternalIP to be empty string, got '%s'", podService.ExternalIP) diff --git a/server/controller/recorder/cache/diffbase/process.go b/server/controller/recorder/cache/diffbase/process.go index 91c7afa7c99..e5ced30206d 100644 --- a/server/controller/recorder/cache/diffbase/process.go +++ b/server/controller/recorder/cache/diffbase/process.go @@ -17,49 +17,39 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddProcess(dbItem *metadbmodel.Process, seq int) { - b.Process[dbItem.Lcuuid] = &Process{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - OSAPPTags: dbItem.OSAPPTags, - ContainerID: dbItem.ContainerID, - DeviceType: dbItem.DeviceType, - DeviceID: dbItem.DeviceID, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_PROCESS_EN, b.Process[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type Process struct { + ResourceBase + Name string + OSAPPTags string + ContainerID string + DeviceType int + DeviceID int } -func (b *DataSet) DeleteProcess(lcuuid string) { - delete(b.Process, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_PROCESS_EN, lcuuid), b.metadata.LogPrefixes) +func (a *Process) reset(dbItem *metadbmodel.Process, tool *tool.Tool) { + a.Name = dbItem.Name + a.OSAPPTags = dbItem.OSAPPTags + a.ContainerID = dbItem.ContainerID + a.DeviceType = dbItem.DeviceType + a.DeviceID = dbItem.DeviceID } -type Process struct { - DiffBase - Name string `json:"name"` - OSAPPTags string `json:"os_app_tags"` - ContainerID string `json:"container_id"` - DeviceType int `json:"device_type"` - DeviceID int `json:"device_id"` +func NewProcessCollection(t *tool.Tool) *ProcessCollection { + c := new(ProcessCollection) + c.collection = newCollectionBuilder[*Process](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_PROCESS_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.Process { return new(metadbmodel.Process) }). + withCacheItemFactory(func() *Process { return new(Process) }). + build() + return c } -func (p *Process) Update(cloudItem *cloudmodel.Process, toolDataSet *tool.DataSet) { - p.Name = cloudItem.Name - p.OSAPPTags = cloudItem.OSAPPTags - p.ContainerID = cloudItem.ContainerID - deviceType, deviceID := toolDataSet.GetProcessDeviceTypeAndID(cloudItem.ContainerID, cloudItem.VTapID) - if p.DeviceType != deviceType || p.DeviceID != deviceID { - p.DeviceType = deviceType - p.DeviceID = deviceID - } - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_PROCESS_EN, p)) +type ProcessCollection struct { + collection[*Process, *metadbmodel.Process] } diff --git a/server/controller/recorder/cache/diffbase/rds_instance.go b/server/controller/recorder/cache/diffbase/rds_instance.go index 40b3f8f3a4d..7c3f8c19a1d 100644 --- a/server/controller/recorder/cache/diffbase/rds_instance.go +++ b/server/controller/recorder/cache/diffbase/rds_instance.go @@ -17,50 +17,43 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddRDSInstance(dbItem *metadbmodel.RDSInstance, seq int) { - b.RDSInstances[dbItem.Lcuuid] = &RDSInstance{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - State: dbItem.State, - Series: dbItem.Series, - Model: dbItem.Model, - RegionLcuuid: dbItem.Region, - AZLcuuid: dbItem.AZ, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_RDS_INSTANCE_EN, b.RDSInstances[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type RDSInstance struct { + ResourceBase + Name string + State int + Series int + Model int + VPCLcuuid string + RegionLcuuid string + AZLcuuid string } -func (b *DataSet) DeleteRDSInstance(lcuuid string) { - delete(b.RDSInstances, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_RDS_INSTANCE_EN, lcuuid), b.metadata.LogPrefixes) +func (a *RDSInstance) reset(dbItem *metadbmodel.RDSInstance, tool *tool.Tool) { + a.Name = dbItem.Name + a.State = dbItem.State + a.Series = dbItem.Series + a.Model = dbItem.Model + a.VPCLcuuid = tool.VPC().GetByID(dbItem.VPCID).Lcuuid() + a.RegionLcuuid = dbItem.Region + a.AZLcuuid = dbItem.AZ } -type RDSInstance struct { - DiffBase - Name string `json:"name"` - State int `json:"state"` - Series int `json:"series"` - Model int `json:"model"` - VPCLcuuid string `json:"vpc_lcuuid"` - RegionLcuuid string `json:"region_lcuuid"` - AZLcuuid string `json:"az_lcuuid"` +func NewRDSInstanceCollection(t *tool.Tool) *RDSInstanceCollection { + c := new(RDSInstanceCollection) + c.collection = newCollectionBuilder[*RDSInstance](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_RDS_INSTANCE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.RDSInstance { return new(metadbmodel.RDSInstance) }). + withCacheItemFactory(func() *RDSInstance { return new(RDSInstance) }). + build() + return c } -func (r *RDSInstance) Update(cloudItem *cloudmodel.RDSInstance) { - r.Name = cloudItem.Name - r.State = cloudItem.State - r.Series = cloudItem.Series - r.Model = cloudItem.Model - r.VPCLcuuid = cloudItem.VPCLcuuid - r.RegionLcuuid = cloudItem.RegionLcuuid - r.AZLcuuid = cloudItem.AZLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_RDS_INSTANCE_EN, r)) +type RDSInstanceCollection struct { + collection[*RDSInstance, *metadbmodel.RDSInstance] } diff --git a/server/controller/recorder/cache/diffbase/redis_instance.go b/server/controller/recorder/cache/diffbase/redis_instance.go index ba0e7a83785..3a77ffe829c 100644 --- a/server/controller/recorder/cache/diffbase/redis_instance.go +++ b/server/controller/recorder/cache/diffbase/redis_instance.go @@ -17,45 +17,39 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddRedisInstance(dbItem *metadbmodel.RedisInstance, seq int) { - b.RedisInstances[dbItem.Lcuuid] = &RedisInstance{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - State: dbItem.State, - PublicHost: dbItem.PublicHost, - RegionLcuuid: dbItem.Region, - AZLcuuid: dbItem.AZ, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_REDIS_INSTANCE_EN, b.RedisInstances[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type RedisInstance struct { + ResourceBase + Name string + State int + PublicHost string + RegionLcuuid string + AZLcuuid string } -func (b *DataSet) DeleteRedisInstance(lcuuid string) { - delete(b.RedisInstances, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_REDIS_INSTANCE_EN, lcuuid), b.metadata.LogPrefixes) +func (a *RedisInstance) reset(dbItem *metadbmodel.RedisInstance, tool *tool.Tool) { + a.Name = dbItem.Name + a.State = dbItem.State + a.PublicHost = dbItem.PublicHost + a.RegionLcuuid = dbItem.Region + a.AZLcuuid = dbItem.AZ } -type RedisInstance struct { - DiffBase - Name string `json:"name"` - State int `json:"state"` - PublicHost string `json:"public_host"` - RegionLcuuid string `json:"region_lcuuid"` - AZLcuuid string `json:"az_lcuuid"` +func NewRedisInstanceCollection(t *tool.Tool) *RedisInstanceCollection { + c := new(RedisInstanceCollection) + c.collection = newCollectionBuilder[*RedisInstance](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_REDIS_INSTANCE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.RedisInstance { return new(metadbmodel.RedisInstance) }). + withCacheItemFactory(func() *RedisInstance { return new(RedisInstance) }). + build() + return c } -func (r *RedisInstance) Update(cloudItem *cloudmodel.RedisInstance) { - r.Name = cloudItem.Name - r.State = cloudItem.State - r.PublicHost = cloudItem.PublicHost - r.RegionLcuuid = cloudItem.RegionLcuuid - r.AZLcuuid = cloudItem.AZLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_REDIS_INSTANCE_EN, r)) +type RedisInstanceCollection struct { + collection[*RedisInstance, *metadbmodel.RedisInstance] } diff --git a/server/controller/recorder/cache/diffbase/region.go b/server/controller/recorder/cache/diffbase/region.go index 05baa9f78d2..2fbaf815ab1 100644 --- a/server/controller/recorder/cache/diffbase/region.go +++ b/server/controller/recorder/cache/diffbase/region.go @@ -17,36 +17,33 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddRegion(dbItem *metadbmodel.Region, seq int) { - b.Regions[dbItem.Lcuuid] = &Region{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - Label: dbItem.Label, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_REGION_EN, b.Regions[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type Region struct { + ResourceBase + Name string + Label string } -func (b *DataSet) DeleteRegion(lcuuid string) { - delete(b.Regions, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_REGION_EN, lcuuid), b.metadata.LogPrefixes) +func (a *Region) reset(dbItem *metadbmodel.Region, tool *tool.Tool) { + a.Name = dbItem.Name + a.Label = dbItem.Label } -type Region struct { - DiffBase - Name string `json:"name"` - Label string `json:"label"` +func NewRegionCollection(t *tool.Tool) *RegionCollection { + c := new(RegionCollection) + c.collection = newCollectionBuilder[*Region](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_REGION_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.Region { return new(metadbmodel.Region) }). + withCacheItemFactory(func() *Region { return new(Region) }). + build() + return c } -func (r *Region) Update(cloudItem *cloudmodel.Region) { - r.Name = cloudItem.Name - r.Label = cloudItem.Label - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_REGION_EN, r)) +type RegionCollection struct { + collection[*Region, *metadbmodel.Region] } diff --git a/server/controller/recorder/cache/diffbase/routing_table.go b/server/controller/recorder/cache/diffbase/routing_table.go index bf46a251973..eda87a84ab0 100644 --- a/server/controller/recorder/cache/diffbase/routing_table.go +++ b/server/controller/recorder/cache/diffbase/routing_table.go @@ -17,39 +17,35 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddRoutingTable(dbItem *metadbmodel.RoutingTable, seq int) { - b.RoutingTables[dbItem.Lcuuid] = &RoutingTable{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Destination: dbItem.Destination, - Nexthop: dbItem.Nexthop, - NexthopType: dbItem.NexthopType, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_ROUTING_TABLE_EN, b.RoutingTables[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type RoutingTable struct { + ResourceBase + Destination string + Nexthop string + NexthopType string } -func (b *DataSet) DeleteRoutingTable(lcuuid string) { - delete(b.RoutingTables, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_ROUTING_TABLE_EN, lcuuid), b.metadata.LogPrefixes) +func (a *RoutingTable) reset(dbItem *metadbmodel.RoutingTable, tool *tool.Tool) { + a.Destination = dbItem.Destination + a.Nexthop = dbItem.Nexthop + a.NexthopType = dbItem.NexthopType } -type RoutingTable struct { - DiffBase - Destination string `json:"destination"` - Nexthop string `json:"nexthop"` - NexthopType string `json:"nexthop_type"` +func NewRoutingTableCollection(t *tool.Tool) *RoutingTableCollection { + c := new(RoutingTableCollection) + c.collection = newCollectionBuilder[*RoutingTable](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_ROUTING_TABLE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.RoutingTable { return new(metadbmodel.RoutingTable) }). + withCacheItemFactory(func() *RoutingTable { return new(RoutingTable) }). + build() + return c } -func (r *RoutingTable) Update(cloudItem *cloudmodel.RoutingTable) { - r.Destination = cloudItem.Destination - r.Nexthop = cloudItem.Nexthop - r.NexthopType = cloudItem.NexthopType - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_ROUTING_TABLE_EN, r)) +type RoutingTableCollection struct { + collection[*RoutingTable, *metadbmodel.RoutingTable] } diff --git a/server/controller/recorder/cache/diffbase/sub_domain.go b/server/controller/recorder/cache/diffbase/sub_domain.go index a00e411f441..fcdff58458d 100644 --- a/server/controller/recorder/cache/diffbase/sub_domain.go +++ b/server/controller/recorder/cache/diffbase/sub_domain.go @@ -17,33 +17,31 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddSubDomain(dbItem *metadbmodel.SubDomain, seq int) { - b.SubDomains[dbItem.Lcuuid] = &SubDomain{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_SUB_DOMAIN_EN, b.SubDomains[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type SubDomain struct { + ResourceBase + Name string } -func (b *DataSet) DeleteSubDomain(lcuuid string) { - delete(b.SubDomains, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_SUB_DOMAIN_EN, lcuuid), b.metadata.LogPrefixes) +func (a *SubDomain) reset(dbItem *metadbmodel.SubDomain, tool *tool.Tool) { + a.Name = dbItem.Name } -type SubDomain struct { - DiffBase - Name string `json:"name"` +func NewSubDomainCollection(t *tool.Tool) *SubDomainCollection { + c := new(SubDomainCollection) + c.collection = newCollectionBuilder[*SubDomain](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_SUB_DOMAIN_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.SubDomain { return new(metadbmodel.SubDomain) }). + withCacheItemFactory(func() *SubDomain { return new(SubDomain) }). + build() + return c } -func (s *SubDomain) Update(cloudItem *cloudmodel.SubDomain) { - s.Name = cloudItem.Name - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_SUB_DOMAIN_EN, s)) +type SubDomainCollection struct { + collection[*SubDomain, *metadbmodel.SubDomain] } diff --git a/server/controller/recorder/cache/diffbase/subnet.go b/server/controller/recorder/cache/diffbase/subnet.go index 20cb4dfc7e7..99bd2ffca0b 100644 --- a/server/controller/recorder/cache/diffbase/subnet.go +++ b/server/controller/recorder/cache/diffbase/subnet.go @@ -17,38 +17,35 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddSubnet(dbItem *metadbmodel.Subnet, seq int) { - b.Subnets[dbItem.Lcuuid] = &Subnet{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - Label: dbItem.Label, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_SUBNET_EN, b.Subnets[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type Subnet struct { + ResourceBase + Name string + Label string + SubDomainLcuuid string } -func (b *DataSet) DeleteSubnet(lcuuid string) { - delete(b.Subnets, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_SUBNET_EN, lcuuid), b.metadata.LogPrefixes) +func (a *Subnet) reset(dbItem *metadbmodel.Subnet, tool *tool.Tool) { + a.Name = dbItem.Name + a.Label = dbItem.Label + a.SubDomainLcuuid = dbItem.SubDomain } -type Subnet struct { - DiffBase - Name string `json:"name"` - Label string `json:"label"` - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func NewSubnetCollection(t *tool.Tool) *SubnetCollection { + c := new(SubnetCollection) + c.collection = newCollectionBuilder[*Subnet](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_SUBNET_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.Subnet { return new(metadbmodel.Subnet) }). + withCacheItemFactory(func() *Subnet { return new(Subnet) }). + build() + return c } -func (s *Subnet) Update(cloudItem *cloudmodel.Subnet) { - s.Name = cloudItem.Name - s.Label = cloudItem.Label - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_SUBNET_EN, s)) +type SubnetCollection struct { + collection[*Subnet, *metadbmodel.Subnet] } diff --git a/server/controller/recorder/cache/diffbase/vinterface.go b/server/controller/recorder/cache/diffbase/vinterface.go index 29b70d9ca90..039f70f94d1 100644 --- a/server/controller/recorder/cache/diffbase/vinterface.go +++ b/server/controller/recorder/cache/diffbase/vinterface.go @@ -17,77 +17,51 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddVInterface(dbItem *metadbmodel.VInterface, seq int, toolDataSet *tool.DataSet) { - var networkLcuuid string - if dbItem.NetworkID != 0 { - networkLcuuid, _ = toolDataSet.GetNetworkLcuuidByID(dbItem.NetworkID) - } - var deviceLcuuid string - if dbItem.DeviceID != 0 { - deviceLcuuid, _ = toolDataSet.GetDeviceLcuuidByID(dbItem.DeviceType, dbItem.DeviceID) - } - var vpcID int - if dbItem.DeviceType != ctrlrcommon.VIF_DEVICE_TYPE_HOST { - vpcID, _ = toolDataSet.GetDeviceVPCIDByID(dbItem.DeviceType, dbItem.DeviceID) - } - if vpcID == 0 { - vpcID, _ = toolDataSet.GetNetworkVPCIDByID(dbItem.NetworkID) - } - b.VInterfaces[dbItem.Lcuuid] = &VInterface{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - Type: dbItem.Type, - VtapID: dbItem.VtapID, - NetnsID: dbItem.NetnsID, - TapMac: dbItem.TapMac, - VPCID: dbItem.VPCID, - DeviceType: dbItem.DeviceType, - DeviceLcuuid: deviceLcuuid, - NetworkLcuuid: networkLcuuid, - RegionLcuuid: dbItem.Region, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_VINTERFACE_EN, b.VInterfaces[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type VInterface struct { + ResourceBase + Name string + Type int + TapMac string + NetnsID uint32 + VtapID uint32 + VPCID int + DeviceType int + DeviceLcuuid string + NetworkLcuuid string + RegionLcuuid string + SubDomainLcuuid string } -func (b *DataSet) DeleteVInterface(lcuuid string) { - delete(b.VInterfaces, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_VINTERFACE_EN, lcuuid), b.metadata.LogPrefixes) +func (a *VInterface) reset(dbItem *metadbmodel.VInterface, tool *tool.Tool) { + a.Name = dbItem.Name + a.Type = dbItem.Type + a.TapMac = dbItem.TapMac + a.NetnsID = dbItem.NetnsID + a.VtapID = dbItem.VtapID + a.VPCID = dbItem.VPCID + a.DeviceType = dbItem.DeviceType + a.DeviceLcuuid = tool.Device().GetByLcuuid(dbItem.DeviceLcuuid).ID() + a.NetworkLcuuid = tool.Network().GetByID(dbItem.NetworkID).Lcuuid() + a.RegionLcuuid = dbItem.Region + a.SubDomainLcuuid = dbItem.SubDomain } -type VInterface struct { - DiffBase - Name string `json:"name"` - Type int `json:"type"` - TapMac string `json:"tap_mac"` - NetnsID uint32 `json:"netns_id"` - VtapID uint32 `json:"vtap_id"` - VPCID int `json:"vpc_id"` - DeviceType int `json:"device_type"` - DeviceLcuuid string `json:"device_lcuuid"` - NetworkLcuuid string `json:"network_lcuuid"` - RegionLcuuid string `json:"region_lcuuid"` - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func NewVInterfaceCollection(t *tool.Tool) *VInterfaceCollection { + c := new(VInterfaceCollection) + c.collection = newCollectionBuilder[*VInterface](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_VINTERFACE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.VInterface { return new(metadbmodel.VInterface) }). + withCacheItemFactory(func() *VInterface { return new(VInterface) }). + build() + return c } -func (v *VInterface) Update(cloudItem *cloudmodel.VInterface) { - v.Name = cloudItem.Name - v.Type = cloudItem.Type - v.TapMac = cloudItem.TapMac - v.NetnsID = cloudItem.NetnsID - v.VtapID = cloudItem.VTapID - v.VPCID = cloudItem.VPCID - v.DeviceLcuuid = cloudItem.DeviceLcuuid - v.NetworkLcuuid = cloudItem.NetworkLcuuid - v.RegionLcuuid = cloudItem.RegionLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_VINTERFACE_EN, v)) +type VInterfaceCollection struct { + collection[*VInterface, *metadbmodel.VInterface] } diff --git a/server/controller/recorder/cache/diffbase/vip.go b/server/controller/recorder/cache/diffbase/vip.go index d85181b7646..d526580b480 100644 --- a/server/controller/recorder/cache/diffbase/vip.go +++ b/server/controller/recorder/cache/diffbase/vip.go @@ -17,36 +17,33 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddVIP(dbItem *metadbmodel.VIP, seq int) { - b.VIP[dbItem.Lcuuid] = &VIP{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - IP: dbItem.IP, - VTapID: dbItem.VTapID, - } - log.Info(addDiffBase(ctrlrcommon.RESOURCE_TYPE_VIP_EN, b.VIP[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type VIP struct { + ResourceBase + IP string + VTapID uint32 } -func (b *DataSet) DeleteVIP(lcuuid string) { - delete(b.VIP, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_VIP_EN, lcuuid), b.metadata.LogPrefixes) +func (a *VIP) reset(dbItem *metadbmodel.VIP, tool *tool.Tool) { + a.IP = dbItem.IP + a.VTapID = dbItem.VTapID } -type VIP struct { - DiffBase - IP string `json:"ip" binding:"required"` - VTapID uint32 `json:"vtap_id" binding:"required"` +func NewVIPCollection(t *tool.Tool) *VIPCollection { + c := new(VIPCollection) + c.collection = newCollectionBuilder[*VIP](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_VIP_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.VIP { return new(metadbmodel.VIP) }). + withCacheItemFactory(func() *VIP { return new(VIP) }). + build() + return c } -func (p *VIP) Update(cloudItem *cloudmodel.VIP) { - p.IP = cloudItem.IP - p.VTapID = cloudItem.VTapID - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_VIP_EN, p)) +type VIPCollection struct { + collection[*VIP, *metadbmodel.VIP] } diff --git a/server/controller/recorder/cache/diffbase/vm.go b/server/controller/recorder/cache/diffbase/vm.go index 7401878285f..c401bc363a6 100644 --- a/server/controller/recorder/cache/diffbase/vm.go +++ b/server/controller/recorder/cache/diffbase/vm.go @@ -17,78 +17,55 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddVM(dbItem *metadbmodel.VM, seq int, toolDataSet *tool.DataSet) { - vpcLcuuid, _ := toolDataSet.GetVPCLcuuidByID(dbItem.VPCID) - networkLcuuid, _ := toolDataSet.GetNetworkLcuuidByID(dbItem.NetworkID) - newItem := &VM{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - Label: dbItem.Label, - IP: dbItem.IP, - Hostname: dbItem.Hostname, - VPCLcuuid: vpcLcuuid, - State: dbItem.State, - HType: dbItem.HType, - LaunchServer: dbItem.LaunchServer, - HostID: dbItem.HostID, - RegionLcuuid: dbItem.Region, - AZLcuuid: dbItem.AZ, - LearnedCloudTags: dbItem.LearnedCloudTags, - NetworkLcuuid: networkLcuuid, - } - b.VMs[dbItem.Lcuuid] = newItem - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_VM_EN, b.VMs[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type VM struct { + ResourceBase + Name string + Label string + IP string + Hostname string + State int + HType int + LaunchServer string + VPCLcuuid string + RegionLcuuid string + AZLcuuid string + LearnedCloudTags map[string]string + NetworkLcuuid string + HostID int } -func (b *DataSet) DeleteVM(lcuuid string) { - delete(b.VMs, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_VM_EN, lcuuid), b.metadata.LogPrefixes) +func (a *VM) reset(dbItem *metadbmodel.VM, tool *tool.Tool) { + a.Name = dbItem.Name + a.Label = dbItem.Label + a.IP = dbItem.IP + a.Hostname = dbItem.Hostname + a.State = dbItem.State + a.HType = dbItem.HType + a.LaunchServer = dbItem.LaunchServer + a.VPCLcuuid = tool.VPC().GetByID(dbItem.VPCID).Lcuuid() + a.RegionLcuuid = dbItem.Region + a.AZLcuuid = dbItem.AZ + a.LearnedCloudTags = dbItem.LearnedCloudTags + a.NetworkLcuuid = tool.Network().GetByID(dbItem.NetworkID).Lcuuid() + a.HostID = tool.Host().GetByIP(dbItem.LaunchServer).ID() } -type VM struct { - DiffBase - Name string `json:"name"` - Label string `json:"label"` - IP string `json:"ip"` - Hostname string `json:"hostname"` - State int `json:"state"` - HType int `json:"htype"` - LaunchServer string `json:"launch_server"` - HostID int `json:"host_id"` - VPCLcuuid string `json:"vpc_lcuuid"` - RegionLcuuid string `json:"region_lcuuid"` - AZLcuuid string `json:"az_lcuuid"` - LearnedCloudTags map[string]string `json:"learned_cloud_tags"` - NetworkLcuuid string `json:"network_lcuuid"` +func NewVMCollection(t *tool.Tool) *VMCollection { + c := new(VMCollection) + c.collection = newCollectionBuilder[*VM](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_VM_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.VM { return new(metadbmodel.VM) }). + withCacheItemFactory(func() *VM { return new(VM) }). + build() + return c } -func (v *VM) Update(cloudItem *cloudmodel.VM, toolDataSet *tool.DataSet) { - v.Name = cloudItem.Name - v.Label = cloudItem.Label - v.IP = cloudItem.IP - v.Hostname = cloudItem.Hostname - v.State = cloudItem.State - v.HType = cloudItem.HType - v.LaunchServer = cloudItem.LaunchServer - v.VPCLcuuid = cloudItem.VPCLcuuid - v.RegionLcuuid = cloudItem.RegionLcuuid - v.AZLcuuid = cloudItem.AZLcuuid - v.LearnedCloudTags = cloudItem.CloudTags - v.NetworkLcuuid = cloudItem.NetworkLcuuid - if cloudItem.LaunchServer != "" { - hostID, exists := toolDataSet.GetHostIDByIP(cloudItem.LaunchServer) - if exists { - v.HostID = hostID - } - } - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_VM_EN, v)) +type VMCollection struct { + collection[*VM, *metadbmodel.VM] } diff --git a/server/controller/recorder/cache/diffbase/vm_pod_node_connection.go b/server/controller/recorder/cache/diffbase/vm_pod_node_connection.go index 6457a7289ef..91531733a3d 100644 --- a/server/controller/recorder/cache/diffbase/vm_pod_node_connection.go +++ b/server/controller/recorder/cache/diffbase/vm_pod_node_connection.go @@ -19,25 +19,29 @@ package diffbase import ( ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddVMPodNodeConnection(dbItem *metadbmodel.VMPodNodeConnection, seq int) { - b.VMPodNodeConnections[dbItem.Lcuuid] = &VMPodNodeConnection{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - SubDomainLcuuid: dbItem.SubDomain, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_VM_POD_NODE_CONNECTION_EN, b.VMPodNodeConnections[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type VMPodNodeConnection struct { + ResourceBase + SubDomainLcuuid string } -func (b *DataSet) DeleteVMPodNodeConnection(lcuuid string) { - delete(b.VMPodNodeConnections, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_VM_POD_NODE_CONNECTION_EN, lcuuid), b.metadata.LogPrefixes) +func (a *VMPodNodeConnection) reset(dbItem *metadbmodel.VMPodNodeConnection, tool *tool.Tool) { + a.SubDomainLcuuid = dbItem.SubDomain } -type VMPodNodeConnection struct { - DiffBase - SubDomainLcuuid string `json:"sub_domain_lcuuid"` +func NewVMPodNodeConnectionCollection(t *tool.Tool) *VMPodNodeConnectionCollection { + c := new(VMPodNodeConnectionCollection) + c.collection = newCollectionBuilder[*VMPodNodeConnection](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_VM_POD_NODE_CONNECTION_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.VMPodNodeConnection { return new(metadbmodel.VMPodNodeConnection) }). + withCacheItemFactory(func() *VMPodNodeConnection { return new(VMPodNodeConnection) }). + build() + return c +} + +type VMPodNodeConnectionCollection struct { + collection[*VMPodNodeConnection, *metadbmodel.VMPodNodeConnection] } diff --git a/server/controller/recorder/cache/diffbase/vpc.go b/server/controller/recorder/cache/diffbase/vpc.go index 989d2a09597..501c63056c5 100644 --- a/server/controller/recorder/cache/diffbase/vpc.go +++ b/server/controller/recorder/cache/diffbase/vpc.go @@ -17,48 +17,41 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddVPC(dbItem *metadbmodel.VPC, seq int) { - b.VPCs[dbItem.Lcuuid] = &VPC{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - Label: dbItem.Label, - Owner: dbItem.Owner, - TunnelID: dbItem.TunnelID, - CIDR: dbItem.CIDR, - RegionLcuuid: dbItem.Region, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_VPC_EN, b.VPCs[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type VPC struct { + ResourceBase + Name string + Label string + TunnelID int + CIDR string + RegionLcuuid string + Owner string } -func (b *DataSet) DeleteVPC(lcuuid string) { - delete(b.VPCs, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_VPC_EN, lcuuid), b.metadata.LogPrefixes) +func (a *VPC) reset(dbItem *metadbmodel.VPC, tool *tool.Tool) { + a.Name = dbItem.Name + a.Label = dbItem.Label + a.TunnelID = dbItem.TunnelID + a.CIDR = dbItem.CIDR + a.RegionLcuuid = dbItem.Region + a.Owner = dbItem.Owner } -type VPC struct { - DiffBase - Name string `json:"name"` - Label string `json:"label"` - Owner string `json:"owner"` - TunnelID int `json:"tunnel_id"` - CIDR string `json:"cidr"` - RegionLcuuid string `json:"region_lcuuid"` +func NewVPCCollection(t *tool.Tool) *VPCCollection { + c := new(VPCCollection) + c.collection = newCollectionBuilder[*VPC](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_VPC_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.VPC { return new(metadbmodel.VPC) }). + withCacheItemFactory(func() *VPC { return new(VPC) }). + build() + return c } -func (v *VPC) Update(cloudItem *cloudmodel.VPC) { - v.Name = cloudItem.Name - v.Label = cloudItem.Label - v.Owner = cloudItem.Owner - v.TunnelID = cloudItem.TunnelID - v.CIDR = cloudItem.CIDR - v.RegionLcuuid = cloudItem.RegionLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_VPC_EN, v)) +type VPCCollection struct { + collection[*VPC, *metadbmodel.VPC] } diff --git a/server/controller/recorder/cache/diffbase/vrouter.go b/server/controller/recorder/cache/diffbase/vrouter.go index 0505b7c5bbe..7b94278fc00 100644 --- a/server/controller/recorder/cache/diffbase/vrouter.go +++ b/server/controller/recorder/cache/diffbase/vrouter.go @@ -17,44 +17,37 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddVRouter(dbItem *metadbmodel.VRouter, seq int, toolDataSet *tool.DataSet) { - vpcLcuuid, _ := toolDataSet.GetVPCLcuuidByID(dbItem.VPCID) - b.VRouters[dbItem.Lcuuid] = &VRouter{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - Name: dbItem.Name, - Label: dbItem.Label, - VPCLcuuid: vpcLcuuid, - RegionLcuuid: dbItem.Region, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_VROUTER_EN, b.VRouters[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type VRouter struct { + ResourceBase + Name string + Label string + VPCLcuuid string + RegionLcuuid string } -func (b *DataSet) DeleteVRouter(lcuuid string) { - delete(b.VRouters, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_VROUTER_EN, lcuuid), b.metadata.LogPrefixes) +func (a *VRouter) reset(dbItem *metadbmodel.VRouter, tool *tool.Tool) { + a.Name = dbItem.Name + a.Label = dbItem.Label + a.VPCLcuuid = tool.VPC().GetByID(dbItem.VPCID).Lcuuid() + a.RegionLcuuid = dbItem.Region } -type VRouter struct { - DiffBase - Name string `json:"name"` - Label string `json:"label"` - VPCLcuuid string `json:"vpc_lcuuid"` - RegionLcuuid string `json:"region_lcuuid"` +func NewVRouterCollection(t *tool.Tool) *VRouterCollection { + c := new(VRouterCollection) + c.collection = newCollectionBuilder[*VRouter](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_VROUTER_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.VRouter { return new(metadbmodel.VRouter) }). + withCacheItemFactory(func() *VRouter { return new(VRouter) }). + build() + return c } -func (v *VRouter) Update(cloudItem *cloudmodel.VRouter) { - v.Name = cloudItem.Name - v.Label = cloudItem.Label - v.VPCLcuuid = cloudItem.VPCLcuuid - v.RegionLcuuid = cloudItem.RegionLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_VROUTER_EN, v)) +type VRouterCollection struct { + collection[*VRouter, *metadbmodel.VRouter] } diff --git a/server/controller/recorder/cache/diffbase/wan_ip.go b/server/controller/recorder/cache/diffbase/wan_ip.go index 456bcf36ea6..4738d9e2cd6 100644 --- a/server/controller/recorder/cache/diffbase/wan_ip.go +++ b/server/controller/recorder/cache/diffbase/wan_ip.go @@ -17,45 +17,33 @@ package diffbase import ( - cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model" ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" ) -func (b *DataSet) AddWANIP(dbItem *metadbmodel.WANIP, seq int, toolDataSet *tool.DataSet) { - // ip subnet id is not used in the current version, so it is commented out to avoid updating the subnet id too frequently, - // which may cause recorder performance issues. - // var subnetLcuuid string - // if dbItem.SubnetID != 0 { - // subnetLcuuid, _ = toolDataSet.GetSubnetLcuuidByID(dbItem.SubnetID) - // } - b.WANIPs[dbItem.Lcuuid] = &WANIP{ - DiffBase: DiffBase{ - Sequence: seq, - Lcuuid: dbItem.Lcuuid, - }, - RegionLcuuid: dbItem.Region, - SubDomainLcuuid: dbItem.SubDomain, - // SubnetLcuuid: subnetLcuuid, - } - b.GetLogFunc()(addDiffBase(ctrlrcommon.RESOURCE_TYPE_WAN_IP_EN, b.WANIPs[dbItem.Lcuuid]), b.metadata.LogPrefixes) +type WANIP struct { + ResourceBase + RegionLcuuid string + SubDomainLcuuid string } -func (b *DataSet) DeleteWANIP(lcuuid string) { - delete(b.WANIPs, lcuuid) - log.Info(deleteDiffBase(ctrlrcommon.RESOURCE_TYPE_WAN_IP_EN, lcuuid), b.metadata.LogPrefixes) +func (a *WANIP) reset(dbItem *metadbmodel.WANIP, tool *tool.Tool) { + a.RegionLcuuid = dbItem.Region + a.SubDomainLcuuid = dbItem.SubDomain } -type WANIP struct { - DiffBase - RegionLcuuid string `json:"region_lcuuid"` - SubDomainLcuuid string `json:"sub_domain_lcuuid"` - SubnetLcuuid string `json:"subnet_lcuuid"` +func NewWANIPCollection(t *tool.Tool) *WANIPCollection { + c := new(WANIPCollection) + c.collection = newCollectionBuilder[*WANIP](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_WAN_IP_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.WANIP { return new(metadbmodel.WANIP) }). + withCacheItemFactory(func() *WANIP { return new(WANIP) }). + build() + return c } -func (w *WANIP) Update(cloudItem *cloudmodel.IP) { - w.RegionLcuuid = cloudItem.RegionLcuuid - // w.SubnetLcuuid = cloudItem.SubnetLcuuid - log.Info(updateDiffBase(ctrlrcommon.RESOURCE_TYPE_WAN_IP_EN, w)) +type WANIPCollection struct { + collection[*WANIP, *metadbmodel.WANIP] } diff --git a/server/controller/recorder/cache/tool/az.go b/server/controller/recorder/cache/tool/az.go new file mode 100644 index 00000000000..8a84cd4dc0e --- /dev/null +++ b/server/controller/recorder/cache/tool/az.go @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// AZ defines cache data structure. +type AZ struct { + lcuuid string + id int + name string +} + +func (t *AZ) IsValid() bool { + return t.lcuuid != "" +} + +func (t *AZ) Lcuuid() string { + return t.lcuuid +} + +func (t *AZ) ID() int { + return t.id +} + +func (t *AZ) Name() string { + return t.name +} + +func (t *AZ) reset(dbItem *metadbmodel.AZ, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name +} + +func NewAZCollection(t *Tool) *AZCollection { + c := new(AZCollection) + c.collection = newCollectionBuilder[*AZ](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_AZ_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.AZ { return new(metadbmodel.AZ) }). + withCacheItemFactory(func() *AZ { return new(AZ) }). + build() + return c +} + +// AZCollection defines a collection that maps individual fields to the AZ cache data structure. +type AZCollection struct { + collection[*AZ, *metadbmodel.AZ] +} diff --git a/server/controller/recorder/cache/tool/az_test.go b/server/controller/recorder/cache/tool/az_test.go new file mode 100644 index 00000000000..bc9276d87f6 --- /dev/null +++ b/server/controller/recorder/cache/tool/az_test.go @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + "testing" + + "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/common" + "github.com/stretchr/testify/assert" +) + +func TestAZ(t *testing.T) { + dbItem := &model.AZ{ + Base: model.Base{ + ID: 1, + Lcuuid: "test_lcuuid", + }, + Name: "test_name", + } + + t.Run("reset", func(t *testing.T) { + a := &AZ{} + a.reset(dbItem, nil) + assert.Equal(t, dbItem.Lcuuid, a.Lcuuid()) + assert.Equal(t, dbItem.ID, a.ID()) + assert.Equal(t, "", a.Name()) + assert.True(t, a.IsValid()) + }) + + t.Run("invalid", func(t *testing.T) { + a := &AZ{} + assert.False(t, a.IsValid()) + }) +} + +func TestNewAZCollection(t *testing.T) { + assert.NotNil(t, NewAZCollection(nil)) +} + +func TestAZCollection(t *testing.T) { + tool := NewTool(&common.Metadata{}) + c := NewAZCollection(tool) + dbItem := &model.AZ{ + Base: model.Base{ + ID: 1, + Lcuuid: "test_lcuuid", + }, + Name: "test_name", + } + + t.Run("add and get", func(t *testing.T) { + c.Add(dbItem) + item := c.GetByLcuuid(dbItem.Lcuuid) + assert.Equal(t, dbItem.Lcuuid, item.Lcuuid()) + assert.Equal(t, dbItem.ID, item.ID()) + + itemByID := c.GetByID(dbItem.ID) + assert.Equal(t, dbItem.Lcuuid, itemByID.Lcuuid()) + assert.Equal(t, dbItem.ID, itemByID.ID()) + }) + + t.Run("update", func(t *testing.T) { + dbItem.Name = "updated_name" + c.Update(dbItem) + item := c.GetByLcuuid(dbItem.Lcuuid) + assert.Equal(t, dbItem.Name, item.Name()) + }) + + t.Run("delete", func(t *testing.T) { + c.Delete(dbItem) + item := c.GetByLcuuid(dbItem.Lcuuid) + assert.False(t, item.IsValid()) + + itemByID := c.GetByID(dbItem.ID) + assert.False(t, itemByID.IsValid()) + }) + + t.Run("get non-existent", func(t *testing.T) { + item := c.GetByLcuuid("non-existent") + assert.False(t, item.IsValid()) + + itemByID := c.GetByID(999) + assert.False(t, itemByID.IsValid()) + }) +} diff --git a/server/controller/recorder/cache/tool/collection.go b/server/controller/recorder/cache/tool/collection.go new file mode 100644 index 00000000000..f2aadc729c1 --- /dev/null +++ b/server/controller/recorder/cache/tool/collection.go @@ -0,0 +1,232 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +const ( + logActionAdd = iota + logActionUpdate + logActionDelete + + hookerAfterAdd = iota + hookerAfterUpdate + hookerAfterDelete +) + +// CacheItem defines cache object must implement the interface +type CacheItem[D DBItem] interface { + ID() int + Lcuuid() string + IsValid() bool // check whether the item is valid + + reset(dbItem D, tool *Tool) // reset the item with dbItem +} + +// DBItem defines database object must implement the interface +type DBItem interface { + GetID() int + GetLcuuid() string +} + +// CollectionExtender allows specific collections to extend basic operations +type CollectionExtender[T CacheItem[D], D DBItem] interface { + OnAfterAdd(item T, dbItem D) + OnAfterUpdate(item T, dbItem D) + OnAfterDelete(item T, dbItem D) +} + +// collectionBuilder use builder pattern to build collection +type collectionBuilder[T CacheItem[D], D DBItem] struct { + collection[T, D] +} + +// newCollectionBuilder +func newCollectionBuilder[T CacheItem[D], D DBItem]() *collectionBuilder[T, D] { + return &collectionBuilder[T, D]{} +} + +// withResourceType 设置资源类型 +func (b *collectionBuilder[T, D]) withResourceType(resourceType string) *collectionBuilder[T, D] { + b.resourceType = resourceType + return b +} + +func (b *collectionBuilder[T, D]) withTool(tool *Tool) *collectionBuilder[T, D] { + b.tool = tool + return b +} + +// withExtender 设置扩展器 +func (b *collectionBuilder[T, D]) withExtender(extender CollectionExtender[T, D]) *collectionBuilder[T, D] { + b.extender = extender + return b +} + +// withDBItemFactory 设置数据库对象工厂函数 +func (b *collectionBuilder[T, D]) withDBItemFactory(factory func() D) *collectionBuilder[T, D] { + b.dbItemFactory = factory + return b +} + +// withCacheItemFactory 设置缓存对象工厂函数 +func (b *collectionBuilder[T, D]) withCacheItemFactory(factory func() T) *collectionBuilder[T, D] { + b.cacheItemFactory = factory + return b +} + +// build 构建集合 +func (b *collectionBuilder[T, D]) build() collection[T, D] { + return collection[T, D]{ + resourceType: b.resourceType, + tool: b.tool, + extender: b.extender, + dbItemFactory: b.dbItemFactory, + cacheItemFactory: b.cacheItemFactory, + + lcuuidToItem: make(map[string]T), + idToItem: make(map[int]T), + } +} + +// 优化后的 Collection +type collection[T CacheItem[D], D DBItem] struct { + resourceType string + tool *Tool + + extender CollectionExtender[T, D] + dbItemFactory func() D + cacheItemFactory func() T + + lcuuidToItem map[string]T + idToItem map[int]T +} + +func (oc *collection[T, D]) Add(dbItem D) { + item := oc.cacheItemFactory() + item.reset(dbItem, oc.tool) + oc.lcuuidToItem[dbItem.GetLcuuid()] = item + oc.idToItem[dbItem.GetID()] = item + if oc.extender != nil { + oc.extender.OnAfterAdd(item, dbItem) + } + oc.tool.GetLogFunc()(addToToolMap(oc.resourceType, dbItem.GetLcuuid()), oc.tool.metadata.LogPrefixes) +} + +func (oc *collection[T, D]) Update(dbItem D) { + if existingItem, ok := oc.idToItem[dbItem.GetID()]; ok { + existingItem.reset(dbItem, oc.tool) + if oc.extender != nil { + oc.extender.OnAfterUpdate(existingItem, dbItem) + } + oc.tool.GetLogFunc()(updateToolMap(oc.resourceType, dbItem.GetLcuuid()), oc.tool.metadata.LogPrefixes) + return + } + // 如果缓存中不存在,则添加 + oc.Add(dbItem) +} + +func (oc *collection[T, D]) Delete(dbItem D) { + item, exists := oc.idToItem[dbItem.GetID()] + delete(oc.lcuuidToItem, dbItem.GetLcuuid()) + delete(oc.idToItem, dbItem.GetID()) + if exists && oc.extender != nil { + oc.extender.OnAfterDelete(item, dbItem) + } + oc.tool.GetLogFunc()(deleteFromToolMap(oc.resourceType, dbItem.GetLcuuid()), oc.tool.metadata.LogPrefixes) +} + +// GetByLcuuid returns the item by lcuuid. +// If not found, returns the zero value of T.T can be checked whether it is valid by IsValid() method. +func (oc *collection[T, D]) GetByLcuuid(lcuuid string) T { + empty := oc.cacheItemFactory() + if lcuuid == "" { + return empty + } + if item, ok := oc.lcuuidToItem[lcuuid]; ok { + return item + } + return empty +} + +// GetOrLoadByLcuuid first tries to get the item from the cache, if it is not in the cache, +// it queries from the database, adds it to the cache and returns it. +func (oc *collection[T, D]) GetOrLoadByLcuuid(lcuuid string) T { + empty := oc.cacheItemFactory() + if lcuuid == "" { + return empty + } + + if item, ok := oc.lcuuidToItem[lcuuid]; ok { + return item + } + + if oc.dbItemFactory == nil { + log.Warningf("dbItemFactory is nil, cannot load %s (lcuuid: %s) from db", oc.resourceType, lcuuid) + return empty + } + log.Warning(collectionNotFoundByLcuuid(oc.resourceType, lcuuid), oc.tool.metadata.LogPrefixes) + + dbItem := oc.dbItemFactory() + result := oc.tool.metadata.GetDB().Where("lcuuid = ?", lcuuid).First(dbItem) + if result.Error != nil { + log.Error(dbResourceByLcuuidNotFound(oc.resourceType, lcuuid), oc.tool.metadata.LogPrefixes) + return empty + } + + oc.Add(dbItem) + return oc.lcuuidToItem[lcuuid] +} + +// GetByID returns the item by ID. +// If not found, returns the zero value of T.T can be checked whether it is valid by IsValid() method. +func (oc *collection[T, D]) GetByID(id int) T { + empty := oc.cacheItemFactory() + if id == 0 { + return empty + } + if item, ok := oc.idToItem[id]; ok { + return item + } + return empty +} + +// GetOrLoadByID first tries to get the item from the cache, if it is not in the cache, +// it queries from the database, adds it to the cache and returns it. +func (oc *collection[T, D]) GetOrLoadByID(id int) T { + empty := oc.cacheItemFactory() + if id == 0 { + return empty + } + if item, ok := oc.idToItem[id]; ok { + return item + } + + if oc.dbItemFactory == nil { + log.Warningf("dbItemFactory is nil, cannot load %s (id: %d) from db", oc.resourceType, id) + return empty + } + log.Warning(collectionNotFoundByID(oc.resourceType, id), oc.tool.metadata.LogPrefixes) + + dbItem := oc.dbItemFactory() + result := oc.tool.metadata.GetDB().Where("id = ?", id).First(dbItem) + if result.Error != nil { + log.Error(dbResourceByIDNotFound(oc.resourceType, id), oc.tool.metadata.LogPrefixes) + return empty + } + + oc.Add(dbItem) + return oc.idToItem[id] +} diff --git a/server/controller/recorder/cache/tool/config_map.go b/server/controller/recorder/cache/tool/config_map.go new file mode 100644 index 00000000000..fb5a6be39f9 --- /dev/null +++ b/server/controller/recorder/cache/tool/config_map.go @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + mapset "github.com/deckarep/golang-set/v2" + + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// ConfigMap defines cache data structure. +type ConfigMap struct { + lcuuid string + id int + name string + podGroupIDs mapset.Set[int] // data source: pod_group_config_map_connection +} + +func (t *ConfigMap) IsValid() bool { + return t.lcuuid != "" +} + +func (t *ConfigMap) Lcuuid() string { + return t.lcuuid +} + +func (t *ConfigMap) ID() int { + return t.id +} + +func (t *ConfigMap) Name() string { + return t.name +} + +func (t *ConfigMap) PodGroupIDs() mapset.Set[int] { + return t.podGroupIDs +} + +func (t *ConfigMap) PodGroupIDsToSlice() []int { + return t.podGroupIDs.ToSlice() +} + +func (t *ConfigMap) AddPodGroupID(id int) { + t.podGroupIDs.Add(id) +} + +func (t *ConfigMap) RemovePodGroupID(id int) { + t.podGroupIDs.Remove(id) +} + +func (t *ConfigMap) reset(dbItem *metadbmodel.ConfigMap, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.podGroupIDs = mapset.NewSet[int]() +} + +func NewConfigMapCollection(t *Tool) *ConfigMapCollection { + c := new(ConfigMapCollection) + c.collection = newCollectionBuilder[*ConfigMap](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_CONFIG_MAP_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.ConfigMap { return new(metadbmodel.ConfigMap) }). + withCacheItemFactory(func() *ConfigMap { return new(ConfigMap) }). + build() + return c +} + +// ConfigMapCollection defines a collection that maps individual fields to the ConfigMap cache data structure. +type ConfigMapCollection struct { + collection[*ConfigMap, *metadbmodel.ConfigMap] +} diff --git a/server/controller/recorder/cache/tool/data_set.go b/server/controller/recorder/cache/tool/data_set.go index 5ffdaca5732..35a2ed9c955 100644 --- a/server/controller/recorder/cache/tool/data_set.go +++ b/server/controller/recorder/cache/tool/data_set.go @@ -133,13 +133,6 @@ type DataSet struct { processGIDToSoftDeletedCount map[uint32]uint } -type ProcessIdentifier struct { - Name string - PodGroupID int - VTapID uint32 - CommandLine string -} - func NewDataSet(md *rcommon.Metadata) *DataSet { return &DataSet{ metadata: md, diff --git a/server/controller/recorder/cache/tool/dhcp_port.go b/server/controller/recorder/cache/tool/dhcp_port.go new file mode 100644 index 00000000000..8cba38bb121 --- /dev/null +++ b/server/controller/recorder/cache/tool/dhcp_port.go @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// DHCPPort defines cache data structure. +type DHCPPort struct { + lcuuid string + id int + name string + regionID int + azID int + vpcID int +} + +func (t *DHCPPort) IsValid() bool { + return t.lcuuid != "" +} + +func (t *DHCPPort) Lcuuid() string { + return t.lcuuid +} + +func (t *DHCPPort) ID() int { + return t.id +} + +func (t *DHCPPort) Name() string { + return t.name +} + +func (t *DHCPPort) Region() int { + return t.regionID +} + +func (t *DHCPPort) AZ() int { + return t.azID +} + +func (t *DHCPPort) VPCID() int { + return t.vpcID +} + +func (t *DHCPPort) reset(dbItem *metadbmodel.DHCPPort, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.azID = tool.AZ().GetByLcuuid(dbItem.AZ).ID() + t.vpcID = dbItem.VPCID +} + +func NewDHCPPortCollection(t *Tool) *DHCPPortCollection { + c := new(DHCPPortCollection) + c.collection = newCollectionBuilder[*DHCPPort](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_DHCP_PORT_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.DHCPPort { return new(metadbmodel.DHCPPort) }). + withCacheItemFactory(func() *DHCPPort { return new(DHCPPort) }). + build() + return c +} + +// DHCPPortCollection defines a collection that maps individual fields to the DHCPPort cache data structure. +type DHCPPortCollection struct { + collection[*DHCPPort, *metadbmodel.DHCPPort] +} diff --git a/server/controller/recorder/cache/tool/host.go b/server/controller/recorder/cache/tool/host.go new file mode 100644 index 00000000000..c22311a5a35 --- /dev/null +++ b/server/controller/recorder/cache/tool/host.go @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// Host defines cache data structure. +type Host struct { + lcuuid string + id int + name string + regionID int + azID int + ip string +} + +func (t *Host) IsValid() bool { + return t.lcuuid != "" +} + +func (t *Host) Lcuuid() string { + return t.lcuuid +} + +func (t *Host) ID() int { + return t.id +} + +func (t *Host) Name() string { + return t.name +} + +func (t *Host) Region() int { + return t.regionID +} + +func (t *Host) AZ() int { + return t.azID +} + +func (t *Host) IP() string { + return t.ip +} + +func (t *Host) reset(dbItem *metadbmodel.Host, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.azID = tool.AZ().GetByLcuuid(dbItem.AZ).ID() + t.ip = dbItem.IP +} + +func NewHostCollection(t *Tool) *HostCollection { + c := new(HostCollection) + c.ipToItem = make(map[string]*Host) + c.collection = newCollectionBuilder[*Host](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_HOST_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.Host { return new(metadbmodel.Host) }). + withCacheItemFactory(func() *Host { return new(Host) }). + withExtender(c). + build() + return c +} + +// HostCollection defines a collection that maps individual fields to the Host cache data structure. +type HostCollection struct { + collection[*Host, *metadbmodel.Host] + ipToItem map[string]*Host +} + +// OnAfterAdd implements CollectionExtender interface +func (c *HostCollection) OnAfterAdd(item *Host, dbItem *metadbmodel.Host) { + if item.IP() != "" { + c.ipToItem[item.IP()] = item + } +} + +// OnAfterUpdate implements CollectionExtender interface +func (c *HostCollection) OnAfterUpdate(item *Host, dbItem *metadbmodel.Host) { + // Remove old ip mapping if exists + for ip, hostItem := range c.ipToItem { + if hostItem == item && ip != item.IP() { + delete(c.ipToItem, ip) + break + } + } + // Add new ip mapping + if item.IP() != "" { + c.ipToItem[item.IP()] = item + } +} + +// OnAfterDelete implements CollectionExtender interface +func (c *HostCollection) OnAfterDelete(item *Host, dbItem *metadbmodel.Host) { + if item.IP() != "" { + delete(c.ipToItem, item.IP()) + } +} + +// GetOrLoadByIP returns the Host by its ip, loading from DB if not found in cache. +func (c *HostCollection) GetOrLoadByIP(ip string) *Host { + if ip == "" { + return new(Host) + } + + item, ok := c.ipToItem[ip] + if ok { + return item + } + log.Warning("cache %s (ip: %s) not found", c.resourceType, ip) + + var dbItem *metadbmodel.Host + if result := c.tool.metadata.GetDB().Where("ip = ?", ip).First(&dbItem); result.RowsAffected == 1 { + c.Add(dbItem) + return c.ipToItem[ip] + } else { + log.Error("db %s (ip: %s) not found", c.resourceType, ip) + return new(Host) + } +} diff --git a/server/controller/recorder/cache/tool/lan_ip.go b/server/controller/recorder/cache/tool/lan_ip.go new file mode 100644 index 00000000000..addbfeabff9 --- /dev/null +++ b/server/controller/recorder/cache/tool/lan_ip.go @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// LANIP defines cache data structure. +type LANIP struct { + lcuuid string + id int + ip string + vInterfaceID int +} + +func (t *LANIP) IsValid() bool { + return t.lcuuid != "" +} + +func (t *LANIP) Lcuuid() string { + return t.lcuuid +} + +func (t *LANIP) ID() int { + return t.id +} + +func (t *LANIP) IP() string { + return t.ip +} + +func (t *LANIP) VInterfaceID() int { + return t.vInterfaceID +} + +func (t *LANIP) reset(dbItem *metadbmodel.LANIP, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.ip = dbItem.IP + t.vInterfaceID = dbItem.VInterfaceID +} + +func NewLANIPCollection(t *Tool) *LANIPCollection { + c := new(LANIPCollection) + c.resetExt() + c.collection = newCollectionBuilder[*LANIP](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_LAN_IP_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.LANIP { return new(metadbmodel.LANIP) }). + withCacheItemFactory(func() *LANIP { return new(LANIP) }). + withExtender(c). + build() + return c +} + +// LANIPCollection defines a collection that maps individual fields to the LANIP cache data structure. +type LANIPCollection struct { + collection[*LANIP, *metadbmodel.LANIP] + LANIPCollectionExt +} diff --git a/server/controller/recorder/cache/tool/lan_ip_ext.go b/server/controller/recorder/cache/tool/lan_ip_ext.go new file mode 100644 index 00000000000..db64e98ada1 --- /dev/null +++ b/server/controller/recorder/cache/tool/lan_ip_ext.go @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +type LANIPCollectionExt struct { + vInterfaceIDToItems map[int][]*LANIP // 一对多映射 +} + +func (c *LANIPCollection) resetExt() { + c.vInterfaceIDToItems = make(map[int][]*LANIP) +} + +// GetByVInterfaceID returns all LANIP items with the specified vInterfaceID +func (c *LANIPCollection) GetByVInterfaceID(vInterfaceID int) []*LANIP { + return c.vInterfaceIDToItems[vInterfaceID] +} + +// OnAfterAdd implements CollectionExtender interface for one-to-many mapping +func (c *LANIPCollection) OnAfterAdd(item *LANIP, dbItem *metadbmodel.LANIP) { + c.vInterfaceIDToItems[item.VInterfaceID()] = append(c.vInterfaceIDToItems[item.VInterfaceID()], item) +} + +// OnAfterUpdate implements CollectionExtender interface +func (c *LANIPCollection) OnAfterUpdate(item *LANIP, dbItem *metadbmodel.LANIP) { + // Remove from old mapping if vInterfaceID changed + for vInterfaceID, items := range c.vInterfaceIDToItems { + for i, lanipItem := range items { + if lanipItem == item && vInterfaceID != item.VInterfaceID() { + // Remove from old vInterfaceID group + c.vInterfaceIDToItems[vInterfaceID] = append(items[:i], items[i+1:]...) + break + } + } + } + + // Add to new mapping + newVInterfaceID := item.VInterfaceID() + found := false + for _, existingItem := range c.vInterfaceIDToItems[newVInterfaceID] { + if existingItem == item { + found = true + break + } + } + if !found { + c.vInterfaceIDToItems[newVInterfaceID] = append(c.vInterfaceIDToItems[newVInterfaceID], item) + } +} + +// OnAfterDelete implements CollectionExtender interface +func (c *LANIPCollection) OnAfterDelete(item *LANIP, dbItem *metadbmodel.LANIP) { + vInterfaceID := item.VInterfaceID() + items := c.vInterfaceIDToItems[vInterfaceID] + + for i, lanipItem := range items { + if lanipItem.ID() == item.ID() { + c.vInterfaceIDToItems[vInterfaceID] = append(items[:i], items[i+1:]...) + break + } + } +} diff --git a/server/controller/recorder/cache/tool/lb.go b/server/controller/recorder/cache/tool/lb.go new file mode 100644 index 00000000000..1bd6cb464d6 --- /dev/null +++ b/server/controller/recorder/cache/tool/lb.go @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// LB defines cache data structure. +type LB struct { + lcuuid string + id int + name string + regionID int + vpcID int +} + +func (t *LB) IsValid() bool { + return t.lcuuid != "" +} + +func (t *LB) Lcuuid() string { + return t.lcuuid +} + +func (t *LB) ID() int { + return t.id +} + +func (t *LB) Name() string { + return t.name +} + +func (t *LB) RegionID() int { + return t.regionID +} + +func (t *LB) VPCID() int { + return t.vpcID +} + +func (t *LB) reset(dbItem *metadbmodel.LB, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.vpcID = dbItem.VPCID +} + +func NewLBCollection(t *Tool) *LBCollection { + c := new(LBCollection) + c.collection = newCollectionBuilder[*LB](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_LB_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.LB { return new(metadbmodel.LB) }). + withCacheItemFactory(func() *LB { return new(LB) }). + build() + return c +} + +// LBCollection defines a collection that maps individual fields to the LB cache data structure. +type LBCollection struct { + collection[*LB, *metadbmodel.LB] +} diff --git a/server/controller/recorder/cache/tool/lb_listener.go b/server/controller/recorder/cache/tool/lb_listener.go new file mode 100644 index 00000000000..be998249ef1 --- /dev/null +++ b/server/controller/recorder/cache/tool/lb_listener.go @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// LBListener defines cache data structure. +type LBListener struct { + lcuuid string + id int + name string + lbID int +} + +func (t *LBListener) IsValid() bool { + return t.lcuuid != "" +} + +func (t *LBListener) Lcuuid() string { + return t.lcuuid +} + +func (t *LBListener) ID() int { + return t.id +} + +func (t *LBListener) Name() string { + return t.name +} + +func (t *LBListener) LBID() int { + return t.lbID +} + +func (t *LBListener) reset(dbItem *metadbmodel.LBListener, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.lbID = dbItem.LBID +} + +func NewLBListenerCollection(t *Tool) *LBListenerCollection { + c := new(LBListenerCollection) + c.collection = newCollectionBuilder[*LBListener](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_LB_LISTENER_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.LBListener { return new(metadbmodel.LBListener) }). + withCacheItemFactory(func() *LBListener { return new(LBListener) }). + build() + return c +} + +// LBListenerCollection defines a collection that maps individual fields to the LBListener cache data structure. +type LBListenerCollection struct { + collection[*LBListener, *metadbmodel.LBListener] +} diff --git a/server/controller/recorder/cache/tool/logger.go b/server/controller/recorder/cache/tool/logger.go index 5a7da8236db..96949e04ab0 100644 --- a/server/controller/recorder/cache/tool/logger.go +++ b/server/controller/recorder/cache/tool/logger.go @@ -87,6 +87,14 @@ func cacheLaunchServerByIDNotFound(resource string, id int) string { return fmt.Sprintf("cache %s launch server (id: %d) not found", resource, id) } +func collectionNotFoundByLcuuid(resource, lcuuid string) string { + return fmt.Sprintf("cache %s (lcuuid: %s) not found", resource, lcuuid) +} + +func collectionNotFoundByID(resource string, id int) string { + return fmt.Sprintf("cache %s (id: %d) not found", resource, id) +} + type LogController struct { // controll log level, set info when triggered by resource change event, set debug when called by timing cache refresh level logging.Level diff --git a/server/controller/recorder/cache/tool/nat_gateway.go b/server/controller/recorder/cache/tool/nat_gateway.go new file mode 100644 index 00000000000..fb18955c913 --- /dev/null +++ b/server/controller/recorder/cache/tool/nat_gateway.go @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// NATGateway defines cache data structure. +type NATGateway struct { + lcuuid string + id int + name string + regionID int + azID int + vpcID int +} + +func (t *NATGateway) IsValid() bool { + return t.lcuuid != "" +} + +func (t *NATGateway) Lcuuid() string { + return t.lcuuid +} + +func (t *NATGateway) ID() int { + return t.id +} + +func (t *NATGateway) Name() string { + return t.name +} + +func (t *NATGateway) RegionID() int { + return t.regionID +} + +func (t *NATGateway) AZID() int { + return t.azID +} + +func (t *NATGateway) VPCID() int { + return t.vpcID +} + +func (t *NATGateway) reset(dbItem *metadbmodel.NATGateway, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.azID = tool.AZ().GetByLcuuid(dbItem.AZ).ID() + t.vpcID = dbItem.VPCID +} + +func NewNATGatewayCollection(t *Tool) *NATGatewayCollection { + c := new(NATGatewayCollection) + c.collection = newCollectionBuilder[*NATGateway](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_NAT_GATEWAY_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.NATGateway { return new(metadbmodel.NATGateway) }). + withCacheItemFactory(func() *NATGateway { return new(NATGateway) }). + build() + return c +} + +// NATGatewayCollection defines a collection that maps individual fields to the NATGateway cache data structure. +type NATGatewayCollection struct { + collection[*NATGateway, *metadbmodel.NATGateway] +} diff --git a/server/controller/recorder/cache/tool/network.go b/server/controller/recorder/cache/tool/network.go new file mode 100644 index 00000000000..1fd87e10d73 --- /dev/null +++ b/server/controller/recorder/cache/tool/network.go @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// Network defines cache data structure. +type Network struct { + lcuuid string + id int + name string + vpcID int +} + +func (t *Network) IsValid() bool { + return t.lcuuid != "" +} + +func (t *Network) Lcuuid() string { + return t.lcuuid +} + +func (t *Network) ID() int { + return t.id +} + +func (t *Network) Name() string { + return t.name +} + +func (t *Network) VPCID() int { + return t.vpcID +} + +func (t *Network) reset(dbItem *metadbmodel.Network, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.vpcID = dbItem.VPCID +} + +func NewNetworkCollection(t *Tool) *NetworkCollection { + c := new(NetworkCollection) + c.collection = newCollectionBuilder[*Network](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_NETWORK_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.Network { return new(metadbmodel.Network) }). + withCacheItemFactory(func() *Network { return new(Network) }). + build() + return c +} + +// NetworkCollection defines a collection that maps individual fields to the Network cache data structure. +type NetworkCollection struct { + collection[*Network, *metadbmodel.Network] +} diff --git a/server/controller/recorder/cache/tool/pod.go b/server/controller/recorder/cache/tool/pod.go new file mode 100644 index 00000000000..ec9f0879a58 --- /dev/null +++ b/server/controller/recorder/cache/tool/pod.go @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// Pod defines cache data structure. +type Pod struct { + lcuuid string + id int + domainLcuuid string + name string + regionID int + azID int + vpcID int + podClusterID int + podNamespaceID int + podGroupID int + podNodeID int +} + +func (t *Pod) IsValid() bool { + return t.lcuuid != "" +} + +func (t *Pod) Lcuuid() string { + return t.lcuuid +} + +func (t *Pod) ID() int { + return t.id +} + +func (t *Pod) DomainLcuuid() string { + return t.domainLcuuid +} + +func (t *Pod) Name() string { + return t.name +} + +func (t *Pod) RegionID() int { + return t.regionID +} + +func (t *Pod) AZID() int { + return t.azID +} + +func (t *Pod) VPCID() int { + return t.vpcID +} + +func (t *Pod) PodClusterID() int { + return t.podClusterID +} + +func (t *Pod) PodNamespaceID() int { + return t.podNamespaceID +} + +func (t *Pod) PodGroupID() int { + return t.podGroupID +} + +func (t *Pod) PodNodeID() int { + return t.podNodeID +} + +func (t *Pod) reset(dbItem *metadbmodel.Pod, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.domainLcuuid = dbItem.Domain + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.azID = tool.AZ().GetByLcuuid(dbItem.AZ).ID() + t.vpcID = dbItem.VPCID + t.podClusterID = dbItem.PodClusterID + t.podNamespaceID = dbItem.PodNamespaceID + t.podGroupID = dbItem.PodGroupID + t.podNodeID = dbItem.PodNodeID +} + +func NewPodCollection(t *Tool) *PodCollection { + c := new(PodCollection) + c.resetExt() + c.collection = newCollectionBuilder[*Pod](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.Pod { return new(metadbmodel.Pod) }). + withCacheItemFactory(func() *Pod { return new(Pod) }). + withExtender(c). + build() + return c +} + +// PodCollection defines a collection that maps individual fields to the Pod cache data structure. +type PodCollection struct { + collection[*Pod, *metadbmodel.Pod] + PodCollectionExt +} diff --git a/server/controller/recorder/cache/tool/pod_cluster.go b/server/controller/recorder/cache/tool/pod_cluster.go new file mode 100644 index 00000000000..8caad3a04b5 --- /dev/null +++ b/server/controller/recorder/cache/tool/pod_cluster.go @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// PodCluster defines cache data structure. +type PodCluster struct { + lcuuid string + id int + name string + regionID int + azID int + vpcID int +} + +func (t *PodCluster) IsValid() bool { + return t.lcuuid != "" +} + +func (t *PodCluster) Lcuuid() string { + return t.lcuuid +} + +func (t *PodCluster) ID() int { + return t.id +} + +func (t *PodCluster) Name() string { + return t.name +} + +func (t *PodCluster) RegionID() int { + return t.regionID +} + +func (t *PodCluster) AZID() int { + return t.azID +} + +func (t *PodCluster) VPCID() int { + return t.vpcID +} + +func (t *PodCluster) reset(dbItem *metadbmodel.PodCluster, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.azID = tool.AZ().GetByLcuuid(dbItem.AZ).ID() + t.vpcID = dbItem.VPCID +} + +func NewPodClusterCollection(t *Tool) *PodClusterCollection { + c := new(PodClusterCollection) + c.collection = newCollectionBuilder[*PodCluster](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_CLUSTER_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodCluster { return new(metadbmodel.PodCluster) }). + withCacheItemFactory(func() *PodCluster { return new(PodCluster) }). + build() + return c +} + +// PodClusterCollection defines a collection that maps individual fields to the PodCluster cache data structure. +type PodClusterCollection struct { + collection[*PodCluster, *metadbmodel.PodCluster] +} diff --git a/server/controller/recorder/cache/tool/pod_ext.go b/server/controller/recorder/cache/tool/pod_ext.go new file mode 100644 index 00000000000..632fda238f2 --- /dev/null +++ b/server/controller/recorder/cache/tool/pod_ext.go @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + "strings" + + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +type PodCollectionExt struct { + containerIDToPodID map[string]int +} + +func (c *PodCollection) resetExt() { + c.containerIDToPodID = make(map[string]int) +} + +// GetByContainerID returns the Pod by its containerID +func (c *PodCollection) GetByContainerID(containerID string) *Pod { + return c.GetByID(c.containerIDToPodID[containerID]) +} + +// OnAfterAdd implements CollectionExtender interface +// Maintains containerID to podID mapping when pod is added +func (c *PodCollection) OnAfterAdd(item *Pod, dbItem *metadbmodel.Pod) { + for _, containerID := range strings.Split(dbItem.ContainerIDs, ", ") { + if containerID != "" { + c.containerIDToPodID[containerID] = item.ID() + } + } +} + +// OnAfterUpdate implements CollectionExtender interface +func (c *PodCollection) OnAfterUpdate(item *Pod, dbItem *metadbmodel.Pod) { + // Remove old containerID mappings for this pod + for containerID, podID := range c.containerIDToPodID { + if podID == item.ID() { + delete(c.containerIDToPodID, containerID) + } + } + + // Add new containerID mappings + for _, containerID := range strings.Split(dbItem.ContainerIDs, ", ") { + if containerID != "" { + c.containerIDToPodID[containerID] = item.ID() + } + } +} + +// OnAfterDelete implements CollectionExtender interface +// Removes containerID mappings when pod is deleted +func (c *PodCollection) OnAfterDelete(item *Pod, dbItem *metadbmodel.Pod) { + for _, containerID := range strings.Split(dbItem.ContainerIDs, ", ") { + if containerID != "" { + delete(c.containerIDToPodID, containerID) + } + } +} diff --git a/server/controller/recorder/cache/tool/pod_group.go b/server/controller/recorder/cache/tool/pod_group.go new file mode 100644 index 00000000000..e419dd7efec --- /dev/null +++ b/server/controller/recorder/cache/tool/pod_group.go @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// PodGroup defines cache data structure. +type PodGroup struct { + lcuuid string + id int + name string + gType int + regionID int + azID int + podNamespaceID int + podClusterID int +} + +func (t *PodGroup) IsValid() bool { + return t.lcuuid != "" +} + +func (t *PodGroup) Lcuuid() string { + return t.lcuuid +} + +func (t *PodGroup) ID() int { + return t.id +} + +func (t *PodGroup) Name() string { + return t.name +} + +func (t *PodGroup) Type() int { + return t.gType +} + +func (t *PodGroup) RegionID() int { + return t.regionID +} + +func (t *PodGroup) AZID() int { + return t.azID +} + +func (t *PodGroup) PodNamespaceID() int { + return t.podNamespaceID +} + +func (t *PodGroup) PodClusterID() int { + return t.podClusterID +} + +func (t *PodGroup) reset(dbItem *metadbmodel.PodGroup, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.gType = dbItem.Type + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.azID = tool.AZ().GetByLcuuid(dbItem.AZ).ID() + t.podNamespaceID = dbItem.PodNamespaceID + t.podClusterID = dbItem.PodClusterID +} + +func NewPodGroupCollection(t *Tool) *PodGroupCollection { + c := new(PodGroupCollection) + c.collection = newCollectionBuilder[*PodGroup](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_GROUP_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodGroup { return new(metadbmodel.PodGroup) }). + withCacheItemFactory(func() *PodGroup { return new(PodGroup) }). + build() + return c +} + +// PodGroupCollection defines a collection that maps individual fields to the PodGroup cache data structure. +type PodGroupCollection struct { + collection[*PodGroup, *metadbmodel.PodGroup] +} diff --git a/server/controller/recorder/cache/tool/pod_group_config_map_connection.go b/server/controller/recorder/cache/tool/pod_group_config_map_connection.go new file mode 100644 index 00000000000..ff454fd4439 --- /dev/null +++ b/server/controller/recorder/cache/tool/pod_group_config_map_connection.go @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// PodGroupConfigMapConnection defines cache data structure. +type PodGroupConfigMapConnection struct { + lcuuid string + id int + configMapID int + podGroupID int +} + +func (t *PodGroupConfigMapConnection) IsValid() bool { + return t.lcuuid != "" +} + +func (t *PodGroupConfigMapConnection) Lcuuid() string { + return t.lcuuid +} + +func (t *PodGroupConfigMapConnection) ID() int { + return t.id +} + +func (t *PodGroupConfigMapConnection) ConfigMapID() int { + return t.configMapID +} + +func (t *PodGroupConfigMapConnection) PodGroupID() int { + return t.podGroupID +} + +func (t *PodGroupConfigMapConnection) reset(dbItem *metadbmodel.PodGroupConfigMapConnection, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.configMapID = dbItem.ConfigMapID + t.podGroupID = dbItem.PodGroupID +} + +func NewPodGroupConfigMapConnectionCollection(t *Tool) *PodGroupConfigMapConnectionCollection { + c := new(PodGroupConfigMapConnectionCollection) + c.collection = newCollectionBuilder[*PodGroupConfigMapConnection](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_GROUP_CONFIG_MAP_CONNECTION_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodGroupConfigMapConnection { return new(metadbmodel.PodGroupConfigMapConnection) }). + withCacheItemFactory(func() *PodGroupConfigMapConnection { return new(PodGroupConfigMapConnection) }). + build() + return c +} + +// PodGroupConfigMapConnectionCollection defines a collection that maps individual fields to the PodGroupConfigMapConnection cache data structure. +type PodGroupConfigMapConnectionCollection struct { + collection[*PodGroupConfigMapConnection, *metadbmodel.PodGroupConfigMapConnection] +} diff --git a/server/controller/recorder/cache/tool/pod_group_config_map_connection_ext.go b/server/controller/recorder/cache/tool/pod_group_config_map_connection_ext.go new file mode 100644 index 00000000000..2c692992d96 --- /dev/null +++ b/server/controller/recorder/cache/tool/pod_group_config_map_connection_ext.go @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// OnAfterAdd implements CollectionExtender interface +// Maintains ConfigMap's podGroupIDs when connection is added +func (c *PodGroupConfigMapConnectionCollection) OnAfterAdd(item *PodGroupConfigMapConnection, dbItem *metadbmodel.PodGroupConfigMapConnection) { + c.tool.ConfigMap().GetByID(item.ConfigMapID()).AddPodGroupID(item.PodGroupID()) +} + +// OnAfterUpdate implements CollectionExtender interface +func (c *PodGroupConfigMapConnectionCollection) OnAfterUpdate(item *PodGroupConfigMapConnection, dbItem *metadbmodel.PodGroupConfigMapConnection) { + // For connection tables, update is usually just add/delete operations + // No special logic needed for update +} + +// OnAfterDelete implements CollectionExtender interface +// Maintains ConfigMap's podGroupIDs when connection is deleted +func (c *PodGroupConfigMapConnectionCollection) OnAfterDelete(item *PodGroupConfigMapConnection, dbItem *metadbmodel.PodGroupConfigMapConnection) { + c.tool.ConfigMap().GetByID(item.ConfigMapID()).RemovePodGroupID(item.PodGroupID()) +} diff --git a/server/controller/recorder/cache/tool/pod_ingress.go b/server/controller/recorder/cache/tool/pod_ingress.go new file mode 100644 index 00000000000..8a1f5a619b6 --- /dev/null +++ b/server/controller/recorder/cache/tool/pod_ingress.go @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// PodIngress defines cache data structure. +type PodIngress struct { + lcuuid string + id int + name string + regionID int + azID int + podNamespaceID int + podClusterID int +} + +func (t *PodIngress) IsValid() bool { + return t.lcuuid != "" +} + +func (t *PodIngress) Lcuuid() string { + return t.lcuuid +} + +func (t *PodIngress) ID() int { + return t.id +} + +func (t *PodIngress) Name() string { + return t.name +} + +func (t *PodIngress) RegionID() int { + return t.regionID +} + +func (t *PodIngress) AZID() int { + return t.azID +} + +func (t *PodIngress) PodNamespaceID() int { + return t.podNamespaceID +} + +func (t *PodIngress) PodClusterID() int { + return t.podClusterID +} + +func (t *PodIngress) reset(dbItem *metadbmodel.PodIngress, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.azID = tool.AZ().GetByLcuuid(dbItem.AZ).ID() + t.podNamespaceID = dbItem.PodNamespaceID + t.podClusterID = dbItem.PodClusterID +} + +func NewPodIngressCollection(t *Tool) *PodIngressCollection { + c := new(PodIngressCollection) + c.collection = newCollectionBuilder[*PodIngress](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_INGRESS_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodIngress { return new(metadbmodel.PodIngress) }). + withCacheItemFactory(func() *PodIngress { return new(PodIngress) }). + build() + return c +} + +// PodIngressCollection defines a collection that maps individual fields to the PodIngress cache data structure. +type PodIngressCollection struct { + collection[*PodIngress, *metadbmodel.PodIngress] +} diff --git a/server/controller/recorder/cache/tool/pod_ingress_rule.go b/server/controller/recorder/cache/tool/pod_ingress_rule.go new file mode 100644 index 00000000000..a62bf70bc1d --- /dev/null +++ b/server/controller/recorder/cache/tool/pod_ingress_rule.go @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// PodIngressRule defines cache data structure. +type PodIngressRule struct { + lcuuid string + id int + name string +} + +func (t *PodIngressRule) IsValid() bool { + return t.lcuuid != "" +} + +func (t *PodIngressRule) Lcuuid() string { + return t.lcuuid +} + +func (t *PodIngressRule) ID() int { + return t.id +} + +func (t *PodIngressRule) Name() string { + return t.name +} + +func (t *PodIngressRule) reset(dbItem *metadbmodel.PodIngressRule, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name +} + +func NewPodIngressRuleCollection(t *Tool) *PodIngressRuleCollection { + c := new(PodIngressRuleCollection) + c.collection = newCollectionBuilder[*PodIngressRule](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_INGRESS_RULE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodIngressRule { return new(metadbmodel.PodIngressRule) }). + withCacheItemFactory(func() *PodIngressRule { return new(PodIngressRule) }). + build() + return c +} + +// PodIngressRuleCollection defines a collection that maps individual fields to the PodIngressRule cache data structure. +type PodIngressRuleCollection struct { + collection[*PodIngressRule, *metadbmodel.PodIngressRule] +} diff --git a/server/controller/recorder/cache/tool/pod_namespace.go b/server/controller/recorder/cache/tool/pod_namespace.go new file mode 100644 index 00000000000..badc06f4573 --- /dev/null +++ b/server/controller/recorder/cache/tool/pod_namespace.go @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// PodNamespace defines cache data structure. +type PodNamespace struct { + lcuuid string + id int + name string + regionID int + azID int + podClusterID int +} + +func (t *PodNamespace) IsValid() bool { + return t.lcuuid != "" +} + +func (t *PodNamespace) Lcuuid() string { + return t.lcuuid +} + +func (t *PodNamespace) ID() int { + return t.id +} + +func (t *PodNamespace) Name() string { + return t.name +} + +func (t *PodNamespace) RegionID() int { + return t.regionID +} + +func (t *PodNamespace) AZID() int { + return t.azID +} + +func (t *PodNamespace) PodClusterID() int { + return t.podClusterID +} + +func (t *PodNamespace) reset(dbItem *metadbmodel.PodNamespace, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.azID = tool.AZ().GetByLcuuid(dbItem.AZ).ID() + t.podClusterID = dbItem.PodClusterID +} + +func NewPodNamespaceCollection(t *Tool) *PodNamespaceCollection { + c := new(PodNamespaceCollection) + c.collection = newCollectionBuilder[*PodNamespace](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_NAMESPACE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodNamespace { return new(metadbmodel.PodNamespace) }). + withCacheItemFactory(func() *PodNamespace { return new(PodNamespace) }). + build() + return c +} + +// PodNamespaceCollection defines a collection that maps individual fields to the PodNamespace cache data structure. +type PodNamespaceCollection struct { + collection[*PodNamespace, *metadbmodel.PodNamespace] +} diff --git a/server/controller/recorder/cache/tool/pod_node.go b/server/controller/recorder/cache/tool/pod_node.go new file mode 100644 index 00000000000..b3cb37967af --- /dev/null +++ b/server/controller/recorder/cache/tool/pod_node.go @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// PodNode defines cache data structure. +type PodNode struct { + lcuuid string + id int + domainLcuuid string + name string + regionID int + azID int + vpcID int + podClusterID int + vmID int +} + +func (t *PodNode) IsValid() bool { + return t.lcuuid != "" +} + +func (t *PodNode) Lcuuid() string { + return t.lcuuid +} + +func (t *PodNode) ID() int { + return t.id +} + +func (t *PodNode) DomainLcuuid() string { + return t.domainLcuuid +} + +func (t *PodNode) Name() string { + return t.name +} + +func (t *PodNode) RegionID() int { + return t.regionID +} + +func (t *PodNode) AZID() int { + return t.azID +} + +func (t *PodNode) VPCID() int { + return t.vpcID +} + +func (t *PodNode) PodClusterID() int { + return t.podClusterID +} + +func (t *PodNode) VMID() int { + return t.vmID +} + +func (t *PodNode) SetVMID(vmID int) { + t.vmID = vmID +} + +func (t *PodNode) reset(dbItem *metadbmodel.PodNode, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.domainLcuuid = dbItem.Domain + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.azID = tool.AZ().GetByLcuuid(dbItem.AZ).ID() + t.vpcID = dbItem.VPCID + t.podClusterID = dbItem.PodClusterID +} + +func NewPodNodeCollection(t *Tool) *PodNodeCollection { + c := new(PodNodeCollection) + c.collection = newCollectionBuilder[*PodNode](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_NODE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodNode { return new(metadbmodel.PodNode) }). + withCacheItemFactory(func() *PodNode { return new(PodNode) }). + build() + return c +} + +// PodNodeCollection defines a collection that maps individual fields to the PodNode cache data structure. +type PodNodeCollection struct { + collection[*PodNode, *metadbmodel.PodNode] +} diff --git a/server/controller/recorder/cache/tool/pod_replica_set.go b/server/controller/recorder/cache/tool/pod_replica_set.go new file mode 100644 index 00000000000..38ac6c50278 --- /dev/null +++ b/server/controller/recorder/cache/tool/pod_replica_set.go @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// PodReplicaSet defines cache data structure. +type PodReplicaSet struct { + lcuuid string + id int + name string + regionID int + azID int + podGroupID int + podNamespaceID int + podClusterID int +} + +func (t *PodReplicaSet) IsValid() bool { + return t.lcuuid != "" +} + +func (t *PodReplicaSet) Lcuuid() string { + return t.lcuuid +} + +func (t *PodReplicaSet) ID() int { + return t.id +} + +func (t *PodReplicaSet) Name() string { + return t.name +} + +func (t *PodReplicaSet) RegionID() int { + return t.regionID +} + +func (t *PodReplicaSet) AZID() int { + return t.azID +} + +func (t *PodReplicaSet) PodGroupID() int { + return t.podGroupID +} + +func (t *PodReplicaSet) PodNamespaceID() int { + return t.podNamespaceID +} + +func (t *PodReplicaSet) PodClusterID() int { + return t.podClusterID +} + +func (t *PodReplicaSet) reset(dbItem *metadbmodel.PodReplicaSet, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.azID = tool.AZ().GetByLcuuid(dbItem.AZ).ID() + t.podGroupID = dbItem.PodGroupID + t.podNamespaceID = dbItem.PodNamespaceID + t.podClusterID = dbItem.PodClusterID +} + +func NewPodReplicaSetCollection(t *Tool) *PodReplicaSetCollection { + c := new(PodReplicaSetCollection) + c.collection = newCollectionBuilder[*PodReplicaSet](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_REPLICA_SET_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodReplicaSet { return new(metadbmodel.PodReplicaSet) }). + withCacheItemFactory(func() *PodReplicaSet { return new(PodReplicaSet) }). + build() + return c +} + +// PodReplicaSetCollection defines a collection that maps individual fields to the PodReplicaSet cache data structure. +type PodReplicaSetCollection struct { + collection[*PodReplicaSet, *metadbmodel.PodReplicaSet] +} diff --git a/server/controller/recorder/cache/tool/pod_service.go b/server/controller/recorder/cache/tool/pod_service.go new file mode 100644 index 00000000000..17eb049582e --- /dev/null +++ b/server/controller/recorder/cache/tool/pod_service.go @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// PodService defines cache data structure. +type PodService struct { + lcuuid string + id int + name string + regionID int + azID int + vpcID int + podClusterID int + podNamespaceID int +} + +func (t *PodService) IsValid() bool { + return t.lcuuid != "" +} + +func (t *PodService) Lcuuid() string { + return t.lcuuid +} + +func (t *PodService) ID() int { + return t.id +} + +func (t *PodService) Name() string { + return t.name +} + +func (t *PodService) RegionID() int { + return t.regionID +} + +func (t *PodService) AZID() int { + return t.azID +} + +func (t *PodService) VPCID() int { + return t.vpcID +} + +func (t *PodService) PodClusterID() int { + return t.podClusterID +} + +func (t *PodService) PodNamespaceID() int { + return t.podNamespaceID +} + +func (t *PodService) reset(dbItem *metadbmodel.PodService, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.azID = tool.AZ().GetByLcuuid(dbItem.AZ).ID() + t.vpcID = dbItem.VPCID + t.podClusterID = dbItem.PodClusterID + t.podNamespaceID = dbItem.PodNamespaceID +} + +func NewPodServiceCollection(t *Tool) *PodServiceCollection { + c := new(PodServiceCollection) + c.collection = newCollectionBuilder[*PodService](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_POD_SERVICE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.PodService { return new(metadbmodel.PodService) }). + withCacheItemFactory(func() *PodService { return new(PodService) }). + build() + return c +} + +// PodServiceCollection defines a collection that maps individual fields to the PodService cache data structure. +type PodServiceCollection struct { + collection[*PodService, *metadbmodel.PodService] +} diff --git a/server/controller/recorder/cache/tool/process.go b/server/controller/recorder/cache/tool/process.go new file mode 100644 index 00000000000..e3ec6b40859 --- /dev/null +++ b/server/controller/recorder/cache/tool/process.go @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// Process defines cache data structure. +type Process struct { + lcuuid string + id int + name string + deviceType int + deviceID int + podGroupID int + podNodeID int + vmID int + vpcID int +} + +func (t *Process) IsValid() bool { + return t.lcuuid != "" +} + +func (t *Process) Lcuuid() string { + return t.lcuuid +} + +func (t *Process) ID() int { + return t.id +} + +func (t *Process) Name() string { + return t.name +} + +func (t *Process) DeviceType() int { + return t.deviceType +} + +func (t *Process) DeviceID() int { + return t.deviceID +} + +func (t *Process) PodGroupID() int { + return t.podGroupID +} + +func (t *Process) PodNodeID() int { + return t.podNodeID +} + +func (t *Process) VMID() int { + return t.vmID +} + +func (t *Process) VPCID() int { + return t.vpcID +} + +func (t *Process) reset(dbItem *metadbmodel.Process, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.deviceType = dbItem.DeviceType + t.deviceID = dbItem.DeviceID + t.podGroupID = dbItem.PodGroupID + t.podNodeID = dbItem.PodNodeID + t.vmID = dbItem.VMID + t.vpcID = dbItem.VPCID +} + +func NewProcessCollection(t *Tool) *ProcessCollection { + c := new(ProcessCollection) + c.resetExt() + c.collection = newCollectionBuilder[*Process](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_PROCESS_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.Process { return new(metadbmodel.Process) }). + withCacheItemFactory(func() *Process { return new(Process) }). + withExtender(c). + build() + return c +} + +// ProcessCollection defines a collection that maps individual fields to the Process cache data structure. +type ProcessCollection struct { + collection[*Process, *metadbmodel.Process] + ProcessCollectionExt +} diff --git a/server/controller/recorder/cache/tool/process_ext.go b/server/controller/recorder/cache/tool/process_ext.go new file mode 100644 index 00000000000..73a191f8bf9 --- /dev/null +++ b/server/controller/recorder/cache/tool/process_ext.go @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +type ProcessIdentifier struct { + Name string + PodGroupID int + VTapID uint32 + CommandLine string +} + +type ProcessCollectionExt struct { + identifierToGID map[ProcessIdentifier]uint32 + gIDToTotalCount map[uint32]uint + gIDToSoftDeletedCount map[uint32]uint +} + +func (c *ProcessCollection) resetExt() { + c.identifierToGID = make(map[ProcessIdentifier]uint32) + c.gIDToTotalCount = make(map[uint32]uint) + c.gIDToSoftDeletedCount = make(map[uint32]uint) +} + +// GetGIDByIdentifier returns the GID for the given process identifier +func (c *ProcessCollection) GetGIDByIdentifier(identifier ProcessIdentifier) (uint32, bool) { + pid, exists := c.identifierToGID[identifier] + return pid, exists +} + +// IsProcessGIDSoftDeleted checks if all processes with the given GID are soft deleted +func (c *ProcessCollection) IsProcessGIDSoftDeleted(gid uint32) bool { + return c.gIDToSoftDeletedCount[gid] == c.gIDToTotalCount[gid] +} + +// GenerateIdentifierByDBProcess generates a ProcessIdentifier from database Process model +func (c *ProcessCollection) GenerateIdentifierByDBProcess(p *metadbmodel.Process) ProcessIdentifier { + return c.GenerateIdentifier(p.Name, p.PodGroupID, p.VTapID, p.CommandLine) +} + +// GenerateIdentifier creates a ProcessIdentifier based on the process attributes +func (c *ProcessCollection) GenerateIdentifier(name string, podGroupID int, vtapID uint32, commandLine string) ProcessIdentifier { + var identifier ProcessIdentifier + if podGroupID == 0 { + identifier = ProcessIdentifier{ + Name: name, + VTapID: vtapID, + CommandLine: commandLine, + } + } else { + identifier = ProcessIdentifier{ + Name: name, + PodGroupID: podGroupID, + } + } + return identifier +} + +// OnAfterAdd implements CollectionExtender interface +// Maintains identifier to GID mapping and GID counters when process is added +func (c *ProcessCollection) OnAfterAdd(item *Process, dbItem *metadbmodel.Process) { + identifier := c.GenerateIdentifierByDBProcess(dbItem) + c.identifierToGID[identifier] = dbItem.GID + c.gIDToTotalCount[dbItem.GID]++ +} + +// OnAfterUpdate implements CollectionExtender interface +// Updates mappings when process is modified +func (c *ProcessCollection) OnAfterUpdate(item *Process, dbItem *metadbmodel.Process) { + // For now, we don't need special update logic + // Could be implemented in the future if needed +} + +// OnAfterDelete implements CollectionExtender interface +// Updates soft delete counter when process is deleted +func (c *ProcessCollection) OnAfterDelete(item *Process, dbItem *metadbmodel.Process) { + c.gIDToSoftDeletedCount[dbItem.GID]++ +} diff --git a/server/controller/recorder/cache/tool/rds_instance.go b/server/controller/recorder/cache/tool/rds_instance.go new file mode 100644 index 00000000000..56643504b11 --- /dev/null +++ b/server/controller/recorder/cache/tool/rds_instance.go @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// RDSInstance defines cache data structure. +type RDSInstance struct { + lcuuid string + id int + name string + regionID int + azID int + vpcID int +} + +func (t *RDSInstance) IsValid() bool { + return t.lcuuid != "" +} + +func (t *RDSInstance) Lcuuid() string { + return t.lcuuid +} + +func (t *RDSInstance) ID() int { + return t.id +} + +func (t *RDSInstance) Name() string { + return t.name +} + +func (t *RDSInstance) RegionID() int { + return t.regionID +} + +func (t *RDSInstance) AZID() int { + return t.azID +} + +func (t *RDSInstance) VPCID() int { + return t.vpcID +} + +func (t *RDSInstance) reset(dbItem *metadbmodel.RDSInstance, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.azID = tool.AZ().GetByLcuuid(dbItem.AZ).ID() + t.vpcID = dbItem.VPCID +} + +func NewRDSInstanceCollection(t *Tool) *RDSInstanceCollection { + c := new(RDSInstanceCollection) + c.collection = newCollectionBuilder[*RDSInstance](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_RDS_INSTANCE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.RDSInstance { return new(metadbmodel.RDSInstance) }). + withCacheItemFactory(func() *RDSInstance { return new(RDSInstance) }). + build() + return c +} + +// RDSInstanceCollection defines a collection that maps individual fields to the RDSInstance cache data structure. +type RDSInstanceCollection struct { + collection[*RDSInstance, *metadbmodel.RDSInstance] +} diff --git a/server/controller/recorder/cache/tool/redis_instance.go b/server/controller/recorder/cache/tool/redis_instance.go new file mode 100644 index 00000000000..3feb43326ec --- /dev/null +++ b/server/controller/recorder/cache/tool/redis_instance.go @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// RedisInstance defines cache data structure. +type RedisInstance struct { + lcuuid string + id int + name string + regionID int + azID int + vpcID int +} + +func (t *RedisInstance) IsValid() bool { + return t.lcuuid != "" +} + +func (t *RedisInstance) Lcuuid() string { + return t.lcuuid +} + +func (t *RedisInstance) ID() int { + return t.id +} + +func (t *RedisInstance) Name() string { + return t.name +} + +func (t *RedisInstance) RegionID() int { + return t.regionID +} + +func (t *RedisInstance) AZID() int { + return t.azID +} + +func (t *RedisInstance) VPCID() int { + return t.vpcID +} + +func (t *RedisInstance) reset(dbItem *metadbmodel.RedisInstance, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.azID = tool.AZ().GetByLcuuid(dbItem.AZ).ID() + t.vpcID = dbItem.VPCID +} + +func NewRedisInstanceCollection(t *Tool) *RedisInstanceCollection { + c := new(RedisInstanceCollection) + c.collection = newCollectionBuilder[*RedisInstance](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_REDIS_INSTANCE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.RedisInstance { return new(metadbmodel.RedisInstance) }). + withCacheItemFactory(func() *RedisInstance { return new(RedisInstance) }). + build() + return c +} + +// RedisInstanceCollection defines a collection that maps individual fields to the RedisInstance cache data structure. +type RedisInstanceCollection struct { + collection[*RedisInstance, *metadbmodel.RedisInstance] +} diff --git a/server/controller/recorder/cache/tool/region.go b/server/controller/recorder/cache/tool/region.go new file mode 100644 index 00000000000..b7141b88293 --- /dev/null +++ b/server/controller/recorder/cache/tool/region.go @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// Region defines cache data structure. +type Region struct { + lcuuid string + id int +} + +func (t *Region) IsValid() bool { + return t.lcuuid != "" +} + +func (t *Region) Lcuuid() string { + return t.lcuuid +} + +func (t *Region) ID() int { + return t.id +} + +func (t *Region) reset(dbItem *metadbmodel.Region, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID +} + +func NewRegionCollection(t *Tool) *RegionCollection { + c := new(RegionCollection) + c.collection = newCollectionBuilder[*Region](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_REGION_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.Region { return new(metadbmodel.Region) }). + withCacheItemFactory(func() *Region { return new(Region) }). + build() + return c +} + +// RegionCollection defines a collection that maps individual fields to the Region cache data structure. +type RegionCollection struct { + collection[*Region, *metadbmodel.Region] +} diff --git a/server/controller/recorder/cache/tool/subnet.go b/server/controller/recorder/cache/tool/subnet.go new file mode 100644 index 00000000000..474ffe5bde2 --- /dev/null +++ b/server/controller/recorder/cache/tool/subnet.go @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// Subnet defines cache data structure. +type Subnet struct { + lcuuid string + id int + name string + networkID int +} + +func (t *Subnet) IsValid() bool { + return t.lcuuid != "" +} + +func (t *Subnet) Lcuuid() string { + return t.lcuuid +} + +func (t *Subnet) ID() int { + return t.id +} + +func (t *Subnet) Name() string { + return t.name +} + +func (t *Subnet) NetworkID() int { + return t.networkID +} + +func (t *Subnet) reset(dbItem *metadbmodel.Subnet, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.networkID = dbItem.NetworkID +} + +func NewSubnetCollection(t *Tool) *SubnetCollection { + c := new(SubnetCollection) + c.collection = newCollectionBuilder[*Subnet](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_SUBNET_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.Subnet { return new(metadbmodel.Subnet) }). + withCacheItemFactory(func() *Subnet { return new(Subnet) }). + build() + return c +} + +// SubnetCollection defines a collection that maps individual fields to the Subnet cache data structure. +type SubnetCollection struct { + collection[*Subnet, *metadbmodel.Subnet] +} diff --git a/server/controller/recorder/cache/tool/tool.go b/server/controller/recorder/cache/tool/tool.go new file mode 100644 index 00000000000..0850b05e376 --- /dev/null +++ b/server/controller/recorder/cache/tool/tool.go @@ -0,0 +1,285 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + rcommon "github.com/deepflowio/deepflow/server/controller/recorder/common" +) + +// Tool consolidates resource data using unified info structs +type Tool struct { + LogController + metadata *rcommon.Metadata + + sequence int + + // Clouds + region *RegionCollection + az *AZCollection + + // Computes + host *HostCollection + vm *VMCollection + + // Networks + vpc *VPCCollection + network *NetworkCollection + subnet *SubnetCollection + vRouter *VRouterCollection + dhcpPort *DHCPPortCollection + vInterface *VInterfaceCollection + lanIP *LANIPCollection + wanIP *WANIPCollection + + // Network services + natGateway *NATGatewayCollection + lb *LBCollection + lbListener *LBListenerCollection + + // Storage services + rdsInstance *RDSInstanceCollection + redisInstance *RedisInstanceCollection + + // Kubernetes + podCluster *PodClusterCollection + podNode *PodNodeCollection + podNamespace *PodNamespaceCollection + podIngress *PodIngressCollection + podIngressRule *PodIngressRuleCollection + podService *PodServiceCollection + podGroup *PodGroupCollection + podReplicaSet *PodReplicaSetCollection + pod *PodCollection + configMap *ConfigMapCollection + podGroupConfigMapConnection *PodGroupConfigMapConnectionCollection + vmPodNodeConnection *VMPodNodeConnectionCollection + + // Processes + process *ProcessCollection + agent *VTapCollection +} + +func NewTool(md *rcommon.Metadata) *Tool { + t := &Tool{ + metadata: md, + } + + // Clouds + t.region = NewRegionCollection(t) + t.az = NewAZCollection(t) + + // Computes + t.host = NewHostCollection(t) + t.vm = NewVMCollection(t) + + // Networks + t.vpc = NewVPCCollection(t) + t.network = NewNetworkCollection(t) + t.subnet = NewSubnetCollection(t) + t.vRouter = NewVRouterCollection(t) + t.dhcpPort = NewDHCPPortCollection(t) + t.vInterface = NewVInterfaceCollection(t) + t.lanIP = NewLANIPCollection(t) + t.wanIP = NewWANIPCollection(t) + + // Network services + t.natGateway = NewNATGatewayCollection(t) + t.lb = NewLBCollection(t) + t.lbListener = NewLBListenerCollection(t) + + // Storage services + t.rdsInstance = NewRDSInstanceCollection(t) + t.redisInstance = NewRedisInstanceCollection(t) + + // Kubernetes + t.podCluster = NewPodClusterCollection(t) + t.podNode = NewPodNodeCollection(t) + t.podNamespace = NewPodNamespaceCollection(t) + t.podIngress = NewPodIngressCollection(t) + t.podIngressRule = NewPodIngressRuleCollection(t) + t.podService = NewPodServiceCollection(t) + t.podGroup = NewPodGroupCollection(t) + t.podReplicaSet = NewPodReplicaSetCollection(t) + t.pod = NewPodCollection(t) + t.configMap = NewConfigMapCollection(t) + t.podGroupConfigMapConnection = NewPodGroupConfigMapConnectionCollection(t) + + // Processes + t.vmPodNodeConnection = NewVMPodNodeConnectionCollection(t) + t.process = NewProcessCollection(t) + t.agent = NewVTapCollection(t) + + return t +} + +func (t Tool) Metadata() *rcommon.Metadata { return t.metadata } +func (t Tool) Sequence() int { return t.sequence } +func (t *Tool) SetSequence(sequence int) { + t.sequence = sequence +} + +// Clouds +func (t Tool) Region() *RegionCollection { return t.region } +func (t Tool) AZ() *AZCollection { return t.az } + +// Computes +func (t Tool) Host() *HostCollection { return t.host } +func (t Tool) VM() *VMCollection { return t.vm } // Networks +func (t Tool) VPC() *VPCCollection { return t.vpc } +func (t Tool) Network() *NetworkCollection { return t.network } +func (t Tool) Subnet() *SubnetCollection { return t.subnet } +func (t Tool) VRouter() *VRouterCollection { return t.vRouter } +func (t Tool) DHCPPort() *DHCPPortCollection { return t.dhcpPort } +func (t Tool) VInterface() *VInterfaceCollection { return t.vInterface } +func (t Tool) LANIP() *LANIPCollection { return t.lanIP } +func (t Tool) WANIP() *WANIPCollection { return t.wanIP } + +// Network services +func (t Tool) NATGateway() *NATGatewayCollection { return t.natGateway } +func (t Tool) LB() *LBCollection { return t.lb } +func (t Tool) LBListener() *LBListenerCollection { return t.lbListener } + +// Storage services +func (t Tool) RDSInstance() *RDSInstanceCollection { return t.rdsInstance } +func (t Tool) RedisInstance() *RedisInstanceCollection { return t.redisInstance } + +// Kubernetes +func (t Tool) PodCluster() *PodClusterCollection { return t.podCluster } +func (t Tool) PodNode() *PodNodeCollection { return t.podNode } +func (t Tool) PodNamespace() *PodNamespaceCollection { return t.podNamespace } +func (t Tool) PodIngress() *PodIngressCollection { return t.podIngress } +func (t Tool) PodIngressRule() *PodIngressRuleCollection { return t.podIngressRule } +func (t Tool) PodService() *PodServiceCollection { return t.podService } +func (t Tool) PodGroup() *PodGroupCollection { return t.podGroup } +func (t Tool) PodReplicaSet() *PodReplicaSetCollection { return t.podReplicaSet } +func (t Tool) Pod() *PodCollection { return t.pod } +func (t Tool) ConfigMap() *ConfigMapCollection { return t.configMap } +func (t Tool) PodGroupConfigMapConnection() *PodGroupConfigMapConnectionCollection { + return t.podGroupConfigMapConnection +} +func (t Tool) VMPodNodeConnection() *VMPodNodeConnectionCollection { return t.vmPodNodeConnection } + +// Processes +func (t Tool) Process() *ProcessCollection { return t.process } +func (t Tool) Agent() *VTapCollection { return t.agent } + +func (t Tool) GetDeviceVPCIDByLcuuid(deviceType int, deviceLcuuid string) (int, bool) { + var vpcID int + switch deviceType { + case ctrlrcommon.VIF_DEVICE_TYPE_VM: + vpcID = t.VM().GetByLcuuid(deviceLcuuid).VPCID() + case ctrlrcommon.VIF_DEVICE_TYPE_VROUTER: + vpcID = t.VRouter().GetByLcuuid(deviceLcuuid).VPCID() + case ctrlrcommon.VIF_DEVICE_TYPE_DHCP_PORT: + vpcID = t.DHCPPort().GetByLcuuid(deviceLcuuid).VPCID() + case ctrlrcommon.VIF_DEVICE_TYPE_NAT_GATEWAY: + vpcID = t.NATGateway().GetByLcuuid(deviceLcuuid).VPCID() + case ctrlrcommon.VIF_DEVICE_TYPE_LB: + vpcID = t.LB().GetByLcuuid(deviceLcuuid).VPCID() + case ctrlrcommon.VIF_DEVICE_TYPE_RDS_INSTANCE: + vpcID = t.RDSInstance().GetByLcuuid(deviceLcuuid).VPCID() + case ctrlrcommon.VIF_DEVICE_TYPE_REDIS_INSTANCE: + vpcID = t.RedisInstance().GetByLcuuid(deviceLcuuid).VPCID() + case ctrlrcommon.VIF_DEVICE_TYPE_POD_NODE: + vpcID = t.PodNode().GetByLcuuid(deviceLcuuid).VPCID() + case ctrlrcommon.VIF_DEVICE_TYPE_POD_SERVICE: + vpcID = t.PodService().GetByLcuuid(deviceLcuuid).VPCID() + case ctrlrcommon.VIF_DEVICE_TYPE_POD: + vpcID = t.Pod().GetByLcuuid(deviceLcuuid).VPCID() + default: + log.Errorf("device type %d not supported", deviceType, t.metadata.LogPrefixes) + return 0, false + } + + return 0, vpcID != 0 +} + +func (t Tool) GetDeviceIDByLcuuid(deviceType int, deviceLcuuid string) (int, bool) { + var id int + switch deviceType { + case ctrlrcommon.VIF_DEVICE_TYPE_VM: + id = t.VM().GetByLcuuid(deviceLcuuid).ID() + case ctrlrcommon.VIF_DEVICE_TYPE_VROUTER: + id = t.VRouter().GetByLcuuid(deviceLcuuid).ID() + case ctrlrcommon.VIF_DEVICE_TYPE_DHCP_PORT: + id = t.DHCPPort().GetByLcuuid(deviceLcuuid).ID() + case ctrlrcommon.VIF_DEVICE_TYPE_NAT_GATEWAY: + id = t.NATGateway().GetByLcuuid(deviceLcuuid).ID() + case ctrlrcommon.VIF_DEVICE_TYPE_LB: + id = t.LB().GetByLcuuid(deviceLcuuid).ID() + case ctrlrcommon.VIF_DEVICE_TYPE_RDS_INSTANCE: + id = t.RDSInstance().GetByLcuuid(deviceLcuuid).ID() + case ctrlrcommon.VIF_DEVICE_TYPE_REDIS_INSTANCE: + id = t.RedisInstance().GetByLcuuid(deviceLcuuid).ID() + case ctrlrcommon.VIF_DEVICE_TYPE_POD_NODE: + id = t.PodNode().GetByLcuuid(deviceLcuuid).ID() + case ctrlrcommon.VIF_DEVICE_TYPE_POD_SERVICE: + id = t.PodService().GetByLcuuid(deviceLcuuid).ID() + case ctrlrcommon.VIF_DEVICE_TYPE_POD: + id = t.Pod().GetByLcuuid(deviceLcuuid).ID() + default: + log.Errorf("device type %d not supported", deviceType, t.metadata.LogPrefixes) + return 0, false + } + + return id, id != 0 +} + +func (t Tool) GetDeviceLcuuidByID(deviceType int, deviceID int) (string, bool) { + var lcuuid string + switch deviceType { + case ctrlrcommon.VIF_DEVICE_TYPE_VM: + lcuuid = t.VM().GetByID(deviceID).Lcuuid() + case ctrlrcommon.VIF_DEVICE_TYPE_VROUTER: + lcuuid = t.VRouter().GetByID(deviceID).Lcuuid() + case ctrlrcommon.VIF_DEVICE_TYPE_DHCP_PORT: + lcuuid = t.DHCPPort().GetByID(deviceID).Lcuuid() + case ctrlrcommon.VIF_DEVICE_TYPE_NAT_GATEWAY: + lcuuid = t.NATGateway().GetByID(deviceID).Lcuuid() + case ctrlrcommon.VIF_DEVICE_TYPE_LB: + lcuuid = t.LB().GetByID(deviceID).Lcuuid() + case ctrlrcommon.VIF_DEVICE_TYPE_RDS_INSTANCE: + lcuuid = t.RDSInstance().GetByID(deviceID).Lcuuid() + case ctrlrcommon.VIF_DEVICE_TYPE_REDIS_INSTANCE: + lcuuid = t.RedisInstance().GetByID(deviceID).Lcuuid() + case ctrlrcommon.VIF_DEVICE_TYPE_POD_NODE: + lcuuid = t.PodNode().GetByID(deviceID).Lcuuid() + case ctrlrcommon.VIF_DEVICE_TYPE_POD_SERVICE: + lcuuid = t.PodService().GetByID(deviceID).Lcuuid() + case ctrlrcommon.VIF_DEVICE_TYPE_POD: + lcuuid = t.Pod().GetByID(deviceID).Lcuuid() + default: + log.Errorf("device type %d not supported", deviceType, t.metadata.LogPrefixes) + return "", false + } + + return lcuuid, lcuuid != "" +} + +func (t Tool) GetProcessDeviceTypeAndID(containterID string, agentID int) (deviceType, deviceID int) { + pod := t.Pod().GetByContainerID(containterID) + if pod.IsValid() { + deviceType = ctrlrcommon.VIF_DEVICE_TYPE_POD + deviceID = pod.ID() + return + } + agent := t.Agent().GetByID(agentID) + deviceType = ctrlrcommon.VTAP_TYPE_TO_DEVICE_TYPE[agent.Type()] + deviceID = agent.LaunchServerID() + return +} diff --git a/server/controller/recorder/cache/tool/vinterface.go b/server/controller/recorder/cache/tool/vinterface.go new file mode 100644 index 00000000000..c0f434099e2 --- /dev/null +++ b/server/controller/recorder/cache/tool/vinterface.go @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// VInterface defines cache data structure. +type VInterface struct { + lcuuid string + id int + name string + ifType int + index int + mac string + regionID int + networkID int + vpcID int + deviceType int + deviceID int + deviceName string +} + +func (t *VInterface) IsValid() bool { + return t.lcuuid != "" +} + +func (t *VInterface) Lcuuid() string { + return t.lcuuid +} + +func (t *VInterface) ID() int { + return t.id +} + +func (t *VInterface) Name() string { + return t.name +} + +func (t *VInterface) Type() int { + return t.ifType +} + +func (t *VInterface) Index() int { + return t.index +} + +func (t *VInterface) Mac() string { + return t.mac +} + +func (t *VInterface) RegionID() int { + return t.regionID +} + +func (t *VInterface) NetworkID() int { + return t.networkID +} + +func (t *VInterface) VPCID() int { + return t.vpcID +} + +func (t *VInterface) DeviceType() int { + return t.deviceType +} + +func (t *VInterface) DeviceID() int { + return t.deviceID +} + +func (t *VInterface) DeviceName() string { + return t.deviceName +} + +func (t *VInterface) reset(dbItem *metadbmodel.VInterface, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.ifType = dbItem.Type + t.index = dbItem.Index + t.mac = dbItem.Mac + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.networkID = dbItem.NetworkID + t.vpcID = dbItem.VPCID + t.deviceType = dbItem.DeviceType + t.deviceID = dbItem.DeviceID + t.resetCustom(dbItem, tool) +} + +func NewVInterfaceCollection(t *Tool) *VInterfaceCollection { + c := new(VInterfaceCollection) + c.resetExt() + c.collection = newCollectionBuilder[*VInterface](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_VINTERFACE_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.VInterface { return new(metadbmodel.VInterface) }). + withCacheItemFactory(func() *VInterface { return new(VInterface) }). + withExtender(c). + build() + return c +} + +// VInterfaceCollection defines a collection that maps individual fields to the VInterface cache data structure. +type VInterfaceCollection struct { + collection[*VInterface, *metadbmodel.VInterface] + VInterfaceCollectionExt +} diff --git a/server/controller/recorder/cache/tool/vinterface_ext.go b/server/controller/recorder/cache/tool/vinterface_ext.go new file mode 100644 index 00000000000..39594067f14 --- /dev/null +++ b/server/controller/recorder/cache/tool/vinterface_ext.go @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +func (item *VInterface) resetCustom(dbItem *metadbmodel.VInterface, tool *Tool) { + // Handle deviceName custom logic + switch dbItem.DeviceType { + case ctrlrcommon.VIF_DEVICE_TYPE_HOST: + item.deviceName = tool.Host().GetByID(dbItem.DeviceID).Name() + case ctrlrcommon.VIF_DEVICE_TYPE_VM: + item.deviceName = tool.VM().GetByID(dbItem.DeviceID).Name() + case ctrlrcommon.VIF_DEVICE_TYPE_VROUTER: + item.deviceName = tool.VRouter().GetByID(dbItem.DeviceID).Name() + case ctrlrcommon.VIF_DEVICE_TYPE_DHCP_PORT: + item.deviceName = tool.DHCPPort().GetByID(dbItem.DeviceID).Name() + case ctrlrcommon.VIF_DEVICE_TYPE_NAT_GATEWAY: + item.deviceName = tool.NATGateway().GetByID(dbItem.DeviceID).Name() + case ctrlrcommon.VIF_DEVICE_TYPE_LB: + item.deviceName = tool.LB().GetByID(dbItem.DeviceID).Name() + case ctrlrcommon.VIF_DEVICE_TYPE_RDS_INSTANCE: + item.deviceName = tool.RDSInstance().GetByID(dbItem.DeviceID).Name() + case ctrlrcommon.VIF_DEVICE_TYPE_REDIS_INSTANCE: + item.deviceName = tool.RedisInstance().GetByID(dbItem.DeviceID).Name() + case ctrlrcommon.VIF_DEVICE_TYPE_POD_NODE: + item.deviceName = tool.PodNode().GetByID(dbItem.DeviceID).Name() + case ctrlrcommon.VIF_DEVICE_TYPE_POD_SERVICE: + item.deviceName = tool.PodService().GetByID(dbItem.DeviceID).Name() + case ctrlrcommon.VIF_DEVICE_TYPE_POD: + item.deviceName = tool.Pod().GetByID(dbItem.DeviceID).Name() + } +} + +type deviceKey struct { + dType int + id int +} + +type VInterfaceCollectionExt struct { + deviceKeyToItems map[deviceKey][]*VInterface +} + +func (ext *VInterfaceCollection) resetExt() { + ext.deviceKeyToItems = make(map[deviceKey][]*VInterface) +} + +func (ext *VInterfaceCollection) GetByDeviceKey(deviceType, deviceID int) []*VInterface { + return ext.deviceKeyToItems[deviceKey{ + dType: deviceType, + id: deviceID, + }] +} + +func (ext *VInterfaceCollection) OnAfterAdd(item *VInterface, dbItem *metadbmodel.VInterface) { + dk := deviceKey{ + dType: dbItem.DeviceType, + id: dbItem.DeviceID, + } + ext.deviceKeyToItems[dk] = append(ext.deviceKeyToItems[dk], item) +} + +func (ext *VInterfaceCollection) OnAfterUpdate(item *VInterface, dbItem *metadbmodel.VInterface) { + // For connection tables, update is usually just add/delete operations + // No special logic needed for update +} + +func (ext *VInterfaceCollection) OnAfterDelete(item *VInterface, dbItem *metadbmodel.VInterface) { + dk := deviceKey{ + dType: dbItem.DeviceType, + id: dbItem.DeviceID, + } + for i, v := range ext.deviceKeyToItems[dk] { + if v.ID() == dbItem.ID { + ext.deviceKeyToItems[dk] = append(ext.deviceKeyToItems[dk][:i], ext.deviceKeyToItems[dk][i+1:]...) + break + } + } +} diff --git a/server/controller/recorder/cache/tool/vm.go b/server/controller/recorder/cache/tool/vm.go new file mode 100644 index 00000000000..c9a3fe6e66c --- /dev/null +++ b/server/controller/recorder/cache/tool/vm.go @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// VM defines cache data structure. +type VM struct { + lcuuid string + id int + name string + regionID int + azID int + vpcID int + hostID int + networkID int + podNodeID int +} + +func (t *VM) IsValid() bool { + return t.lcuuid != "" +} + +func (t *VM) Lcuuid() string { + return t.lcuuid +} + +func (t *VM) ID() int { + return t.id +} + +func (t *VM) Name() string { + return t.name +} + +func (t *VM) RegionID() int { + return t.regionID +} + +func (t *VM) AZID() int { + return t.azID +} + +func (t *VM) VPCID() int { + return t.vpcID +} + +func (t *VM) HostID() int { + return t.hostID +} + +func (t *VM) NetworkID() int { + return t.networkID +} + +func (t *VM) PodNodeID() int { + return t.podNodeID +} + +func (t *VM) SetPodNodeID(podNodeID int) { + t.podNodeID = podNodeID +} + +func (t *VM) reset(dbItem *metadbmodel.VM, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.azID = tool.AZ().GetByLcuuid(dbItem.AZ).ID() + t.vpcID = dbItem.VPCID + t.hostID = dbItem.HostID + t.networkID = dbItem.NetworkID +} + +func NewVMCollection(t *Tool) *VMCollection { + c := new(VMCollection) + c.collection = newCollectionBuilder[*VM](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_VM_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.VM { return new(metadbmodel.VM) }). + withCacheItemFactory(func() *VM { return new(VM) }). + build() + return c +} + +// VMCollection defines a collection that maps individual fields to the VM cache data structure. +type VMCollection struct { + collection[*VM, *metadbmodel.VM] +} diff --git a/server/controller/recorder/cache/tool/vm_pod_node_connection.go b/server/controller/recorder/cache/tool/vm_pod_node_connection.go new file mode 100644 index 00000000000..190cfe68aea --- /dev/null +++ b/server/controller/recorder/cache/tool/vm_pod_node_connection.go @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// VMPodNodeConnection defines cache data structure. +type VMPodNodeConnection struct { + lcuuid string + id int + vmID int + podNodeID int +} + +func (t *VMPodNodeConnection) IsValid() bool { + return t.lcuuid != "" +} + +func (t *VMPodNodeConnection) Lcuuid() string { + return t.lcuuid +} + +func (t *VMPodNodeConnection) ID() int { + return t.id +} + +func (t *VMPodNodeConnection) VMID() int { + return t.vmID +} + +func (t *VMPodNodeConnection) PodNodeID() int { + return t.podNodeID +} + +func (t *VMPodNodeConnection) reset(dbItem *metadbmodel.VMPodNodeConnection, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.vmID = dbItem.VMID + t.podNodeID = dbItem.PodNodeID +} + +func NewVMPodNodeConnectionCollection(t *Tool) *VMPodNodeConnectionCollection { + c := new(VMPodNodeConnectionCollection) + c.collection = newCollectionBuilder[*VMPodNodeConnection](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_VM_POD_NODE_CONNECTION_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.VMPodNodeConnection { return new(metadbmodel.VMPodNodeConnection) }). + withCacheItemFactory(func() *VMPodNodeConnection { return new(VMPodNodeConnection) }). + build() + return c +} + +// VMPodNodeConnectionCollection defines a collection that maps individual fields to the VMPodNodeConnection cache data structure. +type VMPodNodeConnectionCollection struct { + collection[*VMPodNodeConnection, *metadbmodel.VMPodNodeConnection] +} diff --git a/server/controller/recorder/cache/tool/vm_pod_node_connection_ext.go b/server/controller/recorder/cache/tool/vm_pod_node_connection_ext.go new file mode 100644 index 00000000000..876a70f0ef3 --- /dev/null +++ b/server/controller/recorder/cache/tool/vm_pod_node_connection_ext.go @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// OnAfterAdd implements CollectionExtender interface +// Maintains VM-PodNode bidirectional relationship when connection is added +func (c *VMPodNodeConnectionCollection) OnAfterAdd(item *VMPodNodeConnection, dbItem *metadbmodel.VMPodNodeConnection) { + c.tool.VM().GetByID(item.VMID()).SetPodNodeID(item.PodNodeID()) + c.tool.PodNode().GetByID(item.PodNodeID()).SetVMID(item.VMID()) +} + +// OnAfterUpdate implements CollectionExtender interface +func (c *VMPodNodeConnectionCollection) OnAfterUpdate(item *VMPodNodeConnection, dbItem *metadbmodel.VMPodNodeConnection) { + // For connection tables, update is usually just add/delete operations + // No special logic needed for update +} + +// OnAfterDelete implements CollectionExtender interface +// Clears VM-PodNode bidirectional relationship when connection is deleted +func (c *VMPodNodeConnectionCollection) OnAfterDelete(item *VMPodNodeConnection, dbItem *metadbmodel.VMPodNodeConnection) { + c.tool.VM().GetByID(item.VMID()).SetPodNodeID(0) + c.tool.PodNode().GetByID(item.PodNodeID()).SetVMID(0) +} diff --git a/server/controller/recorder/cache/tool/vpc.go b/server/controller/recorder/cache/tool/vpc.go new file mode 100644 index 00000000000..3c0a996b431 --- /dev/null +++ b/server/controller/recorder/cache/tool/vpc.go @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// VPC defines cache data structure. +type VPC struct { + lcuuid string + id int +} + +func (t *VPC) IsValid() bool { + return t.lcuuid != "" +} + +func (t *VPC) Lcuuid() string { + return t.lcuuid +} + +func (t *VPC) ID() int { + return t.id +} + +func (t *VPC) reset(dbItem *metadbmodel.VPC, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID +} + +func NewVPCCollection(t *Tool) *VPCCollection { + c := new(VPCCollection) + c.collection = newCollectionBuilder[*VPC](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_VPC_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.VPC { return new(metadbmodel.VPC) }). + withCacheItemFactory(func() *VPC { return new(VPC) }). + build() + return c +} + +// VPCCollection defines a collection that maps individual fields to the VPC cache data structure. +type VPCCollection struct { + collection[*VPC, *metadbmodel.VPC] +} diff --git a/server/controller/recorder/cache/tool/vrouter.go b/server/controller/recorder/cache/tool/vrouter.go new file mode 100644 index 00000000000..0b9ff72c14a --- /dev/null +++ b/server/controller/recorder/cache/tool/vrouter.go @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// VRouter defines cache data structure. +type VRouter struct { + lcuuid string + id int + name string + regionID int + vpcID int + gwLaunchServer string +} + +func (t *VRouter) IsValid() bool { + return t.lcuuid != "" +} + +func (t *VRouter) Lcuuid() string { + return t.lcuuid +} + +func (t *VRouter) ID() int { + return t.id +} + +func (t *VRouter) Name() string { + return t.name +} + +func (t *VRouter) RegionID() int { + return t.regionID +} + +func (t *VRouter) VPCID() int { + return t.vpcID +} + +func (t *VRouter) GWLaunchServer() string { + return t.gwLaunchServer +} + +func (t *VRouter) reset(dbItem *metadbmodel.VRouter, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.name = dbItem.Name + t.regionID = tool.Region().GetByLcuuid(dbItem.Region).ID() + t.vpcID = dbItem.VPCID + t.gwLaunchServer = dbItem.GWLaunchServer +} + +func NewVRouterCollection(t *Tool) *VRouterCollection { + c := new(VRouterCollection) + c.collection = newCollectionBuilder[*VRouter](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_VROUTER_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.VRouter { return new(metadbmodel.VRouter) }). + withCacheItemFactory(func() *VRouter { return new(VRouter) }). + build() + return c +} + +// VRouterCollection defines a collection that maps individual fields to the VRouter cache data structure. +type VRouterCollection struct { + collection[*VRouter, *metadbmodel.VRouter] +} diff --git a/server/controller/recorder/cache/tool/vtap.go b/server/controller/recorder/cache/tool/vtap.go new file mode 100644 index 00000000000..0d6048f4bbb --- /dev/null +++ b/server/controller/recorder/cache/tool/vtap.go @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// VTap defines cache data structure. +type VTap struct { + id int + lcuuid string + name string + aType int + launchServerID int +} + +func (t *VTap) IsValid() bool { + return t.lcuuid != "" +} + +func (t *VTap) ID() int { + return t.id +} + +func (t *VTap) Lcuuid() string { + return t.lcuuid +} + +func (t *VTap) Name() string { + return t.name +} + +func (t *VTap) Type() int { + return t.aType +} + +func (t *VTap) LaunchServerID() int { + return t.launchServerID +} + +func (t *VTap) reset(dbItem *metadbmodel.VTap, tool *Tool) { + t.id = dbItem.ID + t.lcuuid = dbItem.Lcuuid + t.name = dbItem.Name + t.aType = dbItem.Type + t.launchServerID = dbItem.LaunchServerID +} + +func NewVTapCollection(t *Tool) *VTapCollection { + c := new(VTapCollection) + c.collection = newCollectionBuilder[*VTap](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_VTAP_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.VTap { return new(metadbmodel.VTap) }). + withCacheItemFactory(func() *VTap { return new(VTap) }). + build() + return c +} + +// VTapCollection defines a collection that maps individual fields to the VTap cache data structure. +type VTapCollection struct { + collection[*VTap, *metadbmodel.VTap] +} diff --git a/server/controller/recorder/cache/tool/wan_ip.go b/server/controller/recorder/cache/tool/wan_ip.go new file mode 100644 index 00000000000..d8546f6ccbc --- /dev/null +++ b/server/controller/recorder/cache/tool/wan_ip.go @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// WANIP defines cache data structure. +type WANIP struct { + lcuuid string + id int + ip string + vInterfaceID int +} + +func (t *WANIP) IsValid() bool { + return t.lcuuid != "" +} + +func (t *WANIP) Lcuuid() string { + return t.lcuuid +} + +func (t *WANIP) ID() int { + return t.id +} + +func (t *WANIP) IP() string { + return t.ip +} + +func (t *WANIP) VInterfaceID() int { + return t.vInterfaceID +} + +func (t *WANIP) reset(dbItem *metadbmodel.WANIP, tool *Tool) { + t.lcuuid = dbItem.Lcuuid + t.id = dbItem.ID + t.ip = dbItem.IP + t.vInterfaceID = dbItem.VInterfaceID +} + +func NewWANIPCollection(t *Tool) *WANIPCollection { + c := new(WANIPCollection) + c.resetExt() + c.collection = newCollectionBuilder[*WANIP](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_WAN_IP_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.WANIP { return new(metadbmodel.WANIP) }). + withCacheItemFactory(func() *WANIP { return new(WANIP) }). + withExtender(c). + build() + return c +} + +// WANIPCollection defines a collection that maps individual fields to the WANIP cache data structure. +type WANIPCollection struct { + collection[*WANIP, *metadbmodel.WANIP] + WANIPCollectionExt +} diff --git a/server/controller/recorder/cache/tool/wan_ip_ext.go b/server/controller/recorder/cache/tool/wan_ip_ext.go new file mode 100644 index 00000000000..79e9e266225 --- /dev/null +++ b/server/controller/recorder/cache/tool/wan_ip_ext.go @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +type WANIPCollectionExt struct { + vInterfaceIDToItems map[int][]*WANIP // 一对多映射 +} + +func (c *WANIPCollection) resetExt() { + c.vInterfaceIDToItems = make(map[int][]*WANIP) +} + +// GetByVInterfaceID returns all WANIP items with the specified vInterfaceID +func (c *WANIPCollection) GetByVInterfaceID(vInterfaceID int) []*WANIP { + return c.vInterfaceIDToItems[vInterfaceID] +} + +// OnAfterAdd implements CollectionExtender interface for one-to-many mapping +func (c *WANIPCollection) OnAfterAdd(item *WANIP, dbItem *metadbmodel.WANIP) { + c.vInterfaceIDToItems[item.VInterfaceID()] = append(c.vInterfaceIDToItems[item.VInterfaceID()], item) +} + +// OnAfterUpdate implements CollectionExtender interface +func (c *WANIPCollection) OnAfterUpdate(item *WANIP, dbItem *metadbmodel.WANIP) { + // Remove from old mapping if vInterfaceID changed + for vInterfaceID, items := range c.vInterfaceIDToItems { + for i, wanipItem := range items { + if wanipItem == item && vInterfaceID != item.VInterfaceID() { + // Remove from old vInterfaceID group + c.vInterfaceIDToItems[vInterfaceID] = append(items[:i], items[i+1:]...) + break + } + } + } + + // Add to new mapping + newVInterfaceID := item.VInterfaceID() + found := false + for _, existingItem := range c.vInterfaceIDToItems[newVInterfaceID] { + if existingItem == item { + found = true + break + } + } + if !found { + c.vInterfaceIDToItems[newVInterfaceID] = append(c.vInterfaceIDToItems[newVInterfaceID], item) + } +} + +// OnAfterDelete implements CollectionExtender interface +func (c *WANIPCollection) OnAfterDelete(item *WANIP, dbItem *metadbmodel.WANIP) { + vInterfaceID := item.VInterfaceID() + items := c.vInterfaceIDToItems[vInterfaceID] + + for i, wanipItem := range items { + if wanipItem.ID() == item.ID() { + c.vInterfaceIDToItems[vInterfaceID] = append(items[:i], items[i+1:]...) + break + } + } +} diff --git a/server/controller/recorder/generator/cache_diffbase.go.tpl b/server/controller/recorder/generator/cache_diffbase.go.tpl new file mode 100644 index 00000000000..859201f271b --- /dev/null +++ b/server/controller/recorder/generator/cache_diffbase.go.tpl @@ -0,0 +1,97 @@ +/** + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package diffbase + +import ( + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" + "github.com/deepflowio/deepflow/server/controller/recorder/cache/tool" +) + +type {{.PublicName}} struct { + ResourceBase +{{- range .Fields}} +{{- if .Comment}} + {{.Name}} {{.Type}} // {{.Comment}} +{{- else}} + {{.Name}} {{.Type}} +{{- end}} +{{- end}} +} + +func (a *{{.PublicName}}) reset(dbItem *metadbmodel.{{.PublicName}}, tool *tool.Tool) { +{{- range .Fields}} +{{- if .Ref}} +{{- if and .DbFieldName (or (hasSuffix .Name "Lcuuid") (hasSuffix .Name "LCuuid"))}} +{{- if .RefField}} + a.{{.Name}} = tool.{{.Ref}}().GetBy{{.RefField}}(dbItem.{{.DbFieldName}}).{{.RefField}}() +{{- else}} + a.{{.Name}} = tool.{{.Ref}}().GetByID(dbItem.{{.DbFieldName}}).Lcuuid() +{{- end}} +{{- else}} +{{- if .RefField}} + a.{{.Name}} = tool.{{.Ref}}().GetBy{{.RefField}}(dbItem.{{if .DbFieldName}}{{.DbFieldName}}{{else}}{{.PublicCamelName}}{{end}}).ID() +{{- else}} + a.{{.Name}} = tool.{{.Ref}}().GetByLcuuid(dbItem.{{if .DbFieldName}}{{.DbFieldName}}{{else}}{{.PublicCamelName}}{{end}}).ID() +{{- end}} +{{- end}} +{{- else}} +{{- if and .DbType (ne .DbType .Type)}} +{{- if and (eq .DbType "bytes") (eq .Type "string")}} + a.{{.Name}} = string(dbItem.{{if .DbFieldName}}{{.DbFieldName}}{{else}}{{.PublicCamelName}}{{end}}) +{{- else}} + a.{{.Name}} = {{.Type}}(dbItem.{{if .DbFieldName}}{{.DbFieldName}}{{else}}{{.PublicCamelName}}{{end}}) +{{- end}} +{{- else}} + a.{{.Name}} = dbItem.{{if .DbFieldName}}{{.DbFieldName}}{{else}}{{.PublicCamelName}}{{end}} +{{- end}} +{{- end}} +{{- end}} +} + +{{- $hasHideFields := false}} +{{- range .Fields}} +{{- if .NeedHide}}{{$hasHideFields = true}}{{end}} +{{- end}} +{{- if $hasHideFields}} + +// ToLoggable converts {{.PublicName}} to a loggable format, excluding sensitive fields +func (a {{.PublicName}}) ToLoggable() interface{} { + copied := a +{{- range .Fields}} +{{- if .NeedHide}} + copied.{{.Name}} = "**HIDDEN**" +{{- end}} +{{- end}} + return copied +} +{{- end}} + +func New{{.PublicName}}Collection(t *tool.Tool) *{{.PublicName}}Collection { + c := new({{.PublicName}}Collection) + c.collection = newCollectionBuilder[*{{.PublicName}}](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_{{ToUpper .Name}}_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.{{.PublicName}} { return new(metadbmodel.{{.PublicName}}) }). + withCacheItemFactory(func() *{{.PublicName}} { return new({{.PublicName}}) }). + build() + return c +} + +type {{.PublicName}}Collection struct { + collection[*{{.PublicName}}, *metadbmodel.{{.PublicName}}] +} \ No newline at end of file diff --git a/server/controller/recorder/generator/cache_tool.go.tpl b/server/controller/recorder/generator/cache_tool.go.tpl new file mode 100644 index 00000000000..6aa6e1e44c8 --- /dev/null +++ b/server/controller/recorder/generator/cache_tool.go.tpl @@ -0,0 +1,198 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tool + +import ( +{{- if .HasMapset}} + mapset "github.com/deckarep/golang-set/v2" +{{- end}} + + ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common" + metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model" +) + +// {{.PublicName}} defines cache data structure. +type {{.PublicName}} struct { +{{- range .Fields}} +{{- if .Comment}} + {{.Name}} {{.Type}} // {{.Comment}} +{{- else}} + {{.Name}} {{.Type}} +{{- end}} +{{- end}} +} + +func (t *{{.PublicName}}) IsValid() bool { +{{- range .Fields}} +{{- if .IsValidationField}} + return t.{{.Name}} != "" +{{- end}} +{{- end}} +} +{{range .Fields}} + +func (t *{{$.PublicName}}) {{.PublicCamelName}}() {{.Type}} { + return t.{{.Name}} +} +{{- end}} +{{- range .Fields}} +{{- if .HasSetter}} + +func (t *{{$.PublicName}}) Set{{.PublicName}}({{.Name}} {{.Type}}) { + t.{{.Name}} = {{.Name}} +} +{{- end}} +{{- end}} +{{- range .Fields}} +{{- if .IsPlural}} + +func (t *{{$.PublicName}}) {{.PublicName}}ToSlice() []int { + return t.{{.Name}}.ToSlice() +} + +func (t *{{$.PublicName}}) AddPodGroupID(id int) { + t.{{.Name}}.Add(id) +} + +func (t *{{$.PublicName}}) RemovePodGroupID(id int) { + t.{{.Name}}.Remove(id) +} +{{- end}} +{{- end}} + +func (t *{{.PublicName}}) reset(dbItem *metadbmodel.{{.PublicName}}, tool *Tool) { +{{- range .Fields}} +{{- if and (not .HasSetter) (not .IsPlural) (not .IsCustom)}} +{{- if .Ref}} + t.{{.Name}} = tool.{{.Ref}}().GetByLcuuid(dbItem.{{if .DbFieldName}}{{.DbFieldName}}{{else}}{{.PublicName}}{{end}}).ID() +{{- else}} + t.{{.Name}} = dbItem.{{if .DbFieldName}}{{.DbFieldName}}{{else}}{{.PublicName}}{{end}} +{{- end}} +{{- end}} +{{- end}} +{{- if .HasCustom}} + t.resetCustom(dbItem, tool) +{{- else}} +{{- range .Fields}} +{{- if .IsCustom}} + t.{{.Name}} = dbItem.{{if .DbFieldName}}{{.DbFieldName}}{{else}}{{.PublicName}}{{end}} +{{- end}} +{{- end}} +{{- end}} +{{- range .Fields}} +{{- if .IsPlural}} + t.{{.Name}} = mapset.NewSet[int]() +{{- end}} +{{- end}} +} + +func New{{.PublicName}}Collection(t *Tool) *{{.PublicName}}Collection { + c := new({{.PublicName}}Collection) +{{- if .KeyFields}} +{{- range .KeyFields}} + c.{{.CamelName}}ToItem = make(map[{{.Type}}]*{{$.PublicName}}) +{{- end}} +{{- end}} +{{- if .CollectionExtension}} + c.resetExt() +{{- end}} + c.collection = newCollectionBuilder[*{{.PublicName}}](). + withResourceType(ctrlrcommon.RESOURCE_TYPE_{{ToUpper .Name}}_EN). + withTool(t). + withDBItemFactory(func() *metadbmodel.{{.PublicName}} { return new(metadbmodel.{{.PublicName}}) }). + withCacheItemFactory(func() *{{.PublicName}} { return new({{.PublicName}}) }). +{{- if or .KeyFields .CollectionExtension}} + withExtender(c). +{{- end}} + build() + return c +} + +// {{.PublicName}}Collection defines a collection that maps individual fields to the {{.PublicName}} cache data structure. +type {{.PublicName}}Collection struct { + collection[*{{.PublicName}}, *metadbmodel.{{.PublicName}}] +{{- if .KeyFields}} +{{- range .KeyFields}} + {{.CamelName}}ToItem map[{{.Type}}]*{{$.PublicName}} +{{- end}} +{{- end}} +{{- if .CollectionExtension}} + {{.PublicName}}CollectionExt +{{- end}} +} + +{{- if .KeyFields}} + +// OnAfterAdd implements CollectionExtender interface +func (c *{{.PublicName}}Collection) OnAfterAdd(item *{{.PublicName}}, dbItem *metadbmodel.{{.PublicName}}) { +{{- range .KeyFields}} + if item.{{.PublicCamelName}}() != "" { + c.{{.CamelName}}ToItem[item.{{.PublicCamelName}}()] = item + } +{{- end}} +} + +// OnAfterUpdate implements CollectionExtender interface +func (c *{{.PublicName}}Collection) OnAfterUpdate(item *{{.PublicName}}, dbItem *metadbmodel.{{.PublicName}}) { +{{- range .KeyFields}} + // Remove old {{.Name}} mapping if exists + for {{.CamelName}}, {{$.PublicName | toLowerCamel}}Item := range c.{{.CamelName}}ToItem { + if {{$.PublicName | toLowerCamel}}Item == item && {{.CamelName}} != item.{{.PublicCamelName}}() { + delete(c.{{.CamelName}}ToItem, {{.CamelName}}) + break + } + } + // Add new {{.Name}} mapping + if item.{{.PublicCamelName}}() != "" { + c.{{.CamelName}}ToItem[item.{{.PublicCamelName}}()] = item + } +{{- end}} +} + +// OnAfterDelete implements CollectionExtender interface +func (c *{{.PublicName}}Collection) OnAfterDelete(item *{{.PublicName}}, dbItem *metadbmodel.{{.PublicName}}) { +{{- range .KeyFields}} + if item.{{.PublicCamelName}}() != "" { + delete(c.{{.CamelName}}ToItem, item.{{.PublicCamelName}}()) + } +{{- end}} +} + +{{- range .KeyFields}} +// GetOrLoadBy{{.PublicName}} returns the {{$.PublicName}} by its {{.Name}}, loading from DB if not found in cache. +func (c *{{$.PublicName}}Collection) GetOrLoadBy{{.PublicName}}({{.Name}} {{.Type}}) *{{$.PublicName}} { + if {{.Name}} == "" { + return new({{$.PublicName}}) + } + + item, ok := c.{{.CamelName}}ToItem[{{.Name}}] + if ok { + return item + } + log.Warning("cache %s ({{.Name}}: %s) not found", c.resourceType, {{.Name}}) + + var dbItem *metadbmodel.{{$.PublicName}} + if result := c.tool.metadata.GetDB().Where("{{.Name}} = ?", {{.Name}}).First(&dbItem); result.RowsAffected == 1 { + c.Add(dbItem) + return c.{{.CamelName}}ToItem[{{.Name}}] + } else { + log.Error("db %s ({{.Name}}: %s) not found", c.resourceType, {{.Name}}) + return new({{$.PublicName}}) + } +} +{{- end}} +{{- end}} diff --git a/server/controller/recorder/generator/config/az.yaml b/server/controller/recorder/generator/config/az.yaml new file mode 100644 index 00000000000..ec8ec99bfc0 --- /dev/null +++ b/server/controller/recorder/generator/config/az.yaml @@ -0,0 +1,27 @@ +name: az +public_name: AZ + +cache_diffbase: + enabled: true + fields: + - name: Name + type : string + - name: Label + type : string + - name: RegionLcuuid + type : string + db_field_name: Region + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string \ No newline at end of file diff --git a/server/controller/recorder/generator/config/config_map.yaml b/server/controller/recorder/generator/config/config_map.yaml new file mode 100644 index 00000000000..66ecb640cbb --- /dev/null +++ b/server/controller/recorder/generator/config/config_map.yaml @@ -0,0 +1,34 @@ +name: config_map +public_name: ConfigMap + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: Data + type: string + db_type: bytes + need_hide: true + - name: DataHash + type: string + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: podGroupIDs + public_name: PodGroupIDs + type: mapset.Set[int] + is_plural: true + comment: 'data source: pod_group_config_map_connection' + has_mapset: true diff --git a/server/controller/recorder/generator/config/dhcp_port.yaml b/server/controller/recorder/generator/config/dhcp_port.yaml new file mode 100644 index 00000000000..876bb3cf2aa --- /dev/null +++ b/server/controller/recorder/generator/config/dhcp_port.yaml @@ -0,0 +1,43 @@ +name: dhcp_port +public_name: DHCPPort + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: RegionLcuuid + type: string + db_field_name: Region + - name: AZLcuuid + type: string + db_field_name: AZ + - name: VPCLcuuid + type: string + ref: VPC + db_field_name: VPCID + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: regionID + public_name: Region + type: int + ref: Region + - name: azID + public_name: AZ + type: int + ref: AZ + - name: vpcID + public_name: VPCID + type: int diff --git a/server/controller/recorder/generator/config/floating_ip.yaml b/server/controller/recorder/generator/config/floating_ip.yaml new file mode 100644 index 00000000000..dbffd232832 --- /dev/null +++ b/server/controller/recorder/generator/config/floating_ip.yaml @@ -0,0 +1,16 @@ +name: floating_ip +public_name: FloatingIP + +cache_diffbase: + enabled: true + fields: + - name: RegionLcuuid + type: string + db_field_name: Region + - name: VPCLcuuid + type: string + ref: VPC + db_field_name: VPCID + +cache_tool: + enabled: false \ No newline at end of file diff --git a/server/controller/recorder/generator/config/host.yaml b/server/controller/recorder/generator/config/host.yaml new file mode 100644 index 00000000000..efdd8651b0b --- /dev/null +++ b/server/controller/recorder/generator/config/host.yaml @@ -0,0 +1,55 @@ +name: host +public_name: Host + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: IP + type: string + - name: Hostname + type: string + - name: HType + type: int + - name: VCPUNum + type: int + - name: MemTotal + type: int + - name: ExtraInfo + type: string + - name: RegionLcuuid + type: string + db_field_name: Region + - name: AZLcuuid + type: string + db_field_name: AZ + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: regionID + public_name: Region + type: int + ref: Region + - name: azID + public_name: AZ + type: int + ref: AZ + - name: ip + public_name: IP + type: string + key_fields: + - name: ip + public_name: IP + type: string diff --git a/server/controller/recorder/generator/config/lan_ip.yaml b/server/controller/recorder/generator/config/lan_ip.yaml new file mode 100644 index 00000000000..17fbc7f5b47 --- /dev/null +++ b/server/controller/recorder/generator/config/lan_ip.yaml @@ -0,0 +1,28 @@ +name: lan_ip +public_name: LANIP + +cache_diffbase: + enabled: true + fields: + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: ip + public_name: IP + type: string + - name: vInterfaceID + public_name: VInterfaceID + type: int + has_extension: true + collection_extension: true diff --git a/server/controller/recorder/generator/config/lb.yaml b/server/controller/recorder/generator/config/lb.yaml new file mode 100644 index 00000000000..4e7b0728c9a --- /dev/null +++ b/server/controller/recorder/generator/config/lb.yaml @@ -0,0 +1,41 @@ +name: lb +public_name: LB + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: Model + type: int + - name: VIP + type: string + - name: VPCLcuuid + type: string + ref: VPC + db_field_name: VPCID + - name: RegionLcuuid + type: string + db_field_name: Region + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: vpcID + public_name: VPCID + type: int diff --git a/server/controller/recorder/generator/config/lb_listener.yaml b/server/controller/recorder/generator/config/lb_listener.yaml new file mode 100644 index 00000000000..f1f0cfed875 --- /dev/null +++ b/server/controller/recorder/generator/config/lb_listener.yaml @@ -0,0 +1,33 @@ +name: lb_listener +public_name: LBListener + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: IPs + type: string + - name: SNATIPs + type: string + - name: Port + type: int + - name: Protocol + type: string + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: lbID + public_name: LBID + type: int diff --git a/server/controller/recorder/generator/config/lb_target_server.yaml b/server/controller/recorder/generator/config/lb_target_server.yaml new file mode 100644 index 00000000000..ebe3ceacf24 --- /dev/null +++ b/server/controller/recorder/generator/config/lb_target_server.yaml @@ -0,0 +1,15 @@ +name: lb_target_server +public_name: LBTargetServer + +cache_diffbase: + enabled: true + fields: + - name: IP + type: string + - name: Port + type: int + - name: Protocol + type: string + +cache_tool: + enabled: false \ No newline at end of file diff --git a/server/controller/recorder/generator/config/lb_vm_connection.yaml b/server/controller/recorder/generator/config/lb_vm_connection.yaml new file mode 100644 index 00000000000..f5a65835e85 --- /dev/null +++ b/server/controller/recorder/generator/config/lb_vm_connection.yaml @@ -0,0 +1,9 @@ +name: lb_vm_connection +public_name: LBVMConnection + +cache_diffbase: + enabled: true + fields: [] + +cache_tool: + enabled: false \ No newline at end of file diff --git a/server/controller/recorder/generator/config/nat_gateway.yaml b/server/controller/recorder/generator/config/nat_gateway.yaml new file mode 100644 index 00000000000..7729d94e480 --- /dev/null +++ b/server/controller/recorder/generator/config/nat_gateway.yaml @@ -0,0 +1,40 @@ +name: nat_gateway +public_name: NATGateway + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: FloatingIPs + type: string + - name: RegionLcuuid + type: string + db_field_name: Region + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: azID + public_name: AZID + type: int + ref: AZ + db_field_name: AZ + - name: vpcID + public_name: VPCID + type: int diff --git a/server/controller/recorder/generator/config/nat_rule.yaml b/server/controller/recorder/generator/config/nat_rule.yaml new file mode 100644 index 00000000000..bb529c8584e --- /dev/null +++ b/server/controller/recorder/generator/config/nat_rule.yaml @@ -0,0 +1,9 @@ +name: nat_rule +public_name: NATRule + +cache_diffbase: + enabled: true + fields: [] + +cache_tool: + enabled: false \ No newline at end of file diff --git a/server/controller/recorder/generator/config/nat_vm_connection.yaml b/server/controller/recorder/generator/config/nat_vm_connection.yaml new file mode 100644 index 00000000000..734153dace8 --- /dev/null +++ b/server/controller/recorder/generator/config/nat_vm_connection.yaml @@ -0,0 +1,9 @@ +name: nat_vm_connection +public_name: NATVMConnection + +cache_diffbase: + enabled: true + fields: [] + +cache_tool: + enabled: false \ No newline at end of file diff --git a/server/controller/recorder/generator/config/network.yaml b/server/controller/recorder/generator/config/network.yaml new file mode 100644 index 00000000000..10556b74218 --- /dev/null +++ b/server/controller/recorder/generator/config/network.yaml @@ -0,0 +1,46 @@ +name: network +public_name: Network + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: Label + type: string + - name: TunnelID + type: int + - name: NetType + type: int + - name: SegmentationID + type: int + - name: VPCLcuuid + type: string + ref: VPC + db_field_name: VPCID + - name: RegionLcuuid + type: string + db_field_name: Region + - name: AZLcuuid + type: string + db_field_name: AZ + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: vpcID + public_name: VPCID + type: int diff --git a/server/controller/recorder/generator/config/peer_connection.yaml b/server/controller/recorder/generator/config/peer_connection.yaml new file mode 100644 index 00000000000..8ddfa6c410e --- /dev/null +++ b/server/controller/recorder/generator/config/peer_connection.yaml @@ -0,0 +1,11 @@ +name: peer_connection +public_name: PeerConnection + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + +cache_tool: + enabled: false \ No newline at end of file diff --git a/server/controller/recorder/generator/config/pod.yaml b/server/controller/recorder/generator/config/pod.yaml new file mode 100644 index 00000000000..0fa6450ef56 --- /dev/null +++ b/server/controller/recorder/generator/config/pod.yaml @@ -0,0 +1,93 @@ +name: pod +public_name: Pod +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: domainLcuuid + public_name: DomainLcuuid + type: string + db_field_name: Domain + - name: name + public_name: Name + type: string + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: azID + public_name: AZID + type: int + ref: AZ + db_field_name: AZ + - name: vpcID + public_name: VPCID + type: int + - name: podClusterID + public_name: PodClusterID + type: int + - name: podNamespaceID + public_name: PodNamespaceID + type: int + - name: podGroupID + public_name: PodGroupID + type: int + - name: podNodeID + public_name: PodNodeID + type: int + has_extension: true + collection_extension: true + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: Label + type: string + - name: Annotation + type: string + - name: ENV + type: string + - name: ContainerIDs + type: string + - name: State + type: int + - name: CreatedAt + type: time.Time + - name: PodNodeLcuuid + type: string + ref: PodNode + db_field_name: PodNodeID + - name: PodReplicaSetLcuuid + type: string + ref: PodReplicaSet + db_field_name: PodReplicaSetID + - name: PodGroupLcuuid + type: string + ref: PodGroup + db_field_name: PodGroupID + - name: PodServiceLcuuid + type: string + ref: PodService + db_field_name: PodServiceID + - name: VPCLcuuid + type: string + ref: VPC + db_field_name: VPCID + - name: RegionLcuuid + type: string + db_field_name: Region + - name: AZLcuuid + type: string + db_field_name: AZ + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain diff --git a/server/controller/recorder/generator/config/pod_cluster.yaml b/server/controller/recorder/generator/config/pod_cluster.yaml new file mode 100644 index 00000000000..d29271373e3 --- /dev/null +++ b/server/controller/recorder/generator/config/pod_cluster.yaml @@ -0,0 +1,46 @@ +name: pod_cluster +public_name: PodCluster + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: ClusterName + type: string + - name: RegionLcuuid + type: string + db_field_name: Region + - name: AZLcuuid + type: string + db_field_name: AZ + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: azID + public_name: AZID + type: int + ref: AZ + db_field_name: AZ + - name: vpcID + public_name: VPCID + type: int diff --git a/server/controller/recorder/generator/config/pod_group.yaml b/server/controller/recorder/generator/config/pod_group.yaml new file mode 100644 index 00000000000..678c002b074 --- /dev/null +++ b/server/controller/recorder/generator/config/pod_group.yaml @@ -0,0 +1,71 @@ +name: pod_group +public_name: PodGroup + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: Label + type: string + - name: PodNum + type: int + - name: Type + type: int + - name: Metadata + type: string + db_type: bytes + need_hide: true + - name: MetadataHash + type: string + - name: Spec + type: string + db_type: bytes + need_hide: true + - name: SpecHash + type: string + - name: RegionLcuuid + type: string + db_field_name: Region + - name: AZLcuuid + type: string + db_field_name: AZ + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + - name: NetworkMode + type: int + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: gType + public_name: Type + type: int + db_field_name: Type + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: azID + public_name: AZID + type: int + ref: AZ + db_field_name: AZ + - name: podNamespaceID + public_name: PodNamespaceID + type: int + - name: podClusterID + public_name: PodClusterID + type: int diff --git a/server/controller/recorder/generator/config/pod_group_config_map_connection.yaml b/server/controller/recorder/generator/config/pod_group_config_map_connection.yaml new file mode 100644 index 00000000000..90abfceccc4 --- /dev/null +++ b/server/controller/recorder/generator/config/pod_group_config_map_connection.yaml @@ -0,0 +1,23 @@ +name: pod_group_config_map_connection +public_name: PodGroupConfigMapConnection + +cache_diffbase: + enabled: true + fields: [] + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: configMapID + public_name: ConfigMapID + type: int + - name: podGroupID + public_name: PodGroupID + type: int diff --git a/server/controller/recorder/generator/config/pod_group_port.yaml b/server/controller/recorder/generator/config/pod_group_port.yaml new file mode 100644 index 00000000000..8013f2caeb7 --- /dev/null +++ b/server/controller/recorder/generator/config/pod_group_port.yaml @@ -0,0 +1,14 @@ +name: pod_group_port +public_name: PodGroupPort + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: false \ No newline at end of file diff --git a/server/controller/recorder/generator/config/pod_ingress.yaml b/server/controller/recorder/generator/config/pod_ingress.yaml new file mode 100644 index 00000000000..d1fcf9acf2d --- /dev/null +++ b/server/controller/recorder/generator/config/pod_ingress.yaml @@ -0,0 +1,47 @@ +name: pod_ingress +public_name: PodIngress + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: RegionLcuuid + type: string + db_field_name: Region + - name: AZLcuuid + type: string + db_field_name: AZ + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: azID + public_name: AZID + type: int + ref: AZ + db_field_name: AZ + - name: podNamespaceID + public_name: PodNamespaceID + type: int + - name: podClusterID + public_name: PodClusterID + type: int \ No newline at end of file diff --git a/server/controller/recorder/generator/config/pod_ingress_rule.yaml b/server/controller/recorder/generator/config/pod_ingress_rule.yaml new file mode 100644 index 00000000000..29aaea42249 --- /dev/null +++ b/server/controller/recorder/generator/config/pod_ingress_rule.yaml @@ -0,0 +1,23 @@ +name: pod_ingress_rule +public_name: PodIngressRule + +cache_diffbase: + enabled: true + fields: + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string diff --git a/server/controller/recorder/generator/config/pod_ingress_rule_backend.yaml b/server/controller/recorder/generator/config/pod_ingress_rule_backend.yaml new file mode 100644 index 00000000000..af4e1cb8682 --- /dev/null +++ b/server/controller/recorder/generator/config/pod_ingress_rule_backend.yaml @@ -0,0 +1,12 @@ +name: pod_ingress_rule_backend +public_name: PodIngressRuleBackend + +cache_diffbase: + enabled: true + fields: + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: false \ No newline at end of file diff --git a/server/controller/recorder/generator/config/pod_namespace.yaml b/server/controller/recorder/generator/config/pod_namespace.yaml new file mode 100644 index 00000000000..14f97921a08 --- /dev/null +++ b/server/controller/recorder/generator/config/pod_namespace.yaml @@ -0,0 +1,44 @@ +name: pod_namespace +public_name: PodNamespace + +cache_diffbase: + enabled: true + fields: + - name: RegionLcuuid + type: string + db_field_name: Region + - name: AZLcuuid + type: string + db_field_name: AZ + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + - name: LearnedCloudTags + type: map[string]string + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: azID + public_name: AZID + type: int + ref: AZ + db_field_name: AZ + - name: podClusterID + public_name: PodClusterID + type: int \ No newline at end of file diff --git a/server/controller/recorder/generator/config/pod_node.yaml b/server/controller/recorder/generator/config/pod_node.yaml new file mode 100644 index 00000000000..3dae65b2617 --- /dev/null +++ b/server/controller/recorder/generator/config/pod_node.yaml @@ -0,0 +1,65 @@ +name: pod_node +public_name: PodNode + +cache_diffbase: + enabled: true + fields: + - name: Type + type: int + - name: State + type: int + - name: Hostname + type: string + - name: IP + type: string + - name: VCPUNum + type: int + - name: MemTotal + type: int + - name: RegionLcuuid + type: string + db_field_name: Region + - name: AZLcuuid + type: string + db_field_name: AZ + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: domainLcuuid + public_name: DomainLcuuid + type: string + db_field_name: Domain + - name: name + public_name: Name + type: string + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: azID + public_name: AZID + type: int + ref: AZ + db_field_name: AZ + - name: vpcID + public_name: VPCID + type: int + - name: podClusterID + public_name: PodClusterID + type: int + - name: vmID + public_name: VMID + type: int + has_setter: true diff --git a/server/controller/recorder/generator/config/pod_replica_set.yaml b/server/controller/recorder/generator/config/pod_replica_set.yaml new file mode 100644 index 00000000000..362e6c5f74e --- /dev/null +++ b/server/controller/recorder/generator/config/pod_replica_set.yaml @@ -0,0 +1,54 @@ +name: pod_replica_set +public_name: PodReplicaSet + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: Label + type: string + - name: PodNum + type: int + - name: RegionLcuuid + type: string + db_field_name: Region + - name: AZLcuuid + type: string + db_field_name: AZ + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: azID + public_name: AZID + type: int + ref: AZ + db_field_name: AZ + - name: podGroupID + public_name: PodGroupID + type: int + - name: podNamespaceID + public_name: PodNamespaceID + type: int + - name: podClusterID + public_name: PodClusterID + type: int diff --git a/server/controller/recorder/generator/config/pod_service.yaml b/server/controller/recorder/generator/config/pod_service.yaml new file mode 100644 index 00000000000..7326863edf1 --- /dev/null +++ b/server/controller/recorder/generator/config/pod_service.yaml @@ -0,0 +1,76 @@ +name: pod_service +public_name: PodService + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: Label + type: string + - name: Annotation + type: string + - name: Selector + type: string + - name: ExternalIP + type: string + - name: ServiceClusterIP + type: string + - name: Metadata + type: string + db_type: bytes + need_hide: true + - name: MetadataHash + type: string + - name: Spec + type: string + db_type: bytes + need_hide: true + - name: SpecHash + type: string + - name: PodIngressLcuuid + type: string + ref: PodIngress + db_field_name: PodIngressID + - name: RegionLcuuid + type: string + db_field_name: Region + - name: AZLcuuid + type: string + db_field_name: AZ + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: azID + public_name: AZID + type: int + ref: AZ + db_field_name: AZ + - name: vpcID + public_name: VPCID + type: int + - name: podClusterID + public_name: PodClusterID + type: int + - name: podNamespaceID + public_name: PodNamespaceID + type: int diff --git a/server/controller/recorder/generator/config/pod_service_port.yaml b/server/controller/recorder/generator/config/pod_service_port.yaml new file mode 100644 index 00000000000..0420e432558 --- /dev/null +++ b/server/controller/recorder/generator/config/pod_service_port.yaml @@ -0,0 +1,14 @@ +name: pod_service_port +public_name: PodServicePort + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: false diff --git a/server/controller/recorder/generator/config/process.yaml b/server/controller/recorder/generator/config/process.yaml new file mode 100644 index 00000000000..ac534ea7859 --- /dev/null +++ b/server/controller/recorder/generator/config/process.yaml @@ -0,0 +1,50 @@ +name: process +public_name: Process + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: OSAPPTags + type: string + - name: ContainerID + type: string + - name: DeviceType + type: int + - name: DeviceID + type: int + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: deviceType + public_name: DeviceType + type: int + - name: deviceID + public_name: DeviceID + type: int + - name: podGroupID + public_name: PodGroupID + type: int + - name: podNodeID + public_name: PodNodeID + type: int + - name: vmID + public_name: VMID + type: int + - name: vpcID + public_name: VPCID + type: int + has_extension: true + collection_extension: true diff --git a/server/controller/recorder/generator/config/rds_instance.yaml b/server/controller/recorder/generator/config/rds_instance.yaml new file mode 100644 index 00000000000..2ad5ad3032a --- /dev/null +++ b/server/controller/recorder/generator/config/rds_instance.yaml @@ -0,0 +1,51 @@ +name: rds_instance +public_name: RDSInstance + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: State + type: int + - name: Series + type: int + - name: Model + type: int + - name: VPCLcuuid + type: string + ref: VPC + db_field_name: VPCID + - name: RegionLcuuid + type: string + db_field_name: Region + - name: AZLcuuid + type: string + db_field_name: AZ + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: azID + public_name: AZID + type: int + ref: AZ + db_field_name: AZ + - name: vpcID + public_name: VPCID + type: int diff --git a/server/controller/recorder/generator/config/redis_instance.yaml b/server/controller/recorder/generator/config/redis_instance.yaml new file mode 100644 index 00000000000..fcb32baaff0 --- /dev/null +++ b/server/controller/recorder/generator/config/redis_instance.yaml @@ -0,0 +1,45 @@ +name: redis_instance +public_name: RedisInstance + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: State + type: int + - name: PublicHost + type: string + - name: RegionLcuuid + type: string + db_field_name: Region + - name: AZLcuuid + type: string + db_field_name: AZ + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: azID + public_name: AZID + type: int + ref: AZ + db_field_name: AZ + - name: vpcID + public_name: VPCID + type: int diff --git a/server/controller/recorder/generator/config/region.yaml b/server/controller/recorder/generator/config/region.yaml new file mode 100644 index 00000000000..6687608a307 --- /dev/null +++ b/server/controller/recorder/generator/config/region.yaml @@ -0,0 +1,21 @@ +name: region +public_name: Region + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: Label + type: string + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int diff --git a/server/controller/recorder/generator/config/routing_table.yaml b/server/controller/recorder/generator/config/routing_table.yaml new file mode 100644 index 00000000000..304b001f5c1 --- /dev/null +++ b/server/controller/recorder/generator/config/routing_table.yaml @@ -0,0 +1,15 @@ +name: routing_table +public_name: RoutingTable + +cache_diffbase: + enabled: true + fields: + - name: Destination + type: string + - name: Nexthop + type: string + - name: NexthopType + type: string + +cache_tool: + enabled: false diff --git a/server/controller/recorder/generator/config/sub_domain.yaml b/server/controller/recorder/generator/config/sub_domain.yaml new file mode 100644 index 00000000000..5e5d2d1e4b3 --- /dev/null +++ b/server/controller/recorder/generator/config/sub_domain.yaml @@ -0,0 +1,11 @@ +name: sub_domain +public_name: SubDomain + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + +cache_tool: + enabled: false diff --git a/server/controller/recorder/generator/config/subnet.yaml b/server/controller/recorder/generator/config/subnet.yaml new file mode 100644 index 00000000000..d5684db21b8 --- /dev/null +++ b/server/controller/recorder/generator/config/subnet.yaml @@ -0,0 +1,30 @@ +name: subnet +public_name: Subnet + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: Label + type: string + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: networkID + public_name: NetworkID + type: int diff --git a/server/controller/recorder/generator/config/vinterface.yaml b/server/controller/recorder/generator/config/vinterface.yaml new file mode 100644 index 00000000000..bc390d80b1c --- /dev/null +++ b/server/controller/recorder/generator/config/vinterface.yaml @@ -0,0 +1,79 @@ +name: vinterface +public_name: VInterface + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: Type + type: int + - name: TapMac + type: string + - name: NetnsID + type: uint32 + - name: VtapID + type: uint32 + - name: VPCID + type: int + - name: DeviceType + type: int + - name: DeviceLcuuid + type: string + ref: Device + - name: NetworkLcuuid + type: string + ref: Network + db_field_name: NetworkID + - name: RegionLcuuid + type: string + db_field_name: Region + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: ifType + public_name: Type + type: int + - name: index + public_name: Index + type: int + - name: mac + public_name: Mac + type: string + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: networkID + public_name: NetworkID + type: int + - name: vpcID + public_name: VPCID + type: int + - name: deviceType + public_name: DeviceType + type: int + - name: deviceID + public_name: DeviceID + type: int + - name: deviceName + public_name: DeviceName + type: string + is_custom: true + has_extension: true + collection_extension: true diff --git a/server/controller/recorder/generator/config/vip.yaml b/server/controller/recorder/generator/config/vip.yaml new file mode 100644 index 00000000000..abe44b64f95 --- /dev/null +++ b/server/controller/recorder/generator/config/vip.yaml @@ -0,0 +1,13 @@ +name: vip +public_name: VIP + +cache_diffbase: + enabled: true + fields: + - name: IP + type: string + - name: VTapID + type: uint32 + +cache_tool: + enabled: false diff --git a/server/controller/recorder/generator/config/vm.yaml b/server/controller/recorder/generator/config/vm.yaml new file mode 100644 index 00000000000..d7a055b0009 --- /dev/null +++ b/server/controller/recorder/generator/config/vm.yaml @@ -0,0 +1,78 @@ +name: vm +public_name: VM + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: Label + type: string + - name: IP + type: string + - name: Hostname + type: string + - name: State + type: int + - name: HType + type: int + - name: LaunchServer + type: string + - name: VPCLcuuid + type: string + ref: VPC + db_field_name: VPCID + - name: RegionLcuuid + type: string + db_field_name: Region + - name: AZLcuuid + type: string + db_field_name: AZ + - name: LearnedCloudTags + type: map[string]string + - name: NetworkLcuuid + type: string + ref: Network + db_field_name: NetworkID + - name: HostID + type: int + ref: Host + ref_field: IP + db_field_name: LaunchServer + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: azID + public_name: AZID + type: int + ref: AZ + db_field_name: AZ + - name: vpcID + public_name: VPCID + type: int + - name: hostID + public_name: HostID + type: int + - name: networkID + public_name: NetworkID + type: int + - name: podNodeID + public_name: PodNodeID + type: int + has_setter: true diff --git a/server/controller/recorder/generator/config/vm_pod_node_connection.yaml b/server/controller/recorder/generator/config/vm_pod_node_connection.yaml new file mode 100644 index 00000000000..0501b981175 --- /dev/null +++ b/server/controller/recorder/generator/config/vm_pod_node_connection.yaml @@ -0,0 +1,26 @@ +name: vm_pod_node_connection +public_name: VMPodNodeConnection + +cache_diffbase: + enabled: true + fields: + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: vmID + public_name: VMID + type: int + - name: podNodeID + public_name: PodNodeID + type: int diff --git a/server/controller/recorder/generator/config/vpc.yaml b/server/controller/recorder/generator/config/vpc.yaml new file mode 100644 index 00000000000..23c4f13f1d7 --- /dev/null +++ b/server/controller/recorder/generator/config/vpc.yaml @@ -0,0 +1,30 @@ +name: vpc +public_name: VPC + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: Label + type: string + - name: TunnelID + type: int + - name: CIDR + type: string + - name: RegionLcuuid + type: string + db_field_name: Region + - name: Owner + type: string + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int diff --git a/server/controller/recorder/generator/config/vrouter.yaml b/server/controller/recorder/generator/config/vrouter.yaml new file mode 100644 index 00000000000..90fbc7d192f --- /dev/null +++ b/server/controller/recorder/generator/config/vrouter.yaml @@ -0,0 +1,42 @@ +name: vrouter +public_name: VRouter + +cache_diffbase: + enabled: true + fields: + - name: Name + type: string + - name: Label + type: string + - name: VPCLcuuid + type: string + ref: VPC + db_field_name: VPCID + - name: RegionLcuuid + type: string + db_field_name: Region + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: name + public_name: Name + type: string + - name: regionID + public_name: RegionID + type: int + ref: Region + db_field_name: Region + - name: vpcID + public_name: VPCID + type: int + - name: gwLaunchServer + public_name: GWLaunchServer + type: string diff --git a/server/controller/recorder/generator/config/vtap.yaml b/server/controller/recorder/generator/config/vtap.yaml new file mode 100644 index 00000000000..0f14b501e2e --- /dev/null +++ b/server/controller/recorder/generator/config/vtap.yaml @@ -0,0 +1,25 @@ +name: vtap +public_name: VTap + +cache_diffbase: + enabled: false + +cache_tool: + enabled: true + fields: + - name: id + public_name: ID + type: int + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: name + public_name: Name + type: string + - name: aType + public_name: Type + type: int + - name: launchServerID + public_name: LaunchServerID + type: int \ No newline at end of file diff --git a/server/controller/recorder/generator/config/wan_ip.yaml b/server/controller/recorder/generator/config/wan_ip.yaml new file mode 100644 index 00000000000..f2c4752fbd3 --- /dev/null +++ b/server/controller/recorder/generator/config/wan_ip.yaml @@ -0,0 +1,31 @@ +name: wan_ip +public_name: WANIP + +cache_diffbase: + enabled: true + fields: + - name: RegionLcuuid + type: string + db_field_name: Region + - name: SubDomainLcuuid + type: string + db_field_name: SubDomain + +cache_tool: + enabled: true + fields: + - name: lcuuid + public_name: Lcuuid + type: string + is_validation_field: true + - name: id + public_name: ID + type: int + - name: ip + public_name: IP + type: string + - name: vInterfaceID + public_name: VInterfaceID + type: int + has_extension: true + collection_extension: true diff --git a/server/controller/recorder/generator/generator.go b/server/controller/recorder/generator/generator.go new file mode 100644 index 00000000000..3057e13501c --- /dev/null +++ b/server/controller/recorder/generator/generator.go @@ -0,0 +1,648 @@ +/* + * Copyright (c) 2025 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "bytes" + "fmt" + "log" + "os" + "os/exec" + "path/filepath" + "strings" + "text/template" + "unicode" + + "gopkg.in/yaml.v2" +) + +type Field struct { + Name string `yaml:"name"` + PublicName string `yaml:"public_name"` + Type string `yaml:"type"` + DbType string `yaml:"db_type"` + IsValidationField bool `yaml:"is_validation_field"` + Ref string `yaml:"ref"` + DbFieldName string `yaml:"db_field_name"` + JsonTag string `yaml:"json_tag"` + NeedHide bool `yaml:"need_hide"` + HasSetter bool `yaml:"has_setter"` + HasCustom bool `yaml:"has_custom"` + IsCustom bool `yaml:"is_custom"` + IsPlural bool `yaml:"is_plural"` + Comment string `yaml:"comment"` + RefField string `yaml:"ref_field"` // Added missing RefField field + CamelName string + PublicCamelName string +} + +type KeyField struct { + Name string `yaml:"name"` + PublicName string `yaml:"public_name"` + Type string `yaml:"type"` + CamelName string + PublicCamelName string +} + +type CacheToolConfig struct { + Enabled bool `yaml:"enabled"` + Fields []Field `yaml:"fields"` + KeyFields []KeyField `yaml:"key_fields"` + HasExtension bool `yaml:"has_extension"` + CollectionExtension bool `yaml:"collection_extension"` + HasMapset bool `yaml:"has_mapset"` + HasCustom bool `yaml:"has_custom"` + + // 扩展内容(运行时填充) + ExtensionImports string + ExtensionFields string +} + +type CacheDiffbaseConfig struct { + Enabled bool `yaml:"enabled"` + Fields []Field `yaml:"fields"` +} + +type Config struct { + Name string `yaml:"name"` + PublicName string `yaml:"public_name"` + CacheTool CacheToolConfig `yaml:"cache_tool"` + CacheDiffbase CacheDiffbaseConfig `yaml:"cache_diffbase"` +} + +// CacheToolGenerator 聚合 cache_tool 代码生成器的所有方法 +type CacheToolGenerator struct { + config Config + cacheConfig CacheToolConfig + template *template.Template +} + +// CacheDiffbaseGenerator 聚合 cache_diffbase 代码生成器的所有方法 +type CacheDiffbaseGenerator struct { + config Config + diffbaseConfig CacheDiffbaseConfig + template *template.Template +} + +// NewCacheToolGenerator 创建新的 CacheToolGenerator 实例 +func NewCacheToolGenerator(configFile string) (*CacheToolGenerator, error) { + configData, err := os.ReadFile(configFile) + if err != nil { + return nil, fmt.Errorf("failed to read config file: %v", err) + } + + var config Config + err = yaml.Unmarshal(configData, &config) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal config data: %v", err) + } + + generator := &CacheToolGenerator{config: config} + + // 解析和适配配置 + err = generator.adaptConfig() + if err != nil { + return nil, err + } + + // 处理字段 + generator.processFields() + + // 检查是否有自定义字段 + generator.cacheConfig.HasCustomField() + + // 处理扩展文件 + generator.loadExtensions() + + // 加载模板 + err = generator.loadTemplate() + if err != nil { + return nil, err + } + + return generator, nil +} + +// NewCacheDiffbaseGenerator 创建新的 CacheDiffbaseGenerator 实例 +func NewCacheDiffbaseGenerator(configFile string) (*CacheDiffbaseGenerator, error) { + configData, err := os.ReadFile(configFile) + if err != nil { + return nil, fmt.Errorf("failed to read config file: %v", err) + } + + var config Config + err = yaml.Unmarshal(configData, &config) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal config data: %v", err) + } + + generator := &CacheDiffbaseGenerator{config: config} + + // 解析和适配配置 + err = generator.adaptConfig() + if err != nil { + return nil, err + } + + // 处理字段 + generator.processFields() + + // 加载模板 + err = generator.loadTemplate() + if err != nil { + return nil, err + } + + return generator, nil +} + +// adaptConfig 适配配置结构 +func (g *CacheToolGenerator) adaptConfig() error { + if !g.config.CacheTool.Enabled { + return fmt.Errorf("cache_tool is not enabled in configuration") + } + + g.cacheConfig = g.config.CacheTool + return nil +} + +// processFields 处理字段配置,生成 CamelName 和 PublicCamelName +func (g *CacheToolGenerator) processFields() { + for i := range g.cacheConfig.Fields { + g.cacheConfig.Fields[i].CamelName = toCamel(g.cacheConfig.Fields[i].Name, false) + if g.cacheConfig.Fields[i].PublicName != "" { + g.cacheConfig.Fields[i].PublicCamelName = g.cacheConfig.Fields[i].PublicName + } else { + g.cacheConfig.Fields[i].PublicCamelName = toCamel(g.cacheConfig.Fields[i].Name, true) + } + + // Add logic to handle ref_field + if g.cacheConfig.Fields[i].Ref != "" && g.cacheConfig.Fields[i].RefField != "" { + g.cacheConfig.Fields[i].RefField = toCamel(g.cacheConfig.Fields[i].RefField, true) + } + } + + for i := range g.cacheConfig.KeyFields { + g.cacheConfig.KeyFields[i].CamelName = toCamel(g.cacheConfig.KeyFields[i].Name, false) + g.cacheConfig.KeyFields[i].PublicCamelName = g.cacheConfig.KeyFields[i].PublicName + } +} + +// loadTemplate 加载模板文件 +func (g *CacheToolGenerator) loadTemplate() error { + templateFile := "cache_tool.go.tpl" + + tmpl, err := template.New(templateFile).Funcs(template.FuncMap{ + "ToUpper": toUpper, + "toLowerCamel": toLowerCamel, + "hasSuffix": strings.HasSuffix, + }).ParseFiles(templateFile) + if err != nil { + return fmt.Errorf("failed to parse template: %v", err) + } + + g.template = tmpl + return nil +} + +// loadExtensions 加载扩展文件 +func (g *CacheToolGenerator) loadExtensions() { + extFile := g.config.Name + "_ext.go" + if _, err := os.Stat(extFile); os.IsNotExist(err) { + return // 扩展文件不存在 + } + + content, err := os.ReadFile(extFile) + if err != nil { + log.Printf("failed to read extension file %s: %v", extFile, err) + return + } + + extContent := string(content) + + // 提取导入部分 + g.cacheConfig.ExtensionImports = g.extractImports(extContent) + + // 提取字段部分 + g.cacheConfig.ExtensionFields = g.extractFields(extContent, g.config.PublicName) +} + +// extractImports 提取扩展文件中的 import 部分 +func (g *CacheToolGenerator) extractImports(content string) string { + lines := strings.Split(content, "\n") + var imports []string + inImport := false + + for _, line := range lines { + trimmed := strings.TrimSpace(line) + if strings.HasPrefix(trimmed, "import") { + inImport = true + continue + } + if inImport { + if trimmed == ")" { + break + } + if trimmed != "" && !strings.HasPrefix(trimmed, "//") { + imports = append(imports, "\t"+trimmed) + } + } + } + + if len(imports) > 0 { + return "\n" + strings.Join(imports, "\n") + } + return "" +} + +// extractFields 提取扩展文件中的字段定义 +func (g *CacheToolGenerator) extractFields(content, structName string) string { + structStart := fmt.Sprintf("type %sExt struct", structName) + lines := strings.Split(content, "\n") + + var fields []string + inStruct := false + braceCount := 0 + + for _, line := range lines { + trimmed := strings.TrimSpace(line) + + if strings.Contains(line, structStart) { + inStruct = true + if strings.Contains(line, "{") { + braceCount = 1 + } + continue + } + + if inStruct { + braceCount += strings.Count(line, "{") - strings.Count(line, "}") + + if braceCount <= 0 { + break + } + + // 跳过空行和注释 + if trimmed == "" || strings.HasPrefix(trimmed, "//") { + continue + } + + // 添加字段(保持原有缩进格式) + if !strings.Contains(trimmed, "func") && trimmed != "{" && trimmed != "}" { + fields = append(fields, "\t"+trimmed) + } + } + } + if len(fields) > 0 { + return "\n" + strings.Join(fields, "\n") + } + return "" +} + +// Generate 生成代码文件 +func (g *CacheToolGenerator) Generate() error { + // 构造模板数据,合并基础配置和cache_tool配置 + templateData := struct { + Name string + PublicName string + Fields []Field + KeyFields []KeyField + HasExtension bool + CollectionExtension bool + HasMapset bool + HasCustom bool + ExtensionImports string + ExtensionFields string + }{ + Name: g.config.Name, + PublicName: g.config.PublicName, + Fields: g.cacheConfig.Fields, + KeyFields: g.cacheConfig.KeyFields, + HasExtension: g.cacheConfig.HasExtension, + CollectionExtension: g.cacheConfig.CollectionExtension, + HasMapset: g.cacheConfig.HasMapset, + HasCustom: g.cacheConfig.HasCustom, + ExtensionImports: g.cacheConfig.ExtensionImports, + ExtensionFields: g.cacheConfig.ExtensionFields, + } + + var generatedCode bytes.Buffer + err := g.template.Execute(&generatedCode, templateData) + if err != nil { + return fmt.Errorf("failed to execute template: %v", err) + } + + outputDir := "../cache/tool" + if _, err := os.Stat(outputDir); os.IsNotExist(err) { + if err := os.MkdirAll(outputDir, 0755); err != nil { + return fmt.Errorf("failed to create output directory: %v", err) + } + } + + outputFile := filepath.Join(outputDir, g.config.Name+".go") + err = os.WriteFile(outputFile, generatedCode.Bytes(), 0644) + if err != nil { + return fmt.Errorf("failed to write generated code to file: %v", err) + } + + // 执行 go fmt 格式化生成的代码 + if err := formatGoFile(outputFile); err != nil { + log.Printf("Warning: failed to format %s: %v", outputFile, err) + } + + // 执行 goimports 格式化 import + if err := formatImports(outputFile); err != nil { + log.Printf("Warning: failed to format imports in %s: %v", outputFile, err) + } + + fmt.Printf("Generated code for %s in %s\n", g.config.Name, outputFile) + return nil +} + +// CacheDiffbaseGenerator 的方法实现 + +// adaptConfig 适配配置结构 +func (g *CacheDiffbaseGenerator) adaptConfig() error { + if !g.config.CacheDiffbase.Enabled { + return fmt.Errorf("cache_diffbase is not enabled in configuration") + } + + g.diffbaseConfig = g.config.CacheDiffbase + return nil +} + +// processFields 处理字段配置,生成 CamelName 和 PublicCamelName +func (g *CacheDiffbaseGenerator) processFields() { + for i := range g.diffbaseConfig.Fields { + g.diffbaseConfig.Fields[i].CamelName = toCamel(g.diffbaseConfig.Fields[i].Name, false) + if g.diffbaseConfig.Fields[i].PublicName != "" { + g.diffbaseConfig.Fields[i].PublicCamelName = g.diffbaseConfig.Fields[i].PublicName + } else { + g.diffbaseConfig.Fields[i].PublicCamelName = toCamel(g.diffbaseConfig.Fields[i].Name, true) + } + } +} + +// loadTemplate 加载模板文件 +func (g *CacheDiffbaseGenerator) loadTemplate() error { + templateFile := "cache_diffbase.go.tpl" + + tmpl, err := template.New(templateFile).Funcs(template.FuncMap{ + "ToUpper": toUpper, + "toLowerCamel": toLowerCamel, + "hasSuffix": strings.HasSuffix, + }).ParseFiles(templateFile) + if err != nil { + return fmt.Errorf("failed to parse template: %v", err) + } + + g.template = tmpl + return nil +} + +// Generate 生成代码文件 +func (g *CacheDiffbaseGenerator) Generate() error { + // 构造模板数据 + templateData := struct { + Name string + PublicName string + Fields []Field + }{ + Name: g.config.Name, + PublicName: g.config.PublicName, + Fields: g.diffbaseConfig.Fields, + } + + var generatedCode bytes.Buffer + err := g.template.Execute(&generatedCode, templateData) + if err != nil { + return fmt.Errorf("failed to execute template: %v", err) + } + + outputDir := "../cache/diffbase" + if _, err := os.Stat(outputDir); os.IsNotExist(err) { + if err := os.MkdirAll(outputDir, 0755); err != nil { + return fmt.Errorf("failed to create output directory: %v", err) + } + } + + outputFile := filepath.Join(outputDir, g.config.Name+".go") + err = os.WriteFile(outputFile, generatedCode.Bytes(), 0644) + if err != nil { + return fmt.Errorf("failed to write generated code to file: %v", err) + } + + // 执行 go fmt 格式化生成的代码 + if err := formatGoFile(outputFile); err != nil { + log.Printf("Warning: failed to format %s: %v", outputFile, err) + } + + fmt.Printf("Generated diffbase code for %s in %s\n", g.config.Name, outputFile) + return nil +} + +// HasCustomField 检查 CacheToolConfig 中是否有自定义字段 +func (c *CacheToolConfig) HasCustomField() { + for _, f := range c.Fields { + if f.IsCustom { + c.HasCustom = true + return + } + } +} + +// 工具函数 +func toLowerCamel(s string) string { + if s == "" { + return "" + } + + runes := []rune(s) + i := 0 + for i < len(runes)-1 && unicode.IsUpper(runes[i]) && unicode.IsUpper(runes[i+1]) { + runes[i] = unicode.ToLower(runes[i]) + i++ + } + runes[i] = unicode.ToLower(runes[i]) + return string(runes) +} + +func toCamel(s string, public bool) string { + parts := strings.Split(s, "_") + for i, part := range parts { + if i == 0 && !public { + parts[i] = strings.ToLower(part) + } else { + parts[i] = strings.Title(part) + } + } + return strings.Join(parts, "") +} + +func toUpper(s string) string { + return strings.ToUpper(s) +} + +// formatGoFile 使用 go fmt 格式化 Go 代码文件(通用工具函数) +func formatGoFile(filePath string) error { + cmd := exec.Command("go", "fmt", filePath) + output, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("go fmt failed: %v, output: %s", err, string(output)) + } + return nil +} + +// formatImports 使用 goimports 格式化 import +func formatImports(filePath string) error { + cmd := exec.Command("goimports", "-w", filePath) + output, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("goimports failed: %v, output: %s", err, string(output)) + } + return nil +} + +// generateFromFiles 批量生成多个配置文件 +func generateFromFiles(configFiles []string) error { + var errors []string + successCount := 0 + + for _, configFile := range configFiles { + fmt.Printf("Processing %s...\n", configFile) + + // 读取配置文件判断启用了哪些生成器 + configData, err := os.ReadFile(configFile) + if err != nil { + errors = append(errors, fmt.Sprintf("%s: failed to read config: %v", configFile, err)) + continue + } + + var config Config + err = yaml.Unmarshal(configData, &config) + if err != nil { + errors = append(errors, fmt.Sprintf("%s: failed to unmarshal config: %v", configFile, err)) + continue + } + + // 生成 cache_tool + if config.CacheTool.Enabled { + generator, err := NewCacheToolGenerator(configFile) + if err != nil { + errors = append(errors, fmt.Sprintf("%s (cache_tool): %v", configFile, err)) + } else if err := generator.Generate(); err != nil { + errors = append(errors, fmt.Sprintf("%s (cache_tool): %v", configFile, err)) + } else { + successCount++ + } + } + + // 生成 cache_diffbase + if config.CacheDiffbase.Enabled { + generator, err := NewCacheDiffbaseGenerator(configFile) + if err != nil { + errors = append(errors, fmt.Sprintf("%s (cache_diffbase): %v", configFile, err)) + } else if err := generator.Generate(); err != nil { + errors = append(errors, fmt.Sprintf("%s (cache_diffbase): %v", configFile, err)) + } else { + successCount++ + } + } + } + + fmt.Printf("\nGeneration complete: %d success, %d failed\n", successCount, len(errors)) + + if len(errors) > 0 { + fmt.Println("\nErrors:") + for _, err := range errors { + fmt.Printf(" - %s\n", err) + } + return fmt.Errorf("failed to generate %d files", len(errors)) + } + + return nil +} + +// findConfigFiles 查找配置目录中的所有 yaml 文件 +func findConfigFiles() ([]string, error) { + configDir := "config" + pattern := filepath.Join(configDir, "*.yaml") + + files, err := filepath.Glob(pattern) + if err != nil { + return nil, fmt.Errorf("failed to glob config files: %v", err) + } + + if len(files) == 0 { + return nil, fmt.Errorf("no yaml files found in %s directory", configDir) + } + + return files, nil +} + +func main() { + if len(os.Args) < 2 { + fmt.Println("Usage:") + fmt.Println(" go run generator.go # Generate single file") + fmt.Println(" go run generator.go --all # Generate all config files") + fmt.Println(" go run generator.go config/*.yaml # Generate multiple files") + return + } + + arg := os.Args[1] + + // 批量生成所有配置文件 + if arg == "--all" { + configFiles, err := findConfigFiles() + if err != nil { + log.Fatalf("failed to find config files: %v", err) + } + + if err := generateFromFiles(configFiles); err != nil { + log.Fatalf("batch generation failed: %v", err) + } + return + } + + // 支持通配符模式或多个文件 + var configFiles []string + if strings.Contains(arg, "*") || len(os.Args) > 2 { + // 处理通配符或多个文件参数 + for _, pattern := range os.Args[1:] { + if strings.Contains(pattern, "*") { + matches, err := filepath.Glob(pattern) + if err != nil { + log.Fatalf("failed to glob pattern %s: %v", pattern, err) + } + configFiles = append(configFiles, matches...) + } else { + configFiles = append(configFiles, pattern) + } + } + + if err := generateFromFiles(configFiles); err != nil { + log.Fatalf("batch generation failed: %v", err) + } + return + } + + // 单个文件处理(保持原有兼容性,但使用统一的生成逻辑) + configFile := arg + if err := generateFromFiles([]string{configFile}); err != nil { + log.Fatalf("generation failed: %v", err) + } +}