Skip to content

Commit

Permalink
Merge pull request #369 from otaviof/RHTAP-3384
Browse files Browse the repository at this point in the history
RHTAP-3384: Singleton ChartFS
  • Loading branch information
openshift-merge-bot[bot] authored Jan 7, 2025
2 parents 639ed2c + f097abd commit 59a7d14
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 60 deletions.
6 changes: 5 additions & 1 deletion cmd/rhtap-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
)

func main() {
if err := cmd.NewRootCmd().Cmd().Execute(); err != nil {
c, err := cmd.NewRootCmd()
if err != nil {
os.Exit(1)
}
if err = c.Cmd().Execute(); err != nil {
os.Exit(1)
}
}
29 changes: 23 additions & 6 deletions pkg/chartfs/chartfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/redhat-appstudio/rhtap-cli/installer"
"github.com/redhat-appstudio/rhtap-cli/pkg/config"

"github.com/quay/claircore/pkg/tarfs"
"helm.sh/helm/v3/pkg/chart"
Expand Down Expand Up @@ -79,16 +78,24 @@ func (c *ChartFS) ReadFile(name string) ([]byte, error) {
return nil, err
}

// GetChartForDep returns the Helm chart for the given dependency. It uses
// BufferredFiles to walk through the filesytem and collect the chart files.
func (c *ChartFS) GetChartForDep(dep *config.Dependency) (*chart.Chart, error) {
bf := NewBufferedFiles(c.embeddedFS, dep.Chart)
if err := fs.WalkDir(c.embeddedFS, dep.Chart, bf.Walk); err != nil {
// walkChartDir walks through the chart directory, and loads the chart files.
func (c *ChartFS) walkChartDir(fsys fs.FS, chartPath string) (*chart.Chart, error) {
bf := NewBufferedFiles(fsys, chartPath)
if err := fs.WalkDir(fsys, chartPath, bf.Walk); err != nil {
return nil, err
}
return loader.LoadFiles(bf.Files())
}

// GetChartFiles returns the informed Helm chart path instantiated files.
func (c *ChartFS) GetChartFiles(chartPath string) (*chart.Chart, error) {
chartFiles, err := c.walkChartDir(c.localFS, chartPath)
if err == nil {
return chartFiles, nil
}
return c.walkChartDir(c.embeddedFS, chartPath)
}

// NewChartFS instantiates a ChartFS instance using the embedded tarball,
// and the local base directory.
func NewChartFS(baseDir string) (*ChartFS, error) {
Expand All @@ -102,3 +109,13 @@ func NewChartFS(baseDir string) (*ChartFS, error) {
localFS: os.DirFS(baseDir),
}, nil
}

// NewChartFSForCWD instantiates a ChartFS instance using the current working
// directory.
func NewChartFSForCWD() (*ChartFS, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
return NewChartFS(cwd)
}
7 changes: 1 addition & 6 deletions pkg/chartfs/chartfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package chartfs
import (
"testing"

"github.com/redhat-appstudio/rhtap-cli/pkg/config"

o "github.com/onsi/gomega"
)

Expand All @@ -22,10 +20,7 @@ func TestNewChartFS(t *testing.T) {
})

t.Run("GetChartForDep", func(t *testing.T) {
chart, err := c.GetChartForDep(&config.Dependency{
Chart: "charts/rhtap-openshift",
Namespace: "rhtap",
})
chart, err := c.GetChartFiles("charts/rhtap-openshift")
g.Expect(err).To(o.Succeed())
g.Expect(chart).ToNot(o.BeNil())
g.Expect(chart.Name()).To(o.Equal("rhtap-openshift"))
Expand Down
27 changes: 18 additions & 9 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"os"

"github.com/redhat-appstudio/rhtap-cli/pkg/chartfs"
"github.com/redhat-appstudio/rhtap-cli/pkg/config"
"github.com/redhat-appstudio/rhtap-cli/pkg/constants"
"github.com/redhat-appstudio/rhtap-cli/pkg/flags"
Expand All @@ -14,10 +15,11 @@ import (

// RootCmd is the root command.
type RootCmd struct {
cmd *cobra.Command // root command
flags *flags.Flags // global flags
cfg *config.Config // installer configuration
kube *k8s.Kube // kubernetes client
cmd *cobra.Command // root command
flags *flags.Flags // global flags
cfg *config.Config // installer configuration
cfs *chartfs.ChartFS // embedded filesystem
kube *k8s.Kube // kubernetes client
}

// Cmd exposes the root command, while instantiating the subcommand and their
Expand All @@ -28,8 +30,8 @@ func (r *RootCmd) Cmd() *cobra.Command {
r.cmd.AddCommand(subcmd.NewIntegration(logger, r.cfg, r.kube))

for _, sub := range []subcmd.Interface{
subcmd.NewDeploy(logger, r.flags, r.cfg, r.kube),
subcmd.NewTemplate(logger, r.flags, r.cfg, r.kube),
subcmd.NewDeploy(logger, r.flags, r.cfg, r.cfs, r.kube),
subcmd.NewTemplate(logger, r.flags, r.cfg, r.cfs, r.kube),
subcmd.NewInstaller(r.flags),
} {
r.cmd.AddCommand(subcmd.NewRunner(sub).Cmd())
Expand All @@ -38,9 +40,15 @@ func (r *RootCmd) Cmd() *cobra.Command {
}

// NewRootCmd instantiates the root command, setting up the global flags.
func NewRootCmd() *RootCmd {
func NewRootCmd() (*RootCmd, error) {
f := flags.NewFlags()
cfg := config.NewConfig()

cfs, err := chartfs.NewChartFSForCWD()
if err != nil {
return nil, err
}

cfg := config.NewConfig(cfs)
r := &RootCmd{
flags: f,
cmd: &cobra.Command{
Expand All @@ -52,10 +60,11 @@ func NewRootCmd() *RootCmd {
SilenceUsage: true,
},
cfg: cfg,
cfs: cfs,
kube: k8s.NewKube(f),
}
p := r.cmd.PersistentFlags()
r.flags.PersistentFlags(p)
r.cfg.PersistentFlags(p)
return r
return r, nil
}
24 changes: 14 additions & 10 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"

"github.com/redhat-appstudio/rhtap-cli/pkg/chartfs"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -34,10 +34,10 @@ type Spec struct {

// Config root configuration structure.
type Config struct {
// configPath is the path to the configuration file, private attribute.
configPath string
// Installer is the root configuration for the installer.
Installer Spec `yaml:"rhtapCLI"`
cfs *chartfs.ChartFS // embedded filesystem
configPath string // configuration file path

Installer Spec `yaml:"rhtapCLI"` // root configuration for the installer
}

// PersistentFlags defines the persistent flags for the CLI.
Expand Down Expand Up @@ -114,7 +114,7 @@ func (c *Config) Validate() error {

// UnmarshalYAML reads the configuration file and unmarshal it into the Config.
func (c *Config) UnmarshalYAML() error {
payload, err := os.ReadFile(c.configPath)
payload, err := c.cfs.ReadFile(c.configPath)
if err != nil {
return err
}
Expand All @@ -129,13 +129,17 @@ func (c *Config) UnmarshalYAML() error {

// NewConfigFromFile returns a new Config instance based on the informed file. The
// config file path is kept as a private attribute.
func NewConfigFromFile(configPath string) (*Config, error) {
c := &Config{configPath: configPath}
func NewConfigFromFile(cfs *chartfs.ChartFS, configPath string) (*Config, error) {
c := NewConfig(cfs)
c.configPath = configPath
return c, c.UnmarshalYAML()
}

// NewConfig returns a new Config instance, pointing to the default "config.yaml"
// file location.
func NewConfig() *Config {
return &Config{configPath: "installer/config.yaml"}
func NewConfig(cfs *chartfs.ChartFS) *Config {
return &Config{
configPath: "installer/config.yaml",
cfs: cfs,
}
}
6 changes: 5 additions & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import (
"testing"

o "github.com/onsi/gomega"
"github.com/redhat-appstudio/rhtap-cli/pkg/chartfs"
)

func TestNewConfigFromFile(t *testing.T) {
g := o.NewWithT(t)

cfg, err := NewConfigFromFile("../../installer/config.yaml")
cfs, err := chartfs.NewChartFS("../../installer")
g.Expect(err).To(o.Succeed())

cfg, err := NewConfigFromFile(cfs, "config.yaml")
g.Expect(err).To(o.Succeed())
g.Expect(cfg).NotTo(o.BeNil())
g.Expect(cfg.Installer).NotTo(o.BeNil())
Expand Down
6 changes: 5 additions & 1 deletion pkg/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package engine
import (
"testing"

"github.com/redhat-appstudio/rhtap-cli/pkg/chartfs"
"github.com/redhat-appstudio/rhtap-cli/pkg/config"

o "github.com/onsi/gomega"
Expand All @@ -28,7 +29,10 @@ root:
func TestEngine_Render(t *testing.T) {
g := o.NewWithT(t)

cfg, err := config.NewConfigFromFile("../../installer/config.yaml")
cfs, err := chartfs.NewChartFS("../../installer")
g.Expect(err).To(o.Succeed())

cfg, err := config.NewConfigFromFile(cfs, "config.yaml")
g.Expect(err).To(o.Succeed())

variables := NewVariables()
Expand Down
6 changes: 3 additions & 3 deletions pkg/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ type Installer struct {
// 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)
i.logger.Debug("Loading dependency Helm chart...")
cf, err := i.cfs.GetChartFiles(i.dep.Chart)
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)
return deployer.NewHelm(i.logger, i.flags, i.kube, i.dep.Namespace, cf)
}

// SetValues prepares the values template for the Helm chart installation.
Expand Down
13 changes: 8 additions & 5 deletions pkg/subcmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import (

// Deploy is the deploy subcommand.
type Deploy struct {
cmd *cobra.Command // cobra command
logger *slog.Logger // application logger
flags *flags.Flags // global flags
cfg *config.Config // installer configuration
kube *k8s.Kube // kubernetes client
cmd *cobra.Command // cobra command
logger *slog.Logger // application logger
flags *flags.Flags // global flags
cfg *config.Config // installer configuration
cfs *chartfs.ChartFS // embedded filesystem
kube *k8s.Kube // kubernetes client

valuesTemplatePath string // path to the values template file
}
Expand Down Expand Up @@ -128,6 +129,7 @@ func NewDeploy(
logger *slog.Logger,
f *flags.Flags,
cfg *config.Config,
cfs *chartfs.ChartFS,
kube *k8s.Kube,
) Interface {
d := &Deploy{
Expand All @@ -140,6 +142,7 @@ func NewDeploy(
logger: logger.WithGroup("deploy"),
flags: f,
cfg: cfg,
cfs: cfs,
kube: kube,
}
flags.SetValuesTmplFlag(d.cmd.PersistentFlags(), &d.valuesTemplatePath)
Expand Down
28 changes: 10 additions & 18 deletions pkg/subcmd/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package subcmd
import (
"fmt"
"log/slog"
"os"

"github.com/redhat-appstudio/rhtap-cli/pkg/chartfs"
"github.com/redhat-appstudio/rhtap-cli/pkg/config"
Expand All @@ -16,11 +15,12 @@ import (

// Template represents the "template" subcommand.
type Template struct {
cmd *cobra.Command // cobra command
logger *slog.Logger // application logger
flags *flags.Flags // global flags
cfg *config.Config // installer configuration
kube *k8s.Kube // kubernetes client
cmd *cobra.Command // cobra command
logger *slog.Logger // application logger
flags *flags.Flags // global flags
cfg *config.Config // installer configuration
cfs *chartfs.ChartFS // embedded filesystem
kube *k8s.Kube // kubernetes client

// TODO: add support for "--validate", so the rendered resources are validated
// against the cluster during templating.
Expand Down Expand Up @@ -107,23 +107,13 @@ func (t *Template) Validate() error {

// Run Renders the templates.
func (t *Template) Run() error {
cwd, err := os.Getwd()
if err != nil {
return err
}

cfs, err := chartfs.NewChartFS(cwd)
if err != nil {
return err
}

valuesTmplPayload, err := cfs.ReadFile(t.valuesTemplatePath)
valuesTmplPayload, err := t.cfs.ReadFile(t.valuesTemplatePath)
if err != nil {
return fmt.Errorf("failed to read values template file: %w", err)
}

// Installer for the specific dependency
i := installer.NewInstaller(t.logger, t.flags, t.kube, cfs, &t.dep)
i := installer.NewInstaller(t.logger, t.flags, t.kube, t.cfs, &t.dep)

// Setting values and loading cluster's information.
if err = i.SetValues(
Expand Down Expand Up @@ -161,6 +151,7 @@ func NewTemplate(
logger *slog.Logger,
f *flags.Flags,
cfg *config.Config,
cfs *chartfs.ChartFS,
kube *k8s.Kube,
) *Template {
t := &Template{
Expand All @@ -173,6 +164,7 @@ func NewTemplate(
logger: logger.WithGroup("template"),
flags: f,
cfg: cfg,
cfs: cfs,
kube: kube,
dep: config.Dependency{Namespace: "default"},
showValues: true,
Expand Down

0 comments on commit 59a7d14

Please sign in to comment.