Skip to content

Commit f0f13b4

Browse files
authored
K8SPG-615: Add initialDelaySeconds for backups (#1124)
1 parent bd5bac1 commit f0f13b4

File tree

12 files changed

+235
-0
lines changed

12 files changed

+235
-0
lines changed

Diff for: build/crd/crunchy/generated/postgres-operator.crunchydata.com_postgresclusters.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -1956,6 +1956,13 @@ spec:
19561956
description: Defines details for manual pgBackRest backup
19571957
Jobs
19581958
properties:
1959+
initialDelaySeconds:
1960+
description: |-
1961+
InitialDelaySeconds defines the number of seconds to wait before starting the backup.
1962+
After the backup pod is scheduled, its entrypoint will wait for this number of seconds
1963+
before initiating the backup process.
1964+
format: int64
1965+
type: integer
19591966
options:
19601967
description: |-
19611968
Command line options to include when running the pgBackRest backup command.

Diff for: build/crd/percona/generated/pgv2.percona.com_perconapgclusters.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -2114,6 +2114,13 @@ spec:
21142114
description: Defines details for manual pgBackRest backup
21152115
Jobs
21162116
properties:
2117+
initialDelaySeconds:
2118+
description: |-
2119+
InitialDelaySeconds defines the number of seconds to wait before starting the backup.
2120+
After the backup pod is scheduled, its entrypoint will wait for this number of seconds
2121+
before initiating the backup process.
2122+
format: int64
2123+
type: integer
21172124
options:
21182125
description: |-
21192126
Command line options to include when running the pgBackRest backup command.

Diff for: cmd/pgbackrest/main.go

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strconv"
2525
"strings"
2626
"syscall"
27+
"time"
2728

2829
log "github.com/sirupsen/logrus"
2930
corev1 "k8s.io/api/core/v1"
@@ -78,6 +79,16 @@ func main() {
7879
}
7980
log.Infof("debug flag set to %t", debugFlag)
8081

82+
if delay, found := os.LookupEnv("INITIAL_DELAY_SECS"); found {
83+
delaySecs, err := strconv.ParseInt(delay, 10, 64)
84+
if err != nil {
85+
panic(err)
86+
}
87+
88+
log.Infof("sleeping for %d seconds", delaySecs)
89+
time.Sleep(time.Duration(delaySecs) * time.Second)
90+
}
91+
8192
config, err := NewConfig()
8293
if err != nil {
8394
panic(err)

Diff for: config/crd/bases/pgv2.percona.com_perconapgclusters.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -2521,6 +2521,13 @@ spec:
25212521
description: Defines details for manual pgBackRest backup
25222522
Jobs
25232523
properties:
2524+
initialDelaySeconds:
2525+
description: |-
2526+
InitialDelaySeconds defines the number of seconds to wait before starting the backup.
2527+
After the backup pod is scheduled, its entrypoint will wait for this number of seconds
2528+
before initiating the backup process.
2529+
format: int64
2530+
type: integer
25242531
options:
25252532
description: |-
25262533
Command line options to include when running the pgBackRest backup command.

Diff for: config/crd/bases/postgres-operator.crunchydata.com_postgresclusters.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,13 @@ spec:
19481948
description: Defines details for manual pgBackRest backup
19491949
Jobs
19501950
properties:
1951+
initialDelaySeconds:
1952+
description: |-
1953+
InitialDelaySeconds defines the number of seconds to wait before starting the backup.
1954+
After the backup pod is scheduled, its entrypoint will wait for this number of seconds
1955+
before initiating the backup process.
1956+
format: int64
1957+
type: integer
19511958
options:
19521959
description: |-
19531960
Command line options to include when running the pgBackRest backup command.

Diff for: deploy/bundle.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,13 @@ spec:
28182818
description: Defines details for manual pgBackRest backup
28192819
Jobs
28202820
properties:
2821+
initialDelaySeconds:
2822+
description: |-
2823+
InitialDelaySeconds defines the number of seconds to wait before starting the backup.
2824+
After the backup pod is scheduled, its entrypoint will wait for this number of seconds
2825+
before initiating the backup process.
2826+
format: int64
2827+
type: integer
28212828
options:
28222829
description: |-
28232830
Command line options to include when running the pgBackRest backup command.
@@ -28447,6 +28454,13 @@ spec:
2844728454
description: Defines details for manual pgBackRest backup
2844828455
Jobs
2844928456
properties:
28457+
initialDelaySeconds:
28458+
description: |-
28459+
InitialDelaySeconds defines the number of seconds to wait before starting the backup.
28460+
After the backup pod is scheduled, its entrypoint will wait for this number of seconds
28461+
before initiating the backup process.
28462+
format: int64
28463+
type: integer
2845028464
options:
2845128465
description: |-
2845228466
Command line options to include when running the pgBackRest backup command.

Diff for: deploy/cr.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ spec:
514514
repoName: repo1
515515
options:
516516
- --type=full
517+
# initialDelaySeconds: 120
517518
repos:
518519
- name: repo1
519520
schedules:

Diff for: deploy/crd.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,13 @@ spec:
28182818
description: Defines details for manual pgBackRest backup
28192819
Jobs
28202820
properties:
2821+
initialDelaySeconds:
2822+
description: |-
2823+
InitialDelaySeconds defines the number of seconds to wait before starting the backup.
2824+
After the backup pod is scheduled, its entrypoint will wait for this number of seconds
2825+
before initiating the backup process.
2826+
format: int64
2827+
type: integer
28212828
options:
28222829
description: |-
28232830
Command line options to include when running the pgBackRest backup command.
@@ -28447,6 +28454,13 @@ spec:
2844728454
description: Defines details for manual pgBackRest backup
2844828455
Jobs
2844928456
properties:
28457+
initialDelaySeconds:
28458+
description: |-
28459+
InitialDelaySeconds defines the number of seconds to wait before starting the backup.
28460+
After the backup pod is scheduled, its entrypoint will wait for this number of seconds
28461+
before initiating the backup process.
28462+
format: int64
28463+
type: integer
2845028464
options:
2845128465
description: |-
2845228466
Command line options to include when running the pgBackRest backup command.

Diff for: deploy/cw-bundle.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,13 @@ spec:
28182818
description: Defines details for manual pgBackRest backup
28192819
Jobs
28202820
properties:
2821+
initialDelaySeconds:
2822+
description: |-
2823+
InitialDelaySeconds defines the number of seconds to wait before starting the backup.
2824+
After the backup pod is scheduled, its entrypoint will wait for this number of seconds
2825+
before initiating the backup process.
2826+
format: int64
2827+
type: integer
28212828
options:
28222829
description: |-
28232830
Command line options to include when running the pgBackRest backup command.
@@ -28447,6 +28454,13 @@ spec:
2844728454
description: Defines details for manual pgBackRest backup
2844828455
Jobs
2844928456
properties:
28457+
initialDelaySeconds:
28458+
description: |-
28459+
InitialDelaySeconds defines the number of seconds to wait before starting the backup.
28460+
After the backup pod is scheduled, its entrypoint will wait for this number of seconds
28461+
before initiating the backup process.
28462+
format: int64
28463+
type: integer
2845028464
options:
2845128465
description: |-
2845228466
Command line options to include when running the pgBackRest backup command.

Diff for: internal/controller/postgrescluster/pgbackrest.go

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"reflect"
1212
"regexp"
1313
"sort"
14+
"strconv"
1415
"strings"
1516
"time"
1617

@@ -860,6 +861,14 @@ func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.P
860861
}
861862
}
862863

864+
// K8SPG-615
865+
if m := postgresCluster.Spec.Backups.PGBackRest.Manual; postgresCluster.CompareVersion("2.7.0") >= 0 && m != nil && m.InitialDelaySeconds != 0 {
866+
container.Env = append(container.Env, corev1.EnvVar{
867+
Name: "INITIAL_DELAY_SECS",
868+
Value: strconv.FormatInt(m.InitialDelaySeconds, 10),
869+
})
870+
}
871+
863872
if postgresCluster.CompareVersion("2.7.0") >= 0 {
864873
if annotations != nil {
865874
annotations[naming.DefaultContainerAnnotation] = naming.PGBackRestRepoContainerName

Diff for: internal/controller/postgrescluster/pgbackrest_test.go

+138
Original file line numberDiff line numberDiff line change
@@ -2719,6 +2719,144 @@ volumes:
27192719
}
27202720
})
27212721
})
2722+
2723+
t.Run("InitialDelaySeconds", func(t *testing.T) {
2724+
t.Run("<2.7.0", func(t *testing.T) {
2725+
cluster := &v1beta1.PostgresCluster{
2726+
ObjectMeta: metav1.ObjectMeta{
2727+
Labels: map[string]string{
2728+
naming.LabelVersion: "2.6.0",
2729+
},
2730+
},
2731+
Spec: v1beta1.PostgresClusterSpec{
2732+
Backups: v1beta1.Backups{
2733+
PGBackRest: v1beta1.PGBackRestArchive{
2734+
Manual: &v1beta1.PGBackRestManualBackup{
2735+
InitialDelaySeconds: 30,
2736+
},
2737+
},
2738+
},
2739+
},
2740+
}
2741+
job := generateBackupJobSpecIntent(ctx,
2742+
cluster, v1beta1.PGBackRestRepo{},
2743+
"", "",
2744+
nil, nil,
2745+
)
2746+
2747+
container := job.Template.Spec.Containers[0]
2748+
2749+
var envVarFound bool
2750+
for _, env := range container.Env {
2751+
if env.Name == "INITIAL_DELAY_SECS" {
2752+
envVarFound = true
2753+
}
2754+
}
2755+
2756+
assert.Check(t, envVarFound == false)
2757+
})
2758+
2759+
t.Run(">=2.7.0", func(t *testing.T) {
2760+
cluster := &v1beta1.PostgresCluster{
2761+
ObjectMeta: metav1.ObjectMeta{
2762+
Labels: map[string]string{
2763+
naming.LabelVersion: "2.7.0",
2764+
},
2765+
},
2766+
Spec: v1beta1.PostgresClusterSpec{
2767+
Backups: v1beta1.Backups{
2768+
PGBackRest: v1beta1.PGBackRestArchive{
2769+
Manual: &v1beta1.PGBackRestManualBackup{
2770+
InitialDelaySeconds: 30,
2771+
},
2772+
},
2773+
},
2774+
},
2775+
}
2776+
job := generateBackupJobSpecIntent(ctx,
2777+
cluster, v1beta1.PGBackRestRepo{},
2778+
"", "",
2779+
nil, nil,
2780+
)
2781+
2782+
container := job.Template.Spec.Containers[0]
2783+
2784+
var envVar corev1.EnvVar
2785+
for _, env := range container.Env {
2786+
if env.Name == "INITIAL_DELAY_SECS" {
2787+
envVar = env
2788+
}
2789+
}
2790+
2791+
if assert.Check(t, envVar.Name == "INITIAL_DELAY_SECS") {
2792+
assert.Equal(t, envVar.Value, "30")
2793+
}
2794+
})
2795+
2796+
t.Run("NoManual", func(t *testing.T) {
2797+
cluster := &v1beta1.PostgresCluster{
2798+
ObjectMeta: metav1.ObjectMeta{
2799+
Labels: map[string]string{
2800+
naming.LabelVersion: "2.7.0",
2801+
},
2802+
},
2803+
Spec: v1beta1.PostgresClusterSpec{
2804+
Backups: v1beta1.Backups{
2805+
PGBackRest: v1beta1.PGBackRestArchive{},
2806+
},
2807+
},
2808+
}
2809+
job := generateBackupJobSpecIntent(ctx,
2810+
cluster, v1beta1.PGBackRestRepo{},
2811+
"", "",
2812+
nil, nil,
2813+
)
2814+
2815+
container := job.Template.Spec.Containers[0]
2816+
2817+
var envVarFound bool
2818+
for _, env := range container.Env {
2819+
if env.Name == "INITIAL_DELAY_SECS" {
2820+
envVarFound = true
2821+
}
2822+
}
2823+
2824+
assert.Check(t, envVarFound == false)
2825+
})
2826+
2827+
t.Run("EmptyValue", func(t *testing.T) {
2828+
cluster := &v1beta1.PostgresCluster{
2829+
ObjectMeta: metav1.ObjectMeta{
2830+
Labels: map[string]string{
2831+
naming.LabelVersion: "2.7.0",
2832+
},
2833+
},
2834+
Spec: v1beta1.PostgresClusterSpec{
2835+
Backups: v1beta1.Backups{
2836+
PGBackRest: v1beta1.PGBackRestArchive{
2837+
Manual: &v1beta1.PGBackRestManualBackup{},
2838+
},
2839+
},
2840+
},
2841+
}
2842+
job := generateBackupJobSpecIntent(ctx,
2843+
cluster, v1beta1.PGBackRestRepo{},
2844+
"", "",
2845+
nil, nil,
2846+
)
2847+
2848+
container := job.Template.Spec.Containers[0]
2849+
2850+
var envVarFound bool
2851+
for _, env := range container.Env {
2852+
if env.Name == "INITIAL_DELAY_SECS" {
2853+
envVarFound = true
2854+
}
2855+
}
2856+
2857+
assert.Check(t, envVarFound == false)
2858+
})
2859+
})
27222860
}
27232861

27242862
func TestGenerateRepoHostIntent(t *testing.T) {

Diff for: pkg/apis/postgres-operator.crunchydata.com/v1beta1/pgbackrest_types.go

+6
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ type PGBackRestManualBackup struct {
205205
// https://pgbackrest.org/command.html#command-backup
206206
// +optional
207207
Options []string `json:"options,omitempty"`
208+
209+
// InitialDelaySeconds defines the number of seconds to wait before starting the backup.
210+
// After the backup pod is scheduled, its entrypoint will wait for this number of seconds
211+
// before initiating the backup process.
212+
// +optional
213+
InitialDelaySeconds int64 `json:"initialDelaySeconds,omitempty"`
208214
}
209215

210216
// PGBackRestRepoHost represents a pgBackRest dedicated repository host

0 commit comments

Comments
 (0)