Skip to content

feat(node): add hostport capability and network setup#855

Merged
BSWANG merged 1 commit intoAliyunContainerService:mainfrom
l1b0k:feat/ip_rule
Jul 24, 2025
Merged

feat(node): add hostport capability and network setup#855
BSWANG merged 1 commit intoAliyunContainerService:mainfrom
l1b0k:feat/ip_rule

Conversation

@l1b0k
Copy link
Collaborator

@l1b0k l1b0k commented Jul 24, 2025

  • Add setupHostPort function to configure portmap plugin and network rules
  • Implement configureNetworkRules function for iptables and ip rule setup- Include new hostport command in node capabilities

@l1b0k l1b0k requested a review from BSWANG July 24, 2025 03:04
- Add setupHostPort function to configure portmap plugin and network rules
- Implement configureNetworkRules function for iptables and ip rule setup- Include new hostport command in node capabilities

Signed-off-by: l1b0k <libokang.lbk@alibaba-inc.com>
@codecov
Copy link

codecov bot commented Jul 24, 2025

Codecov Report

Attention: Patch coverage is 51.41243% with 86 lines in your changes missing coverage. Please review.

Project coverage is 35.53%. Comparing base (7ecd7e4) to head (1bae4b9).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
cmd/terway-cli/node.go 0.00% 52 Missing ⚠️
cmd/terway-cli/portmap.go 74.25% 17 Missing and 9 partials ⚠️
plugin/driver/utils/utils_linux.go 0.00% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #855      +/-   ##
==========================================
+ Coverage   35.42%   35.53%   +0.10%     
==========================================
  Files         129      130       +1     
  Lines       18666    18837     +171     
==========================================
+ Hits         6612     6693      +81     
- Misses      11313    11392      +79     
- Partials      741      752      +11     
Flag Coverage Δ
unittests 35.53% <51.41%> (+0.10%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 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.

@BSWANG BSWANG requested a review from Copilot July 24, 2025 07:50
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements hostport capability and network setup functionality for the Terway CNI plugin. The changes add support for configuring portmap plugin with network rules including iptables and IP routing rules to enable hostport functionality in Kubernetes pods.

  • Add setupHostPort function to detect portmap plugin configuration and setup network rules
  • Implement configureNetworkRules function for iptables CONNMARK rules and custom routing table setup
  • Update netlink rule handling to support Mark and Mask filters for improved rule management

Reviewed Changes

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

Show a summary per file
File Description
cmd/terway-cli/node.go Adds hostport task and setupHostPort function to node configuration capabilities
cmd/terway-cli/portmap.go Implements network rules configuration with iptables and routing setup
cmd/terway-cli/portmap_test.go Comprehensive test suite for hostport functionality
plugin/driver/utils/utils_linux.go Enhanced FindIPRule function to support Mark and Mask filtering
plugin/datapath/policy_router_linux.go Refactored to use netlink.NewRule() for better rule initialization
plugin/datapath/policy_router_linux_test.go Updated tests to use netlink.NewRule() pattern
go.mod Moved coreos/go-iptables from indirect to direct dependency

Comment on lines +13 to +23
// Configure iptables and ip rule
func configureNetworkRules() error {
ctx := context.Background()

var (
mark = 0x10
mask = 0x10
tableID = 100
defaultInterface = "eth0"
comment = "terway-portmap" // Rule comment
rulePrio = 600
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

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

Consider extracting these hardcoded configuration values (mark=0x10, tableID=100, rulePrio=600) into named constants or configuration parameters to improve maintainability and avoid magic numbers.

Suggested change
// Configure iptables and ip rule
func configureNetworkRules() error {
ctx := context.Background()
var (
mark = 0x10
mask = 0x10
tableID = 100
defaultInterface = "eth0"
comment = "terway-portmap" // Rule comment
rulePrio = 600
const (
markDefault = 0x10
maskDefault = 0x10
tableIDDefault = 100
rulePriority = 600
)
// Configure iptables and ip rule
func configureNetworkRules() error {
ctx := context.Background()
var (
mark = markDefault
mask = maskDefault
tableID = tableIDDefault
defaultInterface = "eth0"
comment = "terway-portmap" // Rule comment

Copilot uses AI. Check for mistakes.
mark = 0x10
mask = 0x10
tableID = 100
defaultInterface = "eth0"
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

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

Hardcoding 'eth0' as the default interface may not work in all environments. Consider making this configurable or dynamically detecting the primary network interface.

Copilot uses AI. Check for mistakes.
err = ensureNFRules(ipt, testRule)
assert.NoError(t, err, "ensureNFRules should not error when rule already exists")

// clean up
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

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

[nitpick] The comment should be capitalized: 'Clean up' instead of 'clean up'.

Suggested change
// clean up
// Clean up

Copilot uses AI. Check for mistakes.
if rule.Src == nil && rule.Dst == nil && rule.OifName == "" {
return nil, errors.New("both src and dst is nil")
if rule.Src == nil && rule.Dst == nil && rule.OifName == "" && rule.Mask == 0 && rule.Mark == 0 {
return nil, errors.New("rule is empty")
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

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

The error message 'rule is empty' is not very descriptive. Consider providing more context about what fields are required, such as 'rule validation failed: at least one of Src, Dst, OifName, Mask, or Mark must be specified'.

Suggested change
return nil, errors.New("rule is empty")
return nil, errors.New("rule validation failed: at least one of Src, Dst, OifName, Mask, or Mark must be specified")

Copilot uses AI. Check for mistakes.
}

if exists {
fmt.Printf("Rule already exists in %s table %s chain %v\n", rule.Table, rule.Chain, rule.Args)
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

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

Using fmt.Printf for logging in production code is not recommended. Consider using a proper logging framework or at least fmt.Fprintf to stderr for diagnostic messages.

Copilot uses AI. Check for mistakes.
if err != nil {
return fmt.Errorf("failed to add rule: %w", err)
}
fmt.Printf("Added rule to %s table %s chain %v\n", rule.Table, rule.Chain, rule.Args)
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

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

Using fmt.Printf for logging in production code is not recommended. Consider using a proper logging framework or at least fmt.Fprintf to stderr for diagnostic messages.

Copilot uses AI. Check for mistakes.
@BSWANG BSWANG merged commit 8e919e2 into AliyunContainerService:main Jul 24, 2025
9 checks passed
@l1b0k l1b0k deleted the feat/ip_rule branch July 24, 2025 08:44
cursor bot pushed a commit that referenced this pull request Sep 7, 2025
feat(node): add hostport capability and network setup
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.

3 participants