Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit c028493

Browse files
authored
Allow setting of Helm --create-namespace (#23)
## Summary HelmValueComputer now has settable `createNamespace`, which we pass to Helm when installing charts. This is the implementation of the suggestion in #20 (comment) Partially closes DEV-1252. ## How was it tested? `launchpad up` with a BYOC cluster, and with a managed cluster. ## Is this change backwards-compatible? Yes Co-authored-by: Rodrigo Ipince <[email protected]>
1 parent 64632ec commit c028493

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

launchpad/deploy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ type DeployOptions struct {
5959

6060
LifecycleHook hook.LifecycleHook
6161

62-
Namespace string
62+
Namespace string
63+
CreateNamespace bool
6364

6465
RemoteEnvVars map[string]string
6566
Runtime *HelmOptions

launchpad/helm.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func applyHelmCharts(
4242
return nil, errorutil.CombinedError(err, errUnableToAccessHelmReleases)
4343
}
4444

45+
createNamespace := plan.DeployOptions.CreateNamespace
4546
for _, cc := range plan.Charts() {
4647
c, err := actionConfig(ctx, plan.helmDriver, cc.Namespace, settings)
4748
if err != nil {
@@ -53,7 +54,7 @@ func applyHelmCharts(
5354
releases[cc.Name], err = upgradeHelmChart(ctx, cc, settings, c)
5455

5556
if plan.DeployOptions.ReinstallOnHelmUpgradeError {
56-
releases[cc.Name], err = reinstallHelmChart(ctx, cc.Release, cc, settings, c)
57+
releases[cc.Name], err = reinstallHelmChart(ctx, cc.Release, cc, settings, c, createNamespace)
5758
if err != nil {
5859
return nil, errors.WithStack(err)
5960
}
@@ -71,12 +72,12 @@ func applyHelmCharts(
7172
// We will automatically down the old release and up a new release.
7273
// Since the old release is using app name as the release name.
7374
jetlog.Logger(ctx).IndentedPrintln("Detected old install by the project name. Changing to install by project ID.")
74-
releases[cc.Name], err = reinstallHelmChart(ctx, cc.instanceName, cc, settings, c)
75+
releases[cc.Name], err = reinstallHelmChart(ctx, cc.instanceName, cc, settings, c, createNamespace)
7576
if err != nil {
7677
return nil, errors.WithStack(err)
7778
}
7879
} else {
79-
releases[cc.Name], err = installHelmChart(ctx, cc, settings, c)
80+
releases[cc.Name], err = installHelmChart(ctx, cc, settings, c, createNamespace)
8081
if err != nil {
8182
return releases, errors.Wrap(err, "Error installing helm chart")
8283
}
@@ -96,7 +97,7 @@ func applyHelmCharts(
9697
return nil, err
9798
}
9899
} else {
99-
_, err = installHelmChart(ctx, chart, settings, c)
100+
_, err = installHelmChart(ctx, chart, settings, c, createNamespace)
100101
if err != nil {
101102
return nil, err
102103
}
@@ -111,6 +112,7 @@ func installHelmChart(
111112
cc *ChartConfig,
112113
settings *cli.EnvSettings,
113114
config *action.Configuration,
115+
createNamespace bool,
114116
) (*release.Release, error) {
115117
install := action.NewInstall(config)
116118

@@ -121,8 +123,11 @@ func installHelmChart(
121123

122124
install.Namespace = cc.Namespace
123125
install.ReleaseName = cc.Release
124-
// For Jetpack-managed clusters, namespace is created (and permissions are set) by InitNamespace. And we
125-
// cannot set --create-namespace=true because multi-tenant cluster users won't have permissions.
126+
// Setting --create-namespace=true makes Helm attempt to create the namespace and continue if it already
127+
// exists, but it will fail if the user lacks permissions to create namespaces. Thus, we cannot set this
128+
// to true every time, because it might fail for managed multi-tenant clusters where a user's permissions
129+
// are limited.
130+
install.CreateNamespace = createNamespace
126131

127132
install.Wait = cc.Wait
128133
install.Timeout = cc.Timeout
@@ -376,6 +381,7 @@ func reinstallHelmChart(
376381
cc *ChartConfig,
377382
settings *cli.EnvSettings,
378383
config *action.Configuration,
384+
createNamespace bool,
379385
) (*release.Release, error) {
380386
jetlog.Logger(ctx).BoldPrintf("Could not upgrade. Reinstalling...\n")
381387
uninstall := action.NewUninstall(config)
@@ -384,5 +390,5 @@ func reinstallHelmChart(
384390
if err != nil {
385391
return nil, errorutil.CombinedError(err, errUserReinstallFail)
386392
}
387-
return installHelmChart(ctx, cc, settings, config)
393+
return installHelmChart(ctx, cc, settings, config, createNamespace)
388394
}

padcli/command/deploy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ func makeDeployOptions(
162162
Values: appValues,
163163
Timeout: lo.Ternary(len(jetCfg.Jobs()) > 0, 5*time.Minute, 0),
164164
},
165+
CreateNamespace: hvc.CreateNamespace(),
165166
Environment: cmdOpts.RootFlags().Env().String(),
166167
ExternalCharts: jetconfigHelmToChartConfig(jetCfg, ns),
167168
JetCfg: jetCfg,

padcli/helm/helm.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ import (
1313
"go.jetpack.io/launchpad/proto/api"
1414
)
1515

16-
// type cluster interface {
17-
// GetHostname() string
18-
// GetIsPrivate() bool
19-
// GetName() string
20-
// IsLocal() bool
21-
// IsJetpackManaged() bool
22-
// IsRemoteUnmanaged() bool
23-
// }
24-
2516
// ValueComputer transforms jetpack CLI inputs into helm values.
2617
// Right now we mostly copy paste the logic, but the idea is to have individual
2718
// modules that compute sections of the values. e.g. ambassadorModule, cronjobModule, etc.
@@ -31,6 +22,7 @@ type ValueComputer struct {
3122

3223
env api.Environment
3324
namespace string // The final namespace to be used
25+
createNamespace bool // Value used for helm's --create-namespace
3426
execQualifiedSymbol string // Used by jetpack dev <path/to/project> --exec <symbol>
3527
imageProvider *ImageProvider
3628
jetCfg *jetconfig.Config // consider interface
@@ -217,6 +209,14 @@ func (hvc *ValueComputer) Namespace() string {
217209
return hvc.namespace
218210
}
219211

212+
func (hvc *ValueComputer) CreateNamespace() bool {
213+
return hvc.createNamespace
214+
}
215+
216+
func (hvc *ValueComputer) SetCreateNamespace(createNamespace bool) {
217+
hvc.createNamespace = createNamespace
218+
}
219+
220220
func (hvc *ValueComputer) Cluster() provider.Cluster {
221221
return hvc.cluster
222222
}

0 commit comments

Comments
 (0)