Skip to content
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
11 changes: 6 additions & 5 deletions cmd/crossplane/dependency/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ import (
//go:embed help/add.md
var addHelp string

// addCmd adds a dependency to the current project.
type addCmd struct {
// AddCmd adds a dependency to the current project.
type AddCmd struct {
common.GitFlags

Package string `arg:"" help:"Package to add (xpkg OCI reference, k8s:<version>, git repository URL, or HTTP(S) URL)."`
Expand All @@ -57,12 +57,13 @@ type addCmd struct {
GitPath string `help:"Path to CRDs in the git repository." name:"git-path"`
}

func (c *addCmd) Help() string {
// Help returns help for the add command.
func (c *AddCmd) Help() string {
return addHelp
}

// Run executes the add command.
func (c *addCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *config.Config) error {
func (c *AddCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *config.Config) error {
ctx := context.Background()

projFilePath, err := filepath.Abs(c.ProjectFile)
Expand Down Expand Up @@ -111,7 +112,7 @@ func (c *addCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *con
})
}

func (c *addCmd) buildDependency() (v1alpha1.Dependency, error) {
func (c *AddCmd) buildDependency() (v1alpha1.Dependency, error) {
// k8s dependency: k8s:vX.Y.Z
if version, found := strings.CutPrefix(c.Package, "k8s:"); found {
if version == "" {
Expand Down
84 changes: 22 additions & 62 deletions cmd/crossplane/dependency/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,46 @@ limitations under the License.
package dependency

import (
"cmp"
"context"
"fmt"
"path/filepath"

"github.com/alecthomas/kong"
"github.com/spf13/afero"

"github.com/crossplane/crossplane-runtime/v2/pkg/errors"
"github.com/crossplane/crossplane-runtime/v2/pkg/logging"

"github.com/crossplane/cli/v2/cmd/crossplane/common"
"github.com/crossplane/cli/v2/internal/async"
"github.com/crossplane/cli/v2/internal/config"
"github.com/crossplane/cli/v2/internal/dependency"
"github.com/crossplane/cli/v2/internal/project/projectfile"
"github.com/crossplane/cli/v2/internal/schemas/generator"
"github.com/crossplane/cli/v2/internal/schemas/manager"
"github.com/crossplane/cli/v2/internal/schemas/runner"
"github.com/crossplane/cli/v2/internal/terminal"
clixpkg "github.com/crossplane/cli/v2/internal/xpkg"
"github.com/crossplane/cli/v2/pkg/project"

_ "embed"
)

//go:embed help/update-cache.md
var updateHelp string

// updateCacheCmd updates the dependency cache by regenerating all schemas.
type updateCacheCmd struct {
// UpdateCacheCmd updates the dependency cache by regenerating all schemas.
// It is exported so host CLIs embedding the dependency commands can wrap it,
// e.g. to generate additional schemas after the update.
type UpdateCacheCmd struct {
common.GitFlags

ProjectFile string `default:"${project_file}" help:"Path to project definition file." short:"f"`
CacheDir string `env:"CROSSPLANE_XPKG_CACHE" help:"Directory for cached xpkg package contents." name:"cache-dir"`
}

func (c *updateCacheCmd) Help() string {
// Help returns help for the update-cache command.
func (c *UpdateCacheCmd) Help() string {
return updateHelp
}

// Run executes the update-cache command.
func (c *updateCacheCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *config.Config) error {
func (c *UpdateCacheCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *config.Config) error {
ctx := context.Background()

projFilePath, err := filepath.Abs(c.ProjectFile)
Expand All @@ -73,75 +71,37 @@ func (c *updateCacheCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter,
return err
}

client, err := clixpkg.NewClient(clixpkg.WithCacheDir(afero.NewOsFs(), cmp.Or(c.CacheDir, dependency.DefaultCacheDir())), clixpkg.WithImageConfigs(proj.Spec.ImageConfigs))
if err != nil {
return errors.Wrap(err, "cannot create xpkg client")
}

generators := generator.Filter(
generator.AllLanguages(
generator.WithGoModelAccessors(cfg.Features.GenerateGoModelAccessors),
generator.WithGoRuntimeObjects(cfg.Features.GenerateGoRuntimeObjects),
),
proj.Spec.Schemas.GetLanguages(),
)

gitOpts := c.ManagerOptions()
opts := make([]dependency.ManagerOption, 0, 3+len(gitOpts))
opts = append(opts,
dependency.WithProjectFile(c.ProjectFile),
dependency.WithSchemaGenerators(generators),
dependency.WithXpkgClient(client),
)
opts = append(opts, gitOpts...)

m := dependency.NewManager(proj, projFS, opts...)

logger.Debug("Updating all dependencies")
return sp.WrapAsyncWithSuccessSpinners(func(ch async.EventChannel) error {
if err := m.RefreshAll(ctx, ch); err != nil {
return err
}

// Also regenerate schemas for the project's own APIs, so the models
// are complete without a build (e.g. for language tooling in
// functions and tests that import them).
apisExist, err := afero.DirExists(projFS, proj.Spec.Paths.APIs)
if err != nil || !apisExist {
return err
}

ch.SendEvent("Generating project API schemas", async.EventStatusStarted)
schemasFS := afero.NewBasePathFs(projFS, proj.Spec.Paths.Schemas)
schemaRunner := runner.NewRealSchemaRunner(runner.WithImageConfig(proj.Spec.ImageConfigs))
schemaMgr := manager.New(schemasFS, generators, schemaRunner)
apisFS := afero.NewBasePathFs(projFS, proj.Spec.Paths.APIs)
if _, err := schemaMgr.Generate(ctx, manager.NewFSSource(proj.Spec.Paths.APIs, apisFS)); err != nil {
ch.SendEvent("Generating project API schemas", async.EventStatusFailure)
return errors.Wrap(err, "failed to generate project API schemas")
}
ch.SendEvent("Generating project API schemas", async.EventStatusSuccess)

return nil
return project.UpdateSchemas(ctx, cfg, proj, projFS, project.UpdateSchemasOptions{
CacheDir: c.CacheDir,
GitUsername: c.GitUsername,
GitToken: c.GitToken,
GitProxy: c.GitProxy,
GitInsecureHostKey: c.GitInsecureHostKey,
Refresh: true,
EventChannel: ch,
})
})
}

//go:embed help/clean-cache.md
var cleanHelp string

// cleanCacheCmd removes all generated schemas.
type cleanCacheCmd struct {
// CleanCacheCmd removes all generated schemas.
type CleanCacheCmd struct {
ProjectFile string `default:"${project_file}" help:"Path to project definition file." short:"f"`
CacheDir string `env:"CROSSPLANE_XPKG_CACHE" help:"Directory for cached xpkg package contents." name:"cache-dir"`
KeepPackages bool `help:"Keep cached xpkg package contents; remove only generated schemas." name:"keep-packages"`
}

func (c *cleanCacheCmd) Help() string {
// Help returns help for the clean-cache command.
func (c *CleanCacheCmd) Help() string {
return cleanHelp
}

// Run executes the clean-cache command.
func (c *cleanCacheCmd) Run(k *kong.Context, _ logging.Logger) error {
func (c *CleanCacheCmd) Run(k *kong.Context, _ logging.Logger) error {
projFilePath, err := filepath.Abs(c.ProjectFile)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions cmd/crossplane/dependency/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package dependency

// Cmd contains commands for dependency management.
type Cmd struct {
Add addCmd `cmd:"" help:"Add a dependency to the current project." novale:"Google.WordList[\"k8s\"],Google.Units,gitlab.Units"`
UpdateCache updateCacheCmd `cmd:"" help:"Update the dependency cache for the current project."`
CleanCache cleanCacheCmd `cmd:"" help:"Clean the dependency cache."`
Add AddCmd `cmd:"" help:"Add a dependency to the current project." novale:"Google.WordList[\"k8s\"],Google.Units,gitlab.Units"`
UpdateCache UpdateCacheCmd `cmd:"" help:"Update the dependency cache for the current project."`
CleanCache CleanCacheCmd `cmd:"" help:"Clean the dependency cache."`
}
4 changes: 2 additions & 2 deletions cmd/crossplane/dependency/dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ import (

func TestAddGitAuth(t *testing.T) {
testDependencyGitAuth(t, func(serverURL, projectFile, cacheDir, token string) error {
c := &addCmd{Package: serverURL + "/repo.git", GitRef: "main", ProjectFile: projectFile, CacheDir: cacheDir, GitFlags: common.GitFlags{GitToken: token, GitUsername: "test-user"}}
c := &AddCmd{Package: serverURL + "/repo.git", GitRef: "main", ProjectFile: projectFile, CacheDir: cacheDir, GitFlags: common.GitFlags{GitToken: token, GitUsername: "test-user"}}
return c.Run(logging.NewNopLogger(), terminal.NewSpinnerPrinter(io.Discard, false), &config.Config{})
})
}

func TestUpdateCacheGitAuth(t *testing.T) {
testDependencyGitAuth(t, func(_, projectFile, cacheDir, token string) error {
c := &updateCacheCmd{ProjectFile: projectFile, CacheDir: cacheDir, GitFlags: common.GitFlags{GitToken: token, GitUsername: "test-user"}}
c := &UpdateCacheCmd{ProjectFile: projectFile, CacheDir: cacheDir, GitFlags: common.GitFlags{GitToken: token, GitUsername: "test-user"}}
return c.Run(logging.NewNopLogger(), terminal.NewSpinnerPrinter(io.Discard, false), &config.Config{})
})
}
Expand Down
89 changes: 89 additions & 0 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,95 @@ func Build(ctx context.Context, cfg *config.Config, proj *devv1alpha1.Project, p
// unconditionally.
func noopCleanup() {}

// UpdateSchemasOptions configures UpdateSchemas.
type UpdateSchemasOptions struct {
// CacheDir is the xpkg dependency cache directory. Defaults to the
// standard per-user crossplane cache location.
CacheDir string
// GitUsername and GitToken authenticate git-based dependencies over
// HTTPS.
GitUsername string
GitToken string
// GitProxy tunnels git operations via an HTTP CONNECT proxy.
GitProxy string
// GitInsecureHostKey disables SSH host key verification for git-based
// dependencies over SSH.
GitInsecureHostKey bool
// Refresh re-resolves dependency versions and refetches packages even
// when they are cached.
Refresh bool
// EventChannel receives progress events, if set.
EventChannel EventChannel
}

// UpdateSchemas resolves the project's dependencies and generates language
// schemas for them and for the project's own APIs into the project's schemas
// directory - the same work `crossplane dependency update-cache` does. Hosts
// use it e.g. to regenerate a project's models without a build.
func UpdateSchemas(ctx context.Context, cfg *config.Config, proj *devv1alpha1.Project, projFS afero.Fs, o UpdateSchemasOptions) error {
client, err := clixpkg.NewClient(clixpkg.WithCacheDir(afero.NewOsFs(), cmp.Or(o.CacheDir, dependency.DefaultCacheDir())), clixpkg.WithImageConfigs(proj.Spec.ImageConfigs))
if err != nil {
return errors.Wrap(err, "cannot create xpkg client")
}

generators := generator.Filter(
generator.AllLanguages(
generator.WithGoModelAccessors(cfg.Features.GenerateGoModelAccessors),
generator.WithGoRuntimeObjects(cfg.Features.GenerateGoRuntimeObjects),
),
proj.Spec.Schemas.GetLanguages(),
)

opts := []dependency.ManagerOption{
dependency.WithProjectFile(FileName),
dependency.WithSchemaGenerators(generators),
dependency.WithXpkgClient(client),
}
if o.GitToken != "" {
opts = append(opts, dependency.WithGitAuthProvider(&git.HTTPSAuthProvider{
Username: o.GitUsername,
Password: o.GitToken,
}))
}
if o.GitProxy != "" {
opts = append(opts, dependency.WithGitProxyURL(o.GitProxy))
}
if o.GitInsecureHostKey {
opts = append(opts, dependency.WithGitInsecureHostKey(true))
}
m := dependency.NewManager(proj, projFS, opts...)

if o.Refresh {
err = m.RefreshAll(ctx, o.EventChannel)
} else {
err = m.AddAll(ctx, o.EventChannel)
}
if err != nil {
return err
}

// Also generate schemas for the project's own APIs, so the models are
// complete without a build (e.g. for language tooling in functions and
// tests that import them).
apisExist, err := afero.DirExists(projFS, proj.Spec.Paths.APIs)
if err != nil || !apisExist {
return err
}

o.EventChannel.SendEvent("Generating project API schemas", async.EventStatusStarted)
schemasFS := afero.NewBasePathFs(projFS, proj.Spec.Paths.Schemas)
schemaRunner := runner.NewRealSchemaRunner(runner.WithImageConfig(proj.Spec.ImageConfigs))
schemaMgr := manager.New(schemasFS, generators, schemaRunner)
apisFS := afero.NewBasePathFs(projFS, proj.Spec.Paths.APIs)
if _, err := schemaMgr.Generate(ctx, manager.NewFSSource(proj.Spec.Paths.APIs, apisFS)); err != nil {
o.EventChannel.SendEvent("Generating project API schemas", async.EventStatusFailure)
return errors.Wrap(err, "failed to generate project API schemas")
}
o.EventChannel.SendEvent("Generating project API schemas", async.EventStatusSuccess)

return nil
}

// SchemaSource is a source of resources (CRDs/XRDs) for which language
// schemas can be generated.
type SchemaSource = manager.Source
Expand Down
Loading