Skip to content

Support CROSSPLANE_DIFF_DOCKER_NETWORK env var for container networking#255

Open
jtucci wants to merge 2 commits into
crossplane-contrib:mainfrom
jtucci:feat/docker-network-env-var
Open

Support CROSSPLANE_DIFF_DOCKER_NETWORK env var for container networking#255
jtucci wants to merge 2 commits into
crossplane-contrib:mainfrom
jtucci:feat/docker-network-env-var

Conversation

@jtucci

@jtucci jtucci commented Mar 18, 2026

Copy link
Copy Markdown

Description of your changes

When crossplane-diff runs inside a Docker container (e.g. a GitHub Actions container job), function containers created via the Docker socket land on the default bridge network, which is isolated from the job container's network. This makes function containers unreachable, causing all compositions to fail with connection errors.

This adds support for the CROSSPLANE_DIFF_DOCKER_NETWORK environment variable. When set, the value is applied as the render.crossplane.io/runtime-docker-network annotation on all Function resources before they are passed to render.Render(). This tells the Crossplane render runtime to connect function containers to the specified Docker network.

The annotation is applied in both DefaultFunctionProvider (used by the xr command) and CachedFunctionProvider (used by the comp command) via a shared helper function.

Depends on crossplane/crossplane#7216 — that upstream PR must be merged first, and then the go.mod dependency in this repo needs to be bumped to pick up the new annotation support.

========================== 🌍 Earthly Build  ✅ SUCCESS ==========================

Fixes #254

I have:

  • Read and followed Crossplane's contribution process.
  • Run earthly -P +reviewable to ensure this PR is ready for review.
  • Added or updated unit tests.
  • Added or updated e2e tests.
  • Documented this change as needed.
  • Followed the API promotion workflow if this PR introduces, removes, or promotes an API.

@jtucci jtucci force-pushed the feat/docker-network-env-var branch 3 times, most recently from b733a4a to 8407101 Compare March 18, 2026 17:41
@jtucci

jtucci commented Mar 19, 2026

Copy link
Copy Markdown
Author

@jcogilvie could you please review this?

@jcogilvie

Copy link
Copy Markdown
Collaborator

Thanks for the submission.

Once upstream cuts a new version that includes the linked PR, I'll be able to bump our version and then review your change.

Copilot AI review requested due to automatic review settings April 28, 2026 19:01
@jcogilvie jcogilvie force-pushed the feat/docker-network-env-var branch from 8407101 to ea91a74 Compare April 28, 2026 19:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support for running crossplane-diff inside Docker container environments (e.g., GitHub Actions container jobs) by propagating a Docker network selection into the Crossplane render runtime via Function annotations.

Changes:

  • Introduces CROSSPLANE_DIFF_DOCKER_NETWORK env var support and applies render.crossplane.io/runtime-docker-network to Functions prior to rendering.
  • Applies the annotation for both DefaultFunctionProvider (xr) and CachedFunctionProvider (comp) via a shared helper.
  • Adds unit tests for the annotation-application helper.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
cmd/diff/diffprocessor/function_provider.go Adds env var + helper to annotate Functions and wires it into both function providers.
cmd/diff/diffprocessor/function_provider_test.go Adds unit tests covering env-set/unset and existing annotations behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 200 to 205
// Cache for future calls
p.cache[compName] = fns

applyDockerNetworkAnnotation(fns, p.logger)

return fns, nil
Comment on lines +600 to +604
if tt.envValue != "" {
t.Setenv(EnvDockerNetwork, tt.envValue)
} else {
os.Unsetenv(EnvDockerNetwork)
}
@jcogilvie

Copy link
Copy Markdown
Collaborator

It looks like this upstream fix hasn't been released on the 2.2 branch, and it isn't in 2.3.0-rc.0. So I'm still waiting on upstream for this.

@jtucci jtucci force-pushed the feat/docker-network-env-var branch from ea91a74 to 9e6248d Compare May 28, 2026 21:56
Copilot AI review requested due to automatic review settings June 5, 2026 18:01
@jtucci jtucci force-pushed the feat/docker-network-env-var branch from 9e6248d to 92196c9 Compare June 5, 2026 18:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment on lines +715 to +719
if tt.envValue != "" {
t.Setenv(EnvDockerNetwork, tt.envValue)
} else {
os.Unsetenv(EnvDockerNetwork)
}
Comment on lines +731 to +738
// Verify existing annotations are preserved when env is set
if tt.wantNetwork != "" {
for _, fn := range tt.fns {
if v, ok := fn.Annotations["existing-key"]; ok && v != "existing-value" {
t.Errorf("existing annotation was modified: got %q, want %q", v, "existing-value")
}
}
}
@jcogilvie

Copy link
Copy Markdown
Collaborator

It looks like in order for this to work, we have to fix crossplane/cli#75 upstream.

jtucci and others added 2 commits June 17, 2026 14:18
When crossplane-diff runs inside a Docker container (e.g. a GitHub
Actions container job), function containers created via the Docker
socket land on a different Docker network and are unreachable.

This adds support for the CROSSPLANE_DIFF_DOCKER_NETWORK environment
variable. When set, the value is applied as the
render.crossplane.io/runtime-docker-network annotation on all function
resources before they are passed to render.Render(). This tells the
Crossplane render runtime to connect function containers to the
specified Docker network, making them reachable from the caller.

Usage in a GitHub Actions workflow:

    env:
      CROSSPLANE_DIFF_DOCKER_NETWORK: ${{ job.container.network }}

Depends on: crossplane/crossplane#7216

Signed-off-by: Jon Tucci <jonytucci01@gmail.com>
Address review feedback on PR crossplane-contrib#255:

- CachedFunctionProvider.GetFunctionsForComposition now applies the
  Docker network annotation on every call, including cache hits, so the
  annotation always reflects the current value of
  CROSSPLANE_DIFF_DOCKER_NETWORK (rather than whatever was in effect
  when the entry was first cached). Reorder the cache-miss path to
  annotate before storing, so cache state is correct independent of
  slice aliasing.

- Tighten TestApplyDockerNetworkAnnotation: use t.Setenv even for the
  empty case so prior env state is restored on cleanup, and assert the
  pre-existing annotation key is both present and unchanged (the prior
  check only failed on a wrong value, not on accidental removal).

- Add TestCachedFunctionProvider_DockerNetworkAnnotation_CacheHit as a
  regression test for the cache-hit path: prime the cache with the env
  var unset, then set it and request the same composition; the returned
  cached functions must carry the annotation.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
@jcogilvie jcogilvie force-pushed the feat/docker-network-env-var branch from 92196c9 to 128b592 Compare June 17, 2026 21:31
@jcogilvie

Copy link
Copy Markdown
Collaborator

Pushed an updated branch from a maintainer to get this merge-ready:

4d6bb7b (jtucci, original DCO signoff preserved) — rebased on main and amended to include the go.mod bump of github.com/crossplane/cli/v2 to a pseudoversion containing crossplane/cli#65 (merged today), which is the runtime side of the render.crossplane.io/runtime-docker-network annotation this PR sets.

128b592 (new follow-up commit) — addresses the three Copilot review threads:

  • CachedFunctionProvider.GetFunctionsForComposition now applies applyDockerNetworkAnnotation on every call (cache hit and miss). The cache-miss path is also reordered to annotate before storing, so cache state is correct regardless of slice aliasing. (thread)
  • TestApplyDockerNetworkAnnotation always uses t.Setenv (even with an empty value), so prior env state is restored on cleanup and the test no longer leaks into the developer's shell. (thread)
  • The "existing annotation preserved" assertion now requires the key to be both present and unchanged (the prior check would silently pass if the annotation was dropped). The check is gated to the ExistingAnnotations subtest. (thread)
  • Adds TestCachedFunctionProvider_DockerNetworkAnnotation_CacheHit as a regression test: prime the cache with the env var unset, then set it and request the same composition — the cached functions must carry the annotation.

Verified locally with earthly -P +reviewable (lint 0 issues, all tests pass).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support running crossplane-diff from a GitHub Actions container job

3 participants