From 050107ea4ca39b2a19345d35ef27745528dbd7bc Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Tue, 17 Nov 2026 11:46:39 +0100 Subject: [PATCH 1/8] Export project subcommand types for library consumers Rename initCmd/buildCmd/pushCmd/runCmd/simulateCmd/stopCmd to exported names so other CLIs can compose individual project subcommands into their own Kong command trees instead of reimplementing them. Signed-off-by: Christopher Haar --- cmd/crossplane/project/build.go | 11 ++++--- cmd/crossplane/project/build_test.go | 2 +- cmd/crossplane/project/init.go | 12 ++++--- cmd/crossplane/project/project.go | 12 +++---- cmd/crossplane/project/push.go | 13 ++++---- cmd/crossplane/project/run.go | 11 ++++--- cmd/crossplane/project/simulate.go | 11 ++++--- cmd/crossplane/project/simulate_test.go | 44 ++++++++++++------------- cmd/crossplane/project/stop.go | 9 ++--- 9 files changed, 66 insertions(+), 59 deletions(-) diff --git a/cmd/crossplane/project/build.go b/cmd/crossplane/project/build.go index 8e24b2774..81fc73fc4 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 54979ef5c..a8fe86fb4 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 3c7409602..995b70f59 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 5ec95df79..432fd57a9 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 a562cdd1b..4eb39f19c 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 71eeb8ef8..1e6c74f7f 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 5b264fdaf..114f60925 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 b8c160bee..1eef5867f 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 ae82f4b6e..6c9bcd7fb 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 From 93e991715659ad9b7b0e7d635fe3347ec0f635ee Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Tue, 17 Nov 2026 11:52:01 +0100 Subject: [PATCH 2/8] Add pkg/cli facade for other CLIs embedding crossplane commands other CLIs can embed the exported command structs from cmd/crossplane in their own Kong trees, but those commands' Run methods take types from internal packages (terminal.SpinnerPrinter, config.Config) and reference kong.Vars set up by crossplane's main. pkg/cli exposes exactly that contract: type aliases, constructors, Vars/Options/BindRuntime helpers, and a Build/ParseProject/SortImages facade so hosts can build projects in-process and consume the resulting image map directly. Also expose a keychain option on the xpkg client so hosts can authenticate dependency fetches from private registries. Signed-off-by: Christopher Haar --- internal/xpkg/client.go | 17 +++- pkg/cli/build.go | 171 ++++++++++++++++++++++++++++++++++++++++ pkg/cli/cli.go | 142 +++++++++++++++++++++++++++++++++ pkg/cli/cli_test.go | 97 +++++++++++++++++++++++ 4 files changed, 426 insertions(+), 1 deletion(-) create mode 100644 pkg/cli/build.go create mode 100644 pkg/cli/cli.go create mode 100644 pkg/cli/cli_test.go diff --git a/internal/xpkg/client.go b/internal/xpkg/client.go index 25d818480..9fe9ef1f1 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/build.go b/pkg/cli/build.go new file mode 100644 index 000000000..cfc0d66ed --- /dev/null +++ b/pkg/cli/build.go @@ -0,0 +1,171 @@ +/* +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 + +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" + + devv1alpha1 "github.com/crossplane/cli/v2/apis/dev/v1alpha1" + "github.com/crossplane/cli/v2/internal/dependency" + "github.com/crossplane/cli/v2/internal/git" + "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" +) + +// ImageTagMap maps image tags to the images that should be pushed to them, +// as produced by a project build. +type ImageTagMap = project.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 project.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 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 +} + +// 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, 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, ProjectFile)), + 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 := project.NewBuilder( + project.BuildWithMaxConcurrency(cmp.Or(o.MaxConcurrency, 8)), + project.BuildWithFunctionIdentifier(functions.DefaultIdentifier), + project.BuildWithSchemaManager(schemaMgr), + project.BuildWithDependencyManager(depMgr), + project.BuildWithTempDir(tempDir), + project.BuildWithBaseImageCacheDir(functions.DefaultBaseImageCacheDir()), + ) + + buildOpts := []project.BuildOption{ + project.BuildWithLogger(logger), + } + if o.EventChannel != nil { + buildOpts = append(buildOpts, project.BuildWithEventChannel(o.EventChannel)) + } + if o.BasePath != "" { + buildOpts = append(buildOpts, project.BuildWithProjectBasePath(o.BasePath)) + } + + imgMap, deps, err := b.Build(ctx, proj, projFS, buildOpts...) + if err != nil { + _ = os.RemoveAll(tempDir) + return nil, nil, err + } + + return imgMap, deps, nil +} diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go new file mode 100644 index 000000000..4f530e51a --- /dev/null +++ b/pkg/cli/cli.go @@ -0,0 +1,142 @@ +/* +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/maturity" + "github.com/crossplane/cli/v2/internal/project/projectfile" + "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 + +// ProjectFile is the default name of a project's definition file. +const ProjectFile = clixpkg.ProjectFile + +// ProjectAPIVersion and ProjectKind identify the project file's type. +const ( + ProjectAPIVersion = projectfile.APIVersion + ProjectKind = projectfile.Kind +) + +// 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 the default configuration (current version, all feature flags +// off), 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_CONFIG environment variable and then +// the default per-user location, the same lookup order as the standalone +// crossplane CLI. +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. +// +// Kong variables are parser-wide: these keys (project_file, +// package_metadata_file) are shared with the host's own commands. A host +// that uses the same variable names with different values should pass its +// own kong.Vars after these options, since later values win. +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 spinner printer and the signal-aware context. +// +//nolint:revive // The kong.Context is the subject here; it reads better first. +func BindRuntime(k *kong.Context, sp SpinnerPrinter, sigCtx context.Context) { + k.BindTo(sp, (*SpinnerPrinter)(nil)) + k.BindTo(sigCtx, (*context.Context)(nil)) +} + +// 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 +// shown by default, alpha commands are hidden from help until enabled (an +// explicitly typed command name still runs), 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. +// +// Gating walks the entire kong model, so host commands carrying their own +// maturity tags are gated by the same flags; hosts that gate their commands +// differently should not reuse the maturity tag. +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 000000000..5a6c996cc --- /dev/null +++ b/pkg/cli/cli_test.go @@ -0,0 +1,97 @@ +/* +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/google/go-cmp/cmp" + + "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. It records what it received +// so the test can check the host's values arrive, not just values of the +// right types. +type bindingProbe struct { + gotCtx context.Context + gotCfg *cli.Config +} + +func (c *bindingProbe) Run(ctx context.Context, _ logging.Logger, _ cli.SpinnerPrinter, cfg *cli.Config) error { + c.gotCtx = ctx + c.gotCfg = cfg + 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() + host := &hostCLI{Probe: &bindingProbe{}} + + parser, err := kong.New(host, + 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) + } + + t.Run("ProjectVarsInterpolate", func(t *testing.T) { + // Parsing a project subcommand exercises the ${project_file} var; a + // wrong value would still parse, so check the interpolated default. + if _, err := parser.Parse([]string{"project", "stop"}); err != nil { + t.Fatalf("parser.Parse(project stop) failed: %v", err) + } + if diff := cmp.Diff("crossplane-project.yaml", host.Project.Stop.ProjectFile); diff != "" { + t.Errorf("project stop --project-file default: -want, +got:\n%s", diff) + } + }) + + t.Run("RuntimeBindingsResolve", func(t *testing.T) { + k, err := parser.Parse([]string{"probe"}) + if err != nil { + t.Fatalf("parser.Parse(probe) failed: %v", err) + } + ctx := context.Background() + cli.BindRuntime(k, cli.NewSpinnerPrinter(io.Discard, false), ctx) + if err := k.Run(); err != nil { + t.Fatalf("k.Run() failed to resolve bindings: %v", err) + } + if host.Probe.gotCfg != cfg { + t.Error("probe received a different *cli.Config than the host supplied") + } + if host.Probe.gotCtx != ctx { + t.Error("probe received a different context.Context than the host bound") + } + }) +} From e3b3543f453207f9917a1305dcd88d38fd3928e4 Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Tue, 17 Nov 2026 16:24:39 +0100 Subject: [PATCH 3/8] Add AddSchemas to pkg/cli for host meta-API schema generation Host CLIs have their own meta APIs whose models must merge into the same generated models module as the project's dependencies and APIs, so language tests can import both. Expose the schema manager's Add through the facade. Signed-off-by: Christopher Haar --- pkg/cli/build.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pkg/cli/build.go b/pkg/cli/build.go index cfc0d66ed..142457fee 100644 --- a/pkg/cli/build.go +++ b/pkg/cli/build.go @@ -169,3 +169,32 @@ func Build(ctx context.Context, cfg *Config, proj *devv1alpha1.Project, projFS a 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, 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) +} From 33a03f34cd7203b5023c1b01ebf9a77c1b55b076 Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Tue, 17 Nov 2026 16:46:27 +0100 Subject: [PATCH 4/8] Move project machinery from pkg/cli to new pkg/project pkg/cli keeps the terminal, configuration, and Kong binding helpers for embedding crossplane commands; the project-specific surface (parse, build, schema generation) moves to pkg/project. Also export the schema runner so host CLIs can execute language tooling (e.g. language-based tests) with the same container machinery used for schema generation. Signed-off-by: Christopher Haar --- apis/dev/v1alpha1/project_types.go | 8 ++ internal/docker/docker.go | 17 +++ internal/project/projectfile/projectfile.go | 4 +- internal/schemas/runner/run.go | 23 +++- pkg/cli/cli.go | 10 -- pkg/{cli/build.go => project/project.go} | 126 +++++++++++++++----- 6 files changed, 143 insertions(+), 45 deletions(-) rename pkg/{cli/build.go => project/project.go} (57%) diff --git a/apis/dev/v1alpha1/project_types.go b/apis/dev/v1alpha1/project_types.go index 7cf16094f..2acf87d1c 100644 --- a/apis/dev/v1alpha1/project_types.go +++ b/apis/dev/v1alpha1/project_types.go @@ -68,6 +68,14 @@ func SupportedSchemaLanguages() []string { } } +// APIVersion and Kind identify a Project in its serialized form. +const ( + // APIVersion is the apiVersion of a Project file. + APIVersion = "dev.crossplane.io/v1alpha1" + // Kind is the kind of a Project file. + Kind = "Project" +) + // Project defines a Crossplane Project, which can be built into a Crossplane // Configuration package. // diff --git a/internal/docker/docker.go b/internal/docker/docker.go index c92df184c..e249a39e0 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/projectfile/projectfile.go b/internal/project/projectfile/projectfile.go index ffd6a7e59..3b663a1ad 100644 --- a/internal/project/projectfile/projectfile.go +++ b/internal/project/projectfile/projectfile.go @@ -29,9 +29,9 @@ import ( const ( // APIVersion is the supported API version for project files. - APIVersion = "dev.crossplane.io/v1alpha1" + APIVersion = v1alpha1.APIVersion // Kind is the supported Kind for project files. - Kind = "Project" + Kind = v1alpha1.Kind ) // IsProjectFile reads the TypeMeta from the given YAML file and returns true diff --git a/internal/schemas/runner/run.go b/internal/schemas/runner/run.go index 1157d493a..5e79033b7 100644 --- a/internal/schemas/runner/run.go +++ b/internal/schemas/runner/run.go @@ -69,6 +69,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 +98,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{ @@ -139,12 +153,17 @@ func (r RealSchemaRunner) Generate(ctx context.Context, fromFS afero.Fs, baseFol } } - 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/pkg/cli/cli.go b/pkg/cli/cli.go index 4f530e51a..0f491cee2 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -34,7 +34,6 @@ import ( "github.com/crossplane/cli/v2/internal/async" "github.com/crossplane/cli/v2/internal/config" "github.com/crossplane/cli/v2/internal/maturity" - "github.com/crossplane/cli/v2/internal/project/projectfile" "github.com/crossplane/cli/v2/internal/terminal" clixpkg "github.com/crossplane/cli/v2/internal/xpkg" ) @@ -52,15 +51,6 @@ type Config = config.Config // operations such as builds. type EventChannel = async.EventChannel -// ProjectFile is the default name of a project's definition file. -const ProjectFile = clixpkg.ProjectFile - -// ProjectAPIVersion and ProjectKind identify the project file's type. -const ( - ProjectAPIVersion = projectfile.APIVersion - ProjectKind = projectfile.Kind -) - // 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 { diff --git a/pkg/cli/build.go b/pkg/project/project.go similarity index 57% rename from pkg/cli/build.go rename to pkg/project/project.go index 142457fee..91b393677 100644 --- a/pkg/cli/build.go +++ b/pkg/project/project.go @@ -14,7 +14,11 @@ See the License for the specific language governing permissions and limitations under the License. */ -package cli +// 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" @@ -29,10 +33,14 @@ import ( "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" - "github.com/crossplane/cli/v2/internal/project" + 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" @@ -41,9 +49,25 @@ import ( 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 = devv1alpha1.APIVersion + Kind = devv1alpha1.Kind +) + // ImageTagMap maps image tags to the images that should be pushed to them, // as produced by a project build. -type ImageTagMap = project.ImageTagMap +type ImageTagMap = internalproject.ImageTagMap // ParseProject reads, validates, and defaults the project file at // projFilePath within projFS. @@ -55,14 +79,11 @@ func ParseProject(projFS afero.Fs, projFilePath string) (*devv1alpha1.Project, e // 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 project.SortImages(imgMap, repo) + 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 @@ -77,7 +98,7 @@ type BuildOptions struct { GitUsername string GitToken string // EventChannel receives progress events during the build, if set. - EventChannel EventChannel + 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 @@ -87,9 +108,13 @@ type BuildOptions struct { // 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, proj *devv1alpha1.Project, projFS afero.Fs, o BuildOptions) (ImageTagMap, map[string]name.Digest, error) { +// returns the built images by tag, the digests of the embedded function +// image indexes keyed by function repository (dependencies resolved from +// registries are not included), and a cleanup function releasing the build's +// temporary files. The images read from those files lazily, so call cleanup +// only after the images have been consumed (pushed, sideloaded, or written); +// it is safe to call always, including on error. +func Build(ctx context.Context, cfg *config.Config, proj *devv1alpha1.Project, projFS afero.Fs, o BuildOptions) (ImageTagMap, map[string]name.Digest, func(), error) { logger := o.Logger if logger == nil { logger = logging.NewNopLogger() @@ -115,11 +140,11 @@ func Build(ctx context.Context, cfg *Config, proj *devv1alpha1.Project, projFS a } client, err := clixpkg.NewClient(clientOpts...) if err != nil { - return nil, nil, errors.Wrap(err, "cannot create xpkg client") + return nil, nil, noopCleanup, errors.Wrap(err, "cannot create xpkg client") } depOpts := []dependency.ManagerOption{ - dependency.WithProjectFile(cmp.Or(o.ProjectFile, ProjectFile)), + dependency.WithProjectFile(FileName), dependency.WithSchemaFS(schemasFS), dependency.WithSchemaGenerators(generators), dependency.WithSchemaRunner(schemaRunner), @@ -135,41 +160,45 @@ func Build(ctx context.Context, cfg *Config, proj *devv1alpha1.Project, projFS a // 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. + // by the returned cleanup function, once the images have been consumed. tempDir, err := os.MkdirTemp("", "crossplane-build-") if err != nil { - return nil, nil, errors.Wrap(err, "failed to create temporary build directory") + return nil, nil, noopCleanup, errors.Wrap(err, "failed to create temporary build directory") } + cleanup := func() { _ = os.RemoveAll(tempDir) } - b := project.NewBuilder( - project.BuildWithMaxConcurrency(cmp.Or(o.MaxConcurrency, 8)), - project.BuildWithFunctionIdentifier(functions.DefaultIdentifier), - project.BuildWithSchemaManager(schemaMgr), - project.BuildWithDependencyManager(depMgr), - project.BuildWithTempDir(tempDir), - project.BuildWithBaseImageCacheDir(functions.DefaultBaseImageCacheDir()), + 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 := []project.BuildOption{ - project.BuildWithLogger(logger), + buildOpts := []internalproject.BuildOption{ + internalproject.BuildWithLogger(logger), } if o.EventChannel != nil { - buildOpts = append(buildOpts, project.BuildWithEventChannel(o.EventChannel)) + buildOpts = append(buildOpts, internalproject.BuildWithEventChannel(o.EventChannel)) } if o.BasePath != "" { - buildOpts = append(buildOpts, project.BuildWithProjectBasePath(o.BasePath)) + buildOpts = append(buildOpts, internalproject.BuildWithProjectBasePath(o.BasePath)) } imgMap, deps, err := b.Build(ctx, proj, projFS, buildOpts...) if err != nil { - _ = os.RemoveAll(tempDir) - return nil, nil, err + cleanup() + return nil, nil, noopCleanup, err } - return imgMap, deps, nil + return imgMap, deps, cleanup, nil } +// noopCleanup is returned alongside errors so callers can call cleanup +// unconditionally. +func noopCleanup() {} + // SchemaSource is a source of resources (CRDs/XRDs) for which language // schemas can be generated. type SchemaSource = manager.Source @@ -185,7 +214,7 @@ const SchemaSourceTypeCRD = manager.SourceTypeCRD // 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, proj *devv1alpha1.Project, projFS afero.Fs, src SchemaSource) error { +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( @@ -198,3 +227,38 @@ func AddSchemas(ctx context.Context, cfg *Config, proj *devv1alpha1.Project, pro 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. +func NewSchemaRunner(cfgs []pkgv1beta1.ImageConfig) SchemaRunner { + return runner.NewRealSchemaRunner(runner.WithImageConfig(cfgs)) +} + +// 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) +} From 2329252959f65f4db7a06e57321085263ea8ebce Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Tue, 17 Nov 2026 20:58:52 +0100 Subject: [PATCH 5/8] Allow forwarding extra env prefixes into schema runner containers The runner only forwarded CROSSPLANE_-prefixed environment variables to the language tooling containers. Host CLIs need their own variables visible there too - e.g. up's e2e test manifests read cloud credentials from UP_-prefixed variables - so make the prefixes extensible. Signed-off-by: Christopher Haar --- internal/schemas/runner/run.go | 31 ++++++++++++++++++++++++++----- pkg/project/project.go | 11 ++++++++--- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/internal/schemas/runner/run.go b/internal/schemas/runner/run.go index 5e79033b7..7e59f3ce3 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) { @@ -99,13 +110,15 @@ 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. +// give the container a cache that outlives it. Mounts are keyed by container +// path, so one volume can be mounted at several paths; a second mount at the +// same path replaces the first. func WithVolumeMount(name, containerPath string) Option { return func(o *GenerateOptions) { if o.VolumeMounts == nil { o.VolumeMounts = make(map[string]string) } - o.VolumeMounts[name] = containerPath + o.VolumeMounts[containerPath] = name } } @@ -146,10 +159,18 @@ 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 { + // An empty prefix would forward the entire host environment. + if prefix == "" { + continue + } + if strings.HasPrefix(e, prefix) { + envVars = append(envVars, e) + break + } } } @@ -159,7 +180,7 @@ func (r RealSchemaRunner) Generate(ctx context.Context, fromFS afero.Fs, baseFol docker.StartWithEnv(envVars...), docker.StartWithWorkingDirectory(o.WorkDirectory), } - for name, path := range o.VolumeMounts { + for path, name := range o.VolumeMounts { startOpts = append(startOpts, docker.StartWithVolumeMount(name, path)) } diff --git a/pkg/project/project.go b/pkg/project/project.go index 91b393677..904e68d90 100644 --- a/pkg/project/project.go +++ b/pkg/project/project.go @@ -237,9 +237,14 @@ type SchemaRunner = runner.SchemaRunner type SchemaRunnerOption = runner.Option // NewSchemaRunner returns a SchemaRunner that rewrites tooling images -// according to the given image configs. -func NewSchemaRunner(cfgs []pkgv1beta1.ImageConfig) SchemaRunner { - return runner.NewRealSchemaRunner(runner.WithImageConfig(cfgs)) +// 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. From bf0590f76042dda473b925e764f9b2ff93fd3b53 Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Tue, 17 Nov 2026 23:32:21 +0100 Subject: [PATCH 6/8] Export SilentExit from pkg/cli Embedded commands can return a silent ExitError to report a result such as simulate finding changes; hosts need SilentExit to exit with its code without printing an empty error line. Signed-off-by: Christopher Haar --- pkg/cli/cli.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 0f491cee2..35ae89df9 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -33,6 +33,7 @@ import ( "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" @@ -113,6 +114,14 @@ func BindRuntime(k *kong.Context, sp SpinnerPrinter, sigCtx context.Context) { 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 // shown by default, alpha commands are hidden from help until enabled (an From 6ec946152635ef8df46ff211d2190de63de7ae41 Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Tue, 17 Nov 2026 23:37:32 +0100 Subject: [PATCH 7/8] Support extra package annotations and ignore patterns in project builds Host CLIs can carry package metadata and build exclusions outside the project file (e.g. up's upbound-project.yaml extensions). Add build options to apply extra annotations to the configuration and function package metadata - before function dependency digests are computed - and to exclude gitignore-style patterns from resource collection. Signed-off-by: Christopher Haar --- internal/project/build.go | 97 +++++++++++++++++++++++++------------- internal/project/ignore.go | 57 ++++++++++++++++++++++ pkg/project/project.go | 12 +++++ 3 files changed, 133 insertions(+), 33 deletions(-) create mode 100644 internal/project/ignore.go diff --git a/internal/project/build.go b/internal/project/build.go index b07f2a4f2..5e66a9989 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, @@ -219,16 +238,6 @@ func (b *Builder) Build(ctx context.Context, project *devv1alpha1.Project, proje apisSource := afero.NewBasePathFs(projectFS, project.Spec.Paths.APIs) - // Not all projects have operations; ignore them if not present. - operationsSource := afero.NewMemMapFs() - opsExist, err := afero.DirExists(projectFS, project.Spec.Paths.Operations) - if err != nil { - return nil, nil, err - } - if opsExist { - operationsSource = afero.NewBasePathFs(projectFS, project.Spec.Paths.Operations) - } - // Collect resources (XRDs, MRAPs, compositions, and operations). packageFS := afero.NewMemMapFs() o.log.Debug("Collecting resources") @@ -240,7 +249,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, projectFS, project.Spec.Paths.APIs, 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 +259,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, projectFS, project.Spec.Paths.Operations, 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 +286,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 +368,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 +392,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 +452,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{ @@ -685,9 +694,8 @@ func gzipOpener(fsys afero.Fs, path, tempDir string) (tarball.Opener, error) { // LoadCompositions returns the Compositions under a project's APIs path, // discovered the same way project build collects them. 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, projectFS, project.Spec.Paths.APIs, []string{xpv1.CompositionGroupVersionKind.String()}, nil); err != nil { return nil, errors.Wrap(err, "failed to collect compositions") } @@ -710,13 +718,28 @@ func LoadCompositions(project *devv1alpha1.Project, projectFS afero.Fs) ([]*unst return comps, err } -func collectResources(toFS afero.Fs, fromFS afero.Fs, gvks []string) error { - return afero.Walk(fromFS, "/", func(path string, info fs.FileInfo, err error) error { +// collectResources copies resources of the given GVKs found under root in +// projFS into toFS, at paths relative to root. The walk starts at root, so +// walked paths are project-root-relative and ignore patterns (which are +// relative to the project root) apply to them directly. A missing root is +// not an error; not all projects have operations. +func collectResources(toFS afero.Fs, projFS afero.Fs, root string, gvks []string, ignore *IgnoreMatcher) error { + if exists, err := afero.DirExists(projFS, root); err != nil || !exists { + return err + } + + return afero.Walk(projFS, root, 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) @@ -725,7 +748,7 @@ func collectResources(toFS afero.Fs, fromFS afero.Fs, gvks []string) error { } var u metav1.TypeMeta - bs, err := afero.ReadFile(fromFS, path) + bs, err := afero.ReadFile(projFS, path) if err != nil { return errors.Wrapf(err, "failed to read file %q", path) } @@ -738,14 +761,18 @@ func collectResources(toFS afero.Fs, fromFS afero.Fs, gvks []string) error { return nil } - err = toFS.MkdirAll(filepath.Dir(path), 0o755) + rel, err := filepath.Rel(root, path) if err != nil { - return errors.Wrapf(err, "failed to create directory for %q", path) + return errors.Wrapf(err, "failed to determine package path for %q", path) } + dest := filepath.Join("/", rel) - err = afero.WriteFile(toFS, path, bs, 0o644) - if err != nil { - return errors.Wrapf(err, "failed to write file %q to package", path) + if err := toFS.MkdirAll(filepath.Dir(dest), 0o755); err != nil { + return errors.Wrapf(err, "failed to create directory for %q", dest) + } + + if err := afero.WriteFile(toFS, dest, bs, 0o644); err != nil { + return errors.Wrapf(err, "failed to write file %q to package", dest) } return nil @@ -775,7 +802,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 +815,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 +834,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 000000000..9a296a460 --- /dev/null +++ b/internal/project/ignore.go @@ -0,0 +1,57 @@ +/* +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 ( + "path/filepath" + "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(filepath.ToSlash(p), "/"), "/"), isDir) +} diff --git a/pkg/project/project.go b/pkg/project/project.go index 904e68d90..d35786619 100644 --- a/pkg/project/project.go +++ b/pkg/project/project.go @@ -104,6 +104,12 @@ type BuildOptions struct { // 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 @@ -185,6 +191,12 @@ func Build(ctx context.Context, cfg *config.Config, proj *devv1alpha1.Project, p 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 { From 887a9540eb81376b9e1f8fcec4ddcdf5cf10070f Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Tue, 17 Nov 2026 23:58:16 +0100 Subject: [PATCH 8/8] Add DependencySchemas to pkg/project Expose the raw schema resources (CRD manifests, OpenAPI documents) of a project's crd and k8s dependencies, fetched from the same sources the dependency manager generates language schemas from. Hosts use them to validate rendered resources against those dependencies' types. Signed-off-by: Christopher Haar --- pkg/project/project.go | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/pkg/project/project.go b/pkg/project/project.go index d35786619..d6b2c3246 100644 --- a/pkg/project/project.go +++ b/pkg/project/project.go @@ -279,3 +279,60 @@ func WithCopyFromPath(path string) SchemaRunnerOption { 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 +}