Skip to content

Commit 068565c

Browse files
authored
Merge pull request #9155 from mayuka-c/add-revive-rules-part4
[VPA] Enable multiple revive rules - 4
2 parents 0ccfef9 + 1c0ca03 commit 068565c

File tree

92 files changed

+2016
-1984
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+2016
-1984
lines changed

hack/verify-spelling.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ git ls-files --full-name \
3232
| grep -v cluster-autoscaler/cloudprovider/ionoscloud/ionos-cloud-sdk-go \
3333
| grep -v cluster-autoscaler/cloudprovider/volcengine/volcengine-go-sdk \
3434
| grep -v cluster-autoscaler/cloudprovider/civo \
35+
| grep -v vertical-pod-autoscaler/.golangci.yaml \
3536
| xargs misspell -error -o stderr

vertical-pod-autoscaler/.golangci.yaml

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ linters:
33
enable:
44
- forbidigo
55
- modernize
6+
- importas
67
- revive
78
settings:
89
forbidigo:
@@ -12,8 +13,32 @@ linters:
1213
analyze-types: true
1314
modernize:
1415
disable:
15-
# Disabling omitzero linter as it catches isssues with API types, which would be best if kube-api-lint can handle
16+
# Disabling omitzero linter as it catches issues with API types, which would be best if kube-api-lint can handle
1617
- omitzero
18+
19+
importas:
20+
alias:
21+
# Inspired by Kubernetes' import alias conventions for consistency:
22+
# https://github.com/kubernetes/kubernetes/blob/master/hack/.import-aliases
23+
- pkg: "k8s.io/api/admission/v1"
24+
alias: admissionv1
25+
- pkg: k8s.io/api/apps/v1
26+
alias: appsv1
27+
- pkg: "k8s.io/api/autoscaling/v1"
28+
alias: autoscalingv1
29+
- pkg: k8s.io/api/batch/v1
30+
alias: batchv1
31+
- pkg: "k8s.io/api/core/v1"
32+
alias: corev1
33+
- pkg: k8s.io/api/policy/v1
34+
alias: policyv1
35+
- pkg: "k8s.io/apimachinery/pkg/apis/meta/v1"
36+
alias: metav1
37+
- pkg: k8s.io/client-go/kubernetes/typed/core/v1
38+
alias: typedcorev1
39+
- pkg: k8s.io/client-go/listers/core/v1
40+
alias: listersv1
41+
1742
revive:
1843
enable-all-rules: false # Disabling all rules for now and enabling each one later
1944
rules:
@@ -31,6 +56,8 @@ linters:
3156
- name: empty-lines
3257
- name: use-errors-new
3358
- name: early-return
59+
- name: redefines-builtin-id
60+
- name: redundant-import-alias
3461

3562
# Below lists the rules disabled
3663

@@ -65,6 +92,7 @@ linters:
6592
# Flags exported funcs returning unexported types; disabled for intentional encapsulation in internal APIs.
6693
- name: unexported-return
6794
disabled: true
95+
6896
formatters:
6997
enable:
7098
- goimports

vertical-pod-autoscaler/common/flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ package common
2121
import (
2222
"flag"
2323

24-
apiv1 "k8s.io/api/core/v1"
24+
corev1 "k8s.io/api/core/v1"
2525
"k8s.io/klog/v2"
2626
)
2727

@@ -42,7 +42,7 @@ func InitCommonFlags() *CommonFlags {
4242
flag.Float64Var(&cf.KubeApiQps, "kube-api-qps", 50.0, "QPS limit when making requests to Kubernetes apiserver")
4343
flag.Float64Var(&cf.KubeApiBurst, "kube-api-burst", 100.0, "QPS burst limit when making requests to Kubernetes apiserver")
4444
flag.BoolVar(&cf.EnableProfiling, "profiling", false, "Is debug/pprof endpoint enabled")
45-
flag.StringVar(&cf.VpaObjectNamespace, "vpa-object-namespace", apiv1.NamespaceAll, "Specifies the namespace to search for VPA objects. Leave empty to include all namespaces. If provided, the garbage collector will only clean this namespace.")
45+
flag.StringVar(&cf.VpaObjectNamespace, "vpa-object-namespace", corev1.NamespaceAll, "Specifies the namespace to search for VPA objects. Leave empty to include all namespaces. If provided, the garbage collector will only clean this namespace.")
4646
flag.StringVar(&cf.IgnoredVpaObjectNamespaces, "ignored-vpa-object-namespaces", "", "A comma-separated list of namespaces to ignore when searching for VPA objects. Leave empty to avoid ignoring any namespaces. These namespaces will not be cleaned by the garbage collector.")
4747
return cf
4848
}

vertical-pod-autoscaler/pkg/admission-controller/resource/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package resource
1919
import (
2020
"context"
2121

22-
v1 "k8s.io/api/admission/v1"
22+
admissionv1 "k8s.io/api/admission/v1"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424

2525
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/metrics/admission"
@@ -41,5 +41,5 @@ type Handler interface {
4141
// DisallowIncorrectObjects returns whether incorrect objects (eg. unparsable, not passing validations) should be disallowed by Admission Server.
4242
DisallowIncorrectObjects() bool
4343
// GetPatches returns patches for given AdmissionRequest
44-
GetPatches(context.Context, *v1.AdmissionRequest) ([]PatchRecord, error)
44+
GetPatches(context.Context, *admissionv1.AdmissionRequest) ([]PatchRecord, error)
4545
}

vertical-pod-autoscaler/pkg/admission-controller/resource/pod/handler_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424

2525
"github.com/stretchr/testify/assert"
2626
admissionv1 "k8s.io/api/admission/v1"
27-
apiv1 "k8s.io/api/core/v1"
28-
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
corev1 "k8s.io/api/core/v1"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"k8s.io/apimachinery/pkg/runtime"
3030

3131
resource_admission "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource"
@@ -38,15 +38,15 @@ type fakePodPreProcessor struct {
3838
err error
3939
}
4040

41-
func (fpp *fakePodPreProcessor) Process(pod apiv1.Pod) (apiv1.Pod, error) {
41+
func (fpp *fakePodPreProcessor) Process(pod corev1.Pod) (corev1.Pod, error) {
4242
return pod, fpp.err
4343
}
4444

4545
type fakeVpaMatcher struct {
4646
vpa *vpa_types.VerticalPodAutoscaler
4747
}
4848

49-
func (m *fakeVpaMatcher) GetMatchingVPA(_ context.Context, _ *apiv1.Pod) *vpa_types.VerticalPodAutoscaler {
49+
func (m *fakeVpaMatcher) GetMatchingVPA(_ context.Context, _ *corev1.Pod) *vpa_types.VerticalPodAutoscaler {
5050
return m.vpa
5151
}
5252

@@ -59,7 +59,7 @@ func (*fakePatchCalculator) PatchResourceTarget() patch.PatchResourceTarget {
5959
return patch.Pod
6060
}
6161

62-
func (c *fakePatchCalculator) CalculatePatches(_ *apiv1.Pod, _ *vpa_types.VerticalPodAutoscaler) (
62+
func (c *fakePatchCalculator) CalculatePatches(_ *corev1.Pod, _ *vpa_types.VerticalPodAutoscaler) (
6363
[]resource_admission.PatchRecord, error) {
6464
return c.patches, c.err
6565
}
@@ -184,7 +184,7 @@ func TestGetPatches(t *testing.T) {
184184
fvm := &fakeVpaMatcher{vpa: tc.vpa}
185185
h := NewResourceHandler(fppp, fvm, tc.calculators)
186186
patches, err := h.GetPatches(context.Background(), &admissionv1.AdmissionRequest{
187-
Resource: v1.GroupVersionResource{
187+
Resource: metav1.GroupVersionResource{
188188
Version: "v1",
189189
},
190190
Namespace: tc.namespace,

vertical-pod-autoscaler/pkg/admission-controller/resource/pod/patch/calculator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package patch
1818

1919
import (
20-
core "k8s.io/api/core/v1"
20+
corev1 "k8s.io/api/core/v1"
2121

2222
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource"
2323
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
@@ -38,7 +38,7 @@ const (
3838

3939
// Calculator is capable of calculating required patches for pod.
4040
type Calculator interface {
41-
CalculatePatches(pod *core.Pod, vpa *vpa_types.VerticalPodAutoscaler) ([]resource.PatchRecord, error)
41+
CalculatePatches(pod *corev1.Pod, vpa *vpa_types.VerticalPodAutoscaler) ([]resource.PatchRecord, error)
4242
// PatchResourceTarget returns the resource this calculator should calculate patches for.
4343
PatchResourceTarget() PatchResourceTarget
4444
}

vertical-pod-autoscaler/pkg/admission-controller/resource/pod/patch/observed_containers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package patch
1818

1919
import (
20-
core "k8s.io/api/core/v1"
20+
corev1 "k8s.io/api/core/v1"
2121

2222
resource_admission "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource"
2323
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
@@ -26,7 +26,7 @@ import (
2626

2727
type observedContainers struct{}
2828

29-
func (*observedContainers) CalculatePatches(pod *core.Pod, _ *vpa_types.VerticalPodAutoscaler) ([]resource_admission.PatchRecord, error) {
29+
func (*observedContainers) CalculatePatches(pod *corev1.Pod, _ *vpa_types.VerticalPodAutoscaler) ([]resource_admission.PatchRecord, error) {
3030
vpaObservedContainersValue := annotations.GetVpaObservedContainersValue(pod)
3131
return []resource_admission.PatchRecord{GetAddAnnotationPatch(annotations.VpaObservedContainersLabel, vpaObservedContainersValue)}, nil
3232
}

vertical-pod-autoscaler/pkg/admission-controller/resource/pod/patch/observed_containers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/stretchr/testify/assert"
24-
core "k8s.io/api/core/v1"
24+
corev1 "k8s.io/api/core/v1"
2525

2626
resource_admission "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource"
2727
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/annotations"
@@ -38,7 +38,7 @@ func addVpaObservedContainersPatch(containerNames []string) resource_admission.P
3838
func TestCalculatePatches_ObservedContainers(t *testing.T) {
3939
tests := []struct {
4040
name string
41-
pod *core.Pod
41+
pod *corev1.Pod
4242
expectedPatch resource_admission.PatchRecord
4343
}{
4444
{

vertical-pod-autoscaler/pkg/admission-controller/resource/pod/patch/resource_updates.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"fmt"
2222
"strings"
2323

24-
core "k8s.io/api/core/v1"
24+
corev1 "k8s.io/api/core/v1"
2525
"k8s.io/apimachinery/pkg/api/resource"
2626

2727
resource_admission "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource"
@@ -57,7 +57,7 @@ func (*resourcesUpdatesPatchCalculator) PatchResourceTarget() PatchResourceTarge
5757
return Pod
5858
}
5959

60-
func (c *resourcesUpdatesPatchCalculator) CalculatePatches(pod *core.Pod, vpa *vpa_types.VerticalPodAutoscaler) ([]resource_admission.PatchRecord, error) {
60+
func (c *resourcesUpdatesPatchCalculator) CalculatePatches(pod *corev1.Pod, vpa *vpa_types.VerticalPodAutoscaler) ([]resource_admission.PatchRecord, error) {
6161
result := []resource_admission.PatchRecord{}
6262

6363
containersResources, annotationsPerContainer, err := c.recommendationProvider.GetContainersResourcesForPod(pod, vpa)
@@ -110,7 +110,7 @@ func (c *resourcesUpdatesPatchCalculator) CalculatePatches(pod *core.Pod, vpa *v
110110
return result, nil
111111
}
112112

113-
func getContainerPatch(pod *core.Pod, i int, annotationsPerContainer vpa_api_util.ContainerToAnnotationsMap, containerResources vpa_api_util.ContainerResources) ([]resource_admission.PatchRecord, string) {
113+
func getContainerPatch(pod *corev1.Pod, i int, annotationsPerContainer vpa_api_util.ContainerToAnnotationsMap, containerResources vpa_api_util.ContainerResources) ([]resource_admission.PatchRecord, string) {
114114
var patches []resource_admission.PatchRecord
115115
// Add empty resources object if missing.
116116
requests, limits := resourcehelpers.ContainerRequestsAndLimits(pod.Spec.Containers[i].Name, pod)
@@ -130,7 +130,7 @@ func getContainerPatch(pod *core.Pod, i int, annotationsPerContainer vpa_api_uti
130130
return patches, updatesAnnotation
131131
}
132132

133-
func appendPatchesAndAnnotations(patches []resource_admission.PatchRecord, annotations []string, current core.ResourceList, containerIndex int, resources core.ResourceList, fieldName, resourceName string) ([]resource_admission.PatchRecord, []string) {
133+
func appendPatchesAndAnnotations(patches []resource_admission.PatchRecord, annotations []string, current corev1.ResourceList, containerIndex int, resources corev1.ResourceList, fieldName, resourceName string) ([]resource_admission.PatchRecord, []string) {
134134
// Add empty object if it's missing and we're about to fill it.
135135
if current == nil && len(resources) > 0 {
136136
patches = append(patches, GetPatchInitializingEmptyResourcesSubfield(containerIndex, fieldName))
@@ -142,7 +142,7 @@ func appendPatchesAndAnnotations(patches []resource_admission.PatchRecord, annot
142142
return patches, annotations
143143
}
144144

145-
func (c *resourcesUpdatesPatchCalculator) applyCPUStartupBoost(container *core.Container, vpa *vpa_types.VerticalPodAutoscaler, containerResources *vpa_api_util.ContainerResources) ([]resource_admission.PatchRecord, error) {
145+
func (c *resourcesUpdatesPatchCalculator) applyCPUStartupBoost(container *corev1.Container, vpa *vpa_types.VerticalPodAutoscaler, containerResources *vpa_api_util.ContainerResources) ([]resource_admission.PatchRecord, error) {
146146
var patches []resource_admission.PatchRecord
147147

148148
startupBoostPolicy := getContainerStartupBoostPolicy(container, vpa)
@@ -164,7 +164,7 @@ func (c *resourcesUpdatesPatchCalculator) applyCPUStartupBoost(container *core.C
164164
return patches, nil
165165
}
166166

167-
func getContainerStartupBoostPolicy(container *core.Container, vpa *vpa_types.VerticalPodAutoscaler) *vpa_types.StartupBoost {
167+
func getContainerStartupBoostPolicy(container *corev1.Container, vpa *vpa_types.VerticalPodAutoscaler) *vpa_types.StartupBoost {
168168
policy := vpa_api_util.GetContainerResourcePolicy(container.Name, vpa.Spec.ResourcePolicy)
169169
startupBoost := vpa.Spec.StartupBoost
170170
if policy != nil && policy.StartupBoost != nil {
@@ -226,52 +226,52 @@ func (c *resourcesUpdatesPatchCalculator) calculateBoostedCPU(recommendedCPU, or
226226

227227
// capStartupBoostToContainerLimit makes sure startup boost recommendation is not above current limit for the container for CPU.
228228
// It attempts to keep the request 1m below the limit to maintain QoS.
229-
func capStartupBoostToContainerLimit(recommendation core.ResourceList, containerLimits core.ResourceList) {
230-
limit, found := containerLimits[core.ResourceCPU]
229+
func capStartupBoostToContainerLimit(recommendation corev1.ResourceList, containerLimits corev1.ResourceList) {
230+
limit, found := containerLimits[corev1.ResourceCPU]
231231
if !found {
232232
return
233233
}
234234

235-
recommendedValue, found := recommendation[core.ResourceCPU]
235+
recommendedValue, found := recommendation[corev1.ResourceCPU]
236236
if found && recommendedValue.MilliValue() > limit.MilliValue() {
237237
newRecommended := limit.DeepCopy()
238238
if limit.Cmp(resource.MustParse("1m")) > 0 {
239239
newRecommended.Sub(resource.MustParse("1m"))
240240
}
241-
recommendation[core.ResourceCPU] = newRecommended
241+
recommendation[corev1.ResourceCPU] = newRecommended
242242
}
243243
}
244244

245-
func (c *resourcesUpdatesPatchCalculator) applyControlledCPUResources(container *core.Container, vpa *vpa_types.VerticalPodAutoscaler, containerResources *vpa_api_util.ContainerResources, startupBoostPolicy *vpa_types.StartupBoost) error {
245+
func (c *resourcesUpdatesPatchCalculator) applyControlledCPUResources(container *corev1.Container, vpa *vpa_types.VerticalPodAutoscaler, containerResources *vpa_api_util.ContainerResources, startupBoostPolicy *vpa_types.StartupBoost) error {
246246
controlledValues := vpa_api_util.GetContainerControlledValues(container.Name, vpa.Spec.ResourcePolicy)
247247

248-
recommendedRequest := containerResources.Requests[core.ResourceCPU]
249-
originalRequest := container.Resources.Requests[core.ResourceCPU]
248+
recommendedRequest := containerResources.Requests[corev1.ResourceCPU]
249+
originalRequest := container.Resources.Requests[corev1.ResourceCPU]
250250
boostedRequest, err := c.calculateBoostedCPU(recommendedRequest, originalRequest, startupBoostPolicy)
251251
if err != nil {
252252
return err
253253
}
254254

255255
if containerResources.Requests == nil {
256-
containerResources.Requests = core.ResourceList{}
256+
containerResources.Requests = corev1.ResourceList{}
257257
}
258-
containerResources.Requests[core.ResourceCPU] = *boostedRequest
258+
containerResources.Requests[corev1.ResourceCPU] = *boostedRequest
259259

260260
switch controlledValues {
261261
case vpa_types.ContainerControlledValuesRequestsOnly:
262262
capStartupBoostToContainerLimit(containerResources.Requests, container.Resources.Limits)
263263
case vpa_types.ContainerControlledValuesRequestsAndLimits:
264264
if containerResources.Limits == nil {
265-
containerResources.Limits = core.ResourceList{}
265+
containerResources.Limits = corev1.ResourceList{}
266266
}
267267
newLimits, _ := vpa_api_util.GetProportionalLimit(
268-
container.Resources.Limits, // originalLimits
269-
container.Resources.Requests, // originalRequests
270-
core.ResourceList{core.ResourceCPU: *boostedRequest}, // newRequests
271-
core.ResourceList{}, // defaultLimit
268+
container.Resources.Limits, // originalLimits
269+
container.Resources.Requests, // originalRequests
270+
corev1.ResourceList{corev1.ResourceCPU: *boostedRequest}, // newRequests
271+
corev1.ResourceList{}, // defaultLimit
272272
)
273-
if newLimit, ok := newLimits[core.ResourceCPU]; ok {
274-
containerResources.Limits[core.ResourceCPU] = newLimit
273+
if newLimit, ok := newLimits[corev1.ResourceCPU]; ok {
274+
containerResources.Limits[corev1.ResourceCPU] = newLimit
275275
}
276276
default:
277277
// Do nothing

0 commit comments

Comments
 (0)