From 1a98cdaa852688617d4a7dc79c8bd67019396e0b 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 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 From 31a461101502cf4d488350d95ff6d2a4f195f2ce 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 | 128 ++++++++++++++++++++++++++++++ pkg/cli/cli_test.go | 74 +++++++++++++++++ 4 files changed, 389 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 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/build.go b/pkg/cli/build.go new file mode 100644 index 00000000..cfc0d66e --- /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 00000000..815af4d4 --- /dev/null +++ b/pkg/cli/cli.go @@ -0,0 +1,128 @@ +/* +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 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)) +} + +// 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) + } +} From 52e3daf9ac0c9d6223567b8f145f3c44edd79ebd 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 cfc0d66e..142457fe 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 d9fbebdd2ce72b18b5ecc77dccb88bde01603dac 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 --- internal/docker/docker.go | 17 +++++ internal/schemas/runner/run.go | 23 +++++- pkg/cli/cli.go | 10 --- pkg/{cli/build.go => project/project.go} | 97 +++++++++++++++++++----- 4 files changed, 116 insertions(+), 31 deletions(-) rename pkg/{cli/build.go => project/project.go} (65%) 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/schemas/runner/run.go b/internal/schemas/runner/run.go index 1157d493..5e79033b 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 815af4d4..458d0948 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 65% rename from pkg/cli/build.go rename to pkg/project/project.go index 142457fe..53e1b494 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 = 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 = project.ImageTagMap +type ImageTagMap = internalproject.ImageTagMap // ParseProject reads, validates, and defaults the project file at // projFilePath within projFS. @@ -55,7 +79,7 @@ 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. @@ -77,7 +101,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 @@ -89,7 +113,7 @@ type BuildOptions struct { // 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) { +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() @@ -119,7 +143,7 @@ func Build(ctx context.Context, cfg *Config, proj *devv1alpha1.Project, projFS a } depOpts := []dependency.ManagerOption{ - dependency.WithProjectFile(cmp.Or(o.ProjectFile, ProjectFile)), + dependency.WithProjectFile(cmp.Or(o.ProjectFile, FileName)), dependency.WithSchemaFS(schemasFS), dependency.WithSchemaGenerators(generators), dependency.WithSchemaRunner(schemaRunner), @@ -142,23 +166,23 @@ func Build(ctx context.Context, cfg *Config, proj *devv1alpha1.Project, projFS a 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()), + 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...) @@ -185,7 +209,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 +222,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 db2f832618ccc75f8b438247071a726c065f6f88 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 | 19 +++++++++++++++++-- pkg/project/project.go | 11 ++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/internal/schemas/runner/run.go b/internal/schemas/runner/run.go index 5e79033b..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) { @@ -146,10 +157,14 @@ 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 + } } } diff --git a/pkg/project/project.go b/pkg/project/project.go index 53e1b494..ae54992b 100644 --- a/pkg/project/project.go +++ b/pkg/project/project.go @@ -232,9 +232,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 208829b8dd5d9df6d1869763954e15f868ab28c9 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 458d0948..9f98c66f 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" @@ -104,6 +105,14 @@ func BindRuntime(sigCtx context.Context, k *kong.Context, sp SpinnerPrinter) { 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 From 7d3da5b1bb40aa9331e73947a84fd51b15f6f8ba 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 | 59 ++++++++++++++++++++++++++++---------- internal/project/ignore.go | 56 ++++++++++++++++++++++++++++++++++++ pkg/project/project.go | 12 ++++++++ 3 files changed, 112 insertions(+), 15 deletions(-) create mode 100644 internal/project/ignore.go 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/pkg/project/project.go b/pkg/project/project.go index ae54992b..f9f3c4e8 100644 --- a/pkg/project/project.go +++ b/pkg/project/project.go @@ -107,6 +107,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 @@ -184,6 +190,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 1af194737ee2a16426323a3cab8c6cced99f0391 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 f9f3c4e8..00a3bf2b 100644 --- a/pkg/project/project.go +++ b/pkg/project/project.go @@ -274,3 +274,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 +}