Skip to content

Commit

Permalink
Pass debug log to HelmClient (#21)
Browse files Browse the repository at this point in the history
* Pass debug log to HelmClient

and allow setting via Client interface

* Update generated interface post-merge

* Add missing go.sum entry

that didn't get added when running CI commands locally (?)

* Adding relevant mockgen changes

* Make "upgradeCRD*" functions members of HelmClient struct

Co-authored-by: Martin Helmich <[email protected]>
  • Loading branch information
meatballhat and martin-helmich authored Oct 4, 2021
1 parent b0b0540 commit b084219
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
47 changes: 26 additions & 21 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func newClient(options *Options, clientGetter genericclioptions.RESTClientGetter
storage: &storage,
ActionConfig: actionConfig,
linting: options.Linting,
DebugLog: debugLog,
}, nil
}

Expand Down Expand Up @@ -166,7 +167,7 @@ func (c *HelmClient) AddOrUpdateChartRepo(entry repo.Entry) error {
}

if c.storage.Has(entry.Name) {
log.Printf("WARNING: repository name %q already exists", entry.Name)
c.DebugLog("WARNING: repository name %q already exists", entry.Name)
return nil
}

Expand Down Expand Up @@ -311,7 +312,7 @@ func (c *HelmClient) install(spec *ChartSpec) (*release.Release, error) {
return rel, err
}

log.Printf("release installed successfully: %s/%s-%s", rel.Name, rel.Name, rel.Chart.Metadata.Version)
c.DebugLog("release installed successfully: %s/%s-%s", rel.Name, rel.Name, rel.Chart.Metadata.Version)

return rel, nil
}
Expand Down Expand Up @@ -349,7 +350,7 @@ func (c *HelmClient) upgrade(ctx context.Context, spec *ChartSpec) (*release.Rel
}

if !spec.SkipCRDs && spec.UpgradeCRDs {
log.Printf("upgrading crds")
c.DebugLog("upgrading crds")
err = c.upgradeCRDs(ctx, helmChart)
if err != nil {
return nil, err
Expand All @@ -361,7 +362,7 @@ func (c *HelmClient) upgrade(ctx context.Context, spec *ChartSpec) (*release.Rel
return rel, err
}

log.Printf("release upgraded successfully: %s/%s-%s", rel.Name, rel.Name, rel.Chart.Metadata.Version)
c.DebugLog("release upgraded successfully: %s/%s-%s", rel.Name, rel.Name, rel.Chart.Metadata.Version)

return rel, nil
}
Expand All @@ -381,7 +382,7 @@ func (c *HelmClient) deleteChartFromCache(spec *ChartSpec) error {
return err
}

log.Printf("chart removed successfully: %s/%s-%s", helmChart.Name(), spec.ReleaseName, helmChart.AppVersion())
c.DebugLog("chart removed successfully: %s/%s-%s", helmChart.Name(), spec.ReleaseName, helmChart.AppVersion())

return nil
}
Expand All @@ -397,7 +398,7 @@ func (c *HelmClient) uninstallRelease(spec *ChartSpec) error {
return err
}

log.Printf("release uninstalled, response: %v", resp)
c.DebugLog("release uninstalled, response: %v", resp)

return nil
}
Expand All @@ -411,7 +412,7 @@ func (c *HelmClient) uninstallReleaseByName(name string) error {
return err
}

log.Printf("release uninstalled, response: %v", resp)
c.DebugLog("release uninstalled, response: %v", resp)

return nil
}
Expand All @@ -423,7 +424,7 @@ func (c *HelmClient) lint(chartPath string, values map[string]interface{}) error
result := client.Run([]string{chartPath}, values)

for _, err := range result.Errors {
log.Printf("Error %s", err)
c.DebugLog("Error %s", err)
}

if len(result.Errors) > 0 {
Expand Down Expand Up @@ -524,6 +525,10 @@ func (c *HelmClient) LintChart(spec *ChartSpec) error {
return c.lint(chartPath, values)
}

func (c *HelmClient) SetDebugLog(debugLog action.DebugLog) {
c.DebugLog = debugLog
}

// upgradeCRDs upgrades the CRDs of the provided chart
func (c *HelmClient) upgradeCRDs(ctx context.Context, chartInstance *chart.Chart) error {
cfg, err := c.ActionConfig.RESTClientGetter.ToRESTConfig()
Expand All @@ -540,7 +545,7 @@ func (c *HelmClient) upgradeCRDs(ctx context.Context, chartInstance *chart.Chart
if err := c.upgradeCRD(ctx, k8sClient, crd); err != nil {
return err
}
log.Printf("CRD %s upgraded successfully for chart: %s", crd.Name, chartInstance.Metadata.Name)
c.DebugLog("CRD %s upgraded successfully for chart: %s", crd.Name, chartInstance.Metadata.Name)
}

return nil
Expand All @@ -564,13 +569,13 @@ func (c *HelmClient) upgradeCRD(ctx context.Context, k8sClient *clientset.Client
default:
return fmt.Errorf("WARNING: failed to upgrade CRD %q: unsupported api-version %q", crd.Name, typeMeta.APIVersion)
case "apiextensions.k8s.io/v1beta1":
return upgradeCRDV1Beta1(ctx, k8sClient, jsonCRD)
return c.upgradeCRDV1Beta1(ctx, k8sClient, jsonCRD)
case "apiextensions.k8s.io/v1":
return upgradeCRDV1(ctx, k8sClient, jsonCRD)
return c.upgradeCRDV1(ctx, k8sClient, jsonCRD)
}
}

func upgradeCRDV1Beta1(ctx context.Context, cl *clientset.Clientset, rawCRD []byte) error {
func (c *HelmClient) upgradeCRDV1Beta1(ctx context.Context, cl *clientset.Clientset, rawCRD []byte) error {
var crdObj v1beta1.CustomResourceDefinition
if err := json.Unmarshal(rawCRD, &crdObj); err != nil {
return err
Expand Down Expand Up @@ -604,25 +609,25 @@ func upgradeCRDV1Beta1(ctx context.Context, cl *clientset.Clientset, rawCRD []by
}

if reflect.DeepEqual(existingCRDObj.Spec.Versions, crdObj.Spec.Versions) {
log.Printf("INFO: new version of CRD %q contains no changes, skipping upgrade", crdObj.Name)
c.DebugLog("INFO: new version of CRD %q contains no changes, skipping upgrade", crdObj.Name)
return nil
}

crdObj.ResourceVersion = existingCRDObj.ResourceVersion
if _, err := cl.ApiextensionsV1beta1().CustomResourceDefinitions().Update(ctx, &crdObj, metav1.UpdateOptions{DryRun: []string{"All"}}); err != nil {
return err
}
log.Printf("upgrade ran successful for CRD (dry run): %s", crdObj.Name)
c.DebugLog("upgrade ran successful for CRD (dry run): %s", crdObj.Name)

if _, err = cl.ApiextensionsV1beta1().CustomResourceDefinitions().Update(ctx, &crdObj, metav1.UpdateOptions{}); err != nil {
return err
}
log.Printf("upgrade ran successful for CRD: %s", crdObj.Name)
c.DebugLog("upgrade ran successful for CRD: %s", crdObj.Name)

return nil
}

func upgradeCRDV1(ctx context.Context, cl *clientset.Clientset, rawCRD []byte) error {
func (c *HelmClient) upgradeCRDV1(ctx context.Context, cl *clientset.Clientset, rawCRD []byte) error {
var crdObj v1.CustomResourceDefinition
if err := json.Unmarshal(rawCRD, &crdObj); err != nil {
return err
Expand All @@ -635,7 +640,7 @@ func upgradeCRDV1(ctx context.Context, cl *clientset.Clientset, rawCRD []byte) e

// Check to ensure that no previously existing API version is deleted through the upgrade.
if len(existingCRDObj.Spec.Versions) > len(crdObj.Spec.Versions) {
log.Printf("WARNING: new version of CRD %q would remove an existing API version, skipping upgrade", crdObj.Name)
c.DebugLog("WARNING: new version of CRD %q would remove an existing API version, skipping upgrade", crdObj.Name)
return nil
}

Expand Down Expand Up @@ -663,20 +668,20 @@ func upgradeCRDV1(ctx context.Context, cl *clientset.Clientset, rawCRD []byte) e
}

if reflect.DeepEqual(existingCRDObj.Spec.Versions, crdObj.Spec.Versions) {
log.Printf("INFO: new version of CRD %q contains no changes, skipping upgrade", crdObj.Name)
c.DebugLog("INFO: new version of CRD %q contains no changes, skipping upgrade", crdObj.Name)
return nil
}

crdObj.ResourceVersion = existingCRDObj.ResourceVersion
if _, err := cl.ApiextensionsV1().CustomResourceDefinitions().Update(ctx, &crdObj, metav1.UpdateOptions{DryRun: []string{"All"}}); err != nil {
return err
}
log.Printf("upgrade ran successful for CRD (dry run): %s", crdObj.Name)
c.DebugLog("upgrade ran successful for CRD (dry run): %s", crdObj.Name)

if _, err := cl.ApiextensionsV1().CustomResourceDefinitions().Update(ctx, &crdObj, metav1.UpdateOptions{}); err != nil {
return err
}
log.Printf("upgrade ran successful for CRD: %s", crdObj.Name)
c.DebugLog("upgrade ran successful for CRD: %s", crdObj.Name)

return nil
}
Expand All @@ -694,7 +699,7 @@ func (c *HelmClient) getChart(chartName string, chartPathOptions *action.ChartPa
}

if helmChart.Metadata.Deprecated {
log.Printf("WARNING: This chart (%q) is deprecated", helmChart.Metadata.Name)
c.DebugLog("WARNING: This chart (%q) is deprecated", helmChart.Metadata.Name)
}

return helmChart, chartPath, err
Expand Down
2 changes: 2 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package helmclient
import (
"context"

"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/repo"
)
Expand All @@ -22,4 +23,5 @@ type Client interface {
UninstallReleaseByName(name string) error
TemplateChart(spec *ChartSpec) ([]byte, error)
LintChart(spec *ChartSpec) error
SetDebugLog(debugLog action.DebugLog)
}
13 changes: 13 additions & 0 deletions mock/interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type HelmClient struct {
storage *repo.File
ActionConfig *action.Configuration
linting bool
DebugLog action.DebugLog
}

// ChartSpec defines the values of a helm chart
Expand Down

0 comments on commit b084219

Please sign in to comment.