Skip to content

Commit

Permalink
fix: add wekahome crd step to setup/upgrade (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdand-weka authored Sep 25, 2024
1 parent 09548fa commit 1df70ae
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
12 changes: 12 additions & 0 deletions internal/local/bundle/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ func Chart() (string, error) {

return matches[0], nil
}

func ChartCrd() (string, error) {
matches, err := filepath.Glob(filepath.Join(BundlePath(), "wekahome-crds-*.tgz"))
if err != nil {
return "", err
}
if len(matches) == 0 {
return "", fmt.Errorf("no chart crd found in %q", BundlePath())
}

return matches[0], nil
}
51 changes: 51 additions & 0 deletions internal/local/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,33 @@ type HelmOptions struct {
Config *config_v1.Configuration // json config
}

func crdSpec(client helmclient.Client, opts *HelmOptions) (*helmclient.ChartSpec, error) {
namespace := ReleaseNamespace
if opts.NamespaceOverride != "" {
namespace = opts.NamespaceOverride
}

logger.Debug().
Interface("locationOverride", opts.Override).
Msg("Determining chart crd location")

chartLocation, err := getChartCrdLocation(client, opts)
if err != nil {
return nil, err
}

return &helmclient.ChartSpec{
ReleaseName: ReleaseName + "-crds",
ChartName: chartLocation,
Namespace: namespace,
CreateNamespace: true,
UpgradeCRDs: true,
ResetValues: true,
Wait: true,
Timeout: time.Minute * 5,
}, nil
}

func chartSpec(client helmclient.Client, opts *HelmOptions) (*helmclient.ChartSpec, error) {
namespace := ReleaseNamespace
if opts.NamespaceOverride != "" {
Expand Down Expand Up @@ -82,10 +109,34 @@ func chartSpec(client helmclient.Client, opts *HelmOptions) (*helmclient.ChartSp
ResetValues: true,
Wait: true,
WaitForJobs: true,
UpgradeCRDs: true,
Timeout: time.Minute * 5,
}, nil
}

func getChartCrdLocation(client helmclient.Client, opts *HelmOptions) (string, error) {
var chartLocation string

if opts.Override != nil && opts.Override.RemoteDownload {
err := client.AddOrUpdateChartRepo(repo.Entry{
Name: RepositoryName,
URL: RepositoryURL,
})
if err != nil {
return "", fmt.Errorf("failed adding chart repo: %w", err)
}

chartLocation = fmt.Sprintf("%s/%s-crds", RepositoryName, ChartName)
return chartLocation, nil
}

if bundle.IsBundled() {
return bundle.ChartCrd()
}

return "", ErrUnableToFindChart
}

func getChartLocation(client helmclient.Client, opts *HelmOptions) (string, error) {
var chartLocation string

Expand Down
19 changes: 18 additions & 1 deletion internal/local/chart/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,24 @@ func Install(ctx context.Context, opts *HelmOptions) error {
return fmt.Errorf("helm client: %w", err)
}

spec, err := chartSpec(client, opts)
spec, err := crdSpec(client, opts)
if err != nil {
logger.Warn().Err(err).Msg("Failed to prepare crd spec")
}
if spec != nil {
logger.Info().
Str("namespace", spec.Namespace).
Str("chart", spec.ChartName).
Str("release", spec.ReleaseName).
Msg("Installing chart crd")

_, err := client.InstallChart(ctx, spec, nil)
if err != nil {
logger.Warn().Err(err).Msg("Failed to install chart crd")
}
}

spec, err = chartSpec(client, opts)
if err != nil {
return fmt.Errorf("failed to prepare chart spec: %w", err)
}
Expand Down
20 changes: 19 additions & 1 deletion internal/local/chart/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,25 @@ func Upgrade(ctx context.Context, opts *HelmOptions, debug bool) error {
return fmt.Errorf("helm client: %w", err)
}

spec, err := chartSpec(client, opts)
spec, err := crdSpec(client, opts)
if err != nil {
logger.Warn().Err(err).Msg("Failed to prepare crd spec")
}

if spec != nil {
logger.Info().
Str("namespace", spec.Namespace).
Str("chart", spec.ChartName).
Str("release", spec.ReleaseName).
Msg("Installing \\ Upgrading chart crd")

_, err := client.InstallOrUpgradeChart(ctx, spec, nil)
if err != nil {
logger.Warn().Err(err).Msg("Failed to install/upgrade chart crd")
}
}

spec, err = chartSpec(client, opts)
if err != nil {
return fmt.Errorf("failed to prepare chart spec: %w", err)
}
Expand Down

0 comments on commit 1df70ae

Please sign in to comment.