Skip to content

Commit

Permalink
ibu mgmt: move backup/restore tasks to separate utility (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
trewest authored Jun 20, 2024
1 parent d7ca0a6 commit 5cfd38a
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 146 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ linters-settings:
- "github.com/NVIDIA/gpu-operator"
- "github.com/operator-framework/api"
- "github.com/argoproj-labs/argocd-operator/api"
- "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
- "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme"
- "github.com/golang/glog"
- "github.com/rh-ecosystem-edge/kernel-module-management"
- "github.com/povsister/scp"
Expand Down
44 changes: 44 additions & 0 deletions tests/lca/imagebasedupgrade/mgmt/internal/brutil/brutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package brutil

import (
"fmt"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
)

// NewBackRestoreObject returns a BackupRestoreObject.
func NewBackRestoreObject(object runtime.Object, scheme *runtime.Scheme, gvr schema.GroupVersion) *BackupRestoreObject {
return &BackupRestoreObject{
Scheme: scheme,
GVR: gvr,
Object: object,
}
}

// String returns a string representation of the object passed to BackupRestoreObject.
func (b *BackupRestoreObject) String() (string, error) {
if b == nil {
return "", fmt.Errorf("backuprestoreobject is nil")
}

stringRep, err := dataFromDefinition(b.Scheme, b.Object, b.GVR)
if err != nil {
return "", err
}

return stringRep, nil
}

// dataFromDefinition returns a json string representation of the provided resource.
func dataFromDefinition(scheme *runtime.Scheme, obj runtime.Object, version schema.GroupVersion) (string, error) {
codec := serializer.NewCodecFactory(scheme).LegacyCodec(version)

data, err := runtime.Encode(codec, obj)
if err != nil {
return "", err
}

return string(data), nil
}
14 changes: 14 additions & 0 deletions tests/lca/imagebasedupgrade/mgmt/internal/brutil/brutil_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package brutil

import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

// BackupRestoreObject contains information for generating
// string representations of API resources.
type BackupRestoreObject struct {
Scheme *runtime.Scheme
GVR schema.GroupVersion
Object runtime.Object
}
99 changes: 99 additions & 0 deletions tests/lca/imagebasedupgrade/mgmt/internal/brutil/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package brutil

import (
"github.com/openshift-kni/eco-gotests/tests/lca/imagebasedupgrade/mgmt/internal/mgmtparams"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
veleroScheme "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// WorkloadBackup represents ibu-workload backup.
var WorkloadBackup = BackupRestoreObject{
Scheme: veleroScheme.Scheme,
GVR: velerov1.SchemeGroupVersion,
Object: &velerov1.Backup{
ObjectMeta: metav1.ObjectMeta{
Name: mgmtparams.LCAWorkloadName,
Namespace: mgmtparams.LCAOADPNamespace,
},
Spec: velerov1.BackupSpec{
IncludedNamespaces: []string{
mgmtparams.LCAWorkloadName,
},
IncludedNamespaceScopedResources: []string{
"deployments",
"services",
"routes",
},
ExcludedClusterScopedResources: []string{
"persistentVolumes",
},
StorageLocation: "default",
},
},
}

// WorkloadRestore represents ibu-workload restore.
var WorkloadRestore = BackupRestoreObject{
Scheme: veleroScheme.Scheme,
GVR: velerov1.SchemeGroupVersion,
Object: &velerov1.Restore{
ObjectMeta: metav1.ObjectMeta{
Name: mgmtparams.LCAWorkloadName,
Namespace: mgmtparams.LCAOADPNamespace,
Labels: map[string]string{
"velero.io/storage-location": "default",
},
},
Spec: velerov1.RestoreSpec{
BackupName: mgmtparams.LCAWorkloadName,
},
},
}

// KlusterletBackup represents ibu klusterlet backup.
var KlusterletBackup = BackupRestoreObject{
Scheme: veleroScheme.Scheme,
GVR: velerov1.SchemeGroupVersion,
Object: &velerov1.Backup{
ObjectMeta: metav1.ObjectMeta{
Name: mgmtparams.LCAKlusterletName,
Namespace: mgmtparams.LCAOADPNamespace,
},
Spec: velerov1.BackupSpec{
IncludedNamespaces: []string{
mgmtparams.LCAKlusterletNamespace,
},
IncludedClusterScopedResources: []string{
"klusterlets.operator.open-cluster-management.io",
"clusterclaims.cluster.open-cluster-management.io",
"clusterroles.rbac.authorization.k8s.io",
"clusterrolebindings.rbac.authorization.k8s.io",
},
IncludedNamespaceScopedResources: []string{
"deployments",
"serviceaccounts",
"secrets",
},
StorageLocation: "default",
},
},
}

// KlusterletRestore represents ibu klusterlet restore.
var KlusterletRestore = BackupRestoreObject{
Scheme: veleroScheme.Scheme,
GVR: velerov1.SchemeGroupVersion,
Object: &velerov1.Restore{
ObjectMeta: metav1.ObjectMeta{
Name: mgmtparams.LCAKlusterletName,
Namespace: mgmtparams.LCAOADPNamespace,
Labels: map[string]string{
"velero.io/storage-location": "default",
},
},
Spec: velerov1.RestoreSpec{
BackupName: mgmtparams.LCAKlusterletName,
},
},
}

This file was deleted.

15 changes: 15 additions & 0 deletions tests/lca/imagebasedupgrade/mgmt/internal/mgmtparams/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ const (
// Label represents mgmt label that can be used for test cases selection.
Label = "mgmt"

// LCANamespace is the namespace used by the lifecycle-agent.
LCANamespace = "openshift-lifecycle-agent"

// LCAWorkloadName is the name used for creating resources needed to backup workload app.
LCAWorkloadName = "ibu-workload-app"

// LCAOADPNamespace is the namespace used by the OADP operator.
LCAOADPNamespace = "openshift-adp"

// LCAKlusterletName is the name of the backup/restore objects related to the klusterlet.
LCAKlusterletName = "ibu-klusterlet"

// LCAKlusterletNamespace is the namespace that contains the klusterlet.
LCAKlusterletNamespace = "open-cluster-management-agent"

// MGMTLogLevel custom loglevel for the mgmt testing verbose mode.
MGMTLogLevel = 50
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,15 @@ import (
corev1 "k8s.io/api/core/v1"
)

const (
// LCANamespace is the namespace used by the lifecycle-agent.
LCANamespace = "openshift-lifecycle-agent"

// LCAWorkloadName is the name used for creating resources needed to backup workload app.
LCAWorkloadName = "ibu-workload-app"

// LCAOADPNamespace is the namespace used by the OADP operator.
LCAOADPNamespace = "openshift-adp"

// LCAKlusterletNamespace is the namespace that contains the klusterlet.
LCAKlusterletNamespace = "open-cluster-management-agent"
)

var (
// Labels represents the range of labels that can be used for test cases selection.
Labels = append(mgmtparams.Labels, LabelSuite)

// ReporterNamespacesToDump tells to the reporter from where to collect logs.
ReporterNamespacesToDump = map[string]string{
LCANamespace: "lca",
LCAWorkloadName: "workload",
LCAKlusterletNamespace: "klusterlet",
mgmtparams.LCANamespace: "lca",
mgmtparams.LCAWorkloadName: "workload",
mgmtparams.LCAKlusterletNamespace: "klusterlet",
}

// ReporterCRDsToDump tells to the reporter what CRs to dump.
Expand Down
Loading

0 comments on commit 5cfd38a

Please sign in to comment.