Skip to content

Conversation

jlegrone
Copy link
Collaborator

@jlegrone jlegrone commented Jul 28, 2025

What was changed

Added a deployment finalizer to prevent accidental deletion of deployment resources that would be unrecoverable for the controller.

Why?

The Temporal worker controller doesn't persist the original pod template anywhere other than in the live Kubernetes deployment resource. When a deployment is accidentally deleted (e.g., via kubectl delete deployment), the controller cannot recreate it since it doesn't have the old version of the pod template saved anywhere else.

This finalizer follows the Kubernetes finalizers pattern to ensure controlled deletion and prevent unrecoverable state loss.

Checklist

  1. Closes Add Finalizer for Deployment Resources #55

  2. How was this tested: New integration test validates finalizer behavior during deployment deletion

  3. Any docs updates needed: No documentation updates required - this is an internal implementation detail

@jlegrone jlegrone force-pushed the jlegrone/add-deployment-finalizer branch from e121567 to fbc3949 Compare July 28, 2025 19:45
@jlegrone jlegrone requested a review from Copilot July 29, 2025 14:34
Copilot

This comment was marked as outdated.

@jlegrone jlegrone force-pushed the jlegrone/add-deployment-finalizer branch from 9617e3f to 4e13b89 Compare July 29, 2025 22:00
@jlegrone jlegrone force-pushed the jlegrone/add-deployment-finalizer branch 2 times, most recently from a484510 to b6d8d81 Compare August 25, 2025 22:44
jlegrone added a commit that referenced this pull request Aug 25, 2025
Previously the test was manually adding finalizers using controllerutil.AddFinalizer
instead of testing the actual controller logic. Now the test calls the controller's
Reconcile method to properly exercise the finalizer addition logic as intended.

Addresses: #97 (comment)
jlegrone added a commit that referenced this pull request Aug 25, 2025
Changed the exported constant TemporalWorkerDeploymentFinalizer to
temporalWorkerDeploymentFinalizer (lowercase) to make it package-private
since it's only used within the controller package.

Addresses: #97 (comment)
@jlegrone jlegrone requested a review from Copilot August 25, 2025 22:56
Copy link
Contributor

@Copilot 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 adds a deployment finalizer to the TemporalWorkerDeployment controller to prevent accidental deletion of deployment resources that would be unrecoverable. The finalizer ensures controlled deletion following the Kubernetes finalizer pattern, preventing unrecoverable state loss when deployments are accidentally deleted.

Key changes:

  • Added finalizer management in the controller reconciliation loop
  • Implemented proper cleanup logic for managed deployment resources
  • Added comprehensive unit tests covering finalizer behavior and edge cases

Reviewed Changes

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

Show a summary per file
File Description
internal/controller/worker_controller.go Core finalizer implementation with deletion handling and resource cleanup logic
internal/controller/finalizer_test.go Comprehensive unit tests for finalizer behavior, cleanup, and edge cases
internal/tests/internal/integration_test.go Integration test validating deployment deletion protection behavior
internal/testhelpers/make.go Added test utilities for scheme setup and fake client creation
internal/planner/planner.go Minor code cleanup removing unnecessary else clause
internal/k8s/deployments.go Updated comment explaining finalizer management approach

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

jlegrone added a commit that referenced this pull request Aug 26, 2025
- Add detailed documentation comments for hardcoded timeout values explaining
  the rationale behind the chosen durations (2 minutes cleanup timeout and
  5 second poll interval)
- Change field selector fallback logging from Info to debug level (V(1)) to
  reduce noise during normal operation, especially in test environments

Addresses:
- #97 (comment)
- #97 (comment)
jlegrone and others added 11 commits August 26, 2025 19:25
- Added testDeploymentDeletionProtection test to verify deployment resources
  can only be deleted by the controller
- Test validates proper owner references with BlockOwnerDeletion=true
- Verifies controller recreates deployments if directly deleted
- Confirms controller properly cleans up deployments when TWD is deleted
- Replaced logging statements with proper testify assertions for better
  test validation and debugging
- Update GitHub Actions workflow to use go-version-file instead of hardcoded Go 1.21
- Fix revive linting error in planner.go by removing unnecessary else clause
- Fix deprecated result.Requeue usage in finalizer_test.go to use RequeueAfter
- Replace stale list iteration with proper polling using fresh queries
- Add timeout and context cancellation handling with configurable constants
- Implement field selector optimization with backward compatibility fallback
- Replace brittle time.Sleep with condition-based polling in integration tests
- Add comprehensive edge case tests for context cancellation and partial cleanup failures
Previously the test was manually adding finalizers using controllerutil.AddFinalizer
instead of testing the actual controller logic. Now the test calls the controller's
Reconcile method to properly exercise the finalizer addition logic as intended.

Addresses: #97 (comment)
Changed the exported constant TemporalWorkerDeploymentFinalizer to
temporalWorkerDeploymentFinalizer (lowercase) to make it package-private
since it's only used within the controller package.

Addresses: #97 (comment)
Removed duplicate testEnv struct declaration from integration_test.go
as it was already defined in env_helpers.go. This resolves the build
error that was preventing integration tests from running.
- Add detailed documentation comments for hardcoded timeout values explaining
  the rationale behind the chosen durations (2 minutes cleanup timeout and
  5 second poll interval)
- Change field selector fallback logging from Info to debug level (V(1)) to
  reduce noise during normal operation, especially in test environments

Addresses:
- #97 (comment)
- #97 (comment)
@jlegrone jlegrone force-pushed the jlegrone/add-deployment-finalizer branch from 83f07d5 to 0e1a946 Compare August 26, 2025 23:26
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.

Add Finalizer for Deployment Resources
1 participant