/
info.go
254 lines (228 loc) · 7.89 KB
/
info.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package workloads
import (
"context"
"fmt"
"time"
"github.com/fairwindsops/controller-utils/pkg/controller"
"github.com/samber/lo"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
const KindIngress = "Ingress"
// ControllerResult provides a wrapper around a PodResult
type ControllerResult struct {
Kind string
Name string
Namespace string
Annotations map[string]string
Labels map[string]string
PodLabels map[string]string
PodAnnotations map[string]string
UID string
ParentUID string
PodCount float64
Containers []ContainerResult
}
type Ingress struct {
Kind string
Name string
Namespace string
Annotations map[string]string
Labels map[string]string
UID string
APIVersion string
}
// ContainerResult provides a list of validation messages for each container.
type ContainerResult struct {
Name string
Image string
ImageID string
CreationTime time.Time
Resource ResourceResult
}
// ResourceResult provides resources information.
type ResourceResult struct {
Requests ResourcesInfo
Limits ResourcesInfo
}
// ResourcesInfo provides a request/limit item information.
type ResourcesInfo struct {
Memory string
CPU string
}
// NodeSummary gives highlevel overview of node informations
type NodeSummary struct {
Name string
Labels map[string]string
Annotations map[string]string
CreationTimestamp time.Time
Capacity corev1.ResourceList
Allocatable corev1.ResourceList
AllocatedLimits corev1.ResourceList
AllocatedRequests corev1.ResourceList
Utilization NodeUtilization
KubeletVersion string
KubeProxyVersion string
IsControlPlaneNode bool
}
// ClusterWorkloadReport contains k8s workload resources report structure
type ClusterWorkloadReport struct {
ServerVersion string
CreationTime time.Time
SourceName string
SourceType string
Nodes []NodeSummary
Namespaces []corev1.Namespace
Controllers []ControllerResult
Ingresses []Ingress
}
func getOwnerUID(ownerReferences []metav1.OwnerReference) string {
ownerUID := ""
if len(ownerReferences) > 0 {
ownerUID = string(ownerReferences[0].UID)
}
return ownerUID
}
func formatControllers(kind, name, namespace, uid string, ownerReferences []metav1.OwnerReference, containers []ContainerResult, annotations, labels, podLabels, podAnnotations map[string]string) ControllerResult {
var podCount float64 = 0
if kind == "Pod" {
podCount = 1
}
ownerUID := getOwnerUID(ownerReferences)
return ControllerResult{kind, name, namespace, annotations, labels, podLabels, podAnnotations, uid, ownerUID, podCount, containers}
}
func formatContainer(container corev1.Container, containerStatus corev1.ContainerStatus, time metav1.Time) ContainerResult {
resources := ResourceResult{
Requests: ResourcesInfo{
CPU: container.Resources.Requests.Cpu().String(),
Memory: container.Resources.Requests.Memory().String(),
},
Limits: ResourcesInfo{
CPU: container.Resources.Limits.Cpu().String(),
Memory: container.Resources.Limits.Memory().String(),
},
}
if container.Resources.Requests.Cpu().IsZero() && !container.Resources.Limits.Cpu().IsZero() {
resources.Requests.CPU = resources.Limits.CPU
}
if container.Resources.Requests.Memory().IsZero() && !container.Resources.Limits.Memory().IsZero() {
resources.Requests.Memory = resources.Limits.Memory
}
containerResult := ContainerResult{
Name: container.Name,
Image: container.Image,
ImageID: containerStatus.ImageID,
CreationTime: time.UTC(),
Resource: resources,
}
return containerResult
}
// CreateResourceProviderFromAPI creates a new ResourceProvider from an existing k8s interface
func CreateResourceProviderFromAPI(ctx context.Context, dynamicClient dynamic.Interface, restMapper meta.RESTMapper, kube kubernetes.Interface, clusterName string) (*ClusterWorkloadReport, error) {
listOpts := metav1.ListOptions{}
interfaces := []ControllerResult{}
serverVersion, err := kube.Discovery().ServerVersion()
if err != nil {
return nil, fmt.Errorf("error fetching Cluster API version: %v", err)
}
client := controller.Client{
Context: ctx,
Dynamic: dynamicClient,
RESTMapper: restMapper,
}
workloads, err := client.GetAllTopControllersSummary("")
if err != nil {
return nil, fmt.Errorf("error while getting all TopControllers: %v", err)
}
for _, workload := range workloads {
topController := workload.TopController
var containers []ContainerResult
if workload.PodSpec != nil {
for _, ctn := range workload.PodSpec.Containers {
containers = append(containers, formatContainer(ctn, corev1.ContainerStatus{}, topController.GetCreationTimestamp()))
}
}
var podLabels, podAnnotations map[string]string
if workload.PodMetadata != nil {
podLabels = workload.PodMetadata.Labels
podAnnotations = workload.PodMetadata.Annotations
}
controller := formatControllers(topController.GetKind(), topController.GetName(), topController.GetNamespace(), string(topController.GetUID()), topController.GetOwnerReferences(), containers, topController.GetAnnotations(), topController.GetLabels(), podLabels, podAnnotations)
controller.PodCount = float64(workload.RunningPodCount)
interfaces = append(interfaces, controller)
}
// Nodes
nodes, err := kube.CoreV1().Nodes().List(ctx, listOpts)
if err != nil {
return nil, fmt.Errorf("error fetching Nodes: %v", err)
}
nodesSummaries := make([]NodeSummary, 0)
for _, item := range nodes.Items {
node := NodeSummary{
Name: item.GetName(),
Labels: item.GetLabels(),
Annotations: item.GetAnnotations(),
CreationTimestamp: item.GetCreationTimestamp().UTC(),
Capacity: item.Status.Capacity,
Allocatable: item.Status.Allocatable,
KubeletVersion: item.Status.NodeInfo.KubeletVersion,
KubeProxyVersion: item.Status.NodeInfo.KubeProxyVersion,
IsControlPlaneNode: checkIfNodeIsControlPlane(item.GetLabels()),
}
allocated, utilization, err := GetNodeAllocatedResource(ctx, kube, item)
if err != nil {
return nil, fmt.Errorf("error fetching node allocation: %v", err)
}
node.AllocatedLimits = allocated.Limits
node.AllocatedRequests = allocated.Requests
node.Utilization = utilization
nodesSummaries = append(nodesSummaries, node)
}
// Namespaces
namespaces, err := kube.CoreV1().Namespaces().List(ctx, listOpts)
if err != nil {
return nil, fmt.Errorf("error fetching Namespaces: %v", err)
}
// Ingresses
ingresses := []Ingress{}
for _, namespace := range namespaces.Items {
ingressesV1 := kube.NetworkingV1().Ingresses(namespace.Name)
list, err := ingressesV1.List(ctx, listOpts)
if err != nil {
return nil, fmt.Errorf("error fetching ingresses: %v", err)
}
for _, item := range list.Items {
ingress := Ingress{
Kind: KindIngress,
Name: item.Name,
Namespace: item.Namespace,
Annotations: item.Annotations,
Labels: item.Labels,
UID: string(item.UID),
}
if len(item.ManagedFields) > 0 {
ingress.APIVersion = item.ManagedFields[0].APIVersion
}
ingresses = append(ingresses, ingress)
}
}
clusterWorkloadReport := ClusterWorkloadReport{
ServerVersion: serverVersion.Major + "." + serverVersion.Minor,
SourceType: "Cluster",
SourceName: clusterName,
CreationTime: time.Now(),
Nodes: nodesSummaries,
Namespaces: namespaces.Items,
Controllers: interfaces,
Ingresses: ingresses,
}
return &clusterWorkloadReport, nil
}
func checkIfNodeIsControlPlane(labels map[string]string) bool {
keys := lo.Keys[string, string](labels)
return lo.Contains(keys, "node-role.kubernetes.io/control-plane") || lo.Contains(keys, "node-role.kubernetes.io/master")
}