PO to GMP Migration Tool: Refactor Common Conversion Functions - #1990
PO to GMP Migration Tool: Refactor Common Conversion Functions#1990karthunni wants to merge 8 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the migration helpers and PodMonitor converter by extracting common logic into shared helper functions and introducing a commonMonitorSpec struct. Feedback is provided to address a bug in resolveFilterRunning where explicit filterRunning: true configurations are silently dropped, and to add defensive nil checks for the spec parameter in buildPodMonitoring and buildClusterPodMonitoring to prevent potential nil pointer dereferences.
3b27e3b to
973c9b2
Compare
973c9b2 to
e8cd8d0
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request refactors the migration helpers and PodMonitor converter to reduce code duplication and improve maintainability. It introduces a shared commonMonitorSpec struct and extracts several helper functions for resolving scrape intervals, timeouts, proxy URLs, authentication, TLS settings, and unsupported fields. These helpers are then utilized to consolidate the conversion logic for both PodMonitoring and ClusterPodMonitoring resources. I have no feedback to provide as the changes are clean and well-structured.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request refactors the migration logic for PodMonitor resources by extracting common conversion logic, validation, and warning routines into helper functions within pkg/migrate/helpers.go. It also introduces a commonMonitorSpec struct to share configurations between namespaced and cluster-scoped resources, significantly reducing code duplication. The reviewer feedback suggests adding a nil check for the gmpEp parameter in the new applyAuthAndTLS helper function to prevent potential nil pointer dereferences.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request refactors the PodMonitor migration logic by modularizing helper functions into helpers.go and introducing a shared commonMonitorSpec struct to streamline the conversion of both PodMonitoring and ClusterPodMonitoring resources. The review feedback focuses on improving resilience during migration; specifically, it suggests refactoring determineNamespaceScoping, resolveScrapeIntervalAndTimeout, and convertProxyURL to log warnings and fall back to safe defaults or placeholders instead of returning fatal errors when encountering non-fatal configuration issues.
dd4645d to
e7f1688
Compare
e7f1688 to
578df0a
Compare
f690691 to
643c79c
Compare
| return nil, err | ||
| } | ||
| if len(mergedSelector.MatchLabels) == 0 && len(mergedSelector.MatchExpressions) == 0 { | ||
| logger.Warn("Resulting PodMonitoring selector is empty. It will select and scrape all pods in this namespace. Verify if this is intended.") |
There was a problem hiding this comment.
The empty selector check was moved into convertToMonitorSpec, so converting a cluster-scoped monitor now logs Resulting PodMonitoring selector is empty... in this namespace instead of ClusterPodMonitoring across all namespaces. Consider passing the resource kind down or moving the warning to the kind-specific builder.
| return limits | ||
| } | ||
|
|
||
| // toStrictUnstructured converts a struct to a strictly JSON-compatible unstructured map. |
There was a problem hiding this comment.
The comment states JSON is used for unsupported Go primitives like uint64, but ToUnstructured does support uint64 (the real issue is unstructured.DeepCopy panicking on uint64). Also, unmarshaling numbers into float64 loses integer precision above 2^53 for large ScrapeLimits. Consider normalizing limits to int64 or using json.NewDecoder with UseNumber().
| } | ||
|
|
||
| // resolveAttachMetadata appends "node" to metadata if attachMetadata.node is enabled. | ||
| func resolveAttachMetadata(attachMetadata *pomonitoringv1.AttachMetadata, baseMetadata *[]string) *[]string { |
There was a problem hiding this comment.
Because resolveAttachMetadata runs before unionMetadata, setting attachMetadata.node: true returns a non-nil slice (node), which causes unionMetadata to pull in all default metadata labels (container, pod, etc.). It would be helpful to add a comment explaining this tradeoff.
| }, nil | ||
| } | ||
|
|
||
| func (c *PodMonitorConverter) convertToPodMonitoring(pm *pomonitoringv1.PodMonitor, logger *slog.Logger, cache *ResourceCache) (*unstructured.Unstructured, []*unstructured.Unstructured, error) { |
There was a problem hiding this comment.
convertToPodMonitoring and convertToClusterPodMonitoring are structurally identical except for their defaults slice and builder call. Consider collapsing them into a single parameterized helper.
| } | ||
|
|
||
| // applyAuthAndTLS converts credentials and TLS settings for a generic endpoint. | ||
| func (c *conversionContext) applyAuthAndTLS( |
There was a problem hiding this comment.
applyAuthAndTLS accepts i int to format endpoint [%d], but the caller at podmonitor.go:155 wraps the error again, producing redundant error context. Consider dropping i from applyAuthAndTLS and wrapping errors at the call site.
| @@ -77,21 +81,21 @@ type relabelingData struct { | |||
| rewrittenSources []string | |||
There was a problem hiding this comment.
PreScrapeRelabelingResult and ExtractedPreScrapeRules were exported while parseAndCleanNamespaces was un-exported. Since neither struct is used outside pkg/migrate, consider keeping them un-exported.
| func resolveAttachMetadata(attachMetadata *pomonitoringv1.AttachMetadata, baseMetadata *[]string) *[]string { | ||
| if attachMetadata != nil && attachMetadata.Node != nil && *attachMetadata.Node { | ||
| if baseMetadata == nil { | ||
| return &[]string{"node"} |
There was a problem hiding this comment.
Quick nit: resolveAttachMetadata hardcodes "node" directly instead of using the labelNode constant defined earlier in the file.
| } | ||
|
|
||
| // combineAndConvertRelabelings combines promoted pre-scrape rules and converts metricRelabelings. | ||
| func combineAndConvertRelabelings(logger *slog.Logger, promoted []monitoringv1.RelabelingRule, configs []pomonitoringv1.RelabelConfig) ([]monitoringv1.RelabelingRule, error) { |
There was a problem hiding this comment.
combineAndConvertRelabelings returns an error, but convertMetricRelabelings always returns nil error. Consider dropping the error return from both signatures.
| } | ||
|
|
||
| // determineNamespaceScoping resolves the target namespaces from a NamespaceSelector. | ||
| func determineNamespaceScoping(nsSel pomonitoringv1.NamespaceSelector, defaultNS string) ([]string, bool, error) { |
There was a problem hiding this comment.
The newly extracted helpers (determineNamespaceScoping, resolveScrapeIntervalAndTimeout, convertProxyURL, resolveFilterRunning, resolveAttachMetadata, toStrictUnstructured) currently lack direct unit tests in helpers_test.go. Adding table-driven unit tests would ensure boundary conditions and error cases are thoroughly covered.
Refactoring conversion functions to reduce code duplication and simplify implementation for impending ServiceMonitor migration logic.