Skip to content

e2b add network#616

Open
ZhaoQing7892 wants to merge 3 commits into
openkruise:masterfrom
ZhaoQing7892:add_network
Open

e2b add network#616
ZhaoQing7892 wants to merge 3 commits into
openkruise:masterfrom
ZhaoQing7892:add_network

Conversation

@ZhaoQing7892

Copy link
Copy Markdown
Contributor

No description provided.

@kruise-bot kruise-bot requested review from furykerry and zmberg July 8, 2026 09:23
@kruise-bot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign zmberg for approval by writing /assign @zmberg in a comment. For more information see:The Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@codecov

codecov Bot commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 73.17881% with 81 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.86%. Comparing base (e549e5b) to head (ce68f6f).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
pkg/servers/e2b/network.go 39.28% 34 Missing ⚠️
pkg/sandbox-manager/infra/sandboxcr/network.go 84.79% 16 Missing and 10 partials ⚠️
pkg/servers/e2b/create.go 73.33% 6 Missing and 2 partials ⚠️
pkg/cache/index.go 0.00% 7 Missing ⚠️
pkg/servers/e2b/services.go 25.00% 4 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #616      +/-   ##
==========================================
- Coverage   80.08%   79.86%   -0.22%     
==========================================
  Files         229      232       +3     
  Lines       17831    18145     +314     
==========================================
+ Hits        14280    14492     +212     
- Misses       2969     3053      +84     
- Partials      582      600      +18     
Flag Coverage Δ
unittests 79.86% <73.17%> (-0.22%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@zmberg

zmberg commented Jul 10, 2026

Copy link
Copy Markdown
Member

Could you please share the official E2D documentation corresponding to this feature?

Comment thread pkg/sandbox-manager/infra/interface.go Outdated
Request(ctx context.Context, method, path string, port int, body io.Reader) (*http.Response, error) // Make a request to the Sandbox
CSIMount(ctx context.Context, driver string, request string) error // request is string config for csi.NodePublishVolumeRequest
CreateCheckpoint(ctx context.Context, opts CreateCheckpointOptions) (string, error)
CreateSandboxNetwork(ctx context.Context, network SandboxNetworkConfig) error // Create TrafficPolicy CR for the sandbox

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

consider change the func name from XXXSandboxNetwork to XXXNetworkPolicy

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done!

tpList := &agentsv1alpha1.TrafficPolicyList{}
if err := k8sClient.List(ctx, tpList,
client.InNamespace(namespace),
client.MatchingLabels{labelSandboxID: sandboxID},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

plz add an index for TrafficPolicy using sandboxID, so that trafficpolicy list can be more efficient

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done!

}
if len(tpList.Items) > 0 {
tp := &tpList.Items[0]
if tp.Spec.Egress != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what about the ingress setting?

Comment thread pkg/servers/e2b/services.go Outdated
log.Info("sandbox deleted", "id", id)

// Cleanup network CRs (TrafficPolicy) associated with the sandbox.
if err := sbx.DeleteSandboxNetwork(r.Context()); err != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it is not necessary to delete trafficpolicy manually, k8s gc controller will delete the trafficpolicy anyway

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

deleted DeleteSandboxNetwork

}

// DeleteSandboxNetwork deletes the TrafficPolicy CR associated with the sandbox.
func (s *Sandbox) DeleteSandboxNetwork(ctx context.Context) error {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

DeleteSandboxNetwork is not necessary

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

deleted DeleteSandboxNetwork

Priority: 1000,
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
labelSandboxID: sandboxID,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

consider using uid of sandbox CR as the matching label, sandbox id may be too long for label value

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

using MergeFrom + patch of method ensureSandboxUIDLabel()

Comment thread pkg/servers/e2b/network.go Outdated
const allTrafficCIDR = "0.0.0.0/0"

// isCIDROrIP returns true if the entry is a valid CIDR or bare IP address.
func isCIDROrIP(entry string) bool {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

isCIDROrIP allTrafficCIDR/defaultDenyCIDR is duplicated func, consider extract them to a util package in pkg/utils/network

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done!

Comment thread pkg/servers/e2b/create.go Outdated

// Inject sandbox-id label into pod template so that TrafficPolicy CRs can
// select this pod via label selector.
infra.MergePodLabels(sbx, map[string]string{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

consider move the label assignment to L416, so no extra map is required

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

using MergeFrom + patch of method ensureSandboxUIDLabel()

@furykerry

Copy link
Copy Markdown
Member

Could you please share the official E2D documentation corresponding to this feature?

https://e2b.dev/docs/network/internet-access

for _, cidr := range allowOutCIDRs {
allowPeers = append(allowPeers, agentsv1alpha1.TrafficPolicyPeer{CIDR: cidr})
}
for _, fqdn := range allowOutDomains {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Issue: The E2B documentation states: "When any domain is used, the default nameserver 8.8.8.8 is automatically allowed to ensure proper DNS resolution." The PR's buildTrafficPolicy function does not implement this automatic DNS allowlist behavior. When a user specifies allowOut: ["api.example.com"] with a default-deny, the sandbox will be unable to resolve DNS because 8.8.8.8 is not in the allow list.

Consider: In buildTrafficPolicy, when allowOutDomains is non-empty and the default-deny rule is being added, automatically include 8.8.8.8/32 in the allow list

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done!

Comment thread pkg/servers/e2b/create.go Outdated
AllowOut: request.Network.AllowOut,
DenyOut: request.Network.DenyOut,
}); netErr != nil {
log.Error(netErr, "failed to create network CRs, sandbox is still usable")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

network creation failure should fail the sandbox creation entirely for security-sensitive workloads.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done!

TemplateID string `json:"templateID"`
Timeout int `json:"timeout,omitempty"`
AutoPause bool `json:"autoPause,omitempty"`
AllowInternetAccess *bool `json:"allow_internet_access,omitempty"`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Issue: The new field uses snake_case json:"allow_internet_access", while every other E2B model field in this repo is camelCase (templateID, envVars, metadata) and the sibling network fields are camelCase (allowOut, denyOut). E2B's HTTP API surface for these options is camelCase (allowInternetAccess); allow_internet_access/allow_out are the Python SDK parameter names, not the wire format.

Recommendation: Confirm the exact wire format the SDK sends; align to the repo/E2B convention to avoid the toggle silently no-op'ing when the field fails to bind:

Comment thread pkg/servers/e2b/models/sandbox.go Outdated

type SandboxNetworkUpdateConfig struct {
AllowInternetAccess *bool `json:"allow_internet_access,omitempty"`
*SandboxNetworkConfig

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Issue: Embedding *SandboxNetworkConfig relies on encoding/json's field-promotion + auto-allocation. It works for the current shapes but is fragile and non-obvious for an API request model.
Recommendation: Use explicit fields for clarity and stability:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done!

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants