Skip to content

Commit

Permalink
feat: [ISSUE-1401]: Added helm get values option in Helm collector
Browse files Browse the repository at this point in the history
Signed-off-by: Akash Shrivastava <[email protected]>
  • Loading branch information
avaakash committed Dec 14, 2023
1 parent 53113c0 commit 0a2822f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
2 changes: 2 additions & 0 deletions config/crds/troubleshoot.sh_collectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ spec:
type: string
releaseName:
type: string
collectValues:
type: bool
type: object
http:
properties:
Expand Down
2 changes: 2 additions & 0 deletions config/crds/troubleshoot.sh_supportbundles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,8 @@ spec:
type: string
releaseName:
type: string
collectValues:
type: bool
type: object
http:
properties:
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/troubleshoot/v1beta2/collector_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 25 additions & 9 deletions pkg/collect/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
}

0 comments on commit 0a2822f

Please sign in to comment.