Conversation
Co-authored-by: fredleger <2778741+fredleger@users.noreply.github.com>
Co-authored-by: fredleger <2778741+fredleger@users.noreply.github.com>
Co-authored-by: fredleger <2778741+fredleger@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add support to ignore metadata changes in k8s namespaces
k8s: add lifecycle ignore_changes for namespace metadata labels and annotations
Oct 8, 2025
fredleger
approved these changes
Oct 9, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Fixes the issue where Terraform attempts to revert changes to namespace labels and annotations when they are modified by external tools like Rancher. This PR adds lifecycle
ignore_changesrules to allow external customization of namespace metadata without Terraform drift detection.Problem
When external management tools (e.g., Rancher, other controllers) modify labels or annotations on Kubernetes namespaces, Terraform detects these as configuration drift and attempts to realign the metadata to match the module's configuration. This prevents users from customizing namespaces through external tools and causes unnecessary apply cycles.
Solution
Added a
lifecycleblock to thekubernetes_namespaceresource withignore_changesfor metadata labels and annotations:Changes
ignore_changesfor labels and annotations; added annotations support to metadataannotationsvariable for namespace annotations; fixed labels description (previously said "secret")Benefits
✅ Non-breaking change: Existing users don't need to migrate Terraform state
✅ Allows external customization: Tools like Rancher can modify namespace metadata freely
✅ Maintains control: Users can still update labels/annotations via Terraform using
terraform apply -replacewhen needed✅ Bonus feature: Full annotations support added (previously only labels were supported)
Usage
After this change, users can allow external tools to modify namespace labels and annotations without Terraform reverting them. Initial values are still set by Terraform on namespace creation.
If you need to update labels or annotations through Terraform after creation:
terraform apply -replace="kubernetes_namespace.ns"to force recreation, orDesign Rationale
An alternative approach of making
ignore_changesoptional via a variable was considered, but would require usingcountfor conditional resource creation. This would force all existing users to migrate their state withterraform state mv, making it a breaking change. The simpler approach of always ignoring changes aligns with the issue request and maintains backward compatibility.Closes #[issue_number]
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.