diff --git a/collector/metadata/kubernetes/pod_watch.go b/collector/metadata/kubernetes/pod_watch.go index 80bf19252..d72b5d617 100644 --- a/collector/metadata/kubernetes/pod_watch.go +++ b/collector/metadata/kubernetes/pod_watch.go @@ -266,7 +266,7 @@ func OnUpdate(objOld interface{}, objNew interface{}) { // Check the containers' ID newContainerIds := make([]string, 0) - for _, containerStatus := range oldPod.Status.ContainerStatuses { + for _, containerStatus := range newPod.Status.ContainerStatuses { shortenContainerId := TruncateContainerId(containerStatus.ContainerID) if shortenContainerId == "" { continue diff --git a/collector/metadata/kubernetes/pod_watch_test.go b/collector/metadata/kubernetes/pod_watch_test.go index 123b8084b..1d6a5fe0f 100644 --- a/collector/metadata/kubernetes/pod_watch_test.go +++ b/collector/metadata/kubernetes/pod_watch_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -195,9 +196,9 @@ func CreatePod(hasPort bool) *corev1.Pod { return pod } -func TestUpdateDelayDelete(t *testing.T) { - addObjJson := "{\"metadata\": {\"name\": \"testdemo2-5c86748464-26crb\",\"namespace\": \"test-ns\",\"resourceVersion\": \"44895976\"},\"spec\": {\"containers\": [{\"name\": \"testdemo2\",\"ports\": [{\"containerPort\": 9001,\"protocol\": \"TCP\"}]}]},\"status\": {\"phase\": \"Running\",\"podIP\": \"192.168.136.210\",\"containerStatuses\": [{\"name\": \"testdemo2\",\"state\": {\"running\": {\"startedAt\": \"2022-05-25T08:55:36Z\"}},\"lastState\": {},\"ready\": true,\"restartCount\": 5,\"image\": \"\",\"imageID\": \"docker-pullable://10.10.102.213:8443/cloudnevro-test/test-netserver@sha256:6720f648b74ed590f36094a1c7a58b01b6881396409784c17f471ecfe445e3fd\",\"containerID\": \"docker://d505f50edb4e204cf31840e3cb8d26d33f212d4ebef994d0c3fc151d57e17413\",\"started\": true}]}}" - updateObjJson := "{\"metadata\": {\"name\": \"testdemo2-5c86748464-26crb\",\"namespace\": \"test-ns\",\"resourceVersion\": \"47374698\"},\"spec\": {\"containers\": [{\"name\": \"testdemo2\",\"ports\": [{\"containerPort\": 9001,\"protocol\": \"TCP\"}]}]},\"status\": {\"phase\": \"Running\",\"podIP\": \"192.168.136.210\",\"containerStatuses\": [{\"name\": \"testdemo2\",\"state\": {\"terminated\": {\"exitCode\": 143,\"reason\": \"Error\",\"startedAt\": \"2022-05-25T08:55:36Z\",\"finishedAt\": \"2022-06-06T09:04:12Z\",\"containerID\": \"docker://d505f50edb4e204cf31840e3cb8d26d33f212d4ebef994d0c3fc151d57e17413\"}},\"lastState\": {},\"ready\": false,\"restartCount\": 5,\"image\": \"\",\"imageID\": \"docker-pullable://10.10.102.213:8443/cloudnevro-test/test-netserver@sha256:6720f648b74ed590f36094a1c7a58b01b6881396409784c17f471ecfe445e3fd\",\"containerID\": \"docker://d505f50edb4e204cf31840e3cb8d26d33f212d4ebef994d0c3fc151d57e17413\",\"started\": false}]}}" +func TestUpdateAndDelayDelete(t *testing.T) { + addObjJson := "{\"metadata\":{\"name\":\"testdemo2-5c86748464-26crb\",\"namespace\":\"test-ns\",\"resourceVersion\":\"44895976\"},\"spec\":{\"containers\":[{\"name\":\"testdemo2\",\"ports\":[{\"containerPort\":9001,\"protocol\":\"TCP\",\"hostPort\":9001}]}]},\"status\":{\"phase\":\"Running\",\"podIP\":\"192.168.136.210\",\"hostIP\":\"10.10.10.101\",\"containerStatuses\":[{\"name\":\"testdemo2\",\"state\":{\"running\":{\"startedAt\":\"2022-05-25T08:55:36Z\"}},\"lastState\":{},\"ready\":true,\"restartCount\":5,\"image\":\"\",\"imageID\":\"docker-pullable://10.10.102.213:8443/cloudnevro-test/test-netserver@sha256:6720f648b74ed590f36094a1c7a58b01b6881396409784c17f471ecfe445e3fd\",\"containerID\":\"docker://d505f50edb4e204cf31840e3cb8d26d33f212d4ebef994d0c3fc151d57e17413\",\"started\":true}]}}" + updateObjJson := "{\"metadata\":{\"name\":\"testdemo2-5c86748464-26crb\",\"namespace\":\"test-ns\",\"resourceVersion\":\"44895977\"},\"spec\":{\"containers\":[{\"name\":\"testdemo2\",\"ports\":[{\"containerPort\":9001,\"protocol\":\"TCP\",\"hostPort\":9001}]}]},\"status\":{\"phase\":\"Running\",\"podIP\":\"192.168.136.212\",\"hostIP\":\"10.10.10.102\",\"containerStatuses\":[{\"name\":\"testdemo2\",\"state\":{\"running\":{\"startedAt\":\"2022-05-25T08:55:36Z\"}},\"lastState\":{},\"ready\":true,\"restartCount\":5,\"image\":\"\",\"imageID\":\"docker-pullable://10.10.102.213:8443/cloudnevro-test/test-netserver@sha256:6720f648b74ed590f36094a1c7a58b01b6881396409784c17f471ecfe445e3fd\",\"containerID\":\"docker://d000f50edb4e204cf31840e3cb8d26d33f212d4ebef994d0c3fc151d57e17413\",\"started\":true}]}}" addObj := new(corev1.Pod) err := json.Unmarshal([]byte(addObjJson), addObj) if err != nil { @@ -220,12 +221,33 @@ func TestUpdateDelayDelete(t *testing.T) { stopCh := make(chan struct{}) go podDeleteLoop(100*time.Millisecond, 500*time.Millisecond, stopCh) OnUpdate(addObj, updateObj) - time.Sleep(600 * time.Millisecond) - _, ok = MetaDataCache.GetContainerByIpPort(podIp, uint32(port)) - if !ok { - t.Errorf("Not found container [%s:%d]", podIp, port) - } else { - t.Logf("Found container [%s:%d]", podIp, port) - } + + // Check if new Container can be find + _, find := MetaDataCache.GetByContainerId(TruncateContainerId(updateObj.Status.ContainerStatuses[0].ContainerID)) + assert.True(t, find, "NewContainerId did't find in MetaDataCache") + _, find = MetaDataCache.GetContainerByIpPort(updateObj.Status.PodIP, uint32(port)) + assert.True(t, find, "NewContainer IP Port did't find in MetaDataCache") + _, find = MetaDataCache.GetContainerByHostIpPort(updateObj.Status.HostIP, uint32(port)) + assert.True(t, find, "NewHostIp Port did't find in MetaDataCache") + + // Wait for deletes + time.Sleep(1000 * time.Millisecond) + + // Double Check for NewContainer + _, find = MetaDataCache.GetByContainerId(TruncateContainerId(updateObj.Status.ContainerStatuses[0].ContainerID)) + assert.True(t, find, "NewContainerId did't find in MetaDataCache") + _, find = MetaDataCache.GetContainerByIpPort(updateObj.Status.PodIP, uint32(port)) + assert.True(t, find, "NewContainer IP Port did't find in MetaDataCache") + _, find = MetaDataCache.GetContainerByHostIpPort(updateObj.Status.HostIP, uint32(port)) + assert.True(t, find, "NewHostIp Port did't find in MetaDataCache") + + // Check the old Container has been delete + _, find = MetaDataCache.GetByContainerId(TruncateContainerId(addObj.Status.ContainerStatuses[0].ContainerID)) + assert.False(t, find, "OldContainerId should be deletedin MetaDataCache") + _, find = MetaDataCache.GetContainerByIpPort(addObj.Status.PodIP, uint32(port)) + assert.False(t, find, "OldContainer IP should be deleted in MetaDataCache") + _, find = MetaDataCache.GetContainerByHostIpPort(addObj.Status.HostIP, uint32(port)) + assert.False(t, find, "OldHostIp Port should be deleted in MetaDataCache") + stopCh <- struct{}{} }