-
Notifications
You must be signed in to change notification settings - Fork 131
/
summary.go
283 lines (239 loc) · 8.53 KB
/
summary.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2019 FairwindsOps Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package summary
import (
"context"
"strings"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/sets"
vpav1 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
"k8s.io/klog"
"github.com/fairwindsops/goldilocks/pkg/utils"
)
const (
namespaceAllNamespaces = ""
)
// Summary is for storing a summary of recommendation data by namespace/deployment/container
type Summary struct {
Namespaces map[string]namespaceSummary
}
type namespaceSummary struct {
Namespace string `json:"namespace"`
Deployments map[string]deploymentSummary `json:"deployments"`
}
type deploymentSummary struct {
DeploymentName string `json:"deploymentName"`
Containers map[string]containerSummary `json:"containers"`
}
type containerSummary struct {
ContainerName string `json:"containerName"`
// recommendations
LowerBound corev1.ResourceList `json:"lowerBound"`
UpperBound corev1.ResourceList `json:"upperBound"`
Target corev1.ResourceList `json:"target"`
UncappedTarget corev1.ResourceList `json:"uncappedTarget"`
Limits corev1.ResourceList `json:"limits"`
Requests corev1.ResourceList `json:"requests"`
}
// Summarizer represents a source of generating a summary of VPAs
type Summarizer struct {
options
// cached list of vpas
vpas []vpav1.VerticalPodAutoscaler
// cached map of deploy/vpa name -> deployment
deploymentForVPANamed map[string]*appsv1.Deployment
}
// NewSummarizer returns a Summarizer for all goldilocks managed VPAs in all Namespaces
func NewSummarizer(setters ...Option) *Summarizer {
opts := defaultOptions()
for _, setter := range setters {
setter(opts)
}
return &Summarizer{
options: *opts,
}
}
// NewSummarizerForVPAs returns a Summarizer for a known list of VPAs
func NewSummarizerForVPAs(vpas []vpav1.VerticalPodAutoscaler, setters ...Option) *Summarizer {
summarizer := NewSummarizer(setters...)
// set the cached vpas list directly
summarizer.vpas = vpas
return summarizer
}
// GetSummary returns a Summary of the Summarizer using its options
func (s Summarizer) GetSummary() (Summary, error) {
// blank summary
summary := Summary{
Namespaces: map[string]namespaceSummary{},
}
// if the summarizer is filtering for a single namespace,
// then add that namespace by default to the blank summary
if s.namespace != namespaceAllNamespaces {
summary.Namespaces[s.namespace] = namespaceSummary{
Namespace: s.namespace,
Deployments: map[string]deploymentSummary{},
}
}
// cached vpas and deployments
if s.vpas == nil || s.deploymentForVPANamed == nil {
err := s.Update()
if err != nil {
return summary, err
}
}
// nothing to summarize
if len(s.vpas) <= 0 {
return summary, nil
}
for _, vpa := range s.vpas {
klog.V(8).Infof("Analyzing vpa: %v", vpa.Name)
// get or create the namespaceSummary for this VPA's namespace
namespace := vpa.Namespace
var nsSummary namespaceSummary
if val, ok := summary.Namespaces[namespace]; ok {
nsSummary = val
} else {
nsSummary = namespaceSummary{
Namespace: namespace,
Deployments: map[string]deploymentSummary{},
}
summary.Namespaces[namespace] = nsSummary
}
// VPA.Name := Deployment.Name, as that's how goldilocks works
dSummary := deploymentSummary{
DeploymentName: vpa.Name,
Containers: map[string]containerSummary{},
}
deployment, ok := s.deploymentForVPANamed[vpa.Name]
if !ok {
klog.Errorf("no matching Deployment found for VPA/%s", vpa.Name)
continue
}
if vpa.Status.Recommendation == nil {
klog.V(2).Infof("Empty status on %v", dSummary.DeploymentName)
nsSummary.Deployments[dSummary.DeploymentName] = dSummary
summary.Namespaces[nsSummary.Namespace] = nsSummary
continue
}
if len(vpa.Status.Recommendation.ContainerRecommendations) <= 0 {
klog.V(2).Infof("No container recommendations found in the %v vpa.", dSummary.DeploymentName)
nsSummary.Deployments[dSummary.DeploymentName] = dSummary
summary.Namespaces[nsSummary.Namespace] = nsSummary
continue
}
// get the full set of excluded containers for this Deployment
excludedContainers := sets.NewString().Union(s.excludedContainers)
if val, exists := deployment.GetAnnotations()[utils.DeploymentExcludeContainersAnnotation]; exists {
excludedContainers.Insert(strings.Split(val, ",")...)
}
CONTAINER_REC_LOOP:
for _, containerRecommendation := range vpa.Status.Recommendation.ContainerRecommendations {
if excludedContainers.Has(containerRecommendation.ContainerName) {
klog.V(2).Infof("Excluding container Deployment/%s/%s", dSummary.DeploymentName, containerRecommendation.ContainerName)
continue CONTAINER_REC_LOOP
}
var cSummary containerSummary
for _, c := range deployment.Spec.Template.Spec.Containers {
// find the matching container on the deployment
if c.Name == containerRecommendation.ContainerName {
cSummary = containerSummary{
ContainerName: containerRecommendation.ContainerName,
UpperBound: utils.FormatResourceList(containerRecommendation.UpperBound),
LowerBound: utils.FormatResourceList(containerRecommendation.LowerBound),
Target: utils.FormatResourceList(containerRecommendation.Target),
UncappedTarget: utils.FormatResourceList(containerRecommendation.UncappedTarget),
Limits: utils.FormatResourceList(c.Resources.Limits),
Requests: utils.FormatResourceList(c.Resources.Requests),
}
klog.V(6).Infof("Resources for Deployment/%s/%s: Requests: %v Limits: %v", dSummary.DeploymentName, c.Name, cSummary.Requests, cSummary.Limits)
dSummary.Containers[cSummary.ContainerName] = cSummary
continue CONTAINER_REC_LOOP
}
}
}
// update summary maps
nsSummary.Deployments[dSummary.DeploymentName] = dSummary
summary.Namespaces[nsSummary.Namespace] = nsSummary
}
return summary, nil
}
// Update the set of VPAs and Deployments that the Summarizer uses for creating a summary
func (s *Summarizer) Update() error {
err := s.updateVPAs()
if err != nil {
klog.Error(err.Error())
return err
}
err = s.updateDeployments()
if err != nil {
klog.Error(err.Error())
return err
}
return nil
}
func (s *Summarizer) updateVPAs() error {
nsLog := s.namespace
if s.namespace == namespaceAllNamespaces {
nsLog = "all namespaces"
}
klog.V(3).Infof("Looking for VPAs in %s with labels: %v", nsLog, s.vpaLabels)
vpas, err := s.listVPAs(getVPAListOptionsForLabels(s.vpaLabels))
if err != nil {
return err
}
klog.V(10).Infof("Found vpas: %v", vpas)
s.vpas = vpas
return nil
}
func (s Summarizer) listVPAs(listOptions metav1.ListOptions) ([]vpav1.VerticalPodAutoscaler, error) {
vpas, err := s.vpaClient.Client.AutoscalingV1().VerticalPodAutoscalers(s.namespace).List(context.TODO(), listOptions)
if err != nil {
return nil, err
}
return vpas.Items, nil
}
func getVPAListOptionsForLabels(vpaLabels map[string]string) metav1.ListOptions {
return metav1.ListOptions{
LabelSelector: labels.Set(vpaLabels).String(),
}
}
func (s *Summarizer) updateDeployments() error {
nsLog := s.namespace
if s.namespace == namespaceAllNamespaces {
nsLog = "all namespaces"
}
klog.V(3).Infof("Looking for Deployments in %s", nsLog)
deployments, err := s.listDeployments(metav1.ListOptions{})
if err != nil {
return err
}
klog.V(10).Infof("Found deployments: %v", deployments)
// map the deployment.name -> &deployment for easy vpa lookup (since vpa.Name == deployment.Name for matching vpas/deployments)
s.deploymentForVPANamed = map[string]*appsv1.Deployment{}
for _, d := range deployments {
d := d
s.deploymentForVPANamed[d.Name] = &d
}
return nil
}
func (s Summarizer) listDeployments(listOptions metav1.ListOptions) ([]appsv1.Deployment, error) {
deployments, err := s.kubeClient.Client.AppsV1().Deployments(s.namespace).List(context.TODO(), listOptions)
if err != nil {
return nil, err
}
return deployments.Items, nil
}