Skip to content

Commit

Permalink
Refactor metadata flattener (hashicorp#2345)
Browse files Browse the repository at this point in the history
  • Loading branch information
arybolovlev authored Dec 4, 2023
1 parent 96b44b0 commit 76484fd
Show file tree
Hide file tree
Showing 30 changed files with 826 additions and 289 deletions.
3 changes: 3 additions & 0 deletions .changelog/2345.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:note
We have updated the logic of data sources and now the provider will return all annotations and labels attached to the object, regardless of the `ignore_annotations` and `ignore_labels` provider settings. In addition to that, a list of ignored labels when they are attached to `kubernetes_job(_v1)` and `kubernetes_cron_job(_v1)` resources were extended with labels `batch.kubernetes.io/controller-uid` and `batch.kubernetes.io/job-name` since they aim to replace `controller-uid` and `job-name` in the future Kubernetes releases.
```
46 changes: 41 additions & 5 deletions kubernetes/data_source_kubernetes_config_map_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ package kubernetes

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func dataSourceKubernetesConfigMapV1() *schema.Resource {
Expand Down Expand Up @@ -37,11 +38,46 @@ func dataSourceKubernetesConfigMapV1() *schema.Resource {
}

func dataSourceKubernetesConfigMapV1Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
om := meta_v1.ObjectMeta{
Namespace: d.Get("metadata.0.namespace").(string),
Name: d.Get("metadata.0.name").(string),
conn, err := meta.(KubeClientsets).MainClientset()
if err != nil {
return diag.FromErr(err)
}

metadata := expandMetadata(d.Get("metadata").([]interface{}))

om := metav1.ObjectMeta{
Namespace: metadata.Namespace,
Name: metadata.Name,
}
d.SetId(buildId(om))

return resourceKubernetesConfigMapV1Read(ctx, d, meta)
log.Printf("[INFO] Reading config map %s", metadata.Name)
cfgMap, err := conn.CoreV1().ConfigMaps(metadata.Namespace).Get(ctx, metadata.Name, metav1.GetOptions{})
if err != nil {
log.Printf("[DEBUG] Received error: %#v", err)
return diag.FromErr(err)
}
log.Printf("[INFO] Received config map: %#v", cfgMap)

err = d.Set("metadata", flattenMetadataFields(cfgMap.ObjectMeta))
if err != nil {
return diag.FromErr(err)
}

err = d.Set("binary_data", flattenByteMapToBase64Map(cfgMap.BinaryData))
if err != nil {
return diag.FromErr(err)
}

err = d.Set("data", cfgMap.Data)
if err != nil {
return diag.FromErr(err)
}

err = d.Set("immutable", cfgMap.Immutable)
if err != nil {
return diag.FromErr(err)
}

return nil
}
32 changes: 29 additions & 3 deletions kubernetes/data_source_kubernetes_endpoints_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package kubernetes

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -28,11 +29,36 @@ func dataSourceKubernetesEndpointsV1() *schema.Resource {
}

func dataSourceKubernetesEndpointsV1Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn, err := meta.(KubeClientsets).MainClientset()
if err != nil {
return diag.FromErr(err)
}

metadata := expandMetadata(d.Get("metadata").([]interface{}))

om := metav1.ObjectMeta{
Namespace: d.Get("metadata.0.namespace").(string),
Name: d.Get("metadata.0.name").(string),
Namespace: metadata.Namespace,
Name: metadata.Name,
}
d.SetId(buildId(om))

return resourceKubernetesEndpointsV1Read(ctx, d, meta)
log.Printf("[INFO] Reading endpoints %s", metadata.Name)
ep, err := conn.CoreV1().Endpoints(metadata.Namespace).Get(ctx, metadata.Name, metav1.GetOptions{})
if err != nil {
log.Printf("[DEBUG] Received error: %#v", err)
return diag.Errorf("Failed to read endpoint because: %s", err)
}
log.Printf("[INFO] Received endpoints: %#v", ep)

err = d.Set("metadata", flattenMetadataFields(ep.ObjectMeta))
if err != nil {
return diag.FromErr(err)
}

err = d.Set("subset", flattenEndpointsSubsets(ep.Subsets))
if err != nil {
return diag.FromErr(err)
}

return nil
}
39 changes: 36 additions & 3 deletions kubernetes/data_source_kubernetes_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ package kubernetes

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
networking "k8s.io/api/networking/v1beta1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func dataSourceKubernetesIngress() *schema.Resource {
Expand Down Expand Up @@ -134,13 +135,45 @@ func dataSourceKubernetesIngress() *schema.Resource {
}

func dataSourceKubernetesIngressRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn, err := meta.(KubeClientsets).MainClientset()
if err != nil {
return diag.FromErr(err)
}

metadata := expandMetadata(d.Get("metadata").([]interface{}))

om := meta_v1.ObjectMeta{
om := metav1.ObjectMeta{
Namespace: metadata.Namespace,
Name: metadata.Name,
}
d.SetId(buildId(om))

return resourceKubernetesIngressV1Beta1Read(ctx, d, meta)
log.Printf("[INFO] Reading ingress %s", metadata.Name)
ing, err := conn.ExtensionsV1beta1().Ingresses(metadata.Namespace).Get(ctx, metadata.Name, metav1.GetOptions{})
if err != nil {
log.Printf("[DEBUG] Received error: %#v", err)
return diag.FromErr(err)
}
log.Printf("[INFO] Received ingress: %#v", ing)

err = d.Set("metadata", flattenMetadataFields(ing.ObjectMeta))
if err != nil {
return diag.FromErr(err)
}

err = d.Set("spec", flattenIngressSpec(ing.Spec))
if err != nil {
return diag.FromErr(err)
}

err = d.Set("status", []interface{}{
map[string][]interface{}{
"load_balancer": flattenIngressStatus(ing.Status.LoadBalancer),
},
})
if err != nil {
return diag.FromErr(err)
}

return nil
}
38 changes: 35 additions & 3 deletions kubernetes/data_source_kubernetes_ingress_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ package kubernetes

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
networking "k8s.io/api/networking/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func dataSourceKubernetesIngressV1() *schema.Resource {
Expand Down Expand Up @@ -146,13 +147,44 @@ func dataSourceKubernetesIngressV1() *schema.Resource {
}

func dataSourceKubernetesIngressV1Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn, err := meta.(KubeClientsets).MainClientset()
if err != nil {
return diag.FromErr(err)
}
metadata := expandMetadata(d.Get("metadata").([]interface{}))

om := meta_v1.ObjectMeta{
om := metav1.ObjectMeta{
Namespace: metadata.Namespace,
Name: metadata.Name,
}
d.SetId(buildId(om))

return resourceKubernetesIngressV1Read(ctx, d, meta)
log.Printf("[INFO] Reading ingress %s", metadata.Name)
ing, err := conn.NetworkingV1().Ingresses(metadata.Namespace).Get(ctx, metadata.Name, metav1.GetOptions{})
if err != nil {
log.Printf("[DEBUG] Received error: %#v", err)
return diag.FromErr(err)
}
log.Printf("[INFO] Received ingress: %#v", ing)

err = d.Set("metadata", flattenMetadataFields(ing.ObjectMeta))
if err != nil {
return diag.FromErr(err)
}

err = d.Set("spec", flattenIngressV1Spec(ing.Spec))
if err != nil {
return diag.FromErr(err)
}

err = d.Set("status", []interface{}{
map[string][]interface{}{
"load_balancer": flattenIngressV1Status(ing.Status.LoadBalancer),
},
})
if err != nil {
return diag.FromErr(err)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package kubernetes

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func dataSourceKubernetesMutatingWebhookConfigurationV1() *schema.Resource {
Expand Down Expand Up @@ -111,8 +113,33 @@ func dataSourceKubernetesMutatingWebhookConfigurationV1() *schema.Resource {
}

func dataSourceKubernetesMutatingWebhookConfigurationV1Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
name := d.Get("metadata.0.name").(string)
d.SetId(name)
conn, err := meta.(KubeClientsets).MainClientset()
if err != nil {
return diag.FromErr(err)
}

metadata := expandMetadata(d.Get("metadata").([]interface{}))
d.SetId(metadata.Name)

log.Printf("[INFO] Reading mutating webhook configuration %s", metadata.Name)
cfg, err := conn.AdmissionregistrationV1().MutatingWebhookConfigurations().Get(ctx, metadata.Name, metav1.GetOptions{})
if err != nil {
log.Printf("[DEBUG] Received error: %#v", err)
return diag.FromErr(err)
}
log.Printf("[INFO] Received mutating webhook configuration: %#v", cfg)

err = d.Set("metadata", flattenMetadataFields(cfg.ObjectMeta))
if err != nil {
return diag.FromErr(err)
}

log.Printf("[DEBUG] Setting mutating webhook configuration to: %#v", cfg.Webhooks)

err = d.Set("webhook", flattenMutatingWebhooks(cfg.Webhooks))
if err != nil {
return diag.FromErr(err)
}

return resourceKubernetesMutatingWebhookConfigurationV1Read(ctx, d, meta)
return nil
}
16 changes: 10 additions & 6 deletions kubernetes/data_source_kubernetes_namespace_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func dataSourceKubernetesNamespaceV1() *schema.Resource {
Expand Down Expand Up @@ -49,24 +49,27 @@ func dataSourceKubernetesNamespaceV1Read(ctx context.Context, d *schema.Resource
metadata := expandMetadata(d.Get("metadata").([]interface{}))
d.SetId(metadata.Name)

namespace, err := conn.CoreV1().Namespaces().Get(ctx, metadata.Name, meta_v1.GetOptions{})
namespace, err := conn.CoreV1().Namespaces().Get(ctx, metadata.Name, metav1.GetOptions{})
if err != nil {
log.Printf("[DEBUG] Received error: %#v", err)
return diag.FromErr(err)
}
log.Printf("[INFO] Received namespace: %#v", namespace)
err = d.Set("metadata", flattenMetadata(namespace.ObjectMeta, d, meta))

err = d.Set("metadata", flattenMetadataFields(namespace.ObjectMeta))
if err != nil {
return diag.FromErr(err)
}
err = d.Set("spec", flattenNamespaceSpec(&namespace.Spec))

err = d.Set("spec", flattenNamespaceV1Spec(&namespace.Spec))
if err != nil {
return diag.FromErr(err)
}

return nil
}

func flattenNamespaceSpec(in *v1.NamespaceSpec) []interface{} {
func flattenNamespaceV1Spec(in *corev1.NamespaceSpec) []interface{} {
if in == nil || len(in.Finalizers) == 0 {
return []interface{}{}
}
Expand All @@ -76,5 +79,6 @@ func flattenNamespaceSpec(in *v1.NamespaceSpec) []interface{} {
fin[i] = string(f)
}
spec["finalizers"] = fin

return []interface{}{spec}
}
13 changes: 6 additions & 7 deletions kubernetes/data_source_kubernetes_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func dataSourceKubernetesNodesRead(ctx context.Context, d *schema.ResourceData,

listOptions := metav1.ListOptions{}

m := d.Get("metadata").([]interface{})
if len(m) > 0 {
metadata := expandMetadata(m)
metadata := d.Get("metadata").([]interface{})
if len(metadata) > 0 {
metadata := expandMetadata(metadata)
labelMap, err := metav1.LabelSelectorAsMap(&metav1.LabelSelector{MatchLabels: metadata.Labels})
if err != nil {
return diag.FromErr(err)
Expand All @@ -94,13 +94,11 @@ func dataSourceKubernetesNodesRead(ctx context.Context, d *schema.ResourceData,
nodes := make([]interface{}, len(nodesRaw.Items))
for i, v := range nodesRaw.Items {
log.Printf("[INFO] Received node: %s", v.Name)
prefix := fmt.Sprintf("nodes.%d.", i)
n := map[string]interface{}{
"metadata": flattenMetadata(v.ObjectMeta, d, meta, prefix),
nodes[i] = map[string]interface{}{
"metadata": flattenMetadataFields(v.ObjectMeta),
"spec": flattenNodeSpec(v.Spec),
"status": flattenNodeStatus(v.Status),
}
nodes[i] = n
}
if err := d.Set("nodes", nodes); err != nil {
return diag.FromErr(err)
Expand All @@ -113,5 +111,6 @@ func dataSourceKubernetesNodesRead(ctx context.Context, d *schema.ResourceData,
}
id := fmt.Sprintf("%x", idsum.Sum(nil))
d.SetId(id)

return nil
}
4 changes: 2 additions & 2 deletions kubernetes/data_source_kubernetes_nodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func checkParsableQuantity(value string) error {
func TestAccKubernetesDataSourceNodes_basic(t *testing.T) {
dataSourceName := "data.kubernetes_nodes.test"
nodeName := regexp.MustCompile(`^[a-z0-9]+(?:[-.]{1}[a-z0-9]+)*$`)
zeroOrMore := regexp.MustCompile(`^[0-9]+$`)
oneOrMore := regexp.MustCompile(`^[1-9][0-9]*$`)
checkFuncs := resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(dataSourceName, "nodes.#", oneOrMore),
resource.TestMatchResourceAttr(dataSourceName, "nodes.0.metadata.0.labels.%", zeroOrMore),
resource.TestMatchResourceAttr(dataSourceName, "nodes.0.metadata.0.annotations.%", oneOrMore),
resource.TestMatchResourceAttr(dataSourceName, "nodes.0.metadata.0.labels.%", oneOrMore),
resource.TestCheckResourceAttrSet(dataSourceName, "nodes.0.metadata.0.resource_version"),
resource.TestMatchResourceAttr(dataSourceName, "nodes.0.metadata.0.name", nodeName),
resource.TestMatchResourceAttr(dataSourceName, "nodes.0.spec.0.%", oneOrMore),
Expand Down
Loading

0 comments on commit 76484fd

Please sign in to comment.