Repository: kinvolk/lokomotive
Pull request #1348 (2021-01-27 06:10:22)

Velero: restic plugin add tolerations
This commit adds a new attribute `tolerations` which will add tolerations to restic DaemonSet.

Since the cluster has multiple node pools with different purposes, it
adds an ability to deploy the restic DaemonSet on the nodes that need to
backup the PVC.

closes: #1334
Signed-off-by: knrt10 <kautilya@kinvolk.io>

knrt10: @surajssd added
invidian: >@surajssd added

I don't see tests being added.
knrt10: Just to confirm, by config conversion doesn't it mean to add tolerations to restic config test and check whether it is right or not?
invidian: >Just to confirm, by config conversion doesn't it mean to add tolerations to restic config tests and check whether it is right or not?

I think what @surajssd meant was to add tests, that toleration actually appears in the right place in the rendered manifests.
knrt10: @surajssd added the tests. Please check

Patch:
diff --git a/docs/RELEASING.md b/docs/RELEASING.md
index efc16ff3..052e1456 100644
--- a/docs/RELEASING.md
+++ b/docs/RELEASING.md
@@ -191,21 +191,3 @@ for generating new keys for signing.
 
 Before signing a release with a new GPG key, it should be signed by other trusted
 keys and added to the [list of trusted keys in the repository](KEYS.md).
-
-## Updating the docs website
-
-Once the release is done. Create a pull request to the [docs repo](https://github.com/kinvolk/docs) modifying the [_index.md](https://github.com/kinvolk/docs/blob/master/docs/lokomotive/_index.md) file.
-
-Add a new entry at the top of the list with the new version pointing to its corresponding tag:
-
-```diff
- external_docs:
-+- repo: https://github.com/kinvolk/lokomotive.git
-+  name: "0.7"
-+  branch: "v0.7.0"
-+  dir: "docs"
- - repo: https://github.com/kinvolk/lokomotive.git
-   name: "0.6"
-   branch: "v0.6.0"
-   dir: "docs"
-```
diff --git a/docs/configuration-reference/components/velero.md b/docs/configuration-reference/components/velero.md
index 533f1e2e..ad6fc27b 100644
--- a/docs/configuration-reference/components/velero.md
+++ b/docs/configuration-reference/components/velero.md
@@ -104,50 +104,51 @@ component "velero" {
 
 Table of all the arguments accepted by the component.
 
-| Argument                                             | Description                                                                                                                                                                          | Default                                           | Type   | Required |
-|------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|--------|----------|
-| `namespace`                                          | Namespace to install Velero.                                                                                                                                                         | "velero"                                          | string | false    |
-| `provider`                                           | Provider sets which provider block to use for the configuration. Supported values are: `azure`, `openebs` and `restic`.                                                              | -                                                 | string | true     |
-| `metrics`                                            | Configure Prometheus to scrape Velero metrics. Needs the [Prometheus Operator component](prometheus-operator.md) installed.                                                          | -                                                 | object | false    |
-| `metrics.enabled`                                    | Adds Prometheus annotations to Velero deployment if enabled.                                                                                                                         | false                                             | bool   | false    |
-| `metrics.service_monitor`                            | Adds ServiceMonitor resource for Prometheus. Requires `metrics.enabled` as true.                                                                                                     | false                                             | bool   | false    |
-| `azure`                                              | Configure Azure provider for Velero.                                                                                                                                                 | -                                                 | object | false    |
-| `azure.subscription_id`                              | Azure Subscription ID where client application is created. Can be obtained with `az account list`.                                                                                   | -                                                 | string | true     |
-| `azure.tenant_id`                                    | Azure Tenant ID where your subscription is created. Can be obtained with `az account list`.                                                                                          | -                                                 | string | true     |
-| `azure.client_id`                                    | Azure Application Client ID to perform Azure operations.                                                                                                                             | -                                                 | string | true     |
-| `azure.client_secret`                                | Azure Application Client secret.                                                                                                                                                     | -                                                 | string | true     |
-| `azure.resource_group`                               | Azure resource group, where PVC Disks are created. If this argument is wrong, Velero will fail to create PVC snapshots.                                                              | -                                                 | string | true     |
-| `azure.backup_storage_location`                      | Configure backup storage location and metadata.                                                                                                                                      | -                                                 | object | true     |
-| `azure.backup_storage_location.resource_group`       | Name of the resource group containing the storage account for this backup storage location.                                                                                          | -                                                 | string | true     |
-| `azure.backup_storage_location.storage_account`      | Name of the storage account for this backup storage location.                                                                                                                        | -                                                 | string | true     |
-| `azure.backup_storage_location.bucket`               | Name of the storage container to store backups.                                                                                                                                      | -                                                 | string | true     |
-| `azure.volume_snapshot_location`                     | Configure PVC snapshot location.                                                                                                                                                     | -                                                 | object | false    |
-| `azure.volume_snapshot_location.resource_group`      | Azure Resource Group where snapshots will be stored.                                                                                                                                 | Stored in the same resource group as the cluster. | string | false    |
-| `azure.volume_snapshot_location.api_timeout`         | Azure API timeout.                                                                                                                                                                   | "10m"                                             | string | false    |
-| `openebs`                                            | Configure OpenEBS provider for Velero.                                                                                                                                               | -                                                 | object | false    |
-| `openebs.credentials`                                | Content of cloud provider credentials.                                                                                                                                               | -                                                 | string | true     |
-| `openebs.provider`                                   | Cloud provider to use for backup and snapshot storage. Supported values are `gcp` and `aws`.                                                                                         | -                                                 | string | false    |
-| `openebs.backup_storage_location`                    | Configure backup storage location.                                                                                                                                                   | -                                                 | object | true     |
-| `openebs.backup_storage_location.region`             | Cloud provider region for storing backups.                                                                                                                                           | -                                                 | string | true     |
-| `openebs.backup_storage_location.bucket`             | Cloud storage bucket name for storing backups.                                                                                                                                       | -                                                 | string | true     |
-| `openebs.backup_storage_location.provider`           | Cloud provider name for storing backups. Overrides `openebs.provider` field for backup storage.                                                                                      | -                                                 | string | false    |
-| `openebs.backup_storage_location.name`               | Name for backup location object on the cluster.                                                                                                                                      | -                                                 | string | false    |
-| `openebs.volume_snapshot_location`                   | Configure volume snapshot location.                                                                                                                                                  | -                                                 | object | true     |
-| `openebs.volume_snapshot_location.bucket`            | Cloud storage bucket name for storing volume snapshots.                                                                                                                              | -                                                 | string | true     |
-| `openebs.volume_snapshot_location.region`            | Cloud provider region for storing snapshots.                                                                                                                                         |                                                   | string | true     |
-| `openebs.volume_snapshot_location.provider`          | Cloud provider name for storing snapshots. Overrides `openebs.provider` field for backup storage.                                                                                    | -                                                 | string | false    |
-| `openebs.volume_snapshot_location.name`              | Name for snapshot location object on the cluster.                                                                                                                                    | -                                                 | string | false    |
-| `openebs.volume_snapshot_location.prefix`            | Prefix for snapshot names.                                                                                                                                                           | -                                                 | string | false    |
-| `openebs.volume_snapshot_location.local`             | If `true`, backups won't be copied to cloud storage.                                                                                                                                 | false                                             | bool   | false    |
-| `openebs.volume_snapshot_location.openebs_namespace` | Name of the namespace where OpenEBS runs.                                                                                                                                            | -                                                 | string | true     |
-| `openebs.volume_snapshot_location.s3_url`            | S3 API URL.                                                                                                                                                                          | -                                                 | string | false    |
-| `restic`                                             | Configure Restic provider for Velero.                                                                                                                                                | -                                                 | object | false    |
-| `restic.credentials`                                 | Content of cloud provider credentials.                                                                                                                                               | -                                                 | string | true     |
-| `restic.require_volume_annotation`                   | Backup all pod volumes without having to apply annotation on the pod when using restic. To exclude volumes add the annotation `backup.velero.io/backup-volumes-excludes` on the pod. | false                                             | bool   | false
-| `restic.backup_storage_location.provider`            | Cloud provider name for storing backups.                                                                                                                                             | -                                                 | string | false    |
-| `restic.backup_storage_location.bucket`              | Cloud storage bucket name for storing backups.                                                                                                                                       | -                                                 | string | true     |
-| `restic.backup_storage_location.name`                | Name for backup location object on the cluster.                                                                                                                                      | "default"                                         | string | false    |
-| `restic.backup_storage_location.region`              | Cloud provider region for storing snapshots. Required if `restic.backup_storage_location.provider = aws`.                                                                            | -                                                 | string | false    |
+| Argument                                             | Description                                                                                                                                                                          | Default                                           | Type                                                                                                           | Required |
+|------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|----------------------------------------------------------------------------------------------------------------|----------|
+| `namespace`                                          | Namespace to install Velero.                                                                                                                                                         | "velero"                                          | string                                                                                                         | false    |
+| `provider`                                           | Provider sets which provider block to use for the configuration. Supported values are: `azure`, `openebs` and `restic`.                                                              | -                                                 | string                                                                                                         | true     |
+| `metrics`                                            | Configure Prometheus to scrape Velero metrics. Needs the [Prometheus Operator component](prometheus-operator.md) installed.                                                          | -                                                 | object                                                                                                         | false    |
+| `metrics.enabled`                                    | Adds Prometheus annotations to Velero deployment if enabled.                                                                                                                         | false                                             | bool                                                                                                           | false    |
+| `metrics.service_monitor`                            | Adds ServiceMonitor resource for Prometheus. Requires `metrics.enabled` as true.                                                                                                     | false                                             | bool                                                                                                           | false    |
+| `azure`                                              | Configure Azure provider for Velero.                                                                                                                                                 | -                                                 | object                                                                                                         | false    |
+| `azure.subscription_id`                              | Azure Subscription ID where client application is created. Can be obtained with `az account list`.                                                                                   | -                                                 | string                                                                                                         | true     |
+| `azure.tenant_id`                                    | Azure Tenant ID where your subscription is created. Can be obtained with `az account list`.                                                                                          | -                                                 | string                                                                                                         | true     |
+| `azure.client_id`                                    | Azure Application Client ID to perform Azure operations.                                                                                                                             | -                                                 | string                                                                                                         | true     |
+| `azure.client_secret`                                | Azure Application Client secret.                                                                                                                                                     | -                                                 | string                                                                                                         | true     |
+| `azure.resource_group`                               | Azure resource group, where PVC Disks are created. If this argument is wrong, Velero will fail to create PVC snapshots.                                                              | -                                                 | string                                                                                                         | true     |
+| `azure.backup_storage_location`                      | Configure backup storage location and metadata.                                                                                                                                      | -                                                 | object                                                                                                         | true     |
+| `azure.backup_storage_location.resource_group`       | Name of the resource group containing the storage account for this backup storage location.                                                                                          | -                                                 | string                                                                                                         | true     |
+| `azure.backup_storage_location.storage_account`      | Name of the storage account for this backup storage location.                                                                                                                        | -                                                 | string                                                                                                         | true     |
+| `azure.backup_storage_location.bucket`               | Name of the storage container to store backups.                                                                                                                                      | -                                                 | string                                                                                                         | true     |
+| `azure.volume_snapshot_location`                     | Configure PVC snapshot location.                                                                                                                                                     | -                                                 | object                                                                                                         | false    |
+| `azure.volume_snapshot_location.resource_group`      | Azure Resource Group where snapshots will be stored.                                                                                                                                 | Stored in the same resource group as the cluster. | string                                                                                                         | false    |
+| `azure.volume_snapshot_location.api_timeout`         | Azure API timeout.                                                                                                                                                                   | "10m"                                             | string                                                                                                         | false    |
+| `openebs`                                            | Configure OpenEBS provider for Velero.                                                                                                                                               | -                                                 | object                                                                                                         | false    |
+| `openebs.credentials`                                | Content of cloud provider credentials.                                                                                                                                               | -                                                 | string                                                                                                         | true     |
+| `openebs.provider`                                   | Cloud provider to use for backup and snapshot storage. Supported values are `gcp` and `aws`.                                                                                         | -                                                 | string                                                                                                         | false    |
+| `openebs.backup_storage_location`                    | Configure backup storage location.                                                                                                                                                   | -                                                 | object                                                                                                         | true     |
+| `openebs.backup_storage_location.region`             | Cloud provider region for storing backups.                                                                                                                                           | -                                                 | string                                                                                                         | true     |
+| `openebs.backup_storage_location.bucket`             | Cloud storage bucket name for storing backups.                                                                                                                                       | -                                                 | string                                                                                                         | true     |
+| `openebs.backup_storage_location.provider`           | Cloud provider name for storing backups. Overrides `openebs.provider` field for backup storage.                                                                                      | -                                                 | string                                                                                                         | false    |
+| `openebs.backup_storage_location.name`               | Name for backup location object on the cluster.                                                                                                                                      | -                                                 | string                                                                                                         | false    |
+| `openebs.volume_snapshot_location`                   | Configure volume snapshot location.                                                                                                                                                  | -                                                 | object                                                                                                         | true     |
+| `openebs.volume_snapshot_location.bucket`            | Cloud storage bucket name for storing volume snapshots.                                                                                                                              | -                                                 | string                                                                                                         | true     |
+| `openebs.volume_snapshot_location.region`            | Cloud provider region for storing snapshots.                                                                                                                                         |                                                   | string                                                                                                         | true     |
+| `openebs.volume_snapshot_location.provider`          | Cloud provider name for storing snapshots. Overrides `openebs.provider` field for backup storage.                                                                                    | -                                                 | string                                                                                                         | false    |
+| `openebs.volume_snapshot_location.name`              | Name for snapshot location object on the cluster.                                                                                                                                    | -                                                 | string                                                                                                         | false    |
+| `openebs.volume_snapshot_location.prefix`            | Prefix for snapshot names.                                                                                                                                                           | -                                                 | string                                                                                                         | false    |
+| `openebs.volume_snapshot_location.local`             | If `true`, backups won't be copied to cloud storage.                                                                                                                                 | false                                             | bool                                                                                                           | false    |
+| `openebs.volume_snapshot_location.openebs_namespace` | Name of the namespace where OpenEBS runs.                                                                                                                                            | -                                                 | string                                                                                                         | true     |
+| `openebs.volume_snapshot_location.s3_url`            | S3 API URL.                                                                                                                                                                          | -                                                 | string                                                                                                         | false    |
+| `restic`                                             | Configure Restic provider for Velero.                                                                                                                                                | -                                                 | object                                                                                                         | false    |
+| `restic.credentials`                                 | Content of cloud provider credentials.                                                                                                                                               | -                                                 | string                                                                                                         | true     |
+| `restic.require_volume_annotation`                   | Backup all pod volumes without having to apply annotation on the pod when using restic. To exclude volumes add the annotation `backup.velero.io/backup-volumes-excludes` on the pod. | false                                             | bool                                                                                                           | false    |
+| `restic.backup_storage_location.provider`            | Cloud provider name for storing backups.                                                                                                                                             | -                                                 | string                                                                                                         | false    |
+| `restic.backup_storage_location.bucket`              | Cloud storage bucket name for storing backups.                                                                                                                                       | -                                                 | string                                                                                                         | true     |
+| `restic.backup_storage_location.name`                | Name for backup location object on the cluster.                                                                                                                                      | "default"                                         | string                                                                                                         | false    |
+| `restic.backup_storage_location.region`              | Cloud provider region for storing snapshots. Required if `restic.backup_storage_location.provider = aws`.                                                                            | -                                                 | string                                                                                                         | false    |
+| `restic.tolerations`                                 | Tolerations that the Restic provider installation daemonset will tolerate.                                                                                                           | -                                                 | list(object({key = string, effect = string, operator = string, value = string, toleration_seconds = string })) | false    |
 
 ## Applying
 
diff --git a/docs/how-to-guides/backup-rook-ceph-volumes.md b/docs/how-to-guides/backup-rook-ceph-volumes.md
index 15b4bf26..7033d04b 100644
--- a/docs/how-to-guides/backup-rook-ceph-volumes.md
+++ b/docs/how-to-guides/backup-rook-ceph-volumes.md
@@ -43,8 +43,6 @@ Create a file `velero.lokocfg` with the following contents:
 
 ```tf
 component "velero" {
-  provider = "restic"
-
   restic {
     credentials = file("./credentials-velero")
     backup_storage_location {
diff --git a/pkg/components/velero/restic/restic.go b/pkg/components/velero/restic/restic.go
index 298267bd..38491d11 100644
--- a/pkg/components/velero/restic/restic.go
+++ b/pkg/components/velero/restic/restic.go
@@ -23,6 +23,7 @@ import (
 	"github.com/hashicorp/hcl/v2"
 
 	"github.com/kinvolk/lokomotive/internal"
+	"github.com/kinvolk/lokomotive/pkg/components/util"
 )
 
 const indentation = 6
@@ -32,6 +33,8 @@ type Configuration struct {
 	Credentials             string                 `hcl:"credentials"`
 	RequireVolumeAnnotation bool                   `hcl:"require_volume_annotation,optional"`
 	BackupStorageLocation   *BackupStorageLocation `hcl:"backup_storage_location,block"`
+	Tolerations             []util.Toleration      `hcl:"tolerations,block"`
+	TolerationsRaw          string
 }
 
 // BackupStorageLocation configures the backup storage location.
@@ -55,6 +58,13 @@ func (c *Configuration) Values() (string, error) {
 
 	var buf bytes.Buffer
 
+	var err error
+
+	c.TolerationsRaw, err = util.RenderTolerations(c.Tolerations)
+	if err != nil {
+		return "", fmt.Errorf("rendering tolerations: %w", err)
+	}
+
 	v := struct {
 		Configuration       *Configuration
 		CredentialsIndented string
diff --git a/pkg/components/velero/restic/template.go b/pkg/components/velero/restic/template.go
index f69b22f1..7c3ccade 100644
--- a/pkg/components/velero/restic/template.go
+++ b/pkg/components/velero/restic/template.go
@@ -34,6 +34,9 @@ deployRestic: true
 snapshotsEnabled: false
 restic:
   privileged: true
+  {{- if .Configuration.Tolerations }}
+  tolerations: {{ .Configuration.TolerationsRaw }}
+  {{- end }}
 credentials:
   secretContents:
   {{- if .Configuration.Credentials }}


Test patch:
diff --git a/pkg/components/velero/component_test.go b/pkg/components/velero/component_test.go
index fee1fa58..a928da68 100644
--- a/pkg/components/velero/component_test.go
+++ b/pkg/components/velero/component_test.go
@@ -19,6 +19,7 @@ import (
 
 	"github.com/hashicorp/hcl/v2"
 
+	"github.com/kinvolk/lokomotive/pkg/components/internal/testutil"
 	"github.com/kinvolk/lokomotive/pkg/components/util"
 )
 
@@ -195,3 +196,46 @@ component "velero" {
 		t.Fatalf("Rendered manifests shouldn't be empty")
 	}
 }
+
+func TestRenderManifestResticToleration(t *testing.T) {
+	configHCL := `
+component "velero" {
+  provider = "restic"
+  restic {
+    credentials = "foo"
+
+    backup_storage_location {
+      bucket   = "foo"
+      provider = "aws"
+    }
+		
+    tolerations {
+      key                = "TestResticToletrationKey"
+      value              = "TestResticToletrationValue"
+      operator           = "Equal"
+      effect             = "NoSchedule"
+      toleration_seconds = "1"
+    }
+  }
+}
+`
+
+	component := NewConfig()
+
+	body, d := util.GetComponentBody(configHCL, Name)
+	if d.HasErrors() {
+		t.Fatalf("Error getting component body: %v", d)
+	}
+
+	if d = component.LoadConfig(body, &hcl.EvalContext{}); d.HasErrors() {
+		t.Fatalf("Valid config should not return error, got: %v", d)
+	}
+
+	m := testutil.RenderManifests(t, component, Name, configHCL)
+	jsonPath := "{.spec.template.spec.tolerations[0].key}"
+	expected := "TestResticToletrationKey"
+
+	gotConfig := testutil.ConfigFromMap(t, m, "velero/templates/restic-daemonset.yaml")
+
+	testutil.MatchJSONPathStringValue(t, gotConfig, jsonPath, expected)
+}