@@ -83,24 +83,41 @@ func (r *Resources) InspectPVCs(ctx context.Context) (util.Interval, error) {
8383
8484 // Resize inspector
8585 groupSpec := spec .GetServerGroupSpec (group )
86- if requestedSize , ok := groupSpec .Resources .Requests [apiv1 .ResourceStorage ]; ok {
87- if volumeSize , ok := p .Spec .Resources .Requests [apiv1 .ResourceStorage ]; ok {
88- cmp := volumeSize .Cmp (requestedSize )
89- if cmp < 0 {
90- // Size of the volume is smaller than the requested size
91- // Update the pvc with the request size
92- p .Spec .Resources .Requests [apiv1 .ResourceStorage ] = requestedSize
93-
94- log .Debug ().Str ("pvc-capacity" , volumeSize .String ()).Str ("requested" , requestedSize .String ()).Msg ("PVC capacity differs - updating" )
95- kube := r .context .GetKubeCli ()
96- if _ , err := kube .CoreV1 ().PersistentVolumeClaims (r .context .GetNamespace ()).Update (& p ); err != nil {
97- log .Error ().Err (err ).Msg ("Failed to update pvc" )
98- }
86+
87+ if groupSpec .HasVolumeClaimTemplate () {
88+ res := groupSpec .GetVolumeClaimTemplate ().Spec .Resources .Requests
89+ // For pvc only resources.requests is mutable
90+ if compareResourceList (p .Spec .Resources .Requests , res ) {
91+ p .Spec .Resources .Requests = res
92+ log .Debug ().Msg ("volumeClaimTemplate requested resources changed - updating" )
93+ kube := r .context .GetKubeCli ()
94+ if _ , err := kube .CoreV1 ().PersistentVolumeClaims (r .context .GetNamespace ()).Update (& p ); err != nil {
95+ log .Error ().Err (err ).Msg ("Failed to update pvc" )
96+ } else {
9997 r .context .CreateEvent (k8sutil .NewPVCResizedEvent (r .context .GetAPIObject (), p .Name ))
100- } else if cmp > 0 {
101- log .Error ().Str ("server-group" , group .AsRole ()).Str ("pvc-storage-size" , volumeSize .String ()).Str ("requested-size" , requestedSize .String ()).
102- Msg ("Volume size should not shrink" )
103- r .context .CreateEvent (k8sutil .NewCannotShrinkVolumeEvent (r .context .GetAPIObject (), p .Name ))
98+ }
99+ }
100+ } else {
101+ if requestedSize , ok := groupSpec .Resources .Requests [apiv1 .ResourceStorage ]; ok {
102+ if volumeSize , ok := p .Spec .Resources .Requests [apiv1 .ResourceStorage ]; ok {
103+ cmp := volumeSize .Cmp (requestedSize )
104+ if cmp < 0 {
105+ // Size of the volume is smaller than the requested size
106+ // Update the pvc with the request size
107+ p .Spec .Resources .Requests [apiv1 .ResourceStorage ] = requestedSize
108+
109+ log .Debug ().Str ("pvc-capacity" , volumeSize .String ()).Str ("requested" , requestedSize .String ()).Msg ("PVC capacity differs - updating" )
110+ kube := r .context .GetKubeCli ()
111+ if _ , err := kube .CoreV1 ().PersistentVolumeClaims (r .context .GetNamespace ()).Update (& p ); err != nil {
112+ log .Error ().Err (err ).Msg ("Failed to update pvc" )
113+ } else {
114+ r .context .CreateEvent (k8sutil .NewPVCResizedEvent (r .context .GetAPIObject (), p .Name ))
115+ }
116+ } else if cmp > 0 {
117+ log .Error ().Str ("server-group" , group .AsRole ()).Str ("pvc-storage-size" , volumeSize .String ()).Str ("requested-size" , requestedSize .String ()).
118+ Msg ("Volume size should not shrink" )
119+ r .context .CreateEvent (k8sutil .NewCannotShrinkVolumeEvent (r .context .GetAPIObject (), p .Name ))
120+ }
104121 }
105122 }
106123 }
@@ -118,3 +135,21 @@ func (r *Resources) InspectPVCs(ctx context.Context) (util.Interval, error) {
118135
119136 return nextInterval , nil
120137}
138+
139+ func compareResourceList (wanted , given apiv1.ResourceList ) bool {
140+ for k , v := range wanted {
141+ if gv , ok := given [k ]; ! ok {
142+ return true
143+ } else if v .Cmp (gv ) != 0 {
144+ return true
145+ }
146+ }
147+
148+ for k := range given {
149+ if _ , ok := wanted [k ]; ! ok {
150+ return true
151+ }
152+ }
153+
154+ return false
155+ }
0 commit comments