Skip to content

Rework MergePodSpecs logic #6262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 24, 2025
Merged

Conversation

Sovietaced
Copy link
Contributor

@Sovietaced Sovietaced commented Feb 21, 2025

Tracking issue

Related to #6232

This pull request is a follow up to #6232 which introduced some regressions for pod templates with init containers.

Why are the changes needed?

We discovered a bug where init containers from pod templates were being injected into tasks which had no init containers. This is because of an out of order merge of the task pod spec onto the pod template spec.

What changes were proposed in this pull request?

Fundamentally, MergePodSpecs is now used for two different use cases but it was originally designed for one:

  1. Modifying a base pod spec based on a pod template (and handling magic container names)
  2. Overlaying or overriding customized pod specs on top of the base pod spec

MergePodSpecs was originally designed to handle the first use case which gave it some strange behavior when it was used for the second case. Additionally, the signature of MergePodSpecs was documented such that the base pod spec was passed in first and the templates were passed in second but when templates were used they were passed in first which is enormously confusing.

This pull request reworks the two use cases into two different methods and hopes to clean up the semantics of the first use case. Lastly, it adds lots of new test coverage.

How was this patch tested?

Tested in production

Setup process

Screenshots

Check all the applicable boxes

  • I updated the documentation accordingly.
  • All new and existing tests passed.
  • All commits are signed-off.

Related PRs

Docs link

Summary by Bito

This PR refactors pod spec merging functionality by introducing separate methods (MergeBasePodSpecOntoTemplate and MergeOverlayPodSpecOntoBase) while simplifying mergeCustomPodSpec function. The implementation includes clearer error messages, improved template management, and simplified container merging logic. The changes fix bugs in init container handling and enhance pod spec merging semantics with more descriptive error messages for nil pod specs. The test suite provides comprehensive coverage for container handling, scheduler settings, and service account configurations.

Unit tests added: True

Estimated effort to review (1-5, lower is better): 3

@flyte-bot
Copy link
Collaborator

Code Review Agent Run Status

  • Limitations and other issues: ❌ Failure - The AI Code Review Agent skipped reviewing this change because it is configured to exclude certain pull requests based on the source/target branch or the pull request status. You can change the settings here, or contact the agent instance creator at [email protected].

@Sovietaced Sovietaced added the fixed For any bug fixes label Feb 21, 2025
Copy link

codecov bot commented Feb 21, 2025

Codecov Report

Attention: Patch coverage is 78.02198% with 20 lines in your changes missing coverage. Please review.

Project coverage is 36.89%. Comparing base (e9e227b) to head (4745362).
Report is 91 commits behind head on master.

Files with missing lines Patch % Lines
...ns/go/tasks/pluginmachinery/flytek8s/pod_helper.go 76.47% 14 Missing and 6 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6262      +/-   ##
==========================================
+ Coverage   36.86%   36.89%   +0.03%     
==========================================
  Files        1318     1318              
  Lines      134773   134881     +108     
==========================================
+ Hits        49683    49768      +85     
- Misses      80758    80777      +19     
- Partials     4332     4336       +4     
Flag Coverage Δ
unittests-datacatalog 51.58% <ø> (ø)
unittests-flyteadmin 51.87% <ø> (ø)
unittests-flytecopilot 30.99% <ø> (ø)
unittests-flytectl 62.29% <ø> (ø)
unittests-flyteidl 7.22% <ø> (ø)
unittests-flyteplugins 54.04% <78.02%> (+0.04%) ⬆️
unittests-flytepropeller 42.86% <ø> (+0.07%) ⬆️
unittests-flytestdlib 55.35% <ø> (ø)

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.

Signed-off-by: Jason Parraga <[email protected]>
@Sovietaced Sovietaced changed the title Fix issues with injected pod template init containers Rework MergePodSpecs logic Feb 21, 2025
@flyte-bot
Copy link
Collaborator

Code Review Agent Run Status

  • Limitations and other issues: ❌ Failure - The AI Code Review Agent skipped reviewing this change because it is configured to exclude certain pull requests based on the source/target branch or the pull request status. You can change the settings here, or contact the agent instance creator at [email protected].

Signed-off-by: Jason Parraga <[email protected]>
@Sovietaced Sovietaced marked this pull request as ready for review February 21, 2025 06:09
@flyte-bot
Copy link
Collaborator

flyte-bot commented Feb 21, 2025

Code Review Agent Run #51076d

Actionable Suggestions - 0
Additional Suggestions - 1
  • flyteplugins/go/tasks/pluginmachinery/flytek8s/pod_helper_test.go - 1
Review Details
  • Files reviewed - 4 · Commit Range: b51b099..38c6539
    • flyteplugins/go/tasks/pluginmachinery/flytek8s/pod_helper.go
    • flyteplugins/go/tasks/pluginmachinery/flytek8s/pod_helper_test.go
    • flyteplugins/go/tasks/plugins/k8s/ray/ray.go
    • flyteplugins/go/tasks/plugins/k8s/spark/spark.go
  • Files skipped - 0
  • Tools
    • Golangci-lint (Linter) - ✖︎ Failed
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

AI Code Review powered by Bito Logo

@flyte-bot
Copy link
Collaborator

flyte-bot commented Feb 21, 2025

Changelist by Bito

This pull request implements the following key changes.

Key Change Files Impacted
Feature Improvement - Pod Spec Merging Logic Enhancement

pod_helper_test.go - Added new test cases for pod spec merging with container overlays and base specs

ray.go - Updated mergeCustomPodSpec to use simplified pod spec merging logic

spark.go - Migrated to new MergeOverlayPodSpecOntoBase method for pod spec merging

@eapolinario
Copy link
Contributor

Can we make sure this also works with runtime pod templates? This is related to #6204.

Signed-off-by: Jason Parraga <[email protected]>
@flyte-bot
Copy link
Collaborator

flyte-bot commented Feb 22, 2025

Code Review Agent Run #b4db69

Actionable Suggestions - 1
  • flyteplugins/go/tasks/plugins/k8s/ray/ray.go - 1
Review Details
  • Files reviewed - 2 · Commit Range: 38c6539..20543d4
    • flyteplugins/go/tasks/pluginmachinery/flytek8s/pod_helper.go
    • flyteplugins/go/tasks/plugins/k8s/ray/ray.go
  • Files skipped - 0
  • Tools
    • Golangci-lint (Linter) - ✖︎ Failed
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

AI Code Review powered by Bito Logo

@@ -518,7 +518,7 @@ func buildWorkerPodTemplate(primaryContainer *v1.Container, basePodSpec *v1.PodS
}

// Merges a ray head/worker node custom pod specs onto task's generated pod spec
func mergeCustomPodSpec(primaryContainer *v1.Container, podSpec *v1.PodSpec, k8sPod *core.K8SPod) (*v1.PodSpec, error) {
func mergeCustomPodSpec(podSpec *v1.PodSpec, k8sPod *core.K8SPod) (*v1.PodSpec, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Update function calls to match signature

Consider updating function calls to mergeCustomPodSpec in buildHeadPodTemplate and buildWorkerPodTemplate to match the updated signature which no longer requires the primaryContainer parameter.

Code suggestion
Check the AI-generated fix before applying
 -	basePodSpec, err := mergeCustomPodSpec(primaryContainer, basePodSpec, spec.GetK8SPod())
 +	basePodSpec, err := mergeCustomPodSpec(basePodSpec, spec.GetK8SPod())
 @@ -503,1 +503,1 @@
 -	basePodSpec, err := mergeCustomPodSpec(primaryContainer, basePodSpec, spec.GetK8SPod())
 +	basePodSpec, err := mergeCustomPodSpec(basePodSpec, spec.GetK8SPod())

Code Review Run #b4db69


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

Signed-off-by: Jason Parraga <[email protected]>
@Sovietaced
Copy link
Contributor Author

Can we make sure this also works with runtime pod templates? This is related to #6204.

Replied in the issue discussion

@flyte-bot
Copy link
Collaborator

flyte-bot commented Feb 22, 2025

Code Review Agent Run #b6b690

Actionable Suggestions - 1
  • flyteplugins/go/tasks/pluginmachinery/flytek8s/pod_helper_test.go - 1
Review Details
  • Files reviewed - 2 · Commit Range: 20543d4..45811c3
    • flyteplugins/go/tasks/pluginmachinery/flytek8s/pod_helper.go
    • flyteplugins/go/tasks/pluginmachinery/flytek8s/pod_helper_test.go
  • Files skipped - 0
  • Tools
    • Golangci-lint (Linter) - ✖︎ Failed
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

AI Code Review powered by Bito Logo

Signed-off-by: Jason Parraga <[email protected]>
@flyte-bot
Copy link
Collaborator

flyte-bot commented Feb 23, 2025

Code Review Agent Run #9edcdb

Actionable Suggestions - 2
  • flyteplugins/go/tasks/pluginmachinery/flytek8s/pod_helper_test.go - 2
Review Details
  • Files reviewed - 2 · Commit Range: 45811c3..4745362
    • flyteplugins/go/tasks/pluginmachinery/flytek8s/pod_helper.go
    • flyteplugins/go/tasks/pluginmachinery/flytek8s/pod_helper_test.go
  • Files skipped - 0
  • Tools
    • Golangci-lint (Linter) - ✖︎ Failed
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

AI Code Review powered by Bito Logo

Comment on lines +2394 to +2406
expectedError: errors.New("neither the basePodSpec or the overlayPodSpec can be nil"),
},
InitContainers: []v1.Container{
defaultInitContainerTemplate,
primaryInitContainerTemplate,
{
name: "nil base",
basePodSpec: nil,
overlayPodSpec: &v1.PodSpec{},
expectedError: errors.New("neither the basePodSpec or the overlayPodSpec can be nil"),
},
HostNetwork: true,
NodeSelector: map[string]string{
"foo": "bar",
{
name: "nil base and overlay",
basePodSpec: nil,
overlayPodSpec: nil,
expectedError: errors.New("neither the basePodSpec or the overlayPodSpec can be nil"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider more specific error messages

Consider using a more descriptive error message. The current message neither the basePodSpec or the overlayPodSpec can be nil could be clearer by specifying which parameter is nil. Maybe consider separate error messages for each case.

Code suggestion
Check the AI-generated fix before applying
 -			expectedError:  errors.New("neither the basePodSpec or the overlayPodSpec can be nil"),
 +			expectedError:  errors.New("overlayPodSpec cannot be nil"),
 @@ -2400,1 +2400,1 @@
 -			expectedError:  errors.New("neither the basePodSpec or the overlayPodSpec can be nil"),
 +			expectedError:  errors.New("basePodSpec cannot be nil"),
 @@ -2406,1 +2406,1 @@
 -			expectedError:  errors.New("neither the basePodSpec or the overlayPodSpec can be nil"),
 +			expectedError:  errors.New("both basePodSpec and overlayPodSpec cannot be nil"),

Code Review Run #9edcdb


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

Comment on lines +2529 to +2531
result, mergeErr := MergeOverlayPodSpecOntoBase(tt.basePodSpec, tt.overlayPodSpec)
assert.Equal(t, tt.expectedResult, result)
assert.Equal(t, tt.expectedError, mergeErr)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider adding more detailed merge assertions

Consider adding assertions to verify the container and init container merging behavior in more detail. The current assertions only check the final PodSpec result but don't explicitly verify that container properties are merged correctly.

Code suggestion
Check the AI-generated fix before applying
Suggested change
result, mergeErr := MergeOverlayPodSpecOntoBase(tt.basePodSpec, tt.overlayPodSpec)
assert.Equal(t, tt.expectedResult, result)
assert.Equal(t, tt.expectedError, mergeErr)
result, mergeErr := MergeOverlayPodSpecOntoBase(tt.basePodSpec, tt.overlayPodSpec)
assert.Equal(t, tt.expectedResult, result)
assert.Equal(t, tt.expectedError, mergeErr)
if result != nil {
assert.Equal(t, tt.expectedResult.Containers, result.Containers, "Containers should be merged correctly")
assert.Equal(t, tt.expectedResult.InitContainers, result.InitContainers, "InitContainers should be merged correctly")
}

Code Review Run #9edcdb


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

@Sovietaced Sovietaced merged commit 49a2dd6 into flyteorg:master Feb 24, 2025
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed For any bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants