Skip to content

PO to GMP Migration Tool: Service Resolution Functions - #2002

Open
karthunni wants to merge 8 commits into
karthunni/po-migrate-refactorfrom
karthunni/po-migrate-service-resolution
Open

PO to GMP Migration Tool: Service Resolution Functions#2002
karthunni wants to merge 8 commits into
karthunni/po-migrate-refactorfrom
karthunni/po-migrate-service-resolution

Conversation

@karthunni

Copy link
Copy Markdown
Collaborator

This PR implements the core service and port resolution helper functions in pkg/migrate/helpers.go required for the upcoming ServiceMonitor migration work.

Key additions:

  • findServicesBySelector: Traverses the ResourceCache to return Service CRs matching a specified LabelSelector within target namespaces.
  • resolveServicePort: Inspects a Service's spec.ports list to map a target port reference (either a string name or a port number) to the Pod's actual container targetPort value.
  • convertServiceTargetLabels: Maps Service-level labels (as defined in spec.targetLabels of ServiceMonitor) to static metric relabeling rules (action: replace), appending the "exported_" prefix if it clashes with protected Prometheus labels.
  • Added comprehensive unit tests in helpers_test.go verifying all helper behaviors, error paths, and edge cases.

@karthunni karthunni self-assigned this Jul 24, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces helper functions in pkg/migrate/helpers.go along with comprehensive unit tests in pkg/migrate/helpers_test.go to find Services by selector, resolve Service ports to target ports, and convert Service target labels to static metric relabeling rules. The reviewer provided valuable feedback, suggesting a nil check for the ResourceCache receiver to prevent panics, an explicit check for empty port strings to avoid accidental matches with unnamed ports, and handling additional numeric types (float64 and int) in the targetPort type switch to ensure robust parsing of unstructured objects.

Comment thread pkg/migrate/helpers.go Outdated
Comment thread pkg/migrate/helpers.go Outdated
Comment thread pkg/migrate/helpers.go Outdated
@karthunni

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces helper functions in pkg/migrate/helpers.go—including findServicesBySelector, resolveServicePort, and convertServiceTargetLabels—along with comprehensive unit tests to support Kubernetes Service resolution and label mapping during migration. The review feedback highlights that resolveServicePort uses a fragile type assertion for port numbers in unstructured objects and incorrectly propagates fatal errors instead of logging warnings and falling back to the port string. Consequently, the reviewer suggests refactoring resolveServicePort to handle type coercion safely and return fallbacks, and updating the unit tests accordingly.

Comment thread pkg/migrate/helpers.go Outdated
Comment thread pkg/migrate/helpers_test.go
@karthunni

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request adds helper functions to find services by selector, resolve service ports to target ports, and convert service target labels to relabeling rules, along with comprehensive unit tests. The feedback suggests improving the robustness of the service port resolution by logging a warning and using a placeholder instead of returning a fatal error when encountering a malformed port, which prevents the entire migration process from failing.

Comment thread pkg/migrate/helpers.go Outdated
@karthunni
karthunni force-pushed the karthunni/po-migrate-service-resolution branch from 6d133c9 to 648b3e1 Compare July 24, 2026 19:33
@karthunni

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces helper functions to find services by selector, resolve service ports to target ports, and map service labels to metric relabeling rules, along with their corresponding unit tests. Feedback suggests modifying resolveServicePort to log a warning and inject a placeholder port instead of returning a fatal error when encountering a malformed port entry, preventing the entire migration process from failing.

Comment thread pkg/migrate/helpers.go Outdated
@karthunni
karthunni force-pushed the karthunni/po-migrate-service-resolution branch from 648b3e1 to 8c87394 Compare July 27, 2026 19:17
@karthunni
karthunni force-pushed the karthunni/po-migrate-service-resolution branch from 60e7cdc to 436c631 Compare July 28, 2026 15:36
@dashpole dashpole self-assigned this Jul 29, 2026
@karthunni
karthunni marked this pull request as ready for review July 29, 2026 20:46
@karthunni
karthunni force-pushed the karthunni/po-migrate-service-resolution branch from 436c631 to 4bdfc9f Compare July 29, 2026 21:07
@karthunni
karthunni requested a review from bernot-dev July 29, 2026 21:52
Comment thread pkg/migrate/helpers.go Outdated
Comment thread pkg/migrate/helpers.go Outdated
return intstr.IntOrString{}, fmt.Errorf("failed to read port field: %w", err)
}
if !foundField {
return intstr.IntOrString{}, errors.New("service port spec is missing the port number")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In resolveServicePort, encountering a port entry missing port or with an invalid type returns a fatal error immediately. If a Service has multiple ports and an earlier entry is malformed, resolving a valid target port later in the slice fails. Consider skipping malformed entries and returning an error only if no matching port is resolved.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hmmm... This seems good, but we definitely need to warn about dropped ports

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I realize there might be a fundamental misunderstanding here. resolveServicePort is looking for a specific port on the Service I.e the one specified by the ServiceMonitor. So if any of the non-targeted ports have an issue, before we errored out, and after the change, we now log that there is an issue and we skip (even though we skip regardless since the port is irrelevant to us, we're basically just logging about an endpoint we don't care about).

Instead we should only check for malformations if the port matches the one we specified. Now the design choice here is whether we want to

  • Error -> Fail the migration of this ServiceMonitor immediately.
  • Warn and Drop -> Log a warning to skip this specific Service/endpoint, and continue migrating any other healthy Services/endpoints.

Comment thread pkg/migrate/helpers.go Outdated
Comment thread pkg/migrate/helpers.go Outdated
Comment thread pkg/migrate/helpers.go Outdated
Comment thread pkg/migrate/helpers_test.go Outdated
Comment thread pkg/migrate/helpers_test.go
Comment thread pkg/migrate/helpers_test.go
Comment thread pkg/migrate/helpers_test.go Outdated
Comment thread pkg/migrate/helpers_test.go Outdated
@karthunni
karthunni force-pushed the karthunni/po-migrate-service-resolution branch 2 times, most recently from 1c46b70 to 3b0235d Compare July 31, 2026 15:27
@karthunni
karthunni force-pushed the karthunni/po-migrate-service-resolution branch from 3b0235d to 3c2d321 Compare July 31, 2026 21:07
@karthunni
karthunni force-pushed the karthunni/po-migrate-service-resolution branch from b205e75 to 279a454 Compare August 3, 2026 14:58
Comment thread pkg/migrate/helpers.go Outdated
Comment thread pkg/migrate/helpers.go
Comment thread pkg/migrate/helpers_test.go Outdated
Comment thread pkg/migrate/types.go
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.

2 participants