From 3b7331f960a60d8d2e7271d678d2d79157263591 Mon Sep 17 00:00:00 2001 From: Archit Sharma Date: Fri, 3 Nov 2023 16:02:05 +0530 Subject: [PATCH] use versions from deployment to check deprecated type Signed-off-by: Archit Sharma --- pkg/analyze/velero.go | 135 +++++++++++++++++++++++++++++++++--------- 1 file changed, 106 insertions(+), 29 deletions(-) diff --git a/pkg/analyze/velero.go b/pkg/analyze/velero.go index df96f920d..0d53f2f00 100644 --- a/pkg/analyze/velero.go +++ b/pkg/analyze/velero.go @@ -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" @@ -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 @@ -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)...) @@ -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" }