Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Ability to use to CSI plugin for Velero. Supports AWS EBS CSI driver. #1285

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
124 changes: 79 additions & 45 deletions docs/configuration-reference/components/velero.md

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion pkg/components/velero/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
"github.com/kinvolk/lokomotive/pkg/components/velero/azure"
"github.com/kinvolk/lokomotive/pkg/components/velero/csi"
"github.com/kinvolk/lokomotive/pkg/components/velero/openebs"
"github.com/kinvolk/lokomotive/pkg/components/velero/restic"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
Expand All @@ -50,6 +51,8 @@ type component struct {
OpenEBS *openebs.Configuration `hcl:"openebs,block"`
// Restic specific parameters.
Restic *restic.Configuration `hcl:"restic,block"`
// CSI specific configuration.
CSI *csi.Configuration `hcl:"csi,block"`
}

// Metrics represents prometheus specific parameters
Expand Down Expand Up @@ -184,7 +187,7 @@ func (c *component) validate() hcl.Diagnostics {

// getSupportedProviders returns a list of supported providers.
func (c *component) getSupportedProviders() []string {
return []string{"azure", "openebs", "restic"}
return []string{"azure", "openebs", "restic", "csi"}
}

// getProvider returns correct provider interface based on component configuration.
Expand All @@ -199,6 +202,8 @@ func (c *component) getProvider() (provider, error) {
return c.OpenEBS, nil
case "restic":
return c.Restic, nil
case "csi":
return c.CSI, nil
default:
return nil, fmt.Errorf("unknown provider: %s", c.Provider)
}
Expand Down
43 changes: 43 additions & 0 deletions pkg/components/velero/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,46 @@ component "velero" {
t.Fatalf("Rendered manifests shouldn't be empty")
}
}

func TestRenderManifestCSIAWSDriver(t *testing.T) {
configHCL := `
component "velero" {
provider = "csi"

csi {
aws {
credentials = "foo"
backup_storage_location {
bucket = "foo"
region = "foo"
}

volume_snapshot_location {
region = "foo"
}
}
}
}
`

component := NewConfig()

body, diagnostics := util.GetComponentBody(configHCL, Name)
if diagnostics != nil {
t.Fatalf("Error getting component body: %v", diagnostics)
}

diagnostics = component.LoadConfig(body, &hcl.EvalContext{})
if diagnostics.HasErrors() {
t.Fatalf("Valid config should not return error, got: %s", diagnostics)
}

m, err := component.RenderManifests()
if err != nil {
t.Fatalf("Rendering manifests with valid config should succeed, got: %s", err)
}

if len(m) == 0 {
t.Fatalf("Rendered manifests shouldn't be empty")
}
}
56 changes: 56 additions & 0 deletions pkg/components/velero/csi/csi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2020 The Lokomotive Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package csi deals with configuring Velero CSI plugin.
package csi

import (
"github.com/hashicorp/hcl/v2"

"github.com/kinvolk/lokomotive/pkg/components/velero/csi/drivers/aws"
)

// Configuration contains various CSI driver specific sub block.
type Configuration struct {
// AWSDriver represents the AWS EBS CSI driver component.
AWSDriver *aws.Configuration `hcl:"aws,block"`
}

// Values returns the plugin specific values for Velero Helm chart.
func (c *Configuration) Values() (string, error) {
return c.AWSDriver.Values()
}

// Validate validates CSI driver specific parts in the configuration.
func (c *Configuration) Validate() hcl.Diagnostics {
return c.validate()
}

// validate validates component configuration.
func (c *Configuration) validate() hcl.Diagnostics {
diagnostics := hcl.Diagnostics{}
// Since the only supported driver currently is AWS EBS CSI driver, currently we
// don't deal with checks such as only one driver should be configured, based on
// the driver configured, its configuration should be validated.
// This absraction would be needed once more CSI drivers are supported.
if c.AWSDriver == nil {
return append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "CSI driver for AWS not configured",
Detail: "Make sure to configure the `aws` sub block under `csi`",
})
}

return append(diagnostics, c.AWSDriver.Validate()...)
}
149 changes: 149 additions & 0 deletions pkg/components/velero/csi/drivers/aws/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright 2020 The Lokomotive Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package aws deals with configuring Velero aws plugin.
package aws

import (
"bytes"
"fmt"
"text/template"

"github.com/hashicorp/hcl/v2"

"github.com/kinvolk/lokomotive/internal"
)

const indentation = 6

// Configuration contains AWS specific parameters.
type Configuration struct {
Credentials string `hcl:"credentials"`
BackupStorageLocation *BackupStorageLocation `hcl:"backup_storage_location,block"`
VolumeSnapshotLocation *VolumeSnapshotLocation `hcl:"volume_snapshot_location,block"`
}

// BackupStorageLocation configures the backup storage location for AWS plugin.
type BackupStorageLocation struct {
Region string `hcl:"region"`
Bucket string `hcl:"bucket"`
Name string `hcl:"name,optional"`
Prefix string `hcl:"prefix,optional"`
S3ForcePathStyle bool `hcl:"s3_force_path_style,optional"`
S3URL string `hcl:"s3_url,optional"`
PublicURL string `hcl:"public_url,optional"`
}

// validate validates BackupStorageLocation struct fields.
func (b *BackupStorageLocation) validate() hcl.Diagnostics {
var diagnostics hcl.Diagnostics

if b == nil {
b = &BackupStorageLocation{}

diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "'csi.aws.backup_storage_location' block must be specified",
Detail: "Make sure to set the field to valid non-empty value",
})
}

if b.Bucket == "" {
diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "'csi.aws.backup_storage_location.bucket' cannot be empty",
Detail: "Make sure to set the field to valid non-empty value",
})
}

if b.Region == "" {
diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "'csi.aws.backup_storage_location.region' cannot be empty",
Detail: "Make sure to set the field to valid non-empty value",
})
}

return diagnostics
}

// VolumeSnapshotLocation configures the volume snapshot location for the AWS plugin.
type VolumeSnapshotLocation struct {
Region string `hcl:"region"`
Name string `hcl:"name,optional"`
}

// validate validates VolumeSnapshotLocation struct fields.
func (v *VolumeSnapshotLocation) validate() hcl.Diagnostics {
var diagnostics hcl.Diagnostics

if v == nil {
v = &VolumeSnapshotLocation{}

diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "'csi.aws.volume_snapshot_location' block must be specified",
Detail: "Make sure to set the field to valid non-empty value",
})
}

if v.Region == "" {
diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "'csi.aws.volume_snapshot_location.region' cannot be empty",
Detail: "Make sure to set the field to valid non-empty value",
})
}

return diagnostics
}

// Values returns the plugin specific values for Velero Helm chart.
func (c *Configuration) Values() (string, error) {
t := template.Must(template.New("values").Parse(chartValuesTmpl))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use template.Must we need to have unit tests to validate the template.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkg/components/velero/component_test.go has the tests for each provider.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh.. Can we have a tests locally to the package? Otherwise changing code in one package breaks tests in other, this is not nice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added unit tests.


var buf bytes.Buffer

v := struct {
Configuration *Configuration
CredentialsIndented string
}{
Configuration: c,
CredentialsIndented: internal.Indent(c.Credentials, indentation),
}

if err := t.Execute(&buf, v); err != nil {
return "", fmt.Errorf("executing values template: %w", err)
}

return buf.String(), nil
}

// Validate validates AWS plugin specific parts in the configuration.
func (c *Configuration) Validate() hcl.Diagnostics {
var diagnostics hcl.Diagnostics

if c.Credentials == "" {
diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "'credentials' cannot be empty",
Detail: "No credentials found",
})
}

diagnostics = append(diagnostics, c.BackupStorageLocation.validate()...)
diagnostics = append(diagnostics, c.VolumeSnapshotLocation.validate()...)

return diagnostics
}
Loading