github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/internal/packager/helm/utils.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package helm contains operations for working with helm charts.
     5  package helm
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/Racer159/jackal/src/pkg/message"
    11  	"github.com/defenseunicorns/pkg/helpers"
    12  	"helm.sh/helm/v3/pkg/action"
    13  	"helm.sh/helm/v3/pkg/chart"
    14  	"helm.sh/helm/v3/pkg/chartutil"
    15  	"helm.sh/helm/v3/pkg/cli"
    16  	"helm.sh/helm/v3/pkg/cli/values"
    17  	"helm.sh/helm/v3/pkg/getter"
    18  
    19  	"helm.sh/helm/v3/pkg/chart/loader"
    20  )
    21  
    22  // loadChartFromTarball returns a helm chart from a tarball.
    23  func (h *Helm) loadChartFromTarball() (*chart.Chart, error) {
    24  	// Get the path the temporary helm chart tarball
    25  	sourceFile := StandardName(h.chartPath, h.chart) + ".tgz"
    26  
    27  	// Load the loadedChart tarball
    28  	loadedChart, err := loader.Load(sourceFile)
    29  	if err != nil {
    30  		return nil, fmt.Errorf("unable to load helm chart archive: %w", err)
    31  	}
    32  
    33  	if err = loadedChart.Validate(); err != nil {
    34  		return nil, fmt.Errorf("unable to validate loaded helm chart: %w", err)
    35  	}
    36  
    37  	return loadedChart, nil
    38  }
    39  
    40  // parseChartValues reads the context of the chart values into an interface if it exists.
    41  func (h *Helm) parseChartValues() (chartutil.Values, error) {
    42  	valueOpts := &values.Options{}
    43  
    44  	for idx := range h.chart.ValuesFiles {
    45  		path := StandardValuesName(h.valuesPath, h.chart, idx)
    46  		valueOpts.ValueFiles = append(valueOpts.ValueFiles, path)
    47  	}
    48  
    49  	httpProvider := getter.Provider{
    50  		Schemes: []string{"http", "https"},
    51  		New:     getter.NewHTTPGetter,
    52  	}
    53  
    54  	providers := getter.Providers{httpProvider}
    55  	chartValues, err := valueOpts.MergeValues(providers)
    56  	if err != nil {
    57  		return chartValues, err
    58  	}
    59  
    60  	return helpers.MergeMapRecursive(chartValues, h.valuesOverrides), nil
    61  }
    62  
    63  func (h *Helm) createActionConfig(namespace string, spinner *message.Spinner) error {
    64  	// Initialize helm SDK
    65  	actionConfig := new(action.Configuration)
    66  	// Set the settings for the helm SDK
    67  	h.settings = cli.New()
    68  
    69  	// Set the namespace for helm
    70  	h.settings.SetNamespace(namespace)
    71  
    72  	// Setup K8s connection
    73  	err := actionConfig.Init(h.settings.RESTClientGetter(), namespace, "", spinner.Updatef)
    74  
    75  	// Set the actionConfig is the received Helm pointer
    76  	h.actionConfig = actionConfig
    77  
    78  	return err
    79  }