This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
/
objectstatus.go
193 lines (162 loc) · 5.75 KB
/
objectstatus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Copyright (c) 2019 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package objectstatus
import (
"context"
"errors"
"fmt"
"strconv"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"github.com/vmware-tanzu/octant/internal/link"
"github.com/vmware-tanzu/octant/pkg/store"
"github.com/vmware-tanzu/octant/pkg/view/component"
)
type statusKey struct {
apiVersion string
kind string
}
type statusFunc func(context.Context, runtime.Object, store.Store, link.Interface) (ObjectStatus, error)
type statusLookup map[statusKey]statusFunc
var (
// TODO: expose this as a setting via preferences - https://github.com/vmware-tanzu/octant/discussions/2694
terminatingThreshold = time.Minute * 5
defaultStatusLookup = statusLookup{
{apiVersion: "batch/v1beta1", kind: "CronJob"}: cronJob,
{apiVersion: "apps/v1", kind: "DaemonSet"}: daemonSet,
{apiVersion: "apps/v1", kind: "Deployment"}: deploymentAppsV1,
{apiVersion: "apps/v1", kind: "ReplicaSet"}: replicaSetAppsV1,
{apiVersion: "apps/v1", kind: "StatefulSet"}: statefulSet,
{apiVersion: "batch/v1", kind: "Job"}: runJobStatus,
{apiVersion: "v1", kind: "Pod"}: pod,
{apiVersion: "v1", kind: "ReplicationController"}: replicationController,
{apiVersion: "v1", kind: "Service"}: service,
{apiVersion: "v1", kind: "PersistentVolume"}: persistentVolume,
{apiVersion: "v1", kind: "PersistentVolumeClaim"}: persistentVolumeClaim,
{apiVersion: "networking.k8s.io/v1", kind: "Ingress"}: runIngressStatus,
{apiVersion: "apiregistration.k8s.io/v1", kind: "APIService"}: apiService,
}
)
type ObjectStatus struct {
NodeStatus component.NodeStatus
Details []component.Component
Properties []component.Property
}
func (os *ObjectStatus) AddDetail(detail string) {
os.Details = append(os.Details, component.NewText(detail))
}
func (os *ObjectStatus) AddProperty(label string, value component.Component) {
os.Properties = append(os.Properties, component.Property{
Label: label,
Value: value,
})
}
func (os *ObjectStatus) InsertProperty(label string, value component.Component) {
os.Properties = append([]component.Property{{
Label: label,
Value: value,
}}, os.Properties...)
}
func (os *ObjectStatus) AddDetailf(msg string, args ...interface{}) {
os.AddDetail(fmt.Sprintf(msg, args...))
}
func (os *ObjectStatus) SetError() {
os.NodeStatus = component.NodeStatusError
}
func (os *ObjectStatus) SetWarning() {
if os.NodeStatus != component.NodeStatusError {
os.NodeStatus = component.NodeStatusWarning
}
}
func (os *ObjectStatus) Status() component.NodeStatus {
switch os.NodeStatus {
case component.NodeStatusWarning,
component.NodeStatusError,
component.NodeStatusOK:
return os.NodeStatus
default:
return component.NodeStatusOK
}
}
// Status creates an ObjectStatus for an object.
func Status(ctx context.Context, object runtime.Object, o store.Store, link link.Interface) (ObjectStatus, error) {
return status(ctx, object, o, defaultStatusLookup, link)
}
func status(ctx context.Context, object runtime.Object, o store.Store, lookup statusLookup, link link.Interface) (ObjectStatus, error) {
if object == nil {
return ObjectStatus{}, errors.New("object is nil")
}
gvk := object.GetObjectKind().GroupVersionKind()
apiVersion, kind := gvk.ToAPIVersionAndKind()
accessor, err := meta.Accessor(object)
if err != nil {
return ObjectStatus{}, err
}
if accessor.GetDeletionTimestamp() != nil {
return getDeletedObjectStatus(accessor.GetDeletionTimestamp(), kind)
}
if lookup == nil {
return ObjectStatus{}, errors.New("status lookup is nil")
}
var oStatus ObjectStatus
fn, ok := lookup[statusKey{apiVersion: apiVersion, kind: kind}]
if ok {
oStatus, err = fn(ctx, object, o, link)
} else {
oStatus = ObjectStatus{
NodeStatus: component.NodeStatusOK,
Details: []component.Component{component.NewText(fmt.Sprintf("%s %s is OK", apiVersion, kind))},
Properties: []component.Property{},
}
}
if link != nil {
events, err := store.EventsForObject(ctx, object, o)
if err == nil && len(events.Items) > 0 {
var lastEvent = events.Items[0]
for i := 1; i < len(events.Items); i++ {
a, err := strconv.Atoi(events.Items[i].ResourceVersion)
b, err := strconv.Atoi(lastEvent.ResourceVersion)
if err == nil && a > b {
lastEvent = events.Items[i]
}
}
if &lastEvent != nil {
messageLink, err := link.ForObject(&lastEvent, lastEvent.Message)
if err == nil {
oStatus.InsertProperty("Last Event", messageLink)
}
}
}
}
oStatus.InsertProperty("Created", component.NewTimestamp(accessor.GetCreationTimestamp().Time))
oStatus.InsertProperty("Namespace", component.NewText(accessor.GetNamespace()))
return oStatus, nil
}
func surpassTerminationThreshold(deleteTime time.Time, threshold time.Duration) (component.NodeStatus, string) {
now := time.Now()
duration := now.Sub(deleteTime)
if duration > terminatingThreshold {
return component.NodeStatusError, "has been deleting for longer than 5 minutes due to finalizers"
} else {
return component.NodeStatusWarning, "is being deleted"
}
}
func getDeletedObjectStatus(timeDeletion *metav1.Time, kind string) (ObjectStatus, error) {
ns, message := surpassTerminationThreshold(timeDeletion.Time, terminatingThreshold)
return ObjectStatus{
NodeStatus: ns,
Details: []component.Component{
component.NewTextf("%s %s", kind, message),
},
Properties: []component.Property{
{
Label: "Deleted Date",
Value: component.NewTimestamp(timeDeletion.Time),
},
},
}, nil
}