Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #32 from David-VTUK/fix-autodetection
Browse files Browse the repository at this point in the history
Update logic to determine if rancher & backup operator is installed.
  • Loading branch information
David-VTUK authored Sep 8, 2023
2 parents 966a084 + 5d30dc2 commit e0077c2
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"github.com/david-vtuk/prometheus-rancher-exporter/query/rancher"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)

var (
appGVR = schema.GroupVersionResource{Group: "catalog.cattle.io", Version: "v1", Resource: "apps"}
crdGVR = schema.GroupVersionResource{Group: "apiextensions.k8s.io", Version: "v1", Resource: "customresourcedefinitions"}
)

// CheckInstalledRancherApps Determines if both Rancher and the Backup operator are installed
Expand All @@ -18,31 +17,20 @@ func CheckInstalledRancherApps(r rancher.Client) (bool, bool, error) {
var rancherInstalled = false
var rancherBackupInstalled = false

res, err := r.Client.Resource(appGVR).List(context.Background(), v1.ListOptions{})
res, err := r.Client.Resource(crdGVR).List(context.Background(), v1.ListOptions{})
if err != nil {
return false, false, err
}

for _, app := range res.Items {
for _, customResource := range res.Items {

appName, _, err := unstructured.NestedString(app.Object, "metadata", "name")
if err != nil {
return false, false, err
}

appStatus, _, err := unstructured.NestedString(app.Object, "spec", "info", "status")
if err != nil {
return false, false, err
}

if appName == "rancher" && appStatus == "deployed" {
if customResource.GetName() == "catalogs.management.cattle.io" {
rancherInstalled = true
}

if appName == "rancher-backup" && appStatus == "deployed" {
if customResource.GetName() == "backups.resources.cattle.io" {
rancherBackupInstalled = true
}

}

return rancherInstalled, rancherBackupInstalled, nil
Expand Down

0 comments on commit e0077c2

Please sign in to comment.