Skip to content

Commit e08bb8e

Browse files
authored
Clean flatten metadata (#2413)
1 parent 50773e4 commit e08bb8e

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

kubernetes/structures.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,21 @@ func flattenMetadataFields(meta metav1.ObjectMeta) []interface{} {
135135
}
136136

137137
func flattenMetadata(meta metav1.ObjectMeta, d *schema.ResourceData, providerMetadata interface{}) []interface{} {
138-
metadata := expandMetadata(d.Get("metadata").([]interface{}))
138+
metadataAnnotations := d.Get("metadata.0.annotations").(map[string]interface{})
139+
metadataLabels := d.Get("metadata.0.labels").(map[string]interface{})
139140

140141
ignoreAnnotations := providerMetadata.(kubeClientsets).IgnoreAnnotations
141-
removeInternalKeys(meta.Annotations, metadata.Annotations)
142-
removeKeys(meta.Annotations, metadata.Annotations, ignoreAnnotations)
142+
removeInternalKeys(meta.Annotations, metadataAnnotations)
143+
removeKeys(meta.Annotations, metadataAnnotations, ignoreAnnotations)
143144

144145
ignoreLabels := providerMetadata.(kubeClientsets).IgnoreLabels
145-
removeInternalKeys(meta.Labels, metadata.Labels)
146-
removeKeys(meta.Labels, metadata.Labels, ignoreLabels)
146+
removeInternalKeys(meta.Labels, metadataLabels)
147+
removeKeys(meta.Labels, metadataLabels, ignoreLabels)
147148

148149
return flattenMetadataFields(meta)
149150
}
150151

151-
func removeInternalKeys(m map[string]string, d map[string]string) {
152+
func removeInternalKeys(m map[string]string, d map[string]interface{}) {
152153
for k := range m {
153154
if isInternalKey(k) && !isKeyInMap(k, d) {
154155
delete(m, k)
@@ -158,15 +159,15 @@ func removeInternalKeys(m map[string]string, d map[string]string) {
158159

159160
// removeKeys removes given Kubernetes metadata(annotations and labels) keys.
160161
// In that case, they won't be available in the TF state file and will be ignored during apply/plan operations.
161-
func removeKeys(m map[string]string, d map[string]string, ignoreKubernetesMetadataKeys []string) {
162+
func removeKeys(m map[string]string, d map[string]interface{}, ignoreKubernetesMetadataKeys []string) {
162163
for k := range m {
163164
if ignoreKey(k, ignoreKubernetesMetadataKeys) && !isKeyInMap(k, d) {
164165
delete(m, k)
165166
}
166167
}
167168
}
168169

169-
func isKeyInMap(key string, d map[string]string) bool {
170+
func isKeyInMap(key string, d map[string]interface{}) bool {
170171
_, ok := d[key]
171172
return ok
172173
}

website/docs/index.html.markdown

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,27 @@ For further reading, see these examples which demonstrate different approaches t
136136

137137
## Ignore Kubernetes annotations and labels
138138

139-
In certain cases, external systems can add and modify resources annotations and labels for their own purposes. However, Terraform will remove them since they are not presented in the code. It also might be hard to update code accordingly to stay tuned with the changes that come outside. In order to address this `ignore_annotations` and `ignore_labels` attributes were introduced on the provider level. They allow Terraform to ignore certain annotations and labels across all resources. Please bear in mind, that all data sources remain unaffected and the provider always returns all labels and annotations, in spite of the `ignore_annotations` and `ignore_labels` settings. The same is applicable for the pod and job definitions that fall under templates.
139+
In certain cases, external systems can add and modify resources annotations and labels for their own purposes. However, Terraform will remove them since they are not presented in the code. It also might be hard to update code accordingly to stay tuned with the changes that come outside. In order to address this `ignore_annotations` and `ignore_labels` attributes were introduced on the provider level. They allow Terraform to ignore certain annotations and labels across all resources.
140140

141141
Both attributes support RegExp to match metadata objects more effectively.
142142

143+
Please keep in mind that all data sources remain unaffected, and the provider always returns all labels and annotations, despite the `ignore_annotations` and `ignore_labels` settings. The same applies to the pod and job definitions that fall under templates. To ignore certain annotations and/or labels on the template level, please use the `ignore_changes` feature of the [lifecycle](https://developer.hashicorp.com/terraform/language/meta-arguments/lifecycle) meta-argument.
144+
143145
### Examples
144146

147+
The following example demonstrates how to ignore changes related to the `kubectl.kubernetes.io/restartedAt` annotation that were made in the upstream Kubernetes object:
148+
149+
```hcl
150+
resource "kubernetes_deployment_v1" "this" {
151+
// omit the resource config
152+
lifecycle {
153+
ignore_changes = [
154+
spec[0].template[0].metadata[0].annotations["kubectl.kubernetes.io/restartedAt"],
155+
]
156+
}
157+
}
158+
```
159+
145160
The following example demonstrates how to ignore particular annotation keys:
146161

147162
```hcl
@@ -189,5 +204,5 @@ The following arguments are supported:
189204
* `command` - (Required) Command to execute.
190205
* `args` - (Optional) List of arguments to pass when executing the plugin.
191206
* `env` - (Optional) Map of environment variables to set when executing the plugin.
192-
* `ignore_annotations` - (Optional) List of Kubernetes metadata annotations to ignore across all resources handled by this provider for situations where external systems are managing certain resource annotations. Each item is a regular expression.
193-
* `ignore_labels` - (Optional) List of Kubernetes metadata labels to ignore across all resources handled by this provider for situations where external systems are managing certain resource labels. Each item is a regular expression.
207+
* `ignore_annotations` - (Optional) List of Kubernetes metadata annotations to ignore across all resources handled by this provider for situations where external systems are managing certain resource annotations. This option does not affect annotations within a template block. Each item is a regular expression.
208+
* `ignore_labels` - (Optional) List of Kubernetes metadata labels to ignore across all resources handled by this provider for situations where external systems are managing certain resource labels. This option does not affect annotations within a template block. Each item is a regular expression.

0 commit comments

Comments
 (0)