Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(PL-2800): instrument catalog and config load #199

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/joy/build_promote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestBuildPromote(t *testing.T) {
err := cp.Copy("testdata/build_promote/"+tc.name+"/original", catalogDir)
require.NoError(t, err)

cat, err = catalog.Load(catalogDir, nil)
cat, err = catalog.Load(context.Background(), catalogDir, nil)
require.NoError(t, err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/joy/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewDiagnoseCmd(version string) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
cfg := config.FromContext(cmd.Context())

_, err := fmt.Fprintln(cmd.OutOrStdout(), diagnostics.OutputWithGlobalStats(diagnostics.Evaluate(version, cfg)))
_, err := fmt.Fprintln(cmd.OutOrStdout(), diagnostics.OutputWithGlobalStats(diagnostics.Evaluate(cmd.Context(), version, cfg)))
return err
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/joy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func run(params RunParams) error {
flags.Usage = func() {}
_ = flags.Parse(params.args)

cfg, err := config.Load(configDir, catalogDir)
cfg, err := config.Load(context.Background(), configDir, catalogDir)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/joy/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,12 @@ func NewReleaseRenderCmd() *cobra.Command {

// In this case we cannot use the config or catalog loaded from the context
// Since we need to reload at whatever git reference we are at.
cfg, err := config.Load("", cfg.CatalogDir)
cfg, err := config.Load(cmd.Context(), "", cfg.CatalogDir)
if err != nil {
return nil, fmt.Errorf("loading config: %w", err)
}

cat, err := catalog.Load(cfg.CatalogDir, cfg.KnownChartRefs())
cat, err := catalog.Load(cmd.Context(), cfg.CatalogDir, cfg.KnownChartRefs())
if err != nil {
return nil, fmt.Errorf("loading catalog: %w", err)
}
Expand Down Expand Up @@ -420,12 +420,12 @@ func NewReleaseRenderCmd() *cobra.Command {

// In this case we cannot use the config or catalog loaded from the context
// Since we need to reload at whatever git reference we are at.
cfg, err := config.Load("", cfg.CatalogDir)
cfg, err := config.Load(cmd.Context(), "", cfg.CatalogDir)
if err != nil {
return nil, fmt.Errorf("loading config: %w", err)
}

cat, err := catalog.Load(cfg.CatalogDir, cfg.KnownChartRefs())
cat, err := catalog.Load(cmd.Context(), cfg.CatalogDir, cfg.KnownChartRefs())
if err != nil {
return nil, fmt.Errorf("loading catalog: %w", err)
}
Expand Down Expand Up @@ -470,12 +470,12 @@ func NewReleaseRenderCmd() *cobra.Command {

// In this case we cannot use the config or catalog loaded from the context
// Since we need to reload at whatever git reference we are at.
cfg, err := config.Load("", cfg.CatalogDir)
cfg, err := config.Load(cmd.Context(), "", cfg.CatalogDir)
if err != nil {
return nil, fmt.Errorf("loading config: %w", err)
}

cat, err := catalog.Load(cfg.CatalogDir, cfg.KnownChartRefs())
cat, err := catalog.Load(cmd.Context(), cfg.CatalogDir, cfg.KnownChartRefs())
if err != nil {
return nil, fmt.Errorf("loading catalog: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/joy/release_promote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ func TestViewGitLog(t *testing.T) {
func executeReleasePromoteCommand(t *testing.T, cmd *cobra.Command, args ...string) (string, string, error) {
dir := testutils.CloneToTempDir(t, "joy-release-promote-test")

cat, err := catalog.Load(dir, nil)
cat, err := catalog.Load(context.Background(), dir, nil)
require.NoError(t, err)

cfg, err := config.Load(dir, dir)
cfg, err := config.Load(context.Background(), dir, dir)
require.NoError(t, err)

ctx := config.ToContext(catalog.ToContext(context.Background(), cat), cfg)
Expand Down
10 changes: 5 additions & 5 deletions cmd/joy/release_render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestReleaseRender(t *testing.T) {

ctx := config.ToContext(context.Background(), cfg)

cat, err := catalog.Load(cfg.CatalogDir, cfg.KnownChartRefs())
cat, err := catalog.Load(ctx, cfg.CatalogDir, cfg.KnownChartRefs())
require.NoError(t, err)

ctx = catalog.ToContext(ctx, cat)
Expand All @@ -70,7 +70,7 @@ func TestReleaseRender(t *testing.T) {

ctx := config.ToContext(context.Background(), cfg)

cat, err := catalog.Load(cfg.CatalogDir, cfg.KnownChartRefs())
cat, err := catalog.Load(ctx, cfg.CatalogDir, cfg.KnownChartRefs())
require.NoError(t, err)

ctx = catalog.ToContext(ctx, cat)
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestReleaseRender(t *testing.T) {

ctx := config.ToContext(context.Background(), cfg)

cat, err := catalog.Load(cfg.CatalogDir, cfg.KnownChartRefs())
cat, err := catalog.Load(ctx, cfg.CatalogDir, cfg.KnownChartRefs())
require.NoError(t, err)

ctx = catalog.ToContext(ctx, cat)
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestReleaseRender(t *testing.T) {

ctx := config.ToContext(context.Background(), cfg)

cat, err := catalog.Load(cfg.CatalogDir, cfg.KnownChartRefs())
cat, err := catalog.Load(ctx, cfg.CatalogDir, cfg.KnownChartRefs())
require.NoError(t, err)

ctx = catalog.ToContext(ctx, cat)
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestReleaseRender(t *testing.T) {

ctx := config.ToContext(context.Background(), cfg)

cat, err := catalog.Load(cfg.CatalogDir, cfg.KnownChartRefs())
cat, err := catalog.Load(ctx, cfg.CatalogDir, cfg.KnownChartRefs())
require.NoError(t, err)

ctx = catalog.ToContext(ctx, cat)
Expand Down
4 changes: 2 additions & 2 deletions cmd/joy/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func NewRootCmd(version string, preRunConfigs PreRunConfigs) *cobra.Command {
return nil, fmt.Errorf("ensuring catalog up to date: %w", err)
}
var err error
cfg, err = config.Load(configDir, cfg.CatalogDir)
cfg, err = config.Load(cmd.Context(), configDir, cfg.CatalogDir)
if err != nil {
return nil, err
}
Expand All @@ -112,7 +112,7 @@ func NewRootCmd(version string, preRunConfigs PreRunConfigs) *cobra.Command {
return nil
}

cat, err := catalog.Load(cfg.CatalogDir, cfg.KnownChartRefs())
cat, err := catalog.Load(cmd.Context(), cfg.CatalogDir, cfg.KnownChartRefs())
if err != nil {
return fmt.Errorf("loading catalog: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/joy/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewSetupCmd(version string, configDir *string, catalogDir *string) *cobra.C
"Setup joy for first time use.\n\n" +
"It prompts user for catalog directory, optionally cloning it if needed, creates config file and checks for required and optional dependencies.",
RunE: func(cmd *cobra.Command, args []string) error {
return setup.Setup(version, *configDir, *catalogDir, catalogRepo)
return setup.Setup(cmd.Context(), version, *configDir, *catalogDir, catalogRepo)
},
}
cmd.Flags().StringVar(&catalogRepo, "catalog-repo", "", "catalog repo with format ORG/REPO (defaults to prompting user)")
Expand Down
6 changes: 5 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"golang.org/x/mod/semver"
"gopkg.in/yaml.v3"

"github.com/nestoca/joy/internal/observability"
"github.com/nestoca/joy/pkg/helm"
)

Expand Down Expand Up @@ -150,7 +151,10 @@ type Releases struct {
// Load loads config from given configDir (or user home if not specified) and
// optionally overrides loaded config's catalog directory with given catalogDir,
// defaulting to ~/.joy if not specified.
func Load(configDir, catalogDir string) (*Config, error) {
func Load(ctx context.Context, configDir, catalogDir string) (*Config, error) {
_, span := observability.StartTrace(ctx, "load_config")
defer span.End()

homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("getting home directory: %w", err)
Expand Down
7 changes: 4 additions & 3 deletions internal/diagnostics/catalog.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package diagnostics

import (
"context"
"fmt"
"io/fs"
"os"
Expand All @@ -23,12 +24,12 @@ type GitOpts struct {

type CatalogOpts struct {
Stat func(string) (fs.FileInfo, error)
LoadCatalog func(string, []string) (*catalog.Catalog, error)
LoadCatalog func(context.Context, string, []string) (*catalog.Catalog, error)
CheckCatalog func(string) error
Git GitOpts
}

func diagnoseCatalog(cfg *config.Config, opts CatalogOpts) (group Group) {
func diagnoseCatalog(ctx context.Context, cfg *config.Config, opts CatalogOpts) (group Group) {
if opts.Stat == nil {
opts.Stat = os.Stat
}
Expand Down Expand Up @@ -138,7 +139,7 @@ func diagnoseCatalog(cfg *config.Config, opts CatalogOpts) (group Group) {

group.AddMsg(success, "Catalog detected")

cata, err := opts.LoadCatalog(cfg.CatalogDir, cfg.KnownChartRefs())
cata, err := opts.LoadCatalog(ctx, cfg.CatalogDir, cfg.KnownChartRefs())
if err != nil {
group.AddMsg(failed, label("Failed loading catalog", err.Error()))
return
Expand Down
5 changes: 3 additions & 2 deletions internal/diagnostics/catalog_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package diagnostics

import (
"context"
"errors"
"io/fs"
"os"
Expand Down Expand Up @@ -33,7 +34,7 @@ func TestCatalogDiagnostics(t *testing.T) {
GetCurrentCommit: func(string) (string, error) { return "origin/HEAD", nil },
},
CheckCatalog: func(s string) error { return nil },
LoadCatalog: func(string, []string) (*catalog.Catalog, error) {
LoadCatalog: func(context.Context, string, []string) (*catalog.Catalog, error) {
return &catalog.Catalog{
Environments: []*v1alpha1.Environment{},
Releases: cross.ReleaseList{},
Expand Down Expand Up @@ -128,7 +129,7 @@ func TestCatalogDiagnostics(t *testing.T) {

for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
require.Equal(t, tc.Expected, diagnoseCatalog(&config.Config{User: config.User{CatalogDir: "catalogDir"}}, tc.Opts).StripAnsi())
require.Equal(t, tc.Expected, diagnoseCatalog(context.Background(), &config.Config{User: config.User{CatalogDir: "catalogDir"}}, tc.Opts).StripAnsi())
})
}
}
5 changes: 3 additions & 2 deletions internal/diagnostics/diagnose.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package diagnostics

import (
"context"
"fmt"
"strings"

Expand All @@ -11,12 +12,12 @@ import (
"github.com/nestoca/joy/internal/style"
)

func Evaluate(cliVersion string, cfg *config.Config) Groups {
func Evaluate(ctx context.Context, cliVersion string, cfg *config.Config) Groups {
return Groups{
diagnoseExecutable(cfg, cliVersion, ExecutableOptions{}),
diagnoseDependencies(dependencies.AllRequired, dependencies.AllOptional),
diagnoseConfig(cfg, ConfigOpts{}),
diagnoseCatalog(cfg, CatalogOpts{}),
diagnoseCatalog(ctx, cfg, CatalogOpts{}),
}
}

Expand Down
21 changes: 11 additions & 10 deletions internal/setup/setup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package setup

import (
"context"
"fmt"
"os"
"path"
Expand All @@ -20,17 +21,17 @@ const (
separator = "————————————————————————————————————————————————————————————————————————————————"
)

func Setup(version, configDir, catalogDir, catalogRepo string) error {
func Setup(ctx context.Context, version, configDir, catalogDir, catalogRepo string) error {
fmt.Println("👋 Hey there, let's kickstart your most joyful CD experience! ☀️")
fmt.Println(separator)

// Setup catalog and config
fmt.Print("🛠️ Let's first set up your configuration and catalog repo...\n\n")
catalogDir, err := setupCatalog(configDir, catalogDir, catalogRepo)
catalogDir, err := setupCatalog(ctx, configDir, catalogDir, catalogRepo)
if err != nil {
return err
}
cfg, err := setupConfig(configDir, catalogDir)
cfg, err := setupConfig(ctx, configDir, catalogDir)
if err != nil {
return err
}
Expand All @@ -39,13 +40,13 @@ func Setup(version, configDir, catalogDir, catalogRepo string) error {

// Run diagnostics
fmt.Print("🔍 Let's run a few diagnostics to check everything is in order...\n\n")
_, err = fmt.Println(diagnostics.OutputWithGlobalStats(diagnostics.Evaluate(version, cfg)))
_, err = fmt.Println(diagnostics.OutputWithGlobalStats(diagnostics.Evaluate(ctx, version, cfg)))
return err
}

func setupConfig(configDir string, catalogDir string) (*config.Config, error) {
func setupConfig(ctx context.Context, configDir string, catalogDir string) (*config.Config, error) {
// Try loading config file from given or default location
cfg, err := config.Load(configDir, catalogDir)
cfg, err := config.Load(ctx, configDir, catalogDir)
if err != nil {
return nil, fmt.Errorf("loading config: %w", err)
}
Expand All @@ -60,9 +61,9 @@ func setupConfig(configDir string, catalogDir string) (*config.Config, error) {
return cfg, nil
}

func setupCatalog(configDir string, catalogDir string, catalogRepo string) (string, error) {
func setupCatalog(ctx context.Context, configDir string, catalogDir string, catalogRepo string) (string, error) {
var err error
catalogDir, err = getCatalogDir(configDir, catalogDir)
catalogDir, err = getCatalogDir(ctx, configDir, catalogDir)
if err != nil {
return "", err
}
Expand All @@ -82,10 +83,10 @@ func setupCatalog(configDir string, catalogDir string, catalogRepo string) (stri
return catalogDir, nil
}

func getCatalogDir(configDir string, catalogDir string) (string, error) {
func getCatalogDir(ctx context.Context, configDir string, catalogDir string) (string, error) {
if catalogDir == "" {
// Try loading catalog dir from config file to use as prompt default value
cfg, err := config.Load(configDir, catalogDir)
cfg, err := config.Load(ctx, configDir, catalogDir)
if err == nil {
catalogDir = cfg.CatalogDir
} else {
Expand Down
6 changes: 5 additions & 1 deletion pkg/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/nestoca/joy/api/v1alpha1"
"github.com/nestoca/joy/internal/ignore"
"github.com/nestoca/joy/internal/observability"
"github.com/nestoca/joy/internal/release/cross"
"github.com/nestoca/joy/internal/release/filtering"
"github.com/nestoca/joy/internal/yml"
Expand All @@ -34,7 +35,10 @@ type Catalog struct {
Files []*yml.File
}

func Load(dir string, validChartRefs []string) (*Catalog, error) {
func Load(ctx context.Context, dir string, validChartRefs []string) (*Catalog, error) {
_, span := observability.StartTrace(ctx, "load_catalog")
defer span.End()

// Get absolute and clean path of directory, so we can determine whether a release belongs to an environment
// by simply comparing the beginning of their paths.
dir, err := filepath.Abs(dir)
Expand Down
7 changes: 4 additions & 3 deletions pkg/catalog/catalog_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package catalog

import (
"context"
"path/filepath"
"testing"

Expand Down Expand Up @@ -39,7 +40,7 @@ func TestCatalogLoadE2E(t *testing.T) {
err := config.LoadFile(filepath.Join("testdata", tc.Folder, "joy.yaml"), &cfg.Catalog)
require.NoError(t, err)

_, err = Load(filepath.Join("testdata", tc.Folder), cfg.KnownChartRefs())
_, err = Load(context.Background(), filepath.Join("testdata", tc.Folder), cfg.KnownChartRefs())
require.Error(t, err)
require.Contains(t, err.Error(), tc.Error)
})
Expand All @@ -50,7 +51,7 @@ func TestFreeformEnvsAndReleasesLoading(t *testing.T) {
catalogDir, err := filepath.Abs("testdata/freeform")
require.NoError(t, err)

cat, err := Load(catalogDir, nil)
cat, err := Load(context.Background(), catalogDir, nil)
require.NoError(t, err)

envs := cat.Environments
Expand All @@ -74,7 +75,7 @@ func TestFreeformEnvsAndReleasesLoadingWithJoyIgnore(t *testing.T) {
catalogDir, err := filepath.Abs("testdata/freeform-with-joyignore")
require.NoError(t, err)

cat, err := Load(catalogDir, nil)
cat, err := Load(context.Background(), catalogDir, nil)
require.NoError(t, err)

envs := cat.Environments
Expand Down
Loading
Loading