Skip to content

Commit

Permalink
use versions from deployment to check deprecated type
Browse files Browse the repository at this point in the history
Signed-off-by: Archit Sharma <[email protected]>
  • Loading branch information
arcolife committed Nov 3, 2023
1 parent 9899376 commit 3b7331f
Showing 1 changed file with 106 additions and 29 deletions.
135 changes: 106 additions & 29 deletions pkg/analyze/velero.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ package analyzer
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"

appsV1 "k8s.io/api/apps/v1"

restic_types "github.com/replicatedhq/troubleshoot/pkg/analyze/types"
"golang.org/x/mod/semver"

"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
Expand Down Expand Up @@ -43,40 +48,72 @@ func (a *AnalyzeVelero) Analyze(getFile getCollectedFileContents, findFiles getC

func (a *AnalyzeVelero) veleroStatus(analyzer *troubleshootv1beta2.VeleroAnalyze, getFileContents getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
excludeFiles := []string{}
results := []*AnalyzeResult{}

// get backuprepositories.velero.io
backupRepositoriesDir := GetVeleroBackupRepositoriesDirectory()
backupRepositoriesGlob := filepath.Join(backupRepositoriesDir, "*.json")
backupRepositoriesJson, err := findFiles(backupRepositoriesGlob, excludeFiles)
oldVeleroRepoType := false
veleroVersion, err := getVeleroVersion()
// convert semver string to semver.Version
if err != nil {
return nil, errors.Wrapf(err, "failed to find velero backup repositories files under %s", backupRepositoriesDir)
return nil, errors.Wrap(err, "Unable to find velero deployment")
}
backupRepositories := []*velerov1.BackupRepository{}
for key, backupRepositoryJson := range backupRepositoriesJson {
var backupRepositoryArray []*velerov1.BackupRepository
err := json.Unmarshal(backupRepositoryJson, &backupRepositoryArray)
if err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal backup repository json from %s", key)
}
backupRepositories = append(backupRepositories, backupRepositoryArray...)

// Check if the version string is valid semver
if !semver.IsValid(veleroVersion) {
fmt.Printf("Invalid semver: %s\n", veleroVersion)
return nil, errors.Errorf("Invalid velero semver: %s", veleroVersion)
}

// old velero (v1.9.x) has a BackupRepositoryTypeRestic
// get resticrepositories.velero.io
resticRepositoriesDir := GetVeleroResticRepositoriesDirectory()
resticRepositoriesGlob := filepath.Join(resticRepositoriesDir, "*.json")
resticRepositoriesJson, err := findFiles(resticRepositoriesGlob, excludeFiles)
if err != nil {
return nil, errors.Wrapf(err, "failed to find velero restic repositories files under %s", resticRepositoriesDir)
canonicalVersion := semver.Canonical(veleroVersion)
fmt.Printf("Semver: %s\n", canonicalVersion)

// check if veleroVersion is less than 1.10.x
compareResult := semver.Compare(veleroVersion, "1.10.0")
if compareResult < 0 {
fmt.Printf("Version %s is less than %s\n", veleroVersion, "1.10.0")
oldVeleroRepoType = true
}
resticRepositories := []*restic_types.ResticRepository{}
for key, resticRepositoryJson := range resticRepositoriesJson {
var resticRepositoryArray []*restic_types.ResticRepository
err := json.Unmarshal(resticRepositoryJson, &resticRepositories)

if oldVeleroRepoType == true {
// old velero (v1.9.x) has a BackupRepositoryTypeRestic
// get resticrepositories.velero.io
resticRepositoriesDir := GetVeleroResticRepositoriesDirectory()
resticRepositoriesGlob := filepath.Join(resticRepositoriesDir, "*.json")
resticRepositoriesJson, err := findFiles(resticRepositoriesGlob, excludeFiles)
if err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal restic repository json from %s", key)
return nil, errors.Wrapf(err, "failed to find velero restic repositories files under %s", resticRepositoriesDir)
}
resticRepositories := []*restic_types.ResticRepository{}
for key, resticRepositoryJson := range resticRepositoriesJson {
var resticRepositoryArray []*restic_types.ResticRepository
err := json.Unmarshal(resticRepositoryJson, &resticRepositories)
if err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal restic repository json from %s", key)
}
resticRepositories = append(resticRepositories, resticRepositoryArray...)
}
results = append(results, analyzeResticRepositories(resticRepositories)...)

} else {

// velerov1.Version
// get backuprepositories.velero.io
backupRepositoriesDir := GetVeleroBackupRepositoriesDirectory()
backupRepositoriesGlob := filepath.Join(backupRepositoriesDir, "*.json")
backupRepositoriesJson, err := findFiles(backupRepositoriesGlob, excludeFiles)
if err != nil {
return nil, errors.Wrapf(err, "failed to find velero backup repositories files under %s", backupRepositoriesDir)
}
backupRepositories := []*velerov1.BackupRepository{}
for key, backupRepositoryJson := range backupRepositoriesJson {
var backupRepositoryArray []*velerov1.BackupRepository
err := json.Unmarshal(backupRepositoryJson, &backupRepositoryArray)
if err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal backup repository json from %s", key)
}
backupRepositories = append(backupRepositories, backupRepositoryArray...)
}
resticRepositories = append(resticRepositories, resticRepositoryArray...)
results = append(results, analyzeBackupRepositories(backupRepositories)...)

}

// get backups.velero.io
Expand Down Expand Up @@ -243,10 +280,7 @@ func (a *AnalyzeVelero) veleroStatus(analyzer *troubleshootv1beta2.VeleroAnalyze
return nil, errors.Wrapf(err, "failed to find velero logs files under %s", logsDir)
}

results := []*AnalyzeResult{}
results = append(results, analyzeLogs(logs)...)
results = append(results, analyzeBackupRepositories(backupRepositories)...)
results = append(results, analyzeResticRepositories(resticRepositories)...)
results = append(results, analyzeBackups(backups)...)
results = append(results, analyzeBackupStorageLocations(backupStorageLocations)...)
results = append(results, analyzeDeleteBackupRequests(deleteBackupRequests)...)
Expand Down Expand Up @@ -628,6 +662,49 @@ func aggregateResults(results []*AnalyzeResult) []*AnalyzeResult {
return out
}

func getVeleroVersion() (string, error) {
veleroDeployment := "cluster-resources/deployments/velero.json"
veleroDeploymentBytes, err := os.ReadFile(veleroDeployment)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Decode the JSON content into a Deployment struct
var obj interface{}
err = json.Unmarshal(veleroDeploymentBytes, &obj)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Use the deployment object...
deployment, ok := obj.(*appsV1.Deployment)
if !ok {
log.Fatalf("Provided file does not contain a valid deployment")
}

// Use the deployment object...
// For example, to print the name of the deployment
log.Printf("Deployment name is: %s", deployment.ObjectMeta.Name)

// veleroDeploymentMap := []unstructured.Unstructured
// err = json.Unmarshal(veleroDeploymentBytes, &veleroDeploymentMap)
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
veleroVersion := ""

for _, container := range deployment.Spec.Template.Spec.Containers {
if container.Name == "velero" {
container_image := container.Image
veleroVersion = strings.Split(container_image, ":")[1]
return veleroVersion, nil
}
}
return "", errors.Errorf("Unable to get velero version. Could not find velero container in deployment!")
}

func GetVeleroBackupsDirectory() string {
return "cluster-resources/custom-resources/backups.velero.io"
}
Expand Down

0 comments on commit 3b7331f

Please sign in to comment.