Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to ignore PVCs without annotation #948

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charts/k8up/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ keywords:
- backup
- operator
- restic
version: 4.5.0
version: 4.5.1
sources:
- https://github.com/k8up-io/k8up
maintainers:
Expand Down
5 changes: 3 additions & 2 deletions charts/k8up/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# k8up

![Version: 4.5.0](https://img.shields.io/badge/Version-4.5.0-informational?style=flat-square)
![Version: 4.5.1](https://img.shields.io/badge/Version-4.5.1-informational?style=flat-square)

Kubernetes and OpenShift Backup Operator based on restic

Expand All @@ -13,7 +13,7 @@ helm repo add k8up-io https://k8up-io.github.io/k8up
helm install k8up k8up-io/k8up
```
```bash
kubectl apply -f https://github.com/k8up-io/k8up/releases/download/k8up-4.5.0/k8up-crd.yaml
kubectl apply -f https://github.com/k8up-io/k8up/releases/download/k8up-4.5.1/k8up-crd.yaml
```

<!---
Expand Down Expand Up @@ -60,6 +60,7 @@ Document your changes in values.yaml and let `make docs:helm` generate this sect
| k8up.globalResources.requests.cpu | string | `""` | Global CPU resource requests applied to jobs. See [supported units][resource-units]. |
| k8up.globalResources.requests.memory | string | `""` | Global Memory resource requests applied to jobs. See [supported units][resource-units]. |
| k8up.operatorNamespace | string | `""` | Specifies the namespace in which K8up's `EffectiveSchedules` are stored. Defaults to release namespace if left empty. |
| k8up.skipWithoutAnnotation | bool | `false` | Specifies whether K8up should ignore PVCs without the backup annotation (by default, `k8up.io/backup`) |
| k8up.timezone | string | `""` | Specifies the timezone K8up is using for scheduling. Empty value defaults to the timezone in which Kubernetes is deployed. Accepts `tz database` compatible entries, e.g. `Europe/Zurich` |
| metrics.prometheusRule.additionalLabels | object | `{}` | Add labels to the PrometheusRule object |
| metrics.prometheusRule.additionalRules | list | `[]` | Provide additional alert rules in addition to the defaults |
Expand Down
2 changes: 2 additions & 0 deletions charts/k8up/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ spec:
{{- end }}
- name: BACKUP_ENABLE_LEADER_ELECTION
value: "{{ .Values.k8up.enableLeaderElection }}"
- name: BACKUP_SKIP_WITHOUT_ANNOTATION
value: "{{ .Values.k8up.skipWithoutAnnotation }}"
- name: BACKUP_OPERATOR_NAMESPACE
{{- if .Values.k8up.operatorNamespace }}
value: "{{ .Values.k8up.operatorNamespace }}"
Expand Down
5 changes: 3 additions & 2 deletions charts/k8up/test/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func Test_Deployment_ShouldRender_EnvironmentVariables(t *testing.T) {
assert.Equalf(t, wantRepo+":"+wantTag, envs[0].Value, "Deployment does not use required Env Value from %s", wantVar)
assert.Equal(t, "TZ", envs[1].Name)
assert.Equal(t, wantTimezone, envs[1].Value)
assert.Equal(t, "BACKUP_OPERATOR_NAMESPACE", envs[3].Name)
assert.Equal(t, "metadata.namespace", envs[3].ValueFrom.FieldRef.FieldPath)
assert.Equal(t, "BACKUP_SKIP_WITHOUT_ANNOTATION", envs[3].Name)
assert.Equal(t, "BACKUP_OPERATOR_NAMESPACE", envs[4].Name)
assert.Equal(t, "metadata.namespace", envs[4].ValueFrom.FieldRef.FieldPath)
assert.Equal(t, "BACKUP_GLOBALCPU_REQUEST", envs[4].Name, "Deployment does not use configured Env Name")
assert.Equal(t, wantCpuRequest, envs[4].Value, "Deployment does not use configured Env Value")
assert.Equal(t, "VARIABLE", envs[5].Name, "Deployment does not use configured Env Name")
Stogas marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
3 changes: 3 additions & 0 deletions charts/k8up/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ k8up:
# -- Specifies whether leader election should be enabled.
enableLeaderElection: true

# -- Specifies whether K8up should ignore PVCs without the backup annotation (by default, `k8up.io/backup`)
skipWithoutAnnotation: false

# -- Specifies the namespace in which K8up's `EffectiveSchedules` are stored.
# Defaults to release namespace if left empty.
operatorNamespace: ""
Expand Down
1 change: 1 addition & 0 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ var (
&cli.StringFlag{Destination: &cfg.Config.PodExecRoleName, Name: "podexecrolename", EnvVars: []string{"BACKUP_PODEXECROLENAME"}, Value: "pod-executor", Usage: "set the role name that should be used for pod command execution"},

&cli.BoolFlag{Destination: &cfg.Config.EnableLeaderElection, Name: "enable-leader-election", EnvVars: []string{"BACKUP_ENABLE_LEADER_ELECTION"}, Value: true, DefaultText: "enabled", Usage: "enable leader election within the operator Pod"},
&cli.BoolFlag{Destination: &cfg.Config.SkipWithoutAnnotation, Name: "skip-pvcs-without-annotation", EnvVars: []string{"BACKUP_SKIP_WITHOUT_ANNOTATION"}, Value: false, DefaultText: "disabled", Usage: "skip selecting PVCs that don't have the BACKUP_ANNOTATION"},
&cli.StringFlag{Destination: &cfg.Config.BackupCheckSchedule, Name: "checkschedule", EnvVars: []string{"BACKUP_CHECKSCHEDULE"}, Value: "0 0 * * 0", Usage: "the default check schedule"},
&cli.StringFlag{Destination: &cfg.Config.OperatorNamespace, Name: "operator-namespace", EnvVars: []string{"BACKUP_OPERATOR_NAMESPACE"}, Required: true, Usage: "set the namespace in which the K8up operator itself runs"},
},
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/examples/usage/operator.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ OPTIONS:
--podexecaccountname value, --serviceaccount value set the service account name that should be used for the pod command execution (default: "pod-executor") [$BACKUP_PODEXECACCOUNTNAME]
--podexecrolename value set the role name that should be used for pod command execution (default: "pod-executor") [$BACKUP_PODEXECROLENAME]
--enable-leader-election enable leader election within the operator Pod (default: enabled) [$BACKUP_ENABLE_LEADER_ELECTION]
--skip-pvcs-without-annotation skip selecting PVCs that don't have the BACKUP_ANNOTATION (default: disabled) [$BACKUP_SKIP_WITHOUT_ANNOTATION]
--checkschedule value the default check schedule (default: "0 0 * * 0") [$BACKUP_CHECKSCHEDULE]
--operator-namespace value set the namespace in which the K8up operator itself runs [$BACKUP_OPERATOR_NAMESPACE]
--help, -h show help (default: false)
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Tested with:

But it should work with any command that has the ability to output the backup to stdout.

TIP: By default, all PVCs are backed up automatically. If you are using Application-Aware Backups and want to skip backing up any related PVCs, adding the annotation `k8up.io/backup=false` to a PVC object will exclude it from all following backups. Alternatively, you can set the environment variable `BACKUP_SKIP_WITHOUT_ANNOTATION=true` if you want K8up to ignore objects without the annotation.

== PostgreSQL

Note that the PostgreSQL image used requires the utility `pg_dump`.
Expand Down
2 changes: 2 additions & 0 deletions docs/modules/ROOT/pages/how-tos/backup.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ include::example$tutorial/backup.yaml[]
Save the YAML above in a file named `backup.yaml` and use the `kubectl apply -f backup.yaml` command to deploy this configuration to your cluster.

TIP: To have backups run automatically at a regular interval look at xref:how-tos/schedules.adoc[schedules].

TIP: By default, all PVCs are backed up automatically. Adding the annotation `k8up.io/backup=false` to a PVC object will exclude it from all following backups. Alternatively, you can set the environment variable `BACKUP_SKIP_WITHOUT_ANNOTATION=true` if you want K8up to ignore objects without the annotation.
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/how-tos/schedules.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ The file above will instruct the Operator to do backups every 5 minute, prune th

Feel free to adjust the frequencies to your liking. To help you with the crontab syntax, we recommend to check out https://crontab.guru[crontab.guru].

TIP: You can always check the state and configuration of your backup by using `kubectl describe schedule`. By default, all PVCs are backed up automatically. Adding the annotation `k8up.io/backup=false` to a PVC object will excluded it from all following backup.
TIP: You can always check the state and configuration of your backup by using `kubectl describe schedule`. By default, all PVCs are backed up automatically. Adding the annotation `k8up.io/backup=false` to a PVC object will exclude it from all following backups. Alternatively, you can set the environment variable `BACKUP_SKIP_WITHOUT_ANNOTATION=true` if you want K8up to ignore objects without the annotation.
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/references/annotations.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ See xref:references/operator-config-reference.adoc[Operator Configuration refere
|Annotation |Description |Accepted Values |Applicable Resources |xref:references/operator-config-reference.adoc[Configuration Option]

|`k8up.io/backup`
|If defined, this influences whether this pod shall be backed up (`true`) or not (`false`)
|If defined, this influences whether this Pod/PVC shall be backed up (`true`) or not (`false`). If omitted, K8up will default to `true`, unless `$BACKUP_SKIP_WITHOUT_ANNOTATION` is set to `true`.
|Either `'true'` or `'false'`
|`Pod`, `PersistentVolumeClaim`
|`BACKUP_ANNOTATION`
Expand Down
7 changes: 6 additions & 1 deletion operator/backupcontroller/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ func (b *BackupExecutor) listAndFilterPVCs(ctx context.Context, annotation strin
}

if !hasBackupAnnotation {
log.Info("PVC doesn't have annotation, adding to list", "pvc", pvc.GetName())
if cfg.Config.SkipWithoutAnnotation {
log.Info("PVC doesn't have annotation and BACKUP_SKIP_WITHOUT_ANNOTATION is true, skipping PVC", "pvc", pvc.GetName())
continue
} else {
log.Info("PVC doesn't have annotation, adding to list", "pvc", pvc.GetName())
}
} else if shouldBackup, _ := strconv.ParseBool(backupAnnotation); !shouldBackup {
log.Info("PVC skipped due to annotation", "pvc", pvc.GetName(), "annotation", backupAnnotation)
continue
Expand Down
1 change: 1 addition & 0 deletions operator/cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type Configuration struct {
PodFilter string
PromURL string
RestartPolicy string
SkipWithoutAnnotation bool

// Enabling this will ensure there is only one active controller manager.
EnableLeaderElection bool
Expand Down
Loading