Kubernetes 클러스터에서 Velero 백업 및 복원 작업을 관리하기 위한 통합 클라이언트입니다. 백업, 복원, 저장소 위치 관리 등의 기능을 제공합니다.
Velero 클라이언트는 Kubernetes 백업 및 복원 도구인 Velero와 상호작용하기 위한 고수준 인터페이스를 제공합니다. 이 클라이언트는 복잡한 Velero API 호출을 단순화하고, 일관된 에러 처리를 제공하며, 타입 안전성을 보장합니다.
- 백업 관리: 백업 생성, 조회, 삭제
- 복원 관리: 복원 생성, 조회, 삭제
- 저장소 관리: 백업 저장소 위치 및 볼륨 스냅샷 위치 관리
- 리포지토리 관리: 백업 리포지토리 관리
- 볼륨 복원: Pod 볼륨 복원 관리
- 성능 최적화: 효율적인 백업/복원 관리 및 캐싱
- 설정 유연성: 다양한 설정 옵션 지원
import "github.com/taking/kubemigrate/pkg/client/velero"
// 클라이언트 생성
client := velero.NewClient()
// 백업 목록 조회
backups, err := client.GetBackups(ctx, "velero")
if err != nil {
return err
}
// 백업 목록 출력
for _, backup := range backups {
fmt.Printf("Backup: %s (Status: %s)\n", backup.Name, backup.Status.Phase)
}네임스페이스의 백업 목록을 조회합니다.
func (c *client) GetBackups(ctx context.Context, namespace string) ([]velerov1.Backup, error)매개변수:
ctx: 요청을 위한 컨텍스트namespace: 네임스페이스 (일반적으로 "velero")
반환값:
([]velerov1.Backup, error): 백업 목록, 에러
예제:
backups, err := client.GetBackups(ctx, "velero")
if err != nil {
return err
}
for _, backup := range backups {
fmt.Printf("Backup: %s (Status: %s)\n", backup.Name, backup.Status.Phase)
}특정 백업의 상세 정보를 조회합니다.
func (c *client) GetBackup(ctx context.Context, namespace, name string) (*velerov1.Backup, error)매개변수:
ctx: 요청을 위한 컨텍스트namespace: 네임스페이스name: 백업 이름
반환값:
(*velerov1.Backup, error): 백업 상세 정보, 에러
새로운 백업을 생성합니다.
func (c *client) CreateBackup(ctx context.Context, namespace string, backup *velerov1.Backup) error매개변수:
ctx: 요청을 위한 컨텍스트namespace: 네임스페이스backup: 백업 객체
백업을 삭제합니다.
func (c *client) DeleteBackup(ctx context.Context, namespace, name string) error네임스페이스의 복원 목록을 조회합니다.
func (c *client) GetRestores(ctx context.Context, namespace string) ([]velerov1.Restore, error)특정 복원의 상세 정보를 조회합니다.
func (c *client) GetRestore(ctx context.Context, namespace, name string) (*velerov1.Restore, error)새로운 복원을 생성합니다.
func (c *client) CreateRestore(ctx context.Context, namespace string, restore *velerov1.Restore) error복원을 삭제합니다.
func (c *client) DeleteRestore(ctx context.Context, namespace, name string) error네임스페이스의 백업 리포지토리 목록을 조회합니다.
func (c *client) GetBackupRepositories(ctx context.Context, namespace string) ([]velerov1.BackupRepository, error)특정 백업 리포지토리의 상세 정보를 조회합니다.
func (c *client) GetBackupRepository(ctx context.Context, namespace, name string) (*velerov1.BackupRepository, error)네임스페이스의 백업 저장소 위치 목록을 조회합니다.
func (c *client) GetBackupStorageLocations(ctx context.Context, namespace string) ([]velerov1.BackupStorageLocation, error)특정 백업 저장소 위치의 상세 정보를 조회합니다.
func (c *client) GetBackupStorageLocation(ctx context.Context, namespace, name string) (*velerov1.BackupStorageLocation, error)네임스페이스의 볼륨 스냅샷 위치 목록을 조회합니다.
func (c *client) GetVolumeSnapshotLocations(ctx context.Context, namespace string) ([]velerov1.VolumeSnapshotLocation, error)특정 볼륨 스냅샷 위치의 상세 정보를 조회합니다.
func (c *client) GetVolumeSnapshotLocation(ctx context.Context, namespace, name string) (*velerov1.VolumeSnapshotLocation, error)네임스페이스의 Pod 볼륨 복원 목록을 조회합니다.
func (c *client) GetPodVolumeRestores(ctx context.Context, namespace string) ([]velerov1.PodVolumeRestore, error)특정 Pod 볼륨 복원의 상세 정보를 조회합니다.
func (c *client) GetPodVolumeRestore(ctx context.Context, namespace, name string) (*velerov1.PodVolumeRestore, error)client := velero.NewClient()import "github.com/taking/kubemigrate/internal/config"
cfg := config.VeleroConfig{
KubeConfig: config.KubeConfig{
KubeConfig: "base64-encoded-kubeconfig",
Namespace: "velero",
},
}
client, err := velero.NewClientWithConfig(cfg)
if err != nil {
return err
}모든 메서드는 적절한 에러 처리를 포함합니다:
// 백업 조회
backup, err := client.GetBackup(ctx, "velero", "my-backup")
if err != nil {
return fmt.Errorf("백업 조회 실패: %w", err)
}
// 백업 상태 확인
if backup.Status.Phase == velerov1.BackupPhaseCompleted {
fmt.Println("백업이 성공적으로 완료되었습니다.")
} else if backup.Status.Phase == velerov1.BackupPhaseFailed {
fmt.Printf("백업이 실패했습니다: %s\n", backup.Status.Message)
}- 네임스페이스 확인: Velero는 일반적으로 "velero" 네임스페이스에 설치됨
- 상태 확인: 백업/복원 작업 후 상태를 확인하여 성공 여부 판단
- 에러 처리: 모든 Velero 작업에 대한 적절한 에러 처리
- 컨텍스트 사용: 취소 및 타임아웃을 위해 항상 컨텍스트 전달
import velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
// 백업 객체 생성
backup := &velerov1.Backup{
ObjectMeta: metav1.ObjectMeta{
Name: "my-backup",
},
Spec: velerov1.BackupSpec{
IncludedNamespaces: []string{"default"},
StorageLocation: "default",
VolumeSnapshotLocations: []string{"default"},
},
}
// 백업 생성
err := client.CreateBackup(ctx, "velero", backup)
if err != nil {
return err
}
fmt.Println("백업이 생성되었습니다.")// 백업 목록 조회
backups, err := client.GetBackups(ctx, "velero")
if err != nil {
return err
}
fmt.Printf("총 %d개의 백업이 있습니다:\n", len(backups))
for _, backup := range backups {
fmt.Printf("- %s (Status: %s, Created: %s)\n",
backup.Name, backup.Status.Phase, backup.CreationTimestamp)
}// 복원 객체 생성
restore := &velerov1.Restore{
ObjectMeta: metav1.ObjectMeta{
Name: "my-restore",
},
Spec: velerov1.RestoreSpec{
BackupName: "my-backup",
},
}
// 복원 생성
err := client.CreateRestore(ctx, "velero", restore)
if err != nil {
return err
}
fmt.Println("복원이 생성되었습니다.")// 백업 저장소 위치 목록 조회
locations, err := client.GetBackupStorageLocations(ctx, "velero")
if err != nil {
return err
}
for _, location := range locations {
fmt.Printf("저장소 위치: %s (Provider: %s, Access Mode: %s)\n",
location.Name, location.Spec.Provider, location.Spec.AccessMode)
}// 볼륨 스냅샷 위치 목록 조회
snapshotLocations, err := client.GetVolumeSnapshotLocations(ctx, "velero")
if err != nil {
return err
}
for _, location := range snapshotLocations {
fmt.Printf("스냅샷 위치: %s (Provider: %s)\n",
location.Name, location.Spec.Provider)
}// 백업 리포지토리 목록 조회
repositories, err := client.GetBackupRepositories(ctx, "velero")
if err != nil {
return err
}
for _, repo := range repositories {
fmt.Printf("리포지토리: %s (Status: %s)\n",
repo.Name, repo.Status.Phase)
}// Pod 볼륨 복원 목록 조회
podRestores, err := client.GetPodVolumeRestores(ctx, "velero")
if err != nil {
return err
}
for _, restore := range podRestores {
fmt.Printf("Pod 볼륨 복원: %s (Status: %s)\n",
restore.Name, restore.Status.Phase)
}// 백업 삭제
err := client.DeleteBackup(ctx, "velero", "my-backup")
if err != nil {
return err
}
fmt.Println("백업이 삭제되었습니다.")클라이언트 테스트는 다음과 같이 실행할 수 있습니다:
go test ./pkg/client/velero/... -v현재 테스트는 다음 기능들을 커버합니다:
NewClient()- 기본 클라이언트 생성NewClientWithConfig()- 설정을 통한 클라이언트 생성GetBackups()- 백업 목록 조회GetBackup()- 특정 백업 조회GetRestores()- 복원 목록 조회GetRestore()- 특정 복원 조회GetBackupRepositories()- 백업 리포지토리 목록 조회GetBackupRepository()- 특정 백업 리포지토리 조회GetBackupStorageLocations()- 백업 저장소 위치 목록 조회GetBackupStorageLocation()- 특정 백업 저장소 위치 조회GetVolumeSnapshotLocations()- 볼륨 스냅샷 위치 목록 조회GetVolumeSnapshotLocation()- 특정 볼륨 스냅샷 위치 조회GetPodVolumeRestores()- Pod 볼륨 복원 목록 조회GetPodVolumeRestore()- 특정 Pod 볼륨 복원 조회
func TestVeleroClient(t *testing.T) {
// 기본 클라이언트 생성
client := velero.NewClient()
if client == nil {
t.Fatal("NewClient() returned nil")
}
// 백업 목록 조회 테스트
ctx := context.Background()
backups, err := client.GetBackups(ctx, "velero")
if err != nil {
t.Logf("GetBackups failed as expected: %v", err)
} else {
t.Log("GetBackups succeeded - this might indicate a real cluster is available")
}
}