diff --git a/cmd/crossplane/project/build.go b/cmd/crossplane/project/build.go index 8e24b277..81fc73fc 100644 --- a/cmd/crossplane/project/build.go +++ b/cmd/crossplane/project/build.go @@ -50,8 +50,8 @@ import ( //go:embed help/build.md var buildHelp string -// buildCmd builds a project into Crossplane packages. -type buildCmd struct { +// BuildCmd builds a project into Crossplane packages. +type BuildCmd struct { ProjectFile string `default:"${project_file}" help:"Path to project definition." short:"f"` Repository string `help:"Override the repository in the project file." optional:""` OutputDir string `default:"_output" help:"Output directory for packages." short:"o"` @@ -64,12 +64,13 @@ type buildCmd struct { projFS afero.Fs } -func (c *buildCmd) Help() string { +// Help returns help for the build command. +func (c *BuildCmd) Help() string { return buildHelp } // AfterApply parses flags and reads the project file. -func (c *buildCmd) AfterApply() error { +func (c *BuildCmd) AfterApply() error { projFilePath, err := filepath.Abs(c.ProjectFile) if err != nil { return err @@ -88,7 +89,7 @@ func (c *buildCmd) AfterApply() error { } // Run executes the build command. -func (c *buildCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *config.Config) error { +func (c *BuildCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *config.Config) error { ctx := context.Background() if c.Repository != "" { diff --git a/cmd/crossplane/project/build_test.go b/cmd/crossplane/project/build_test.go index 54979ef5..a8fe86fb 100644 --- a/cmd/crossplane/project/build_test.go +++ b/cmd/crossplane/project/build_test.go @@ -74,7 +74,7 @@ spec: sp := terminal.NewSpinnerPrinter(io.Discard, false) cfg := &config.Config{} - c := &buildCmd{ProjectFile: projectFile, CacheDir: filepath.Join(dir, "cache"), GitToken: token, GitUsername: "test-user", MaxConcurrency: 1} + c := &BuildCmd{ProjectFile: projectFile, CacheDir: filepath.Join(dir, "cache"), GitToken: token, GitUsername: "test-user", MaxConcurrency: 1} if err := c.AfterApply(); err != nil { t.Fatal(err) } diff --git a/cmd/crossplane/project/init.go b/cmd/crossplane/project/init.go index 3c740960..995b70f5 100644 --- a/cmd/crossplane/project/init.go +++ b/cmd/crossplane/project/init.go @@ -36,19 +36,21 @@ import ( //go:embed help/init.md var initHelp string -// initCmd initializes a new project. -type initCmd struct { +// InitCmd initializes a new project. +type InitCmd struct { Name string `arg:"" help:"The name of the new project."` Directory string `help:"Directory to initialize. Defaults to project name" short:"d" type:"path"` Registry string `default:"example.com/my-org" help:"Override the registry in the project file." optional:"" short:"r"` Repository string `help:"Override the repository name in the project file. Defaults to the project name." optional:""` } -func (c *initCmd) Help() string { +// Help returns help for the init command. +func (c *InitCmd) Help() string { return initHelp } -func (c *initCmd) Run(sp terminal.SpinnerPrinter) error { +// Run executes the init command. +func (c *InitCmd) Run(sp terminal.SpinnerPrinter) error { // Validate the project name is a valid DNS-1035 label. if errs := validation.IsDNS1035Label(c.Name); len(errs) > 0 { return errors.Errorf("'%s' is not a valid project name. DNS-1035 constraints: %s", c.Name, strings.Join(errs, "; ")) @@ -112,7 +114,7 @@ spec: }) } -func (c *initCmd) checkTargetDirectory() error { +func (c *InitCmd) checkTargetDirectory() error { f, err := os.Stat(c.Directory) switch { case os.IsNotExist(err): diff --git a/cmd/crossplane/project/project.go b/cmd/crossplane/project/project.go index 5ec95df7..432fd57a 100644 --- a/cmd/crossplane/project/project.go +++ b/cmd/crossplane/project/project.go @@ -19,10 +19,10 @@ package project // Cmd contains project subcommands. type Cmd struct { - Init initCmd `cmd:"" help:"Initialize a new project."` - Build buildCmd `cmd:"" help:"Build a project into Crossplane packages."` - Push pushCmd `cmd:"" help:"Push a built project to an OCI registry."` - Run runCmd `cmd:"" help:"Build and run a project in a local dev control plane." novale:"Google.WordList[\"admin\"]"` - Simulate simulateCmd `cmd:"" help:"Preview how a project's Compositions would change a cluster's composite resources." maturity:"alpha"` - Stop stopCmd `cmd:"" help:"Tear down a local dev control plane."` + Init InitCmd `cmd:"" help:"Initialize a new project."` + Build BuildCmd `cmd:"" help:"Build a project into Crossplane packages."` + Push PushCmd `cmd:"" help:"Push a built project to an OCI registry."` + Run RunCmd `cmd:"" help:"Build and run a project in a local dev control plane." novale:"Google.WordList[\"admin\"]"` + Simulate SimulateCmd `cmd:"" help:"Preview how a project's Compositions would change a cluster's composite resources." maturity:"alpha"` + Stop StopCmd `cmd:"" help:"Tear down a local dev control plane."` } diff --git a/cmd/crossplane/project/push.go b/cmd/crossplane/project/push.go index a562cdd1..4eb39f19 100644 --- a/cmd/crossplane/project/push.go +++ b/cmd/crossplane/project/push.go @@ -44,8 +44,8 @@ import ( //go:embed help/push.md var pushHelp string -// pushCmd pushes a built project to an OCI registry. -type pushCmd struct { +// PushCmd pushes a built project to an OCI registry. +type PushCmd struct { ProjectFile string `default:"${project_file}" help:"Path to project definition." short:"f"` Repository string `help:"Override the repository in the project file." optional:""` Tag string `default:"" help:"Tag for the pushed package. Defaults to a time-based semver-like tag." short:"t"` @@ -60,13 +60,14 @@ type pushCmd struct { transport http.RoundTripper } -func (c *pushCmd) Help() string { +// Help returns help for the push command. +func (c *PushCmd) Help() string { return pushHelp } // AfterApply parses flags, reads the project file, and prepares the push // runtime (filesystem views and HTTP transport). -func (c *pushCmd) AfterApply() error { +func (c *PushCmd) AfterApply() error { projFilePath, err := filepath.Abs(c.ProjectFile) if err != nil { return err @@ -102,7 +103,7 @@ func (c *pushCmd) AfterApply() error { } // Run executes the push command. -func (c *pushCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter) error { +func (c *PushCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter) error { ctx := context.Background() if c.Repository != "" { @@ -154,7 +155,7 @@ func (c *pushCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter) error { return nil } -func (c *pushCmd) loadPackages() (project.ImageTagMap, error) { +func (c *PushCmd) loadPackages() (project.ImageTagMap, error) { opener := func() (io.ReadCloser, error) { return c.packageFS.Open(c.PackageFile) } diff --git a/cmd/crossplane/project/run.go b/cmd/crossplane/project/run.go index 71eeb8ef..1e6c74f7 100644 --- a/cmd/crossplane/project/run.go +++ b/cmd/crossplane/project/run.go @@ -60,8 +60,8 @@ import ( //go:embed help/run.md var runHelp string -// runCmd builds a project and runs it in a local dev control plane. -type runCmd struct { +// RunCmd builds a project and runs it in a local dev control plane. +type RunCmd struct { ProjectFile string `default:"${project_file}" help:"Path to project definition." short:"f"` Repository string `help:"Override the repository." optional:""` MaxConcurrency uint `default:"8" help:"Max concurrent builds."` @@ -83,12 +83,13 @@ type runCmd struct { extraResources []runtime.RawExtension } -func (c *runCmd) Help() string { +// Help returns help for the run command. +func (c *RunCmd) Help() string { return runHelp } // AfterApply parses flags and reads the project file. -func (c *runCmd) AfterApply() error { +func (c *RunCmd) AfterApply() error { projFilePath, err := filepath.Abs(c.ProjectFile) if err != nil { return err @@ -134,7 +135,7 @@ func (c *runCmd) AfterApply() error { } // Run executes the run command. -func (c *runCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *config.Config) error { //nolint:gocyclo // Main command orchestration. +func (c *RunCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *config.Config) error { //nolint:gocyclo // Main command orchestration. ctx := context.Background() if c.Repository != "" { diff --git a/cmd/crossplane/project/simulate.go b/cmd/crossplane/project/simulate.go index 5b264fda..114f6092 100644 --- a/cmd/crossplane/project/simulate.go +++ b/cmd/crossplane/project/simulate.go @@ -46,9 +46,9 @@ var ( simulateExamples string ) -// simulateCmd previews how a project's compositions would change the +// SimulateCmd previews how a project's compositions would change the // composite resources in a cluster. -type simulateCmd struct { +type SimulateCmd struct { simulate.Flags `embed:""` simulate.CompositionFlags `embed:""` @@ -56,13 +56,14 @@ type simulateCmd struct { ProjectFile string `default:"${project_file}" help:"Path to project definition." short:"f"` } -func (c *simulateCmd) Help() string { +// Help returns help for the simulate command. +func (c *SimulateCmd) Help() string { return simulateHelp + simulate.Help(true) + simulateExamples } // Run previews the project's compositions against the cluster, with its // functions built locally. -func (c *simulateCmd) Run(sigCtx context.Context, k *kong.Context, logger logging.Logger, sp terminal.SpinnerPrinter, cliCfg *config.Config) error { +func (c *SimulateCmd) Run(sigCtx context.Context, k *kong.Context, logger logging.Logger, sp terminal.SpinnerPrinter, cliCfg *config.Config) error { return simulate.NewRunner(&c.Flags).Run(sigCtx, k, logger, sp, &simulate.Inputs{Build: func(ctx context.Context) (*simulate.Built, error) { return c.buildProject(ctx, logger, sp, cliCfg) }, Selection: &c.CompositionFlags}) @@ -71,7 +72,7 @@ func (c *simulateCmd) Run(sigCtx context.Context, k *kong.Context, logger loggin // buildProject returns the project's compositions, its locally built // functions, and the packages its provider dependencies resolve through. // --image-configs, when set, replaces the project's ImageConfigs for both. -func (c *simulateCmd) buildProject(ctx context.Context, logger logging.Logger, sp terminal.SpinnerPrinter, cliCfg *config.Config) (*simulate.Built, error) { +func (c *SimulateCmd) buildProject(ctx context.Context, logger logging.Logger, sp terminal.SpinnerPrinter, cliCfg *config.Config) (*simulate.Built, error) { projFilePath, err := filepath.Abs(c.ProjectFile) if err != nil { return nil, errors.Wrap(err, "cannot get absolute path of project file") diff --git a/cmd/crossplane/project/simulate_test.go b/cmd/crossplane/project/simulate_test.go index b8c160be..1eef5867 100644 --- a/cmd/crossplane/project/simulate_test.go +++ b/cmd/crossplane/project/simulate_test.go @@ -37,11 +37,11 @@ import ( ) // parseSimulate parses args as `simulate` flags, running kong's validation. -func parseSimulate(t *testing.T, args ...string) (*simulateCmd, error) { +func parseSimulate(t *testing.T, args ...string) (*SimulateCmd, error) { t.Helper() var cli struct { - Simulate simulateCmd `cmd:""` + Simulate SimulateCmd `cmd:""` } parser, err := kong.New(&cli, kong.Vars{"project_file": "crossplane-project.yaml"}, kong.Exit(func(int) {})) if err != nil { @@ -59,7 +59,7 @@ func TestSimulateFlags(t *testing.T) { } type want struct { - cmd *simulateCmd + cmd *SimulateCmd err bool } @@ -71,7 +71,7 @@ func TestSimulateFlags(t *testing.T) { }{ "Defaults": { reason: "With no flags the defaults should apply.", - want: want{cmd: &simulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -87,7 +87,7 @@ func TestSimulateFlags(t *testing.T) { "--max-concurrency", "2", "-o", "json", "--crossplane-version", "v2.3.4", "--as", "jane", "--as-group", "admins", }, - want: want{cmd: &simulateCmd{MaxConcurrency: 2, ProjectFile: "p.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{MaxConcurrency: 2, ProjectFile: "p.yaml", Flags: simulate.Flags{ Context: "staging", MaxIterations: 3, Timeout: 2 * time.Minute, @@ -104,7 +104,7 @@ func TestSimulateFlags(t *testing.T) { "--eventual-state", "--max-recv-message-size", "16", "--minimize-composition", "--analyze-on", "always", "--dry-run-on", "existing", }, - want: want{cmd: &simulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -121,7 +121,7 @@ func TestSimulateFlags(t *testing.T) { reason: "--function-credentials should land on its field; Run loads the Secrets.", args: []string{"--function-credentials", creds}, want: want{ - cmd: &simulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + cmd: &SimulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -144,7 +144,7 @@ func TestSimulateFlags(t *testing.T) { "PlanFlags": { reason: "The provider plan flags should land on their fields.", args: []string{"--providers", dir, "--image-configs", creds, "--session-id", "s1"}, - want: want{cmd: &simulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -158,7 +158,7 @@ func TestSimulateFlags(t *testing.T) { "RenderEngineFlags": { reason: "The render engine flags should land on the embedded EngineFlags.", args: []string{"--crossplane-binary", creds, "--session-id", "s1"}, - want: want{cmd: &simulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -170,7 +170,7 @@ func TestSimulateFlags(t *testing.T) { "RenderDockerNetwork": { reason: "--crossplane-docker-network should land on the embedded EngineFlags.", args: []string{"--crossplane-docker-network", "ci"}, - want: want{cmd: &simulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -182,7 +182,7 @@ func TestSimulateFlags(t *testing.T) { "RenderDockerNetworkEnv": { reason: "CROSSPLANE_DOCKER_NETWORK should land on the embedded EngineFlags.", env: map[string]string{"CROSSPLANE_DOCKER_NETWORK": "env-net"}, - want: want{cmd: &simulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -195,7 +195,7 @@ func TestSimulateFlags(t *testing.T) { reason: "--crossplane-docker-network should win over CROSSPLANE_DOCKER_NETWORK.", env: map[string]string{"CROSSPLANE_DOCKER_NETWORK": "env-net"}, args: []string{"--crossplane-docker-network", "ci"}, - want: want{cmd: &simulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -207,7 +207,7 @@ func TestSimulateFlags(t *testing.T) { "RenderBinaryWithNetwork": { reason: "A local render binary ignores a Docker network rather than rejecting it.", args: []string{"--crossplane-binary", creds, "--crossplane-docker-network", "ci"}, - want: want{cmd: &simulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -219,7 +219,7 @@ func TestSimulateFlags(t *testing.T) { "SkipPlan": { reason: "--skip-plan alone should be accepted.", args: []string{"--skip-plan"}, - want: want{cmd: &simulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -231,7 +231,7 @@ func TestSimulateFlags(t *testing.T) { "SkipPlanWithProviders": { reason: "--providers does nothing with --skip-plan, so Run warns rather than Validate rejecting it.", args: []string{"--skip-plan", "--providers", dir}, - want: want{cmd: &simulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -244,7 +244,7 @@ func TestSimulateFlags(t *testing.T) { "SkipPlanWithImageConfigs": { reason: "--image-configs also rewrites function images, so it applies with --skip-plan.", args: []string{"--skip-plan", "--image-configs", creds}, - want: want{cmd: &simulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -257,7 +257,7 @@ func TestSimulateFlags(t *testing.T) { "SkipPlanWithSessionID": { reason: "--session-id does nothing with --skip-plan, so Run warns rather than Validate rejecting it.", args: []string{"--skip-plan", "--session-id", "s1"}, - want: want{cmd: &simulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -275,7 +275,7 @@ func TestSimulateFlags(t *testing.T) { "ZeroConcurrency": { reason: "Zero build slots should be accepted; the builder runs one build at a time.", args: []string{"--max-concurrency", "0"}, - want: want{cmd: &simulateCmd{ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -326,7 +326,7 @@ func TestSimulateFlags(t *testing.T) { "RenderImageUncomparable": { reason: "A render image with no comparable version should be accepted.", args: []string{"--crossplane-image", "example.com/crossplane:stable"}, - want: want{cmd: &simulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ + want: want{cmd: &SimulateCmd{MaxConcurrency: 8, ProjectFile: "crossplane-project.yaml", Flags: simulate.Flags{ MaxIterations: dp.DefaultMaxRenderIterations, Timeout: 2 * time.Minute, Output: "plan", @@ -371,7 +371,7 @@ func TestSimulateBuildProjectRejectsComposition(t *testing.T) { t.Fatal(err) } - c := &simulateCmd{ProjectFile: path} + c := &SimulateCmd{ProjectFile: path} _, err := c.buildProject(context.Background(), logging.NewNopLogger(), nil, &config.Config{}) if err == nil || !strings.Contains(err.Error(), "failed to parse project file") { t.Errorf("buildProject(-f %s): want a project file parse error, got %v", path, err) @@ -405,7 +405,7 @@ func TestSimulateBuildProjectErrors(t *testing.T) { for name, tc := range cases { t.Run(name, func(t *testing.T) { - c := &simulateCmd{ProjectFile: filepath.Join("testdata", "project", tc.project, "crossplane-project.yaml")} + c := &SimulateCmd{ProjectFile: filepath.Join("testdata", "project", tc.project, "crossplane-project.yaml")} _, err := c.buildProject(context.Background(), logging.NewNopLogger(), nil, &config.Config{}) if err == nil || !strings.Contains(err.Error(), tc.want) { t.Errorf("\n%s\nbuildProject(%s): want error containing %q, got %v", tc.reason, tc.project, tc.want, err) @@ -420,7 +420,7 @@ func TestSimulateNameFlagParse(t *testing.T) { "CommaSeparated": {"--name=a/db,b/db"}, } { t.Run(name, func(t *testing.T) { - var c simulateCmd + var c SimulateCmd k, err := kong.New(&c, kong.Vars{"project_file": "crossplane-project.yaml"}) if err != nil { t.Fatal(err) diff --git a/cmd/crossplane/project/stop.go b/cmd/crossplane/project/stop.go index ae82f4b6..6c9bcd7f 100644 --- a/cmd/crossplane/project/stop.go +++ b/cmd/crossplane/project/stop.go @@ -36,19 +36,20 @@ import ( //go:embed help/stop.md var stopHelp string -// stopCmd tears down a local dev control plane. -type stopCmd struct { +// StopCmd tears down a local dev control plane. +type StopCmd struct { ProjectFile string `default:"${project_file}" help:"Path to project definition." short:"f"` ControlPlaneName string `help:"Name of the dev control plane. Defaults to project name."` RegistryDir string `help:"Directory for local registry images."` } -func (c *stopCmd) Help() string { +// Help returns help for the stop command. +func (c *StopCmd) Help() string { return stopHelp } // Run executes the stop command. -func (c *stopCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter) error { +func (c *StopCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter) error { ctx := context.Background() name := c.ControlPlaneName diff --git a/internal/docker/docker.go b/internal/docker/docker.go index c92df184..e249a39e 100644 --- a/internal/docker/docker.go +++ b/internal/docker/docker.go @@ -36,6 +36,7 @@ import ( "github.com/google/go-containerregistry/pkg/name" "github.com/moby/moby/api/pkg/stdcopy" "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/mount" "github.com/moby/moby/api/types/network" "github.com/moby/moby/client" "github.com/spf13/afero" @@ -230,6 +231,22 @@ type copyFilesConfig struct { // StartContainerOption provides optional options for StartContainer. type StartContainerOption func(*startContainerConfig) +// StartWithVolumeMount mounts a named Docker volume at the given path in the +// container, e.g. to give the container a cache that outlives it. +func StartWithVolumeMount(name, containerPath string) StartContainerOption { + return func(cfg *startContainerConfig) { + if cfg.hostConfig == nil { + cfg.hostConfig = &container.HostConfig{} + } + cfg.hostConfig.Mounts = append(cfg.hostConfig.Mounts, mount.Mount{ + Type: mount.TypeVolume, + Source: name, + Target: containerPath, + VolumeOptions: &mount.VolumeOptions{NoCopy: true}, + }) + } +} + // StartWithCommand sets the command to use when starting a container. func StartWithCommand(cmd []string) StartContainerOption { return func(cfg *startContainerConfig) { diff --git a/internal/project/build.go b/internal/project/build.go index b07f2a4f..0444feb7 100644 --- a/internal/project/build.go +++ b/internal/project/build.go @@ -23,6 +23,7 @@ import ( "fmt" "io" "io/fs" + "maps" "os" "path/filepath" "slices" @@ -127,9 +128,27 @@ func BuildWithTempDir(dir string) BuilderOption { type BuildOption func(o *buildOptions) type buildOptions struct { - log logging.Logger - projectBasePath string - eventCh async.EventChannel + log logging.Logger + projectBasePath string + eventCh async.EventChannel + extraAnnotations map[string]string + ignore *IgnoreMatcher +} + +// BuildWithExtraAnnotations adds annotations to the built packages' metadata, +// on the configuration and every embedded function. +func BuildWithExtraAnnotations(annotations map[string]string) BuildOption { + return func(o *buildOptions) { + o.extraAnnotations = annotations + } +} + +// BuildWithIgnore excludes files matching the given gitignore-style patterns +// (relative to the project root) from resource collection during the build. +func BuildWithIgnore(patterns []string) BuildOption { + return func(o *buildOptions) { + o.ignore = NewIgnoreMatcher(patterns) + } } // BuildWithLogger provides a logger for progress updates during the build. @@ -193,7 +212,7 @@ func (b *Builder) Build(ctx context.Context, project *devv1alpha1.Project, proje APIVersion: xpmetav1.SchemeGroupVersion.String(), Kind: xpmetav1.ConfigurationKind, }, - ObjectMeta: cfgMetaFromProject(project), + ObjectMeta: cfgMetaFromProject(project, o.extraAnnotations), Spec: xpmetav1.ConfigurationSpec{ MetaSpec: xpmetav1.MetaSpec{ Crossplane: project.Spec.Crossplane, @@ -240,7 +259,7 @@ func (b *Builder) Build(ctx context.Context, project *devv1alpha1.Project, proje xpv1.CompositionGroupVersionKind.String(), extv1alpha1.ManagedResourceActivationPolicyGroupVersionKind.String(), } - if err := collectResources(packageFS, apisSource, apiGVKs); err != nil { + if err := collectResources(packageFS, apisSource, apiGVKs, o.ignore); err != nil { o.eventCh.SendEvent("Collecting resources", async.EventStatusFailure) return nil, nil, errors.Wrap(err, "failed to collect API resources") } @@ -250,7 +269,7 @@ func (b *Builder) Build(ctx context.Context, project *devv1alpha1.Project, proje xpv1alpha1.WatchOperationGroupVersionKind.String(), xpv1alpha1.CronOperationGroupVersionKind.String(), } - if err := collectResources(packageFS, operationsSource, opsGVKs); err != nil { + if err := collectResources(packageFS, operationsSource, opsGVKs, o.ignore); err != nil { o.eventCh.SendEvent("Collecting resources", async.EventStatusFailure) return nil, nil, errors.Wrap(err, "failed to collect operation resources") } @@ -277,7 +296,7 @@ func (b *Builder) Build(ctx context.Context, project *devv1alpha1.Project, proje // Build the resolved functions. o.log.Debug("Building functions") - imgMap, deps, digests, err := b.buildFunctions(ctx, projectFS, project, fns, o.projectBasePath, o.eventCh) + imgMap, deps, digests, err := b.buildFunctions(ctx, projectFS, project, fns, o.projectBasePath, o.extraAnnotations, o.eventCh) if err != nil { return nil, nil, err } @@ -359,7 +378,7 @@ func resolveFunctions(project *devv1alpha1.Project, projectFS afero.Fs) ([]devv1 } // buildFunctions builds the given list of embedded functions. -func (b *Builder) buildFunctions(ctx context.Context, projectFS afero.Fs, project *devv1alpha1.Project, fns []devv1alpha1.Function, basePath string, eventCh async.EventChannel) (ImageTagMap, []xpmetav1.Dependency, map[string]name.Digest, error) { +func (b *Builder) buildFunctions(ctx context.Context, projectFS afero.Fs, project *devv1alpha1.Project, fns []devv1alpha1.Function, basePath string, extraAnnotations map[string]string, eventCh async.EventChannel) (ImageTagMap, []xpmetav1.Dependency, map[string]name.Digest, error) { var ( imgMap = make(map[name.Tag]v1.Image) imgMu sync.Mutex @@ -383,7 +402,7 @@ func (b *Builder) buildFunctions(ctx context.Context, projectFS afero.Fs, projec eventCh.SendEvent(eventText, async.EventStatusStarted) fnRepo := fmt.Sprintf("%s_%s", project.Spec.Repository, fnName) - imgs, err := b.buildFunction(ctx, projectFS, project, fn, basePath) + imgs, err := b.buildFunction(ctx, projectFS, project, fn, basePath, extraAnnotations) if err != nil { eventCh.SendEvent(eventText, async.EventStatusFailure) return errors.Wrapf(err, "failed to build function %q", fnName) @@ -443,14 +462,14 @@ func (b *Builder) buildFunctions(ctx context.Context, projectFS afero.Fs, projec // buildFunction builds the package images for a single function. It resolves // the function's runtime images (either by building from source or by loading // a pre-built tarball) and then wraps each one with the package metadata. -func (b *Builder) buildFunction(ctx context.Context, projectFS afero.Fs, project *devv1alpha1.Project, fn devv1alpha1.Function, basePath string) ([]v1.Image, error) { +func (b *Builder) buildFunction(ctx context.Context, projectFS afero.Fs, project *devv1alpha1.Project, fn devv1alpha1.Function, basePath string, extraAnnotations map[string]string) ([]v1.Image, error) { fnName := fn.Name() meta := &xpmetav1.Function{ TypeMeta: metav1.TypeMeta{ APIVersion: xpmetav1.SchemeGroupVersion.String(), Kind: xpmetav1.FunctionKind, }, - ObjectMeta: fnMetaFromProject(project, fnName), + ObjectMeta: fnMetaFromProject(project, fnName, extraAnnotations), Spec: xpmetav1.FunctionSpec{ MetaSpec: xpmetav1.MetaSpec{ Capabilities: []string{ @@ -687,7 +706,7 @@ func gzipOpener(fsys afero.Fs, path, tempDir string) (tarball.Opener, error) { func LoadCompositions(project *devv1alpha1.Project, projectFS afero.Fs) ([]*unstructured.Unstructured, error) { src := afero.NewBasePathFs(projectFS, project.Spec.Paths.APIs) compFS := afero.NewMemMapFs() - if err := collectResources(compFS, src, []string{xpv1.CompositionGroupVersionKind.String()}); err != nil { + if err := collectResources(compFS, src, []string{xpv1.CompositionGroupVersionKind.String()}, nil); err != nil { return nil, errors.Wrap(err, "failed to collect compositions") } @@ -710,13 +729,19 @@ func LoadCompositions(project *devv1alpha1.Project, projectFS afero.Fs) ([]*unst return comps, err } -func collectResources(toFS afero.Fs, fromFS afero.Fs, gvks []string) error { +func collectResources(toFS afero.Fs, fromFS afero.Fs, gvks []string, ignore *IgnoreMatcher) error { return afero.Walk(fromFS, "/", func(path string, info fs.FileInfo, err error) error { if err != nil { return err } if info.IsDir() { + if ignore.Match(path, true) { + return filepath.SkipDir + } + return nil + } + if ignore.Match(path, false) { return nil } ext := filepath.Ext(path) @@ -775,7 +800,7 @@ func runtimeDependencies(proj *devv1alpha1.Project) []xpmetav1.Dependency { return deps } -func cfgMetaFromProject(proj *devv1alpha1.Project) metav1.ObjectMeta { +func cfgMetaFromProject(proj *devv1alpha1.Project, extra map[string]string) metav1.ObjectMeta { meta := proj.ObjectMeta.DeepCopy() if meta.Annotations == nil { @@ -788,10 +813,12 @@ func cfgMetaFromProject(proj *devv1alpha1.Project) metav1.ObjectMeta { meta.Annotations["meta.crossplane.io/description"] = proj.Spec.Description meta.Annotations["meta.crossplane.io/readme"] = proj.Spec.Readme + maps.Copy(meta.Annotations, extra) + return *meta } -func fnMetaFromProject(proj *devv1alpha1.Project, fnName string) metav1.ObjectMeta { +func fnMetaFromProject(proj *devv1alpha1.Project, fnName string, extra map[string]string) metav1.ObjectMeta { meta := proj.ObjectMeta.DeepCopy() meta.Name = fmt.Sprintf("%s-%s", meta.Name, fnName) @@ -805,6 +832,8 @@ func fnMetaFromProject(proj *devv1alpha1.Project, fnName string) metav1.ObjectMe meta.Annotations["meta.crossplane.io/license"] = proj.Spec.License meta.Annotations["meta.crossplane.io/description"] = fmt.Sprintf("Function %s from project %s", fnName, proj.Name) + maps.Copy(meta.Annotations, extra) + return *meta } diff --git a/internal/project/ignore.go b/internal/project/ignore.go new file mode 100644 index 00000000..24e71ce3 --- /dev/null +++ b/internal/project/ignore.go @@ -0,0 +1,56 @@ +/* +Copyright 2026 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package project + +import ( + "strings" + + "github.com/go-git/go-git/v5/plumbing/format/gitignore" +) + +// IgnoreMatcher evaluates paths in a project to determine if they should be +// ignored during a build. Patterns follow gitignore semantics and are +// evaluated relative to the project root. +type IgnoreMatcher struct { + m gitignore.Matcher +} + +// NewIgnoreMatcher returns an IgnoreMatcher with the given patterns. It +// returns nil when there are no patterns; a nil matcher matches nothing. +func NewIgnoreMatcher(patterns []string) *IgnoreMatcher { + if len(patterns) == 0 { + return nil + } + + ps := make([]gitignore.Pattern, len(patterns)) + for i, p := range patterns { + ps[i] = gitignore.ParsePattern(p, nil) + } + + return &IgnoreMatcher{ + m: gitignore.NewMatcher(ps), + } +} + +// Match returns whether the given path should be ignored. Paths should be +// relative to the project root. +func (im *IgnoreMatcher) Match(p string, isDir bool) bool { + if im == nil { + return false + } + return im.m.Match(strings.Split(strings.Trim(p, "/"), "/"), isDir) +} diff --git a/internal/schemas/runner/run.go b/internal/schemas/runner/run.go index 1157d493..5a0bea97 100644 --- a/internal/schemas/runner/run.go +++ b/internal/schemas/runner/run.go @@ -43,6 +43,7 @@ type SchemaRunner interface { // RealSchemaRunner implements the SchemaRunner interface. type RealSchemaRunner struct { configStore xpkg.ConfigStore + envPrefixes []string } // NewRealSchemaRunner creates a new RealSchemaRunner. @@ -57,6 +58,16 @@ func NewRealSchemaRunner(opts ...ROption) *RealSchemaRunner { // ROption configures the SchemaRunner. type ROption func(*RealSchemaRunner) +// WithEnvPrefixes forwards environment variables with the given prefixes into +// the tooling container, in addition to the default CROSSPLANE_ prefix. Host +// CLIs use this to make their own variables visible to language tooling +// (e.g. tests reading credentials from the environment). +func WithEnvPrefixes(prefixes ...string) ROption { + return func(r *RealSchemaRunner) { + r.envPrefixes = append(r.envPrefixes, prefixes...) + } +} + // WithImageConfig adds image rewriting rules to the SchemaRunner. func WithImageConfig(cfgs []pkgv1beta1.ImageConfig) ROption { return func(r *RealSchemaRunner) { @@ -69,6 +80,9 @@ type GenerateOptions struct { CopyToPath string CopyFromPath string WorkDirectory string + + // VolumeMounts maps named Docker volumes to container paths. + VolumeMounts map[string]string } // Option is a function that modifies GenerateOptions. @@ -95,6 +109,17 @@ func WithWorkDirectory(dir string) Option { } } +// WithVolumeMount mounts a named Docker volume at the given path, e.g. to +// give the container a cache that outlives it. +func WithVolumeMount(name, containerPath string) Option { + return func(o *GenerateOptions) { + if o.VolumeMounts == nil { + o.VolumeMounts = make(map[string]string) + } + o.VolumeMounts[name] = containerPath + } +} + // DefaultGenerateOptions provides default values. func DefaultGenerateOptions() GenerateOptions { return GenerateOptions{ @@ -132,19 +157,28 @@ func (r RealSchemaRunner) Generate(ctx context.Context, fromFS afero.Fs, baseFol return errors.Wrapf(err, "failed to create tar from fs") } + prefixes := append([]string{"CROSSPLANE_"}, r.envPrefixes...) var envVars []string for _, e := range os.Environ() { - if strings.HasPrefix(e, "CROSSPLANE_") { - envVars = append(envVars, e) + for _, prefix := range prefixes { + if strings.HasPrefix(e, prefix) { + envVars = append(envVars, e) + break + } } } - cid, err := docker.StartContainer(ctx, "", imageName, + startOpts := []docker.StartContainerOption{ docker.StartWithCopyFiles(tarBuffer, o.CopyToPath), docker.StartWithCommand(command), docker.StartWithEnv(envVars...), docker.StartWithWorkingDirectory(o.WorkDirectory), - ) + } + for name, path := range o.VolumeMounts { + startOpts = append(startOpts, docker.StartWithVolumeMount(name, path)) + } + + cid, err := docker.StartContainer(ctx, "", imageName, startOpts...) if err != nil { return err } diff --git a/internal/xpkg/client.go b/internal/xpkg/client.go index 25d81848..9fe9ef1f 100644 --- a/internal/xpkg/client.go +++ b/internal/xpkg/client.go @@ -17,6 +17,7 @@ limitations under the License. package xpkg import ( + "github.com/google/go-containerregistry/pkg/authn" "github.com/spf13/afero" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -34,6 +35,7 @@ type options struct { cacheFs afero.Fs cacheDir string imageConfigs []pkgv1beta1.ImageConfig + keychain authn.Keychain } // ClientOption configures a new client. @@ -55,6 +57,14 @@ func WithImageConfigs(ics []pkgv1beta1.ImageConfig) ClientOption { } } +// WithAuthKeychain sets the authn.Keychain used to authenticate registry +// requests when fetching packages. Defaults to authn.DefaultKeychain. +func WithAuthKeychain(k authn.Keychain) ClientOption { + return func(o *options) { + o.keychain = k + } +} + // NewClient assembles a Client that fetches packages from remote registries. func NewClient(opts ...ClientOption) (Client, error) { o := &options{} @@ -79,8 +89,13 @@ func NewClient(opts ...ClientOption) (Client, error) { cache = xpkg.NewFsPackageCache(o.cacheDir, o.cacheFs) } + fetcherOpts := []RemoteFetcherOption{} + if o.keychain != nil { + fetcherOpts = append(fetcherOpts, WithKeychain(o.keychain)) + } + return WrapClient(xpkg.NewCachedClient( - NewRemoteFetcher(), + NewRemoteFetcher(fetcherOpts...), parser.New(metaScheme, objScheme), cache, NewStaticImageConfigStore(o.imageConfigs), diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go new file mode 100644 index 00000000..9f98c66f --- /dev/null +++ b/pkg/cli/cli.go @@ -0,0 +1,127 @@ +/* +Copyright 2026 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package cli exposes the minimal surface a host CLI needs to embed the +// crossplane CLI's command packages (e.g. cmd/crossplane/project) in its own +// Kong command tree: the types bound into Kong's context by the crossplane +// CLI's main, constructors for them, and the kong.Vars its command struct +// tags reference. +package cli + +import ( + "context" + "io" + + "github.com/alecthomas/kong" + "github.com/spf13/afero" + + "github.com/crossplane/crossplane-runtime/v2/pkg/logging" + runtimexpkg "github.com/crossplane/crossplane-runtime/v2/pkg/xpkg" + + "github.com/crossplane/cli/v2/internal/async" + "github.com/crossplane/cli/v2/internal/config" + "github.com/crossplane/cli/v2/internal/exitcode" + "github.com/crossplane/cli/v2/internal/maturity" + "github.com/crossplane/cli/v2/internal/terminal" + clixpkg "github.com/crossplane/cli/v2/internal/xpkg" +) + +// SpinnerPrinter prints progress spinners to the console. The crossplane +// commands receive one via Kong binding; see BindRuntime. +type SpinnerPrinter = terminal.SpinnerPrinter + +// Config is the crossplane CLI configuration, carrying feature flags that +// affect command behavior. The crossplane commands receive a *Config via Kong +// binding; see Options. +type Config = config.Config + +// EventChannel carries asynchronous progress events during long-running +// operations such as builds. +type EventChannel = async.EventChannel + +// NewSpinnerPrinter returns a SpinnerPrinter writing to out. If pretty is +// true, animated spinners are rendered; otherwise plain text. +func NewSpinnerPrinter(out io.Writer, pretty bool) SpinnerPrinter { + return terminal.NewSpinnerPrinter(out, pretty) +} + +// NewNopSpinnerPrinter returns a SpinnerPrinter that renders nothing and just +// runs the wrapped functions. +func NewNopSpinnerPrinter() SpinnerPrinter { + return terminal.NewNopSpinnerPrinter() +} + +// LoadConfig loads the crossplane CLI configuration from path. A missing file +// yields a zero-value configuration, not an error. +func LoadConfig(fs afero.Fs, path string) (*Config, error) { + return config.Load(fs, path) +} + +// ResolveConfigPath resolves the configuration file path from the given flag +// value, falling back to the CROSSPLANE_CLI_CONFIG environment variable and +// then the default per-user location. +func ResolveConfigPath(flag string) string { + return config.ResolvePath(flag) +} + +// Vars returns the kong.Vars referenced by the crossplane command structs' +// tags (e.g. ${project_file}). Hosts must include them in their parser +// options or kong.Must will fail at startup. +func Vars() kong.Vars { + return kong.Vars{ + "project_file": clixpkg.ProjectFile, + "package_metadata_file": runtimexpkg.MetaFile, + } +} + +// Options returns the parse-time kong options required to embed crossplane +// commands: Vars plus bindings for the logger and configuration their Run +// methods take. Append these to the host's own kong.Must options. +func Options(logger logging.Logger, cfg *Config) []kong.Option { + return []kong.Option{ + Vars(), + kong.BindTo(logger, (*logging.Logger)(nil)), + kong.Bind(cfg), + } +} + +// BindRuntime performs the post-parse bindings the crossplane CLI's main does +// before running a command: the signal-aware context and the spinner printer. +func BindRuntime(sigCtx context.Context, k *kong.Context, sp SpinnerPrinter) { + k.BindTo(sp, (*SpinnerPrinter)(nil)) + k.BindTo(sigCtx, (*context.Context)(nil)) +} + +// SilentExit reports whether err is a silent exit request from a command (a +// result such as simulate finding changes, not an error) and the status to +// exit with. Hosts embedding crossplane commands should exit with the code +// without printing anything when it returns true. +func SilentExit(err error) (int, bool) { + return exitcode.SilentExit(err) +} + +// ApplyMaturity applies the crossplane CLI's maturity gating to a host +// CLI's kong model based on the feature flags in cfg: beta commands are +// enabled by default, alpha commands must be enabled explicitly, and non-GA +// commands get a maturity banner in their help. Hosts embedding crossplane +// commands call this after constructing their parser so embedded commands +// are gated by the same configuration file as the crossplane CLI itself. +func ApplyMaturity(app *kong.Application, cfg *Config) { + maturity.Apply(app, map[maturity.Level]bool{ + maturity.LevelBeta: !cfg.Features.DisableBeta, + maturity.LevelAlpha: cfg.Features.EnableAlpha, + }) +} diff --git a/pkg/cli/cli_test.go b/pkg/cli/cli_test.go new file mode 100644 index 00000000..60e32388 --- /dev/null +++ b/pkg/cli/cli_test.go @@ -0,0 +1,74 @@ +/* +Copyright 2026 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cli_test + +import ( + "context" + "io" + "testing" + + "github.com/alecthomas/kong" + + "github.com/crossplane/crossplane-runtime/v2/pkg/logging" + + "github.com/crossplane/cli/v2/cmd/crossplane/project" + "github.com/crossplane/cli/v2/pkg/cli" +) + +// bindingProbe takes the same bindings the embedded crossplane commands' +// Run methods take, so running it proves a host parser configured with +// Options and BindRuntime can resolve them all. +type bindingProbe struct{} + +func (c *bindingProbe) Run(_ context.Context, _ logging.Logger, _ cli.SpinnerPrinter, _ *cli.Config) error { + return nil +} + +type hostCLI struct { + Project project.Cmd `cmd:""` + Probe bindingProbe `cmd:""` +} + +// TestHostEmbedding verifies that a host CLI embedding the crossplane project +// commands can construct a parser with Options (kong.Vars present), parse a +// project command, and resolve every binding its Run methods require. +func TestHostEmbedding(t *testing.T) { + cfg := &cli.Config{} + logger := logging.NewNopLogger() + + parser, err := kong.New(&hostCLI{}, + append([]kong.Option{kong.Name("host"), kong.Exit(func(int) {})}, + cli.Options(logger, cfg)...)...) + if err != nil { + t.Fatalf("kong.New() with cli.Options() failed: %v", err) + } + + // Parsing a project subcommand exercises the ${project_file} var. + if _, err := parser.Parse([]string{"project", "stop"}); err != nil { + t.Fatalf("parser.Parse(project stop) failed: %v", err) + } + + // Running the probe exercises every binding type. + k, err := parser.Parse([]string{"probe"}) + if err != nil { + t.Fatalf("parser.Parse(probe) failed: %v", err) + } + cli.BindRuntime(context.Background(), k, cli.NewSpinnerPrinter(io.Discard, false)) + if err := k.Run(); err != nil { + t.Fatalf("k.Run() failed to resolve bindings: %v", err) + } +} diff --git a/pkg/project/project.go b/pkg/project/project.go new file mode 100644 index 00000000..00a3bf2b --- /dev/null +++ b/pkg/project/project.go @@ -0,0 +1,333 @@ +/* +Copyright 2026 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package project exposes the crossplane CLI's project machinery to host +// CLIs: parsing project files, building projects into package images, and +// generating language schemas. Terminal, configuration, and Kong binding +// helpers live in pkg/cli. +package project + +import ( + "cmp" + "context" + "os" + + "github.com/google/go-containerregistry/pkg/authn" + "github.com/google/go-containerregistry/pkg/name" + v1 "github.com/google/go-containerregistry/pkg/v1" + "github.com/spf13/afero" + + "github.com/crossplane/crossplane-runtime/v2/pkg/errors" + "github.com/crossplane/crossplane-runtime/v2/pkg/logging" + + pkgv1beta1 "github.com/crossplane/crossplane/apis/v2/pkg/v1beta1" + + devv1alpha1 "github.com/crossplane/cli/v2/apis/dev/v1alpha1" + "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/git" + internalproject "github.com/crossplane/cli/v2/internal/project" + "github.com/crossplane/cli/v2/internal/project/functions" + "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" + clixpkg "github.com/crossplane/cli/v2/internal/xpkg" +) + +// Config is the crossplane CLI configuration; see pkg/cli for loading it. +type Config = config.Config + +// EventChannel carries asynchronous progress events during long-running +// operations such as builds. +type EventChannel = async.EventChannel + +// FileName is the default name of a project's definition file. +const FileName = clixpkg.ProjectFile + +// APIVersion and Kind identify the project file's type. +const ( + APIVersion = projectfile.APIVersion + Kind = projectfile.Kind +) + +// ImageTagMap maps image tags to the images that should be pushed to them, +// as produced by a project build. +type ImageTagMap = internalproject.ImageTagMap + +// ParseProject reads, validates, and defaults the project file at +// projFilePath within projFS. +func ParseProject(projFS afero.Fs, projFilePath string) (*devv1alpha1.Project, error) { + return projectfile.Parse(projFS, projFilePath) +} + +// SortImages analyzes an image map produced by a project build and picks out +// the configuration image and the embedded function images, grouped by the +// repository they belong to. +func SortImages(imgMap ImageTagMap, repo string) (v1.Image, map[name.Repository][]v1.Image, error) { + return internalproject.SortImages(imgMap, repo) +} + +// BuildOptions configures a project build. +type BuildOptions struct { + // ProjectFile is the path of the project file, used to resolve + // dependency updates. Defaults to the ProjectFile constant. + ProjectFile string + // MaxConcurrency bounds concurrent embedded function builds. Defaults + // to 8. + MaxConcurrency uint + // CacheDir is the xpkg dependency cache directory. Defaults to the + // standard per-user crossplane cache location. + CacheDir string + // Keychain authenticates registry requests when fetching dependencies. + // Defaults to the standard container credential helpers. + Keychain authn.Keychain + // GitUsername and GitToken authenticate git-based dependencies over + // HTTPS. + GitUsername string + GitToken string + // EventChannel receives progress events during the build, if set. + EventChannel async.EventChannel + // Logger receives debug output during the build, if set. + Logger logging.Logger + // BasePath is the real on-disk base path of projFS, needed to build + // embedded functions whose runtimes are built with Docker. + BasePath string + // ExtraPackageAnnotations adds annotations to the built packages' + // metadata, on the configuration and every embedded function. + ExtraPackageAnnotations map[string]string + // Ignore excludes files matching the given gitignore-style patterns + // (relative to the project root) from resource collection. + Ignore []string +} + +// Build builds a project into its package images, generating schemas and +// resolving dependencies the same way `crossplane project build` does. It +// returns the built images by tag along with the digests of any dependencies +// that were resolved during the build. +func Build(ctx context.Context, cfg *config.Config, proj *devv1alpha1.Project, projFS afero.Fs, o BuildOptions) (ImageTagMap, map[string]name.Digest, error) { + logger := o.Logger + if logger == nil { + logger = logging.NewNopLogger() + } + + schemasFS := afero.NewBasePathFs(projFS, proj.Spec.Paths.Schemas) + generators := generator.Filter( + generator.AllLanguages( + generator.WithGoModelAccessors(cfg.Features.GenerateGoModelAccessors), + generator.WithGoRuntimeObjects(cfg.Features.GenerateGoRuntimeObjects), + ), + proj.Spec.Schemas.GetLanguages(), + ) + schemaRunner := runner.NewRealSchemaRunner(runner.WithImageConfig(proj.Spec.ImageConfigs)) + schemaMgr := manager.New(schemasFS, generators, schemaRunner) + + clientOpts := []clixpkg.ClientOption{ + clixpkg.WithCacheDir(afero.NewOsFs(), cmp.Or(o.CacheDir, dependency.DefaultCacheDir())), + clixpkg.WithImageConfigs(proj.Spec.ImageConfigs), + } + if o.Keychain != nil { + clientOpts = append(clientOpts, clixpkg.WithAuthKeychain(o.Keychain)) + } + client, err := clixpkg.NewClient(clientOpts...) + if err != nil { + return nil, nil, errors.Wrap(err, "cannot create xpkg client") + } + + depOpts := []dependency.ManagerOption{ + dependency.WithProjectFile(cmp.Or(o.ProjectFile, FileName)), + dependency.WithSchemaFS(schemasFS), + dependency.WithSchemaGenerators(generators), + dependency.WithSchemaRunner(schemaRunner), + dependency.WithXpkgClient(client), + } + if o.GitToken != "" { + depOpts = append(depOpts, dependency.WithGitAuthProvider(&git.HTTPSAuthProvider{ + Username: o.GitUsername, + Password: o.GitToken, + })) + } + depMgr := dependency.NewManager(proj, projFS, depOpts...) + + // The builder may decompress function runtime tarballs into this + // directory; the built images read from it lazily, so it is only removed + // once the images have been consumed. Callers that push or sideload the + // returned images synchronously are unaffected. + tempDir, err := os.MkdirTemp("", "crossplane-build-") + if err != nil { + return nil, nil, errors.Wrap(err, "failed to create temporary build directory") + } + + b := internalproject.NewBuilder( + internalproject.BuildWithMaxConcurrency(cmp.Or(o.MaxConcurrency, 8)), + internalproject.BuildWithFunctionIdentifier(functions.DefaultIdentifier), + internalproject.BuildWithSchemaManager(schemaMgr), + internalproject.BuildWithDependencyManager(depMgr), + internalproject.BuildWithTempDir(tempDir), + internalproject.BuildWithBaseImageCacheDir(functions.DefaultBaseImageCacheDir()), + ) + + buildOpts := []internalproject.BuildOption{ + internalproject.BuildWithLogger(logger), + } + if o.EventChannel != nil { + buildOpts = append(buildOpts, internalproject.BuildWithEventChannel(o.EventChannel)) + } + if o.BasePath != "" { + buildOpts = append(buildOpts, internalproject.BuildWithProjectBasePath(o.BasePath)) + } + if len(o.ExtraPackageAnnotations) > 0 { + buildOpts = append(buildOpts, internalproject.BuildWithExtraAnnotations(o.ExtraPackageAnnotations)) + } + if len(o.Ignore) > 0 { + buildOpts = append(buildOpts, internalproject.BuildWithIgnore(o.Ignore)) + } + + imgMap, deps, err := b.Build(ctx, proj, projFS, buildOpts...) + if err != nil { + _ = os.RemoveAll(tempDir) + return nil, nil, err + } + + return imgMap, deps, nil +} + +// SchemaSource is a source of resources (CRDs/XRDs) for which language +// schemas can be generated. +type SchemaSource = manager.Source + +// SchemaSourceType identifies the kind of resources a SchemaSource provides. +type SchemaSourceType = manager.SourceType + +// SchemaSourceTypeCRD indicates a source containing CRDs/XRDs. +const SchemaSourceTypeCRD = manager.SourceTypeCRD + +// AddSchemas generates language schemas for the resources provided by src +// into the project's schemas directory, using the same generators and layout +// as schema generation for the project's dependencies and APIs. Host CLIs +// use this to merge schemas for their own meta APIs into the project's +// models. +func AddSchemas(ctx context.Context, cfg *config.Config, proj *devv1alpha1.Project, projFS afero.Fs, src SchemaSource) error { + schemasFS := afero.NewBasePathFs(projFS, proj.Spec.Paths.Schemas) + generators := generator.Filter( + generator.AllLanguages( + generator.WithGoModelAccessors(cfg.Features.GenerateGoModelAccessors), + generator.WithGoRuntimeObjects(cfg.Features.GenerateGoRuntimeObjects), + ), + proj.Spec.Schemas.GetLanguages(), + ) + schemaRunner := runner.NewRealSchemaRunner(runner.WithImageConfig(proj.Spec.ImageConfigs)) + + return manager.New(schemasFS, generators, schemaRunner).Add(ctx, src) +} + +// SchemaRunner runs language tooling in containers over a filesystem. It is +// used for schema generation and by host CLIs to execute language-based +// tests. +type SchemaRunner = runner.SchemaRunner + +// SchemaRunnerOption configures a single SchemaRunner.Generate invocation. +type SchemaRunnerOption = runner.Option + +// NewSchemaRunner returns a SchemaRunner that rewrites tooling images +// according to the given image configs. Environment variables with the given +// extra prefixes are forwarded into the tooling containers in addition to +// the default CROSSPLANE_ prefix. +func NewSchemaRunner(cfgs []pkgv1beta1.ImageConfig, extraEnvPrefixes ...string) SchemaRunner { + return runner.NewRealSchemaRunner( + runner.WithImageConfig(cfgs), + runner.WithEnvPrefixes(extraEnvPrefixes...), + ) +} + +// WithWorkDirectory sets the working directory for a Generate invocation. +func WithWorkDirectory(dir string) SchemaRunnerOption { + return runner.WithWorkDirectory(dir) +} + +// WithCopyToPath sets the container path the input filesystem is copied to. +func WithCopyToPath(path string) SchemaRunnerOption { + return runner.WithCopyToPath(path) +} + +// WithCopyFromPath sets the container path results are copied back from. +func WithCopyFromPath(path string) SchemaRunnerOption { + return runner.WithCopyFromPath(path) +} + +// WithVolumeMount mounts a named Docker volume at the given container path, +// e.g. to give the container a cache that outlives it. +func WithVolumeMount(name, containerPath string) SchemaRunnerOption { + return runner.WithVolumeMount(name, containerPath) +} + +// DependencySchemasOptions configures DependencySchemas. +type DependencySchemasOptions struct { + // GitUsername and GitToken authenticate git-based dependencies over + // HTTPS. + GitUsername string + GitToken string +} + +// DependencySchemaSet holds the schema inputs provided by a project's crd +// and k8s dependencies. +type DependencySchemaSet struct { + // CRDs are filesystems of CustomResourceDefinition manifests, one per + // crd-type dependency. + CRDs []afero.Fs + // OpenAPIs are filesystems of OpenAPI v3 documents, one per k8s-type + // dependency. + OpenAPIs []afero.Fs +} + +// DependencySchemas fetches the raw schema resources of the project's crd +// and k8s dependencies - the same sources the dependency manager generates +// language schemas from. Hosts use them e.g. to validate rendered resources. +func DependencySchemas(ctx context.Context, proj *devv1alpha1.Project, o DependencySchemasOptions) (*DependencySchemaSet, error) { + authProvider := &git.HTTPSAuthProvider{ + Username: o.GitUsername, + Password: o.GitToken, + } + + set := &DependencySchemaSet{} + for _, dep := range proj.Spec.Dependencies { + var src manager.Source + switch { + case dep.Git != nil: + src = manager.NewGitSource(dep, &git.DefaultCloner{}, authProvider) + case dep.HTTP != nil: + src = manager.NewHTTPSource(dep) + case dep.K8s != nil: + src = manager.NewK8sSource(dep) + default: + continue + } + + fs, err := src.Resources(ctx) + if err != nil { + return nil, errors.Wrapf(err, "failed to fetch resources of dependency %q", src.ID()) + } + switch src.Type() { + case manager.SourceTypeOpenAPI: + set.OpenAPIs = append(set.OpenAPIs, fs) + case manager.SourceTypeCRD: + set.CRDs = append(set.CRDs, fs) + } + } + + return set, nil +}