Skip to content

Commit 910aa4b

Browse files
authored
Merge pull request vmware-tanzu#1040 from zhengxiexie/topic/zhengxie/main/refactor_inventory
Refactor inventory utils
2 parents b7ae1df + 3e6b8b2 commit 910aa4b

File tree

4 files changed

+83
-72
lines changed

4 files changed

+83
-72
lines changed

pkg/controllers/inventory/pod_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c *InventoryController) handlePod(obj interface{}) {
4848
case cache.DeletedFinalStateUnknown:
4949
pod, ok = obj1.Obj.(*v1.Pod)
5050
if !ok {
51-
err := fmt.Errorf("Obj is not valid *v1.Pod")
51+
err := fmt.Errorf("obj is not valid *v1.Pod")
5252
log.Error(err, "DeletedFinalStateUnknown Obj is not *v1.Pod")
5353
return
5454
}

pkg/nsx/services/inventory/build.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (s *InventoryService) BuildPod(pod *corev1.Pod) (retry bool) {
1818
log.Info("Add Pod ", "Pod", pod.Name, "Namespace", pod.Namespace)
1919
retry = false
2020
// Calculate the services related to this Pod from pendingAdd or inventory store.
21-
containerApplicationIds := []string{}
21+
var containerApplicationIds []string
2222
if s.pendingAdd[string(pod.UID)] != nil {
2323
containerApplicationInstance := s.pendingAdd[string(pod.UID)].(*containerinventory.ContainerApplicationInstance)
2424
containerApplicationIds = containerApplicationInstance.ContainerApplicationIds
@@ -35,7 +35,7 @@ func (s *InventoryService) BuildPod(pod *corev1.Pod) (retry bool) {
3535
err := s.Client.Get(context.TODO(), types.NamespacedName{Name: namespaceName}, namespace)
3636
if err != nil {
3737
retry = true
38-
log.Error(errors.New("Not find namespace for Pod"), "Failed to build Pod", "Pod", pod)
38+
log.Error(errors.New("not find namespace for Pod"), "Failed to build Pod", "Pod", pod)
3939
return
4040
}
4141

@@ -105,7 +105,7 @@ func (s *InventoryService) BuildInventoryCluster() containerinventory.ContainerC
105105

106106
clusterType := InventoryClusterTypeWCP
107107
clusterName := s.NSXConfig.Cluster
108-
networkErrors := []common.NetworkError{}
108+
var networkErrors []common.NetworkError
109109
infra := &containerinventory.ContainerInfrastructureInfo{}
110110
infra.InfraType = InventoryInfraTypeVsphere
111111
newContainerCluster := containerinventory.ContainerCluster{

pkg/nsx/services/inventory/inventory.go

Lines changed: 23 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,6 @@ import (
1414
"k8s.io/client-go/tools/cache"
1515
)
1616

17-
type InventoryType string
18-
19-
const (
20-
// Inventory object types
21-
ContainerCluster InventoryType = "ContainerCluster"
22-
ContainerClusterNode InventoryType = "ContainerClusterNode"
23-
ContainerProject InventoryType = "ContainerProject"
24-
ContainerApplication InventoryType = "ContainerApplication"
25-
ContainerApplicationInstance InventoryType = "ContainerApplicationInstance"
26-
ContainerNetworkPolicy InventoryType = "ContainerNetworkPolicy"
27-
ContainerIngressPolicy InventoryType = "ContainerIngressPolicy"
28-
29-
// Inventory cluster type
30-
InventoryClusterTypeWCP = "WCP"
31-
32-
InventoryClusterCNIType = "NSX-Operator"
33-
34-
// Inventory network status
35-
NetworkStatusHealthy = "HEALTHY"
36-
NetworkStatusUnhealthy = "UNHEALTHY"
37-
38-
// Inventory infra
39-
InventoryInfraTypeVsphere = "vSphere"
40-
41-
InventoryMaxDisTags = 20
42-
InventoryK8sPrefix = "dis:k8s:"
43-
MaxTagLen = 256
44-
MaxResourceTypeLen = 128
45-
)
46-
47-
type InventoryKey struct {
48-
InventoryType InventoryType
49-
ExternalId string
50-
Key string
51-
}
52-
53-
const (
54-
operationCreate = "CREATE"
55-
operationUpdate = "UPDATE"
56-
operationDelete = "DELETE"
57-
operationNone = "NONE"
58-
59-
InventoryStatusUp = "UP"
60-
InventoryStatusDown = "DOWN"
61-
InventoryStatusUnknown = "UNKNOWN"
62-
)
63-
6417
var (
6518
log = &logger.Log
6619
emptyKeySet = sets.New[InventoryKey]()
@@ -223,34 +176,34 @@ func (s *InventoryService) SyncInventoryObject(bufferedKeys sets.Set[InventoryKe
223176
return retryKeys, err
224177
}
225178

179+
func (s *InventoryService) prepareDelete(resourceType InventoryType, externalId string, inventoryObject interface{}) {
180+
deletedInfo := map[string]interface{}{
181+
"resource_type": resourceType,
182+
"external_id": externalId,
183+
}
184+
s.requestBuffer = append(s.requestBuffer, containerinventory.ContainerInventoryObject{
185+
ContainerObject: deletedInfo,
186+
ObjectUpdateType: operationDelete,
187+
})
188+
switch resourceType {
189+
case ContainerApplicationInstance:
190+
s.pendingDelete[externalId] = inventoryObject.(*containerinventory.ContainerApplicationInstance)
191+
}
192+
}
193+
226194
func (s *InventoryService) DeleteResource(externalId string, resourceType InventoryType) error {
227195
log.V(1).Info("Delete inventory resource", "resource_type", resourceType, "external_id", externalId)
228-
var inventoryObject interface{}
229-
exists := false
230196
switch resourceType {
231-
232197
case ContainerApplicationInstance:
233-
inventoryObject = s.ApplicationInstanceStore.GetByKey(externalId)
234-
if inventoryObject != nil {
235-
exists = true
198+
inventoryObject := s.ApplicationInstanceStore.GetByKey(externalId)
199+
if inventoryObject == nil {
200+
return nil
236201
}
202+
s.prepareDelete(resourceType, externalId, inventoryObject)
203+
return s.DeleteContainerApplicationInstance(externalId, inventoryObject.(*containerinventory.ContainerApplicationInstance))
237204
default:
238205
return fmt.Errorf("unknown resource_type : %v for external_id %s", resourceType, externalId)
239206
}
240-
241-
if exists {
242-
deletedInfo := make(map[string]interface{})
243-
deletedInfo["resource_type"] = resourceType
244-
deletedInfo["external_id"] = externalId
245-
s.requestBuffer = append(s.requestBuffer, containerinventory.ContainerInventoryObject{ContainerObject: deletedInfo,
246-
ObjectUpdateType: operationDelete})
247-
s.pendingDelete[externalId] = inventoryObject.(*containerinventory.ContainerApplicationInstance)
248-
249-
if resourceType == ContainerApplicationInstance {
250-
return s.DeleteContainerApplicationInstance(externalId, inventoryObject.(*containerinventory.ContainerApplicationInstance))
251-
}
252-
}
253-
return nil
254207
}
255208

256209
func (s *InventoryService) sendNSXRequestAndUpdateInventoryStore() error {
@@ -262,7 +215,9 @@ func (s *InventoryService) sendNSXRequestAndUpdateInventoryStore() error {
262215
containerinventory.ContainerInventoryData{ContainerInventoryObjects: s.requestBuffer})
263216

264217
// Update NSX Inventory store when the request succeeds.
265-
log.V(1).Info("NSX request response", "response status code", resp.StatusCode)
218+
if resp != nil {
219+
log.V(1).Info("NSX request response", "response status code", resp.StatusCode)
220+
}
266221
if err == nil {
267222
err = s.updateInventoryStore()
268223
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package inventory
2+
3+
type InventoryType string
4+
5+
const (
6+
// ContainerCluster represents the inventory type for a container cluster,
7+
// which is a collection of container nodes managed as a single entity.
8+
ContainerCluster InventoryType = "ContainerCluster"
9+
// ContainerClusterNode represents the inventory type for a node within a container cluster.
10+
// Each node is an individual unit of compute resources.
11+
ContainerClusterNode InventoryType = "ContainerClusterNode"
12+
// ContainerProject represents the inventory type for a containerized project,
13+
// typically mapping to a Kubernetes namespace or similar concept.
14+
ContainerProject InventoryType = "ContainerProject"
15+
// ContainerApplication represents the inventory type for an application
16+
// typically mapping to cluster services.
17+
ContainerApplication InventoryType = "ContainerApplication"
18+
// ContainerApplicationInstance represents the inventory type for a specific instance
19+
// typically mapping to pods.
20+
ContainerApplicationInstance InventoryType = "ContainerApplicationInstance"
21+
// ContainerNetworkPolicy represents the inventory type for network policies
22+
// typically mapping to Kubernetes network policies.
23+
ContainerNetworkPolicy InventoryType = "ContainerNetworkPolicy"
24+
ContainerIngressPolicy InventoryType = "ContainerIngressPolicy"
25+
26+
// InventoryClusterTypeWCP Inventory cluster type
27+
InventoryClusterTypeWCP = "WCP"
28+
InventoryClusterCNIType = "NSX-Operator"
29+
30+
// NetworkStatusHealthy Inventory network status
31+
NetworkStatusHealthy = "HEALTHY"
32+
NetworkStatusUnhealthy = "UNHEALTHY"
33+
34+
// InventoryInfraTypeVsphere Inventory infra
35+
InventoryInfraTypeVsphere = "vSphere"
36+
37+
InventoryMaxDisTags = 20
38+
InventoryK8sPrefix = "dis:k8s:"
39+
MaxTagLen = 256
40+
MaxResourceTypeLen = 128
41+
42+
operationCreate = "CREATE"
43+
operationUpdate = "UPDATE"
44+
operationDelete = "DELETE"
45+
operationNone = "NONE"
46+
47+
InventoryStatusUp = "UP"
48+
InventoryStatusDown = "DOWN"
49+
InventoryStatusUnknown = "UNKNOWN"
50+
)
51+
52+
type InventoryKey struct {
53+
InventoryType InventoryType
54+
ExternalId string
55+
Key string
56+
}

0 commit comments

Comments
 (0)