Skip to content

Commit

Permalink
feat(web): return pods in termination phase
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Aug 4, 2022
1 parent ec6ef90 commit db51446
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 19 deletions.
25 changes: 16 additions & 9 deletions internal/pkg/rpaas/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
71 changes: 71 additions & 0 deletions internal/pkg/rpaas/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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
},
Expand Down
21 changes: 11 additions & 10 deletions pkg/rpaas/client/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit db51446

Please sign in to comment.