Skip to content

Commit a94debb

Browse files
changes according to requests
1 parent aee63fe commit a94debb

File tree

8 files changed

+84
-27
lines changed

8 files changed

+84
-27
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ require (
1717
k8s.io/api v0.18.2
1818
k8s.io/apiextensions-apiserver v0.18.2
1919
k8s.io/apimachinery v0.18.2
20-
k8s.io/client-go v11.0.0+incompatible
20+
k8s.io/client-go v0.18.2
2121
k8s.io/code-generator v0.18.2
22-
sigs.k8s.io/kind v0.5.1 // indirect
2322
)

manifests/operatorconfiguration.crd.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ spec:
123123
type: boolean
124124
enable_pod_disruption_budget:
125125
type: boolean
126-
enable_pvc_resize:
127-
type: boolean
126+
enable_storage_resize:
127+
type: string
128+
enum:
129+
- "ebs"
130+
- "pvc"
131+
- "off"
128132
enable_sidecars:
129133
type: boolean
130134
infrastructure_roles_secret_name:

pkg/apis/acid.zalan.do/v1/crds.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,19 @@ var OperatorConfigCRDResourceValidation = apiextv1beta1.CustomResourceValidation
905905
"enable_pod_disruption_budget": {
906906
Type: "boolean",
907907
},
908-
"enable_pvc_resize": {
909-
Type: "boolean",
908+
"enable_storage_resize": {
909+
Type: "string",
910+
Enum: []apiextv1beta1.JSON{
911+
{
912+
Raw: []byte(`"ebs"`),
913+
},
914+
{
915+
Raw: []byte(`"pvc"`),
916+
},
917+
{
918+
Raw: []byte(`"off"`),
919+
},
920+
},
910921
},
911922
"enable_sidecars": {
912923
Type: "boolean",

pkg/apis/acid.zalan.do/v1/operator_configuration_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type KubernetesMetaConfiguration struct {
5353
WatchedNamespace string `json:"watched_namespace,omitempty"`
5454
PDBNameFormat config.StringTemplate `json:"pdb_name_format,omitempty"`
5555
EnablePodDisruptionBudget *bool `json:"enable_pod_disruption_budget,omitempty"`
56-
EnablePvcResize bool `json:"enable_pvc_resize,omitempty"`
56+
EnableStorageResize string `json:"enable_storage_resize,omitempty"`
5757
EnableInitContainers *bool `json:"enable_init_containers,omitempty"`
5858
EnableSidecars *bool `json:"enable_sidecars,omitempty"`
5959
SecretNameTemplate config.StringTemplate `json:"secret_name_template,omitempty"`

pkg/cluster/sync.go

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,26 @@ func (c *Cluster) Sync(newSpec *acidv1.Postgresql) error {
5757
return err
5858
}
5959

60-
// potentially enlarge volumes before changing the statefulset. By doing that
61-
// in this order we make sure the operator is not stuck waiting for a pod that
62-
// cannot start because it ran out of disk space.
63-
// TODO: handle the case of the cluster that is downsized and enlarged again
64-
// (there will be a volume from the old pod for which we can't act before the
65-
// the statefulset modification is concluded)
66-
c.logger.Debugf("syncing persistent volumes")
67-
if err = c.syncVolumes(); err != nil {
68-
err = fmt.Errorf("could not sync persistent volumes: %v", err)
69-
return err
60+
if c.OpConfig.EnableStorageResize == "pvc" {
61+
c.logger.Debugf("syncing persistent volume claims")
62+
if err = c.syncVolumeClaims(); err != nil {
63+
err = fmt.Errorf("could not sync persistent volume claims: %v", err)
64+
return err
65+
}
66+
} else if c.OpConfig.EnableStorageResize == "ebs" {
67+
// potentially enlarge volumes before changing the statefulset. By doing that
68+
// in this order we make sure the operator is not stuck waiting for a pod that
69+
// cannot start because it ran out of disk space.
70+
// TODO: handle the case of the cluster that is downsized and enlarged again
71+
// (there will be a volume from the old pod for which we can't act before the
72+
// the statefulset modification is concluded)
73+
c.logger.Debugf("syncing persistent volumes")
74+
if err = c.syncVolumes(); err != nil {
75+
err = fmt.Errorf("could not sync persistent volumes: %v", err)
76+
return err
77+
}
78+
} else {
79+
c.logger.Infof("Storage resize is disabled (enable_storage_resize is off). Skipping volume sync.")
7080
}
7181

7282
if err = c.enforceMinResourceLimits(&c.Spec); err != nil {
@@ -571,6 +581,26 @@ func (c *Cluster) syncRoles() (err error) {
571581
return nil
572582
}
573583

584+
// syncVolumeClaims reads all persistent volume claims and checks that their size matches the one declared in the statefulset.
585+
func (c *Cluster) syncVolumeClaims() error {
586+
c.setProcessName("syncing volume claims")
587+
588+
act, err := c.volumeClaimsNeedResizing(c.Spec.Volume)
589+
if err != nil {
590+
return fmt.Errorf("could not compare size of the volume claims: %v", err)
591+
}
592+
if !act {
593+
return nil
594+
}
595+
if err := c.resizeVolumeClaims(c.Spec.Volume); err != nil {
596+
return fmt.Errorf("could not sync volume claims: %v", err)
597+
}
598+
599+
c.logger.Infof("volume claims have been synced successfully")
600+
601+
return nil
602+
}
603+
574604
// syncVolumes reads all persistent volumes and checks that their size matches the one declared in the statefulset.
575605
func (c *Cluster) syncVolumes() error {
576606
c.setProcessName("syncing volumes")
@@ -582,14 +612,8 @@ func (c *Cluster) syncVolumes() error {
582612
if !act {
583613
return nil
584614
}
585-
if c.OpConfig.EnablePvcResize {
586-
if err := c.resizeVolumeClaims(c.Spec.Volume); err != nil {
587-
return fmt.Errorf("could not sync volume claims: %v", err)
588-
}
589-
} else {
590-
if err := c.resizeVolumes(c.Spec.Volume, []volumes.VolumeResizer{&volumes.EBSVolumeResizer{AWSRegion: c.OpConfig.AWSRegion}}); err != nil {
591-
return fmt.Errorf("could not sync volumes: %v", err)
592-
}
615+
if err := c.resizeVolumes(c.Spec.Volume, []volumes.VolumeResizer{&volumes.EBSVolumeResizer{AWSRegion: c.OpConfig.AWSRegion}}); err != nil {
616+
return fmt.Errorf("could not sync volumes: %v", err)
593617
}
594618

595619
c.logger.Infof("volumes have been synced successfully")

pkg/cluster/volumes.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,25 @@ func (c *Cluster) resizeVolumes(newVolume acidv1.Volume, resizers []volumes.Volu
189189
return nil
190190
}
191191

192+
func (c *Cluster) volumeClaimsNeedResizing(newVolume acidv1.Volume) (bool, error) {
193+
newSize, err := resource.ParseQuantity(newVolume.Size)
194+
manifestSize := quantityToGigabyte(newSize)
195+
if err != nil {
196+
return false, fmt.Errorf("could not parse volume size from the manifest: %v", err)
197+
}
198+
pvcs, err := c.listPersistentVolumeClaims()
199+
if err != nil {
200+
return false, fmt.Errorf("could not receive persistent volume claims: %v", err)
201+
}
202+
for _, pvc := range pvcs {
203+
currentSize := quantityToGigabyte(pvc.Spec.Resources.Requests[v1.ResourceStorage])
204+
if currentSize != manifestSize {
205+
return true, nil
206+
}
207+
}
208+
return false, nil
209+
}
210+
192211
func (c *Cluster) volumesNeedResizing(newVolume acidv1.Volume) (bool, error) {
193212
vols, manifestSize, err := c.listVolumesWithManifestSize(newVolume)
194213
if err != nil {

pkg/controller/operator_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (c *Controller) importConfigurationFromCRD(fromCRD *acidv1.OperatorConfigur
6565
result.WatchedNamespace = fromCRD.Kubernetes.WatchedNamespace
6666
result.PDBNameFormat = fromCRD.Kubernetes.PDBNameFormat
6767
result.EnablePodDisruptionBudget = util.CoalesceBool(fromCRD.Kubernetes.EnablePodDisruptionBudget, util.True())
68-
result.EnablePvcResize = fromCRD.Kubernetes.EnablePvcResize
68+
result.EnableStorageResize = fromCRD.Kubernetes.EnableStorageResize
6969
result.EnableInitContainers = util.CoalesceBool(fromCRD.Kubernetes.EnableInitContainers, util.True())
7070
result.EnableSidecars = util.CoalesceBool(fromCRD.Kubernetes.EnableSidecars, util.True())
7171
result.SecretNameTemplate = fromCRD.Kubernetes.SecretNameTemplate

pkg/util/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ type Config struct {
142142
CustomPodAnnotations map[string]string `name:"custom_pod_annotations"`
143143
EnablePodAntiAffinity bool `name:"enable_pod_antiaffinity" default:"false"`
144144
PodAntiAffinityTopologyKey string `name:"pod_antiaffinity_topology_key" default:"kubernetes.io/hostname"`
145-
EnablePvcResize bool `name:"enable_pvc_resize" default:"false"`
145+
EnableStorageResize string `name:"enable_storage_resize" default:"ebs"`
146146
// deprecated and kept for backward compatibility
147147
EnableLoadBalancer *bool `name:"enable_load_balancer"`
148148
MasterDNSNameFormat StringTemplate `name:"master_dns_name_format" default:"{cluster}.{team}.{hostedzone}"`

0 commit comments

Comments
 (0)