Skip to content

Commit

Permalink
Simplify the API
Browse files Browse the repository at this point in the history
User provides only the volume specifics for the PVC.
The name of the volume is pre-configured as walpvc-cluster-name.
The volume is mounted at constants.PostgresPVCWalMount.
  • Loading branch information
rafia sabih committed Aug 6, 2024
1 parent 5b5aca0 commit 4bc1d9c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 120 deletions.
114 changes: 51 additions & 63 deletions pkg/apis/cpo.opensource.cybertec.at/v1/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -1349,84 +1349,72 @@ var PostgresCRDResourceValidation = apiextv1.CustomResourceValidation{
},
"walPvc": {
Type: "object",
Nullable: true,
Required: []string{"size"},
Properties: map[string]apiextv1.JSONSchemaProps{
"waldir": {
Type: "string",
},
"oldwaldir": {
Type: "string",
"iops": {
Type: "integer",
},
"walvolume": {
Type: "object",
Required: []string{"size"},
"selector": {
Type: "object",
Properties: map[string]apiextv1.JSONSchemaProps{
"iops": {
Type: "integer",
},
"selector": {
Type: "object",
Properties: map[string]apiextv1.JSONSchemaProps{
"matchExpressions": {
Type: "array",
Items: &apiextv1.JSONSchemaPropsOrArray{
Schema: &apiextv1.JSONSchemaProps{
Type: "object",
Required: []string{"key", "operator"},
Properties: map[string]apiextv1.JSONSchemaProps{
"key": {
Type: "string",
"matchExpressions": {
Type: "array",
Items: &apiextv1.JSONSchemaPropsOrArray{
Schema: &apiextv1.JSONSchemaProps{
Type: "object",
Required: []string{"key", "operator"},
Properties: map[string]apiextv1.JSONSchemaProps{
"key": {
Type: "string",
},
"operator": {
Type: "string",
Enum: []apiextv1.JSON{
{
Raw: []byte(`"DoesNotExist"`),
},
"operator": {
Type: "string",
Enum: []apiextv1.JSON{
{
Raw: []byte(`"DoesNotExist"`),
},
{
Raw: []byte(`"Exists"`),
},
{
Raw: []byte(`"In"`),
},
{
Raw: []byte(`"NotIn"`),
},
},
{
Raw: []byte(`"Exists"`),
},
"values": {
Type: "array",
Items: &apiextv1.JSONSchemaPropsOrArray{
Schema: &apiextv1.JSONSchemaProps{
Type: "string",
},
},
{
Raw: []byte(`"In"`),
},
{
Raw: []byte(`"NotIn"`),
},
},
},
"values": {
Type: "array",
Items: &apiextv1.JSONSchemaPropsOrArray{
Schema: &apiextv1.JSONSchemaProps{
Type: "string",
},
},
},
},
},
"matchLabels": {
Type: "object",
XPreserveUnknownFields: util.True(),
},
},
},
"size": {
Type: "string",
Pattern: "^(\\d+(e\\d+)?|\\d+(\\.\\d+)?(e\\d+)?[EPTGMK]i?)$",
},
"storageClass": {
Type: "string",
},
"subPath": {
Type: "string",
},
"throughput": {
Type: "integer",
"matchLabels": {
Type: "object",
XPreserveUnknownFields: util.True(),
},
},
},
"size": {
Type: "string",
Pattern: "^(\\d+(e\\d+)?|\\d+(\\.\\d+)?(e\\d+)?[EPTGMK]i?)$",
},
"storageClass": {
Type: "string",
},
"subPath": {
Type: "string",
},
"throughput": {
Type: "integer",
},
},
},
},
Expand Down
8 changes: 1 addition & 7 deletions pkg/apis/cpo.opensource.cybertec.at/v1/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,7 @@ type PostgresSpec struct {
Backup *Backup `json:"backup,omitempty"`
TDE *TDE `json:"tde,omitempty"`
Monitoring *Monitoring `json:"monitor,omitempty"`
WalPvc *PVCVolume `json:"walPvc,omitempty"`
}

type PVCVolume struct {
WalVolume Volume `json:"walvolume,omitempty"`
WalDir string `json:"waldir,omitempty"`
OldWalDir string `json:"oldwaldir,omitempty"`
WalPvc *Volume `json:"walPvc,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
19 changes: 1 addition & 18 deletions pkg/apis/cpo.opensource.cybertec.at/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1088,11 +1088,7 @@ func (c *Cluster) Update(oldSpec, newSpec *cpov1.Postgresql) error {
}
if oldSpec.Spec.WalPvc != nil {
//if pvc wal is removed then carry the relevant env vars to the new sts
c.Spec.WalPvc = &cpov1.PVCVolume{
OldWalDir: oldSpec.Spec.WalPvc.WalDir,
WalVolume: cpov1.Volume{},
WalDir: "",
}

}

if c.restoreInProgress() {
Expand Down
29 changes: 15 additions & 14 deletions pkg/cluster/k8sres.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,10 @@ func isBootstrapOnlyParameter(param string) bool {
return result
}

func getWALPVCName(cluster_name string) string {
return "walpvc" + cluster_name
}

func generateVolumeMounts(volume cpov1.Volume) []v1.VolumeMount {
return []v1.VolumeMount{
{
Expand All @@ -659,10 +663,10 @@ func generateVolumeMounts(volume cpov1.Volume) []v1.VolumeMount {
}
}

func generateWalVolumeMounts(volume cpov1.Volume, dir string) v1.VolumeMount {
func generateWalVolumeMounts(volume cpov1.Volume, cluster_name string) v1.VolumeMount {
return v1.VolumeMount{
Name: dir,
MountPath: constants.PostgresWalMount, //TODO: fetch from manifest
Name: getWALPVCName(cluster_name),
MountPath: constants.PostgresPVCWalMount,
SubPath: volume.SubPath,
}
}
Expand Down Expand Up @@ -1013,12 +1017,9 @@ func (c *Cluster) generateSpiloPodEnvVars(
envVars = append(envVars, v1.EnvVar{Name: "cpo_monitoring_stack", Value: "true"})
}
if spec.WalPvc != nil {
if spec.WalPvc.WalDir != "" {
envVars = append(envVars, v1.EnvVar{Name: "WALDIR", Value: spec.WalPvc.WalDir})
if spec.WalPvc != nil {
envVars = append(envVars, v1.EnvVar{Name: "WALDIR", Value: constants.PostgresPVCWalMount})
envVars = append(envVars, v1.EnvVar{Name: "OLD_WALDIR", Value: ""})
} else if spec.WalPvc.OldWalDir != "" {
envVars = append(envVars, v1.EnvVar{Name: "WALDIR", Value: ""})
envVars = append(envVars, v1.EnvVar{Name: "OLD_WALDIR", Value: spec.WalPvc.OldWalDir})
}
}
if c.OpConfig.EnablePgVersionEnvVar {
Expand Down Expand Up @@ -1381,8 +1382,8 @@ func (c *Cluster) generateStatefulSet(spec *cpov1.PostgresSpec) (*appsv1.Statefu

volumeMounts := generateVolumeMounts(spec.Volume)

if spec.WalPvc != nil && spec.WalPvc.WalDir != "" {
volumeMounts = append(volumeMounts, generateWalVolumeMounts(spec.WalPvc.WalVolume, spec.WalPvc.WalDir))
if spec.WalPvc != nil {
volumeMounts = append(volumeMounts, generateWalVolumeMounts(*spec.WalPvc, c.Spec.ClusterName))
}

// configure TLS with a custom secret volume
Expand Down Expand Up @@ -1529,9 +1530,9 @@ func (c *Cluster) generateStatefulSet(spec *cpov1.PostgresSpec) (*appsv1.Statefu
additionalVolumes = append(additionalVolumes, c.generateCertSecretVolume())
}
}
if spec.WalPvc != nil && spec.WalPvc.WalDir != "" {
WalPvcClaim, err = c.generatePersistentVolumeClaimTemplate(spec.WalPvc.WalVolume.Size,
spec.WalPvc.WalVolume.StorageClass, spec.WalPvc.WalVolume.Selector, spec.WalPvc.WalDir)
if spec.WalPvc != nil {
WalPvcClaim, err = c.generatePersistentVolumeClaimTemplate(spec.WalPvc.Size,
spec.WalPvc.StorageClass, spec.WalPvc.Selector, getWALPVCName(spec.ClusterName))
if err != nil {
c.logger.Errorf("could not generate volume claim template for WAL directory: %v", err)
}
Expand Down Expand Up @@ -1605,7 +1606,7 @@ func (c *Cluster) generateStatefulSet(spec *cpov1.PostgresSpec) (*appsv1.Statefu
}

final_vols := []v1.PersistentVolumeClaim{*volumeClaimTemplate}
if spec.WalPvc != nil && spec.WalPvc.WalDir != "" {
if spec.WalPvc != nil {
final_vols = []v1.PersistentVolumeClaim{*volumeClaimTemplate, *WalPvcClaim}
}

Expand Down
16 changes: 4 additions & 12 deletions pkg/cluster/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ func (c *Cluster) syncMonitoringSecret(oldSpec, newSpec *cpov1.Postgresql) error
}

func (c *Cluster) syncWalPvc(oldSpec, newSpec *cpov1.Postgresql) error {
c.logger.Info("syncing PVC for WAL with Spec")
c.logger.Info("syncing PVC for WAL")
c.setProcessName("syncing PVC for WAL")

if newSpec.Spec.WalPvc == nil && oldSpec.Spec.WalPvc != nil {
Expand All @@ -1774,8 +1774,7 @@ func (c *Cluster) syncWalPvc(oldSpec, newSpec *cpov1.Postgresql) error {
return fmt.Errorf("Could not list PVCs")
} else {
for _, pvc := range pvcs {
c.logger.Infof("Current PVC name is %v", pvc.Name)
if strings.Contains(pvc.Name, oldSpec.Spec.WalPvc.WalDir) {
if strings.Contains(pvc.Name, getWALPVCName(c.Spec.ClusterName)) {
c.logger.Infof("deleting WAL-PVC %q", util.NameFromMeta(pvc.ObjectMeta))
if err := c.KubeClient.PersistentVolumeClaims(pvc.Namespace).Delete(context.TODO(), pvc.Name, c.deleteOptions); err != nil {
return fmt.Errorf("could not delete WAL PVC: %v", err)
Expand All @@ -1784,17 +1783,10 @@ func (c *Cluster) syncWalPvc(oldSpec, newSpec *cpov1.Postgresql) error {
}
containers := c.Statefulset.Spec.Template.Spec.Containers
for _, con := range containers {
c.logger.Infof("changing env-vars for wal pvc %v", con)
con.Env = append(con.Env, v1.EnvVar{Name: "WALDIR", Value: ""})
con.Env = append(con.Env, v1.EnvVar{Name: "OLD_WALDIR", Value: oldSpec.Spec.WalPvc.WalDir})
c.logger.Infof("changed env-vars for wal pvc %v", con.Env)
con.Env = append(con.Env, v1.EnvVar{Name: "WALDIR", Value: constants.PostgresWalMount})
con.Env = append(con.Env, v1.EnvVar{Name: "OLD_WALDIR", Value: constants.PostgresPVCWalMount})
}
}
c.Spec.WalPvc = &cpov1.PVCVolume{
OldWalDir: oldSpec.Spec.WalPvc.WalDir,
WalVolume: cpov1.Volume{},
WalDir: "",
}
}
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/util/constants/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ const (
RunVolumeName = "postgresql-run"
RunVolumePath = "/var/run/postgresql"

PostgresWalMount = "/home/postgres/pgwal"
PostgresWalMount = "/home/postgres/pgwal"
PostgresPVCWalMount = "/home/postgres/pvc/pgwal"
)

0 comments on commit 4bc1d9c

Please sign in to comment.