From db5144656cde21ef83476d22b7d9c7ebb9cf0e84 Mon Sep 17 00:00:00 2001 From: Claudio Netto Date: Thu, 4 Aug 2022 15:49:18 -0300 Subject: [PATCH] feat(web): return pods in termination phase --- internal/pkg/rpaas/k8s.go | 25 +++++++----- internal/pkg/rpaas/k8s_test.go | 71 +++++++++++++++++++++++++++++++++ pkg/rpaas/client/types/types.go | 21 +++++----- 3 files changed, 98 insertions(+), 19 deletions(-) diff --git a/internal/pkg/rpaas/k8s.go b/internal/pkg/rpaas/k8s.go index 48d63e758..2ea712624 100644 --- a/internal/pkg/rpaas/k8s.go +++ b/internal/pkg/rpaas/k8s.go @@ -1917,6 +1917,12 @@ func (m *k8sRpaasManager) newPodStatus(ctx context.Context, pod *corev1.Pod) (cl phase = corev1.PodUnknown } + var terminatedAt time.Time + if d := pod.DeletionTimestamp; d != nil { + phase = "Terminating" + terminatedAt = d.In(time.UTC) + } + errors, err := m.getErrorsForPod(ctx, pod) if err != nil { return clientTypes.Pod{}, err @@ -1937,15 +1943,16 @@ func (m *k8sRpaasManager) newPodStatus(ctx context.Context, pod *corev1.Pod) (cl } return clientTypes.Pod{ - CreatedAt: pod.CreationTimestamp.Time.In(time.UTC), - Name: pod.Name, - IP: pod.Status.PodIP, - HostIP: pod.Status.HostIP, - Status: string(phase), - Ports: getPortsForPod(pod), - Errors: errors, - Restarts: restarts, - Ready: ready, + CreatedAt: pod.CreationTimestamp.In(time.UTC), + TerminatedAt: terminatedAt, + Name: pod.Name, + IP: pod.Status.PodIP, + HostIP: pod.Status.HostIP, + Status: string(phase), + Ports: getPortsForPod(pod), + Errors: errors, + Restarts: restarts, + Ready: ready, }, nil } diff --git a/internal/pkg/rpaas/k8s_test.go b/internal/pkg/rpaas/k8s_test.go index eac24c183..1e0578802 100644 --- a/internal/pkg/rpaas/k8s_test.go +++ b/internal/pkg/rpaas/k8s_test.go @@ -4145,6 +4145,39 @@ func Test_k8sRpaasManager_GetInstanceInfo(t *testing.T) { }, }, }, + &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-instance-6f86f957b7-klmno", + Namespace: "rpaasv2", + Labels: map[string]string{ + "nginx.tsuru.io/app": "nginx", + "nginx.tsuru.io/resource-name": "my-instance", + }, + CreationTimestamp: metav1.NewTime(t0), + DeletionTimestamp: &metav1.Time{Time: t0.Add(time.Minute)}, + UID: types.UID("my-instance-6f86f957b7-klmno"), + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "nginx", + Ports: []corev1.ContainerPort{ + {Name: "http", HostPort: int32(30000)}, + {Name: "https", HostPort: int32(30001)}, + {Name: "nginx-metrics", HostPort: int32(30002)}, + }, + }, + }, + }, + Status: corev1.PodStatus{ + Phase: corev1.PodRunning, + PodIP: "172.16.100.20", + HostIP: "10.10.10.10", + ContainerStatuses: []corev1.ContainerStatus{ + {Name: "nginx", Ready: true}, + }, + }, + }, &metricsv1beta1.PodMetrics{ ObjectMeta: metav1.ObjectMeta{ Name: "my-instance-6f86f957b7-abcde", @@ -4165,6 +4198,26 @@ func Test_k8sRpaasManager_GetInstanceInfo(t *testing.T) { }, }, }, + &metricsv1beta1.PodMetrics{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-instance-6f86f957b7-klmno", + Namespace: "rpaasv2", + Labels: map[string]string{ + "nginx.tsuru.io/app": "nginx", + "nginx.tsuru.io/resource-name": "my-instance", + }, + UID: types.UID("my-instance-6f86f957b7-klmno"), + }, + Containers: []metricsv1beta1.ContainerMetrics{ + { + Name: "nginx", + Usage: corev1.ResourceList{ + "cpu": resource.MustParse("30m"), + "memory": resource.MustParse("10Mi"), + }, + }, + }, + }, &corev1.Event{ ObjectMeta: metav1.ObjectMeta{ Name: "my-instance-6f86f957b7-fghij.1", @@ -4267,6 +4320,24 @@ func Test_k8sRpaasManager_GetInstanceInfo(t *testing.T) { }, }, }, + { + Name: "my-instance-6f86f957b7-klmno", + IP: "172.16.100.20", + HostIP: "10.10.10.10", + Status: "Terminating", + CreatedAt: time.Date(2020, 4, 2, 16, 10, 0, 0, time.UTC), + TerminatedAt: time.Date(2020, 4, 2, 16, 11, 0, 0, time.UTC), + Ready: true, + Ports: []clientTypes.PodPort{ + {Name: "http", HostPort: int32(30000)}, + {Name: "https", HostPort: int32(30001)}, + {Name: "nginx-metrics", HostPort: int32(30002)}, + }, + Metrics: &clientTypes.PodMetrics{ + CPU: "30m", + Memory: "10Mi", + }, + }, } return info }, diff --git a/pkg/rpaas/client/types/types.go b/pkg/rpaas/client/types/types.go index c5c6c07f6..41d0d2434 100644 --- a/pkg/rpaas/client/types/types.go +++ b/pkg/rpaas/client/types/types.go @@ -88,16 +88,17 @@ type PodError struct { } type Pod struct { - CreatedAt time.Time `json:"createdAt,omitempty"` - Name string `json:"name"` - IP string `json:"ip"` - HostIP string `json:"host"` - Status string `json:"status"` - Ports []PodPort `json:"ports,omitempty"` - Errors []PodError `json:"errors,omitempty"` - Restarts int32 `json:"restarts"` - Ready bool `json:"ready"` - Metrics *PodMetrics `json:"metrics,omitempty"` + CreatedAt time.Time `json:"createdAt,omitempty"` + TerminatedAt time.Time `json:"terminatedAt,omitempty"` + Name string `json:"name"` + IP string `json:"ip"` + HostIP string `json:"host"` + Status string `json:"status"` + Ports []PodPort `json:"ports,omitempty"` + Errors []PodError `json:"errors,omitempty"` + Restarts int32 `json:"restarts"` + Ready bool `json:"ready"` + Metrics *PodMetrics `json:"metrics,omitempty"` } type PodMetrics struct {