From 0a2822f43aa7d445d54a1d39a8ab52d4ded98208 Mon Sep 17 00:00:00 2001 From: Akash Shrivastava Date: Thu, 14 Dec 2023 14:22:11 +0530 Subject: [PATCH] feat: [ISSUE-1401]: Added helm get values option in Helm collector Signed-off-by: Akash Shrivastava --- config/crds/troubleshoot.sh_collectors.yaml | 2 ++ .../crds/troubleshoot.sh_supportbundles.yaml | 2 ++ .../troubleshoot/v1beta2/collector_shared.go | 1 + pkg/collect/helm.go | 34 ++++++++++++++----- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/config/crds/troubleshoot.sh_collectors.yaml b/config/crds/troubleshoot.sh_collectors.yaml index 203fe76f5..2aa7f157e 100644 --- a/config/crds/troubleshoot.sh_collectors.yaml +++ b/config/crds/troubleshoot.sh_collectors.yaml @@ -357,6 +357,8 @@ spec: type: string releaseName: type: string + collectValues: + type: bool type: object http: properties: diff --git a/config/crds/troubleshoot.sh_supportbundles.yaml b/config/crds/troubleshoot.sh_supportbundles.yaml index 4d5928f23..228e0f3d2 100644 --- a/config/crds/troubleshoot.sh_supportbundles.yaml +++ b/config/crds/troubleshoot.sh_supportbundles.yaml @@ -1949,6 +1949,8 @@ spec: type: string releaseName: type: string + collectValues: + type: bool type: object http: properties: diff --git a/pkg/apis/troubleshoot/v1beta2/collector_shared.go b/pkg/apis/troubleshoot/v1beta2/collector_shared.go index 4c16c74f1..25f6efa51 100644 --- a/pkg/apis/troubleshoot/v1beta2/collector_shared.go +++ b/pkg/apis/troubleshoot/v1beta2/collector_shared.go @@ -257,6 +257,7 @@ type Helm struct { CollectorMeta `json:",inline" yaml:",inline"` Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` ReleaseName string `json:"releaseName,omitempty" yaml:"releaseName,omitempty"` + CollectValues bool `json:"collectValues,omitempty" yaml:"collectValues,omitempty"` } type Goldpinger struct { diff --git a/pkg/collect/helm.go b/pkg/collect/helm.go index c886c1120..9f95f8c09 100644 --- a/pkg/collect/helm.go +++ b/pkg/collect/helm.go @@ -39,10 +39,11 @@ type ReleaseInfo struct { // Helm release version information struct type VersionInfo struct { - Revision string `json:"revision"` - Date string `json:"date"` - Status string `json:"status"` - IsPending bool `json:"isPending,omitempty"` + Revision string `json:"revision"` + Date string `json:"date"` + Status string `json:"status"` + IsPending bool `json:"isPending,omitempty"` + Values map[string]interface{} `json:"values,omitempty"` } func (c *CollectHelm) Title() string { @@ -57,7 +58,7 @@ func (c *CollectHelm) Collect(progressChan chan<- interface{}) (CollectorResult, output := NewResult() - releaseInfos, err := helmReleaseHistoryCollector(c.Collector.ReleaseName, c.Collector.Namespace) + releaseInfos, err := helmReleaseHistoryCollector(c.Collector.ReleaseName, c.Collector.Namespace, c.Collector.CollectValues) if err != nil { return nil, errors.Wrap(err, "failed to get Helm release history") } @@ -72,6 +73,9 @@ func (c *CollectHelm) Collect(progressChan chan<- interface{}) (CollectorResult, } filePath := fmt.Sprintf("helm/%s.json", namespace) + if c.Collector.ReleaseName != "" { + filePath = fmt.Sprintf("helm/%s/%s.json", namespace, c.Collector.ReleaseName) + } err := output.SaveResult(c.BundlePath, filePath, bytes.NewBuffer(helmHistoryJson)) if err != nil { @@ -82,7 +86,7 @@ func (c *CollectHelm) Collect(progressChan chan<- interface{}) (CollectorResult, return output, nil } -func helmReleaseHistoryCollector(releaseName string, namespace string) ([]ReleaseInfo, error) { +func helmReleaseHistoryCollector(releaseName string, namespace string, collectValues bool) ([]ReleaseInfo, error) { var results []ReleaseInfo actionConfig := new(action.Configuration) @@ -103,7 +107,7 @@ func helmReleaseHistoryCollector(releaseName string, namespace string) ([]Releas ChartVersion: r.Chart.Metadata.Version, AppVersion: r.Chart.Metadata.AppVersion, Namespace: r.Namespace, - VersionInfo: getVersionInfo(actionConfig, releaseName), + VersionInfo: getVersionInfo(actionConfig, releaseName, collectValues), }) return results, nil } @@ -124,26 +128,31 @@ func helmReleaseHistoryCollector(releaseName string, namespace string) ([]Releas ChartVersion: r.Chart.Metadata.Version, AppVersion: r.Chart.Metadata.AppVersion, Namespace: r.Namespace, - VersionInfo: getVersionInfo(actionConfig, r.Name), + VersionInfo: getVersionInfo(actionConfig, r.Name, collectValues), }) } return results, nil } -func getVersionInfo(actionConfig *action.Configuration, releaseName string) []VersionInfo { +func getVersionInfo(actionConfig *action.Configuration, releaseName string, collectValues bool) []VersionInfo { versionCollect := []VersionInfo{} history, _ := action.NewHistory(actionConfig).Run(releaseName) for _, release := range history { + values := map[string]interface{}{} + if collectValues { + values = getHelmValues(actionConfig, releaseName, release.Version) + } versionCollect = append(versionCollect, VersionInfo{ Revision: strconv.Itoa(release.Version), Date: release.Info.LastDeployed.String(), Status: release.Info.Status.String(), IsPending: release.Info.Status.IsPending(), + Values: values, }) } return versionCollect @@ -158,3 +167,10 @@ func helmReleaseInfoByNamespaces(releaseInfo []ReleaseInfo) map[string][]Release return releaseInfoByNamespace } + +func getHelmValues(actionConfig *action.Configuration, releaseName string, revision int) map[string]interface{} { + getAction := action.NewGetValues(actionConfig) + getAction.Version = revision + helmValues, _ := getAction.Run(releaseName) + return helmValues +}