Skip to content

Commit

Permalink
address pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig O'Donnell committed Nov 21, 2023
1 parent 5b3b32e commit 823c13a
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 68 deletions.
26 changes: 14 additions & 12 deletions pkg/reporting/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/replicatedhq/kots/pkg/util"
troubleshootpreflight "github.com/replicatedhq/troubleshoot/pkg/preflight"
"github.com/segmentio/ksuid"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
veleroclientv1 "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1"
helmrelease "helm.sh/helm/v3/pkg/release"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -272,15 +273,20 @@ func GetReportingInfo(appID string) *types.ReportingInfo {
}

if clientset != nil && veleroClient != nil {
report, err := getSnapshotReport(store.GetStore(), clientset, veleroClient, appID, r.ClusterID)
bsl, err := snapshot.FindBackupStoreLocation(context.TODO(), clientset, veleroClient, util.PodNamespace)
if err != nil {
logger.Debugf("failed to get snapshot report: %v", err.Error())
logger.Debugf("failed to find backup store location: %v", err.Error())
} else {
r.SnapshotProvider = report.Provider
r.SnapshotFullSchedule = report.FullSchedule
r.SnapshotFullTTL = report.FullTTL
r.SnapshotPartialSchedule = report.PartialSchedule
r.SnapshotPartialTTL = report.PartialTTL
report, err := getSnapshotReport(store.GetStore(), bsl, appID, r.ClusterID)
if err != nil {
logger.Debugf("failed to get snapshot report: %v", err.Error())
} else {
r.SnapshotProvider = report.Provider
r.SnapshotFullSchedule = report.FullSchedule
r.SnapshotFullTTL = report.FullTTL
r.SnapshotPartialSchedule = report.PartialSchedule
r.SnapshotPartialTTL = report.PartialTTL
}
}
}

Expand Down Expand Up @@ -362,13 +368,9 @@ func getGitOpsReport(clientset kubernetes.Interface, appID string, clusterID str
return false, ""
}

func getSnapshotReport(kotsStore store.Store, clientset kubernetes.Interface, veleroClient veleroclientv1.VeleroV1Interface, appID string, clusterID string) (*SnapshotReport, error) {
func getSnapshotReport(kotsStore store.Store, bsl *velerov1.BackupStorageLocation, appID string, clusterID string) (*SnapshotReport, error) {
report := &SnapshotReport{}

bsl, err := snapshot.FindBackupStoreLocation(context.TODO(), clientset, veleroClient, util.PodNamespace)
if err != nil {
return nil, errors.Wrap(err, "failed to find backup store location")
}
if bsl == nil {
return nil, errors.New("no backup store location found")
}
Expand Down
75 changes: 19 additions & 56 deletions pkg/reporting/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import (
"github.com/replicatedhq/kots/pkg/store"
mock_store "github.com/replicatedhq/kots/pkg/store/mock"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
velerofake "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/fake"
veleroclientv1 "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
)

func Test_getSnapshotReport(t *testing.T) {
Expand All @@ -28,30 +22,25 @@ func Test_getSnapshotReport(t *testing.T) {
t.Setenv("POD_NAMESPACE", "default")

testVeleroNamespace := "velero"
veleroNamespaceConfigmap := &corev1.ConfigMap{
testBsl := &velerov1.BackupStorageLocation{
ObjectMeta: metav1.ObjectMeta{
Name: "kotsadm-velero-namespace",
},
Data: map[string]string{
"veleroNamespace": testVeleroNamespace,
},
}
veleroDeployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "velero",
Name: "default",
Namespace: testVeleroNamespace,
},
Spec: velerov1.BackupStorageLocationSpec{
Provider: "aws",
Default: true,
},
}

testAppID := "test-app-id"
testClusterID := "test-cluster-id"

type args struct {
kotsStore store.Store
clientset kubernetes.Interface
veleroClient veleroclientv1.VeleroV1Interface
appID string
clusterID string
kotsStore store.Store
bsl *velerov1.BackupStorageLocation
appID string
clusterID string
}
tests := []struct {
name string
Expand All @@ -64,10 +53,7 @@ func Test_getSnapshotReport(t *testing.T) {
name: "happy path with schedule and ttl",
args: args{
kotsStore: mockStore,
clientset: fake.NewSimpleClientset(veleroNamespaceConfigmap, veleroDeployment),
veleroClient: velerofake.NewSimpleClientset(
backupStorageLocationWithProvider(testVeleroNamespace, "aws"),
).VeleroV1(),
bsl: testBsl,
appID: testAppID,
clusterID: testClusterID,
},
Expand Down Expand Up @@ -97,10 +83,7 @@ func Test_getSnapshotReport(t *testing.T) {
name: "happy path with default ttl only",
args: args{
kotsStore: mockStore,
clientset: fake.NewSimpleClientset(veleroNamespaceConfigmap, veleroDeployment),
veleroClient: velerofake.NewSimpleClientset(
backupStorageLocationWithProvider(testVeleroNamespace, "aws"),
).VeleroV1(),
bsl: testBsl,
appID: testAppID,
clusterID: testClusterID,
},
Expand Down Expand Up @@ -129,11 +112,10 @@ func Test_getSnapshotReport(t *testing.T) {
{
name: "no backup storage location",
args: args{
kotsStore: mockStore,
clientset: fake.NewSimpleClientset(veleroNamespaceConfigmap, veleroDeployment),
veleroClient: velerofake.NewSimpleClientset().VeleroV1(),
appID: testAppID,
clusterID: testClusterID,
kotsStore: mockStore,
bsl: nil,
appID: testAppID,
clusterID: testClusterID,
},
mockStoreExpectations: func() {},
wantErr: true,
Expand All @@ -142,10 +124,7 @@ func Test_getSnapshotReport(t *testing.T) {
name: "failed to list clusters",
args: args{
kotsStore: mockStore,
clientset: fake.NewSimpleClientset(veleroNamespaceConfigmap, veleroDeployment),
veleroClient: velerofake.NewSimpleClientset(
backupStorageLocationWithProvider(testVeleroNamespace, "aws"),
).VeleroV1(),
bsl: testBsl,
appID: testAppID,
clusterID: testClusterID,
},
Expand All @@ -158,10 +137,7 @@ func Test_getSnapshotReport(t *testing.T) {
name: "failed to get app",
args: args{
kotsStore: mockStore,
clientset: fake.NewSimpleClientset(veleroNamespaceConfigmap, veleroDeployment),
veleroClient: velerofake.NewSimpleClientset(
backupStorageLocationWithProvider(testVeleroNamespace, "aws"),
).VeleroV1(),
bsl: testBsl,
appID: testAppID,
clusterID: testClusterID,
},
Expand All @@ -181,7 +157,7 @@ func Test_getSnapshotReport(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.mockStoreExpectations()
got, err := getSnapshotReport(tt.args.kotsStore, tt.args.clientset, tt.args.veleroClient, tt.args.appID, tt.args.clusterID)
got, err := getSnapshotReport(tt.args.kotsStore, tt.args.bsl, tt.args.appID, tt.args.clusterID)
if (err != nil) != tt.wantErr {
t.Errorf("getSnapshotReport() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -192,16 +168,3 @@ func Test_getSnapshotReport(t *testing.T) {
})
}
}

func backupStorageLocationWithProvider(namespace string, provider string) *velerov1.BackupStorageLocation {
return &velerov1.BackupStorageLocation{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
Namespace: namespace,
},
Spec: velerov1.BackupStorageLocationSpec{
Provider: provider,
Default: true,
},
}
}
88 changes: 88 additions & 0 deletions pkg/snapshot/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package snapshot

import (
"context"
"reflect"
"testing"

"github.com/replicatedhq/kots/pkg/snapshot/types"
"github.com/stretchr/testify/require"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
velerofake "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/fake"
veleroclientv1 "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
testclient "k8s.io/client-go/kubernetes/fake"
"k8s.io/utils/pointer"
)
Expand Down Expand Up @@ -510,3 +516,85 @@ func Test_isMinioMigration(t *testing.T) {
})
}
}

func TestFindBackupStoreLocation(t *testing.T) {

testVeleroNamespace := "velero"
testBsl := &velerov1.BackupStorageLocation{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
Namespace: testVeleroNamespace,
},
Spec: velerov1.BackupStorageLocationSpec{
Provider: "aws",
Default: true,
},
}
veleroNamespaceConfigmap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "kotsadm-velero-namespace",
},
Data: map[string]string{
"veleroNamespace": testVeleroNamespace,
},
}
veleroDeployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "velero",
Namespace: testVeleroNamespace,
},
}

type args struct {
clientset kubernetes.Interface
veleroClient veleroclientv1.VeleroV1Interface
kotsadmNamespace string
}
tests := []struct {
name string
args args
want *velerov1.BackupStorageLocation
wantErr bool
}{
{
name: "backup store location found",
args: args{
clientset: fake.NewSimpleClientset(veleroNamespaceConfigmap, veleroDeployment),
veleroClient: velerofake.NewSimpleClientset(testBsl).VeleroV1(),
kotsadmNamespace: "default",
},
want: testBsl,
},
{
name: "return nil if no backup store location found",
args: args{
clientset: fake.NewSimpleClientset(veleroNamespaceConfigmap, veleroDeployment),
veleroClient: velerofake.NewSimpleClientset().VeleroV1(),
kotsadmNamespace: "default",
},
want: nil,
},
{
name: "return nil if no velero deployment found",
args: args{
clientset: fake.NewSimpleClientset(veleroNamespaceConfigmap),
veleroClient: velerofake.NewSimpleClientset(testBsl).VeleroV1(),
kotsadmNamespace: "default",
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
got, err := FindBackupStoreLocation(ctx, tt.args.clientset, tt.args.veleroClient, tt.args.kotsadmNamespace)
if (err != nil) != tt.wantErr {
t.Errorf("FindBackupStoreLocation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FindBackupStoreLocation() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 823c13a

Please sign in to comment.