Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions test/pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,33 @@ func GetClockIfRoles(Ifs []string, nodeName *string) (roleInt []MetricRole, err
return roleInt, nil
}

// getIfaceAlias generates the PHC iface alias used in openshift_ptp_* metrics.
// Must stay aligned with linuxptp-daemon/cloud-event-proxy GetAlias:
//
// ens2f0 -> ens2fx, ens2f0np0 -> ens2fx, ens1f0.100 -> ens1fx.100
func getIfaceAlias(ifname string) string {
if ifname == "" {
return ""
}
if alreadyAliasedPattern.MatchString(ifname) {
return ifname
}
matches := ifaceAliasPattern.FindStringSubmatch(ifname)
if len(matches) < 3 {
return ifname
}
alias := matches[1] + "x"
if len(matches) > 3 && matches[3] != "" {
alias += matches[3]
}
return alias
}

var (
alreadyAliasedPattern = regexp.MustCompile(`^(.+?)x(\..+)?$`)
ifaceAliasPattern = regexp.MustCompile(`^(.+?)(\d+)(?:np\d+)?(\..+)?$`)
Comment on lines +201 to +203

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

set -euo pipefail

git ls-files test/pkg/metrics/metrics.go
echo '--- outline ---'
ast-grep outline test/pkg/metrics/metrics.go --view expanded || true
echo '--- relevant lines ---'
sed -n '160,260p' test/pkg/metrics/metrics.go | cat -n
echo '--- searches ---'
rg -n "getIfaceAlias|ifaceAliasPattern|alreadyAliasedPattern|getMetric|PHC|alias" test/pkg/metrics/metrics.go test/pkg/metrics -S

Repository: openshift/ptp-operator

Length of output: 11623


🏁 Script executed:

set -euo pipefail

# Find any tests or docs describing the alias contract.
rg -n "enox|bondx|ifaceAliasPattern|getIfaceAlias|PHC|linuxptp|alias" . -S --glob '!**/vendor/**' --glob '!**/node_modules/**'

Repository: openshift/ptp-operator

Length of output: 50379


🏁 Script executed:

set -euo pipefail

# Check whether the candidate aliases are actually used by current tests.
sed -n '1,220p' test/pkg/metrics/metrics_test.go | cat -n

Repository: openshift/ptp-operator

Length of output: 236


🏁 Script executed:

set -euo pipefail

# If there is a README or fixture documenting metric interface naming, inspect it.
rg -n "interface.*metric|PHC|linuxptp|alias" test pkg README.md . -S --glob '!**/vendor/**' --glob '!**/node_modules/**'

Repository: openshift/ptp-operator

Length of output: 50379


🏁 Script executed:

set -euo pipefail

sed -n '180,250p' test/pkg/metrics/metrics.go | cat -n

Repository: openshift/ptp-operator

Length of output: 3220


🌐 Web query:

linuxptp-daemon cloud-event-proxy GetAlias interface alias ens2f0 ens2fx source code

💡 Result:

The term GetAlias as referenced in the context of linuxptp-daemon and cloud-event-proxy is not a standard standalone API function, but rather refers to an internal mechanism used for mapping PTP network interfaces to their respective PHC (PTP Hardware Clock) identifiers [1]. In the linuxptp-daemon implementation, the alias store is used to match hardware interfaces (like ens2f0) to their PHC IDs [1]. This is critical in environments with multi-port NICs where different physical interfaces may share the same PHC, or where naming prefixes differ (e.g., comparing ens2f0 vs ens2fx) [1]. The daemon registers these interfaces in an alias store using the phcID and the network interface name, ensuring that the convergeConfig logic correctly associates PTP processes with the appropriate hardware [1]. Recent updates to the cloud-event-proxy (such as commit ceae33f) have explicitly improved the handling of these aliases by fetching them before the proxy begins listening to the event socket [2]. This ensures that alias mappings are correctly initialized if the cloud-event-proxy restarts while the linuxptp-daemon is already active, preventing defaults that might otherwise fall back to incorrect interface naming [2]. The GetAlias name itself is common in various software SDKs (such as AWS Lambda or other cloud management APIs) [3][4], but within the specific Red Hat/OpenShift PTP stack, it typically relates to the internal helper methods or storage management logic used by the ptp_operator_plugin and linuxptp-daemon to maintain the consistency of interface-to-PHC mapping [2][1].

Citations:


🌐 Web query:

site:github.com/openshift/linuxptp-daemon GetAlias ens2f0 ens2fx code

💡 Result:

The term "GetAlias" in the context of ens2f0 and ens2fx appears to be related to a specific internal mechanism within the openshift/linuxptp-daemon project, which manages PTP (Precision Time Protocol) configurations on Kubernetes clusters [1][2][3]. The linuxptp-daemon uses an alias management system to handle interface naming differences, particularly when multiple physical interfaces share the same PHC (PTP Hardware Clock) [1]. In the source code for this daemon, alias.AddInterface(phc, ifname) and alias.CalculateAliases() are used to register and map network interface names to their corresponding PHC IDs [1]. This ensures that PTP processes (like ptp4l) correctly associate network devices—such as ens2f0—even if their hardware identifiers or naming conventions differ across nodes [1]. There is no standard Linux kernel command or general-purpose utility called GetAlias for network interface renaming. The pattern you provided likely refers to a function or method within a specialized application (like the OpenShift PTP operator's components) designed to abstract hardware interface naming for configuration consistency [1][3]. If you are encountering this in a codebase, it is part of the custom logic used to normalize interface names for PTP hardware clock synchronization [1]. If you are looking for how to handle or rename network interfaces in standard Linux, you should use tools like ip link or systemd's predictable network interface naming policies instead.

Citations:


🏁 Script executed:

set -euo pipefail

echo '--- package files ---'
git ls-files 'test/pkg/metrics/*' 'test/pkg/*' | sed -n '1,120p'

echo '--- interface-name mentions in metrics package ---'
rg -n "ens[0-9]|bond[0-9]|eth[0-9]|enp|np[0-9]|vlan|alias|GetAlias|getIfaceAlias|ifaceAliasPattern" test/pkg/metrics -S

echo '--- surrounding callers ---'
sed -n '100,380p' test/pkg/metrics/metrics.go | cat -n

Repository: openshift/ptp-operator

Length of output: 14751


🏁 Script executed:

set -euo pipefail

# Show just the call sites and helper behavior in a compact way.
python3 - <<'PY'
from pathlib import Path
p = Path('test/pkg/metrics/metrics.go')
text = p.read_text()
for i, line in enumerate(text.splitlines(), 1):
    if 179 <= i <= 250 or 300 <= i <= 380:
        print(f"{i:4d}: {line}")
PY

Repository: openshift/ptp-operator

Length of output: 7318


Restrict PHC aliasing to the documented interface forms.
ifaceAliasPattern rewrites any numeric-suffixed name (eno1enox, bond0bondx, eth1.100ethx.100), so getMetric can fall back to the wrong interface when the aliased metric exists. Narrow this to the PHC naming contract (ens2f0/ens2f0np0/ens1f0.100) and add negative cases for ordinary NIC, bridge, and VLAN names.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/pkg/metrics/metrics.go` around lines 201 - 203, Restrict
ifaceAliasPattern to matching only the documented PHC interface forms, including
ens2f0, ens2f0np0, and ens1f0.100, while preserving the optional representor and
VLAN suffix handling needed by getMetric. Update the associated tests with
negative cases for ordinary NIC, bridge, and VLAN names so these names are never
aliased.

Source: Path instructions

)

// gets a metric value string for a given node and interface
func getMetric(nodeName, aIf, metricName string) (metric string, err error) {
const (
Expand All @@ -201,11 +228,12 @@ func getMetric(nodeName, aIf, metricName string) (metric string, err error) {
metrics := buf.String()
node := ptpPods.Items[index].Spec.NodeName

// Build a list of interface names to try: first the original, then the aliased (masked) version.
// Cards with a PHC per port keep the original name; cards sharing a PHC use the masked alias.
// Build a list of interface names to try: first the original, then the PHC alias.
// Cards with a PHC per port keep the original name; cards sharing a PHC use the masked alias
// (e.g. ens2f0 -> ens2fx, ens2f0np0 -> ens2fx).
ifCandidates := []string{aIf}
aliasedIf := aIf[:len(aIf)-1] + "x"
if aliasedIf != aIf {
aliasedIf := getIfaceAlias(aIf)
if aliasedIf != "" && aliasedIf != aIf {
Comment on lines +235 to +236

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

set -e
printf 'File size and relevant slices:\n'
wc -l test/pkg/metrics/metrics.go
sed -n '210,290p' test/pkg/metrics/metrics.go
printf '\nSearch for candidate/QuoteMeta/regexp usage in file:\n'
rg -n 'getIfaceAlias|QuoteMeta|MustCompile|candidate|ifCandidates' test/pkg/metrics/metrics.go

Repository: openshift/ptp-operator

Length of output: 4234


Escape candidate before building the regex. test/pkg/metrics/metrics.go:240-249 Raw aliases like ens1fx.100 include regex metacharacters, so the unescaped interpolation can match the wrong metric. Use regexp.QuoteMeta(candidate) in all three branches and add a VLAN-alias regression test.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/pkg/metrics/metrics.go` around lines 235 - 236, Escape each candidate
alias with regexp.QuoteMeta before interpolating it into regex patterns in all
three branches of the alias-matching logic around getIfaceAlias. Preserve the
existing matching behavior while preventing metacharacters in raw aliases from
changing the regex, and add a regression test covering a VLAN alias such as
ens1fx.100.

Source: Path instructions

ifCandidates = append(ifCandidates, aliasedIf)
}

Expand Down