@@ -28,13 +28,11 @@ import (
2828
2929func MakeVPA (objectMeta * metav1.ObjectMeta , targetRef * autov2.CrossVersionObjectReference , vpa * v1alpha1.VPASpec ) * vpav1.VerticalPodAutoscaler {
3030 containerName := GetVPAContainerName (objectMeta )
31- updatePolicy := vpa .UpdatePolicy
32- UpdateVPAUpdatePolicy (updatePolicy , vpa .ResourceUnit )
31+ updatePolicy := UpdateVPAUpdatePolicy (vpa .UpdatePolicy , vpa .ResourceUnit )
3332 if vpa .ResourceUnit != nil {
3433 objectMeta .Labels [LabelCustomResourceUnit ] = "true"
3534 }
36- resourcePolicy := vpa .ResourcePolicy
37- UpdateResourcePolicy (resourcePolicy , containerName )
35+ resourcePolicy := UpdateResourcePolicy (vpa .ResourcePolicy , containerName )
3836
3937 return & vpav1.VerticalPodAutoscaler {
4038 TypeMeta : metav1.TypeMeta {
@@ -67,19 +65,24 @@ func GetVPAContainerName(objectMeta *metav1.ObjectMeta) string {
6765 return containerName
6866}
6967
70- func UpdateVPAUpdatePolicy (updatePolicy * vpav1.PodUpdatePolicy , resourceUnit * v1alpha1.ResourceUnit ) {
71- if updatePolicy != nil && resourceUnit != nil {
68+ func UpdateVPAUpdatePolicy (updatePolicy * vpav1.PodUpdatePolicy , resourceUnit * v1alpha1.ResourceUnit ) * vpav1.PodUpdatePolicy {
69+ resultUpdatePolicy := updatePolicy
70+ if resourceUnit != nil {
71+ if resultUpdatePolicy == nil {
72+ resultUpdatePolicy = & vpav1.PodUpdatePolicy {}
73+ }
7274 off := vpav1 .UpdateModeOff
73- updatePolicy .UpdateMode = & off
75+ resultUpdatePolicy .UpdateMode = & off
7476 }
77+ return resultUpdatePolicy
7578}
7679
77- func UpdateResourcePolicy (resourcePolicy * vpav1.PodResourcePolicy , containerName string ) {
80+ func UpdateResourcePolicy (resourcePolicy * vpav1.PodResourcePolicy , containerName string ) * vpav1.PodResourcePolicy {
81+ var containerPolicies []vpav1.ContainerResourcePolicy
82+ containerScalingMode := vpav1 .ContainerScalingModeAuto
7883 if resourcePolicy != nil {
79- var containerPolicies []vpav1.ContainerResourcePolicy
80- containerScalingMode := vpav1 .ContainerScalingModeAuto
8184 if resourcePolicy .ContainerPolicies == nil {
82- resourcePolicy . ContainerPolicies = []vpav1.ContainerResourcePolicy {}
85+ containerPolicies = []vpav1.ContainerResourcePolicy {}
8386 }
8487 for _ , policy := range resourcePolicy .ContainerPolicies {
8588 if policy .ContainerName == containerName {
@@ -88,15 +91,21 @@ func UpdateResourcePolicy(resourcePolicy *vpav1.PodResourcePolicy, containerName
8891 break
8992 }
9093 }
91- // if resource policy is not set, set the default policy, so the vpa policy won't be applied to other containers
92- if len (containerPolicies ) == 0 {
93- containerPolicies = []vpav1.ContainerResourcePolicy {
94- {
95- ContainerName : containerName ,
96- Mode : & containerScalingMode ,
97- },
98- }
94+ }
95+
96+ // if resource policy is not set, set the default policy, so the vpa policy won't be applied to other containers
97+ if len (containerPolicies ) == 0 {
98+ containerPolicies = []vpav1.ContainerResourcePolicy {
99+ {
100+ ContainerName : containerName ,
101+ Mode : & containerScalingMode ,
102+ },
99103 }
100- resourcePolicy .ContainerPolicies = containerPolicies
101104 }
105+ resultResourcePolicy := resourcePolicy
106+ if resultResourcePolicy == nil {
107+ resultResourcePolicy = & vpav1.PodResourcePolicy {}
108+ }
109+ resultResourcePolicy .ContainerPolicies = containerPolicies
110+ return resultResourcePolicy
102111}
0 commit comments