-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating a single installer for all the components, including the "template" subcommand using dry-run mode.
- Loading branch information
Showing
3 changed files
with
181 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,151 @@ | ||
package installer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/redhat-appstudio/rhtap-cli/pkg/chartfs" | ||
"github.com/redhat-appstudio/rhtap-cli/pkg/config" | ||
"github.com/redhat-appstudio/rhtap-cli/pkg/deployer" | ||
"github.com/redhat-appstudio/rhtap-cli/pkg/engine" | ||
"github.com/redhat-appstudio/rhtap-cli/pkg/flags" | ||
"github.com/redhat-appstudio/rhtap-cli/pkg/hooks" | ||
"github.com/redhat-appstudio/rhtap-cli/pkg/k8s" | ||
"github.com/redhat-appstudio/rhtap-cli/pkg/printer" | ||
"helm.sh/helm/v3/pkg/chartutil" | ||
) | ||
|
||
type Installer struct { | ||
cfg *config.Spec // installer configuration | ||
logger *slog.Logger // application logger | ||
flags *flags.Flags // global flags | ||
kube *k8s.Kube // kubernetes client | ||
dep *config.Dependency // dependency to install | ||
cfs *chartfs.ChartFS // chart file system | ||
|
||
valuesBytes []byte // rendered values | ||
values chartutil.Values // helm chart values | ||
} | ||
|
||
// prepareHelmClient prepares the Helm client for the given dependency, which also | ||
// specifies the default namespace for the Helm Chart. | ||
func (i *Installer) prepareHelmClient() (*deployer.Helm, error) { | ||
i.logger.Debug("Loading dependency Helm chart (from CFS)") | ||
chart, err := i.cfs.GetChartForDep(i.dep) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
i.logger.Debug("Loading Helm client for dependency and namespace") | ||
return deployer.NewHelm(i.logger, i.flags, i.kube, i.dep.Namespace, chart) | ||
} | ||
|
||
// SetValues prepares the values template for the Helm chart installation. | ||
func (i *Installer) SetValues( | ||
ctx context.Context, | ||
cfg *config.Spec, | ||
valuesTmpl string, | ||
) error { | ||
i.logger.Debug("Preparing values template context") | ||
variables := engine.NewVariables() | ||
err := variables.SetInstaller(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
if err = variables.SetOpenShift(ctx, i.kube); err != nil { | ||
return err | ||
} | ||
|
||
i.logger.Debug("Rendering values template") | ||
i.valuesBytes, err = engine.NewEngine(i.kube, valuesTmpl).Render(variables) | ||
return err | ||
} | ||
|
||
// PrintRawValues prints the raw values template to the console. | ||
func (i *Installer) PrintRawValues() { | ||
i.logger.Debug("Showing raw results of rendered values template") | ||
fmt.Printf("#\n# Values (Raw)\n#\n\n%s\n", i.valuesBytes) | ||
} | ||
|
||
// RenderValues parses the values template and prepares the Helm chart values. | ||
func (i *Installer) RenderValues() error { | ||
if i.valuesBytes == nil { | ||
return fmt.Errorf("values not set") | ||
} | ||
|
||
i.logger.Debug("Preparing rendered values for Helm installation") | ||
var err error | ||
i.values, err = chartutil.ReadValues(i.valuesBytes) | ||
return err | ||
} | ||
|
||
// PrintValues prints the parsed values to the console. | ||
func (i *Installer) PrintValues() { | ||
i.logger.Debug("Showing parsed values") | ||
printer.ValuesPrinter("Values", i.values) | ||
} | ||
|
||
// Install performs the installation of the Helm chart, including the pre and post | ||
// hooks execution. | ||
func (i *Installer) Install() error { | ||
if i.values == nil { | ||
return fmt.Errorf("values not set") | ||
} | ||
hc, err := i.prepareHelmClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
hook := hooks.NewHooks(i.cfs, i.dep, os.Stdout, os.Stderr) | ||
if !i.flags.DryRun { | ||
i.logger.Debug("Running pre-deploy hook script...") | ||
if err = hook.PreDeploy(i.values); err != nil { | ||
return err | ||
} | ||
} else { | ||
i.logger.Debug("Skipping pre-deploy hook script (dry-run)") | ||
} | ||
|
||
// Performing the installation, or upgrade, of the Helm chart dependency, | ||
// using the values rendered before hand. | ||
i.logger.Debug("Installing the Helm chart") | ||
if err = hc.Install(i.values); err != nil { | ||
return err | ||
} | ||
// Verifying if the instaltion was successful, by running the Helm chart | ||
// tests interactively. | ||
i.logger.Debug("Verifying the Helm chart release") | ||
if err = hc.Verify(); err != nil { | ||
return err | ||
} | ||
|
||
if !i.flags.DryRun { | ||
i.logger.Debug("Running post-deploy hook script...") | ||
if err = hook.PostDeploy(i.values); err != nil { | ||
return err | ||
} | ||
} else { | ||
i.logger.Debug("Skipping post-deploy hook script (dry-run)") | ||
} | ||
|
||
i.logger.Info("Helm chart installed!") | ||
return nil | ||
} | ||
|
||
func NewInstaller() *Installer { | ||
return &Installer{} | ||
// NewInstaller instantiates a new installer for the given dependency. | ||
func NewInstaller( | ||
logger *slog.Logger, | ||
f *flags.Flags, | ||
kube *k8s.Kube, | ||
cfs *chartfs.ChartFS, | ||
dep *config.Dependency, | ||
) *Installer { | ||
return &Installer{ | ||
logger: dep.LoggerWith(logger), | ||
flags: f, | ||
kube: kube, | ||
cfs: cfs, | ||
dep: dep, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.