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

Commit

Permalink
Merge pull request #881 from kinvolk/imran/velero-for-packet
Browse files Browse the repository at this point in the history
Update Velero component for Packet [openebs and restic plugin support]
  • Loading branch information
ipochi authored Sep 22, 2020
2 parents 8093a06 + 3fb6e97 commit 2ff68cf
Show file tree
Hide file tree
Showing 15 changed files with 1,040 additions and 283 deletions.
153 changes: 104 additions & 49 deletions docs/configuration-reference/components/velero.md

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
// Package internal contains the utility functions used across the codebase.
package internal

import (
"strings"
)

const (
// NamespaceLabelKey acts a placeholder for the generic key name
// `lokomotive.kinvolk.io/name`.
Expand Down Expand Up @@ -57,3 +61,28 @@ func MergeMaps(m1, m2 map[string]string) map[string]string {

return final
}

// Indent indents the given string after splitting it first on `\n`
// and adds the space padding to each token by the provided indent number.
func Indent(data string, indent int) string {
lines := strings.Split(data, "\n")

var gap string

// Calculate the gap/indent.
for i := 0; i < indent; i++ {
gap += " "
}

// For each line add the gap/indent.
for ind := range lines {
lines[ind] = gap + lines[ind]
}

// If the last line is empty then remove the indent from it.
if lines[len(lines)-1] == gap {
lines[len(lines)-1] = ""
}

return strings.Join(lines, "\n")
}
63 changes: 63 additions & 0 deletions internal/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,66 @@ func TestMergeMapsNil(t *testing.T) {
t.Errorf("expected map to be empty but not nil, got: %v", final)
}
}

func TestIndent(t *testing.T) {
type args struct {
data string
indent int
}

tests := []struct {
name string
args args
want string
}{
{
args: args{
data: `foo:
bar:
- baz`,
indent: 2,
},
want: ` foo:
bar:
- baz`,
},
{
args: args{
data: "singleline",
indent: 3,
},
want: " singleline",
},
{
args: args{
data: `a:
b: foobar
`,
indent: 4,
},
want: ` a:
b: foobar
`,
},
{
args: args{
data: `[default]
aws_access_key=test_key
aws_secret_access_key=secret_key`,
indent: 6,
},
want: ` [default]
aws_access_key=test_key
aws_secret_access_key=secret_key`,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := internal.Indent(tt.args.data, tt.args.indent); got != tt.want {
t.Errorf("indent() = \n%v\nwant =\n%v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/components/gangway/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

const name = "gangway"

func init() { //nolint:gocheckinit
func init() { //nolint:gochecknoinits
components.Register(name, newComponent())
}

Expand Down
31 changes: 4 additions & 27 deletions pkg/components/linkerd/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ package linkerd

import (
"fmt"
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/linkerd/linkerd2/pkg/tls"
"helm.sh/helm/v3/pkg/chartutil"

"github.com/kinvolk/lokomotive/internal"
internaltemplate "github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
Expand Down Expand Up @@ -134,29 +134,6 @@ func (c *component) Metadata() components.Metadata {
}
}

func indent(data string, indent int) string {
lines := strings.Split(data, "\n")

var gap string

// Calculate the gap/indent.
for i := 0; i < indent; i++ {
gap += " "
}

// For each line add the gap/indent.
for ind := range lines {
lines[ind] = gap + lines[ind]
}

// If the last line is empty then remove the indent from it.
if lines[len(lines)-1] == gap {
lines[len(lines)-1] = ""
}

return strings.Join(lines, "\n")
}

func mergeValuesFiles(dst, src string) (string, error) {
d, err := chartutil.ReadValues([]byte(dst))
if err != nil {
Expand All @@ -178,9 +155,9 @@ func generateCertificates() (cert, error) {
}

return cert{
Key: indent(root.Cred.EncodePrivateKeyPEM(), 8),
Cert: indent(root.Cred.Crt.EncodeCertificatePEM(), 8),
CA: indent(root.Cred.Crt.EncodeCertificatePEM(), 4),
Key: internal.Indent(root.Cred.EncodePrivateKeyPEM(), 8),
Cert: internal.Indent(root.Cred.Crt.EncodeCertificatePEM(), 8),
CA: internal.Indent(root.Cred.Crt.EncodeCertificatePEM(), 4),
Expiry: root.Cred.Crt.Certificate.NotAfter.String(),
}, nil
}
70 changes: 0 additions & 70 deletions pkg/components/linkerd/component_test.go

This file was deleted.

20 changes: 20 additions & 0 deletions pkg/components/velero/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

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

"github.com/hashicorp/hcl/v2"
)

Expand All @@ -31,17 +36,32 @@ type Configuration struct {

// BackupStorageLocation stores information about storage account used for backups on Azure
type BackupStorageLocation struct {
Name string `hcl:"name,optional"`
ResourceGroup string `hcl:"resource_group,optional"`
StorageAccount string `hcl:"storage_account,optional"`
Bucket string `hcl:"bucket,optional"`
}

// VolumeSnapshotLocation stores information where disk snapshots will be stored on Azure
type VolumeSnapshotLocation struct {
Name string `hcl:"name,optional"`
ResourceGroup string `hcl:"resource_group,optional"`
APITimeout string `hcl:"api_timeout,optional"`
}

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

var buf bytes.Buffer

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

return buf.String(), nil
}

// Validate validates azure specific parts in the configuration
func (c *Configuration) Validate() hcl.Diagnostics {
var diagnostics hcl.Diagnostics
Expand Down
55 changes: 55 additions & 0 deletions pkg/components/velero/azure/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 azure

const chartValuesTmpl = `
configuration:
provider: azure
backupStorageLocation:
name: azure
provider: velero.io/azure
bucket: {{ .BackupStorageLocation.Bucket }}
config:
resourceGroup: {{ .BackupStorageLocation.ResourceGroup }}
storageAccount: {{ .BackupStorageLocation.StorageAccount }}
volumeSnapshotLocation:
{{- if .VolumeSnapshotLocation.Name }}
name: {{ .VolumeSnapshotLocation.Name }}
{{- end }}
provider: velero.io/azure
config:
{{- if .VolumeSnapshotLocation.ResourceGroup }}
resourceGroup: {{ .VolumeSnapshotLocation.ResourceGroup }}
{{- end }}
apitimeout: {{ .VolumeSnapshotLocation.APITimeout }}
credentials:
secretContents:
cloud: |
AZURE_SUBSCRIPTION_ID: "{{ .SubscriptionID }}"
AZURE_TENANT_ID: "{{ .TenantID }}"
AZURE_CLIENT_ID: "{{ .ClientID }}"
AZURE_CLIENT_SECRET: "{{ .ClientSecret }}"
AZURE_RESOURCE_GROUP: "{{ .ResourceGroup }}"
initContainers:
- image: velero/velero-plugin-for-microsoft-azure:v1.0.0
imagePullPolicy: IfNotPresent
name: velero-plugin-for-azure
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /target
name: plugins
`
Loading

0 comments on commit 2ff68cf

Please sign in to comment.