Skip to content

Commit 532772c

Browse files
authored
do not call EBS api when there are no pvs (zalando#1851)
* do not call EBS api when there are no pvs * no extra aws api call in executeEBSMigration, operate on fetched cluster.EBSVolumes
1 parent eecd131 commit 532772c

File tree

4 files changed

+18
-30
lines changed

4 files changed

+18
-30
lines changed

pkg/cluster/cluster.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ func New(cfg Config, kubeClient k8sutil.KubernetesClient, pgSpec acidv1.Postgres
149149
cluster.EBSVolumes = make(map[string]volumes.VolumeProperties)
150150
if cfg.OpConfig.StorageResizeMode != "pvc" || cfg.OpConfig.EnableEBSGp3Migration {
151151
cluster.VolumeResizer = &volumes.EBSVolumeResizer{AWSRegion: cfg.OpConfig.AWSRegion}
152-
153152
}
154153

155154
return cluster

pkg/cluster/sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (c *Cluster) Sync(newSpec *acidv1.Postgresql) error {
6969
return err
7070
}
7171

72-
if c.OpConfig.EnableEBSGp3Migration {
72+
if c.OpConfig.EnableEBSGp3Migration && len(c.EBSVolumes) > 0 {
7373
err = c.executeEBSMigration()
7474
if nil != err {
7575
return err

pkg/cluster/volumes.go

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ func (c *Cluster) syncUnderlyingEBSVolume() error {
137137
for _, s := range errors {
138138
c.logger.Warningf(s)
139139
}
140-
// c.logger.Errorf("failed to modify %d of %d volumes", len(c.EBSVolumes), len(errors))
141140
}
142141
return nil
143142
}
@@ -149,7 +148,11 @@ func (c *Cluster) populateVolumeMetaData() error {
149148
if err != nil {
150149
return fmt.Errorf("could not list persistent volumes: %v", err)
151150
}
152-
c.logger.Debugf("found %d volumes, size of known volumes %d", len(pvs), len(c.EBSVolumes))
151+
if len(pvs) == 0 {
152+
c.EBSVolumes = make(map[string]volumes.VolumeProperties)
153+
return fmt.Errorf("no persistent volumes found")
154+
}
155+
c.logger.Debugf("found %d persistent volumes, size of known volumes %d", len(pvs), len(c.EBSVolumes))
153156

154157
volumeIds := []string{}
155158
var volumeID string
@@ -167,7 +170,7 @@ func (c *Cluster) populateVolumeMetaData() error {
167170
return err
168171
}
169172

170-
if len(currentVolumes) != len(c.EBSVolumes) {
173+
if len(currentVolumes) != len(c.EBSVolumes) && len(c.EBSVolumes) > 0 {
171174
c.logger.Debugf("number of ebs volumes (%d) discovered differs from already known volumes (%d)", len(currentVolumes), len(c.EBSVolumes))
172175
}
173176

@@ -205,7 +208,7 @@ func (c *Cluster) syncVolumeClaims() error {
205208

206209
// syncVolumes reads all persistent volumes and checks that their size matches the one declared in the statefulset.
207210
func (c *Cluster) syncEbsVolumes() error {
208-
c.setProcessName("syncing EBS and Claims volumes")
211+
c.setProcessName("syncing EBS volumes")
209212

210213
act, err := c.volumesNeedResizing()
211214
if err != nil {
@@ -451,29 +454,17 @@ func quantityToGigabyte(q resource.Quantity) int64 {
451454
}
452455

453456
func (c *Cluster) executeEBSMigration() error {
454-
if !c.OpConfig.EnableEBSGp3Migration {
455-
return nil
456-
}
457-
c.logger.Infof("starting EBS gp2 to gp3 migration")
458-
459457
pvs, err := c.listPersistentVolumes()
460458
if err != nil {
461459
return fmt.Errorf("could not list persistent volumes: %v", err)
462460
}
463-
c.logger.Debugf("found %d volumes, size of known volumes %d", len(pvs), len(c.EBSVolumes))
464-
465-
volumeIds := []string{}
466-
var volumeID string
467-
for _, pv := range pvs {
468-
volumeID, err = c.VolumeResizer.ExtractVolumeID(pv.Spec.AWSElasticBlockStore.VolumeID)
469-
if err != nil {
470-
continue
471-
}
472-
473-
volumeIds = append(volumeIds, volumeID)
461+
if len(pvs) == 0 {
462+
c.logger.Warningf("no persistent volumes found - skipping EBS migration")
463+
return nil
474464
}
465+
c.logger.Debugf("found %d volumes, size of known volumes %d", len(pvs), len(c.EBSVolumes))
475466

476-
if len(volumeIds) == len(c.EBSVolumes) {
467+
if len(pvs) == len(c.EBSVolumes) {
477468
hasGp2 := false
478469
for _, v := range c.EBSVolumes {
479470
if v.VolumeType == "gp2" {
@@ -487,15 +478,10 @@ func (c *Cluster) executeEBSMigration() error {
487478
}
488479
}
489480

490-
awsVolumes, err := c.VolumeResizer.DescribeVolumes(volumeIds)
491-
if nil != err {
492-
return err
493-
}
494-
495481
var i3000 int64 = 3000
496482
var i125 int64 = 125
497483

498-
for _, volume := range awsVolumes {
484+
for _, volume := range c.EBSVolumes {
499485
if volume.VolumeType == "gp2" && volume.Size < c.OpConfig.EnableEBSGp3MigrationMaxSize {
500486
c.logger.Infof("modifying EBS volume %s to type gp3 migration (%d)", volume.VolumeID, volume.Size)
501487
err = c.VolumeResizer.ModifyVolume(volume.VolumeID, aws.String("gp3"), &volume.Size, &i3000, &i125)

pkg/cluster/volumes_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,10 @@ func TestMigrateEBS(t *testing.T) {
224224
resizer.EXPECT().ModifyVolume(gomock.Eq("ebs-volume-1"), gomock.Eq(aws.String("gp3")), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
225225

226226
cluster.VolumeResizer = resizer
227-
cluster.executeEBSMigration()
227+
err := cluster.populateVolumeMetaData()
228+
assert.NoError(t, err)
229+
err = cluster.executeEBSMigration()
230+
assert.NoError(t, err)
228231
}
229232

230233
func initTestVolumesAndPods(client k8sutil.KubernetesClient, namespace, clustername string, labels labels.Set, volumes []testVolume) {

0 commit comments

Comments
 (0)