Skip to content

Commit

Permalink
feat: add helm.Init() function for embedding helm cmds into other sof…
Browse files Browse the repository at this point in the history
…tware

Signed-off-by: Ilya Lesikov <[email protected]>
  • Loading branch information
ilya-lesikov committed Feb 28, 2025
1 parent 11da3da commit d579a58
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
23 changes: 23 additions & 0 deletions cmd/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,26 @@ func loadReleasesInMemory(actionConfig *action.Configuration) {
// Must reset namespace to the proper one
mem.SetNamespace(settings.Namespace())
}

func Init() (*cobra.Command, error) {
kube.ManagedFieldsManager = "helm"

actionConfig := new(action.Configuration)
cmd, err := newRootCmd(actionConfig, os.Stdout, os.Args[1:])
if err != nil {
return nil, err
}

cobra.OnInitialize(func() {
helmDriver := os.Getenv("HELM_DRIVER")
if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), helmDriver, debug); err != nil {
log.Fatal(err)
}

if helmDriver == "memory" {
loadReleasesInMemory(actionConfig)
}
})

return cmd, nil
}
4 changes: 2 additions & 2 deletions cmd/helm/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string
// This call is required to gather configuration information prior to
// execution.
flags.ParseErrorsWhitelist.UnknownFlags = true
flags.Parse(args)
// flags.Parse(args)

registryClient, err := newDefaultRegistryClient(false)
if err != nil {
Expand Down Expand Up @@ -199,7 +199,7 @@ func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string
)

// Find and add plugins
loadPlugins(cmd, out)
// loadPlugins(cmd, out)

// Check permissions on critical files
checkPerms()
Expand Down
10 changes: 4 additions & 6 deletions pkg/chart/loader/load_dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var localCacheDir string
var DepsBuildFunc func() error
var SetChartPathFunc func(string)

var NoChartLockWarning = `Cannot automatically download chart dependencies without Chart.lock or requirements.lock.`

func LocalCacheDir() (string, error) {
if localCacheDir == "" {
userHomeDir, err := os.UserHomeDir()
Expand Down Expand Up @@ -102,12 +104,8 @@ func LoadChartDependencies(
}

if chartMetadataLock == nil {
if len(chartMetadata.Dependencies) > 0 {
logboek.Context(ctx).Error().LogLn("Cannot build chart dependencies and preload charts without lock file (.helm/Chart.lock or .helm/requirements.lock)")
logboek.Context(ctx).Error().LogLn("It is recommended to add Chart.lock file to your project repository or remove chart dependencies.")
logboek.Context(ctx).Error().LogLn()
logboek.Context(ctx).Error().LogLn("To generate a lock file run 'werf helm dependency update .helm' and commit resulting .helm/Chart.lock or .helm/requirements.lock (it is not required to commit whole .helm/charts directory, better add it to the .gitignore).")
logboek.Context(ctx).Error().LogLn()
if len(chartMetadata.Dependencies) > 0 && NoChartLockWarning != "" {
logboek.Context(ctx).Warn().LogLn(NoChartLockWarning)
}

return res, nil
Expand Down
5 changes: 5 additions & 0 deletions pkg/werf/secrets/secrets_runtime_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var _ runtimedata.RuntimeData = (*SecretsRuntimeData)(nil)
var CoalesceTablesFunc func(dst, src map[string]interface{}) map[string]interface{}
var SecretsWorkingDir string
var ChartDir string
var DisableSecrets bool

type SecretsRuntimeData struct {

Check warning on line 25 in pkg/werf/secrets/secrets_runtime_data.go

View workflow job for this annotation

GitHub Actions / golangci-lint

exported: type name will be used as secrets.SecretsRuntimeData by other packages, and that stutters; consider calling this RuntimeData (revive)
decryptedSecretValues map[string]interface{}
Expand All @@ -40,6 +41,10 @@ func (secretsRuntimeData *SecretsRuntimeData) DecodeAndLoadSecrets(
secretsManager *secrets_manager.SecretsManager,
opts runtimedata.DecodeAndLoadSecretsOptions,
) error {
if DisableSecrets {
return nil
}

var secretsWorkingDir string
if !noSecretsWorkingDir && SecretsWorkingDir != "" {
secretsWorkingDir = SecretsWorkingDir
Expand Down

0 comments on commit d579a58

Please sign in to comment.