Skip to content

Commit

Permalink
Add provider level ignored metadata keys
Browse files Browse the repository at this point in the history
This introduces a new provider configuration parameter
`ignored_metadata_keys` that allows the user to specify expressions to
ignore matching metadata annotations and labels on all kubernetes
resources.

This tries to be backwards compatible by hardcoding a list of internal
keys in `internalKeys`, which were previously ignored already.

Fixes hashicorp#746
  • Loading branch information
ctrox committed Nov 16, 2020
1 parent 7cc826c commit 72ee6d4
Show file tree
Hide file tree
Showing 45 changed files with 179 additions and 128 deletions.
5 changes: 3 additions & 2 deletions kubernetes/data_source_kubernetes_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package kubernetes

import (
"context"
"log"

"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"
"log"
)

func dataSourceKubernetesNamespace() *schema.Resource {
Expand Down Expand Up @@ -51,7 +52,7 @@ func dataSourceKubernetesNamespaceRead(ctx context.Context, d *schema.ResourceDa
return diag.FromErr(err)
}
log.Printf("[INFO] Received namespace: %#v", namespace)
err = d.Set("metadata", flattenMetadata(namespace.ObjectMeta, d))
err = d.Set("metadata", flattenMetadata(namespace.ObjectMeta, d, meta.(ProviderConfig).IgnoredKeys()))
if err != nil {
return diag.FromErr(err)
}
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/data_source_kubernetes_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package kubernetes
import (
"context"
"fmt"
"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"
"log"
)

func dataSourceKubernetesPod() *schema.Resource {
Expand Down Expand Up @@ -57,7 +58,7 @@ func dataSourceKubernetesPodRead(ctx context.Context, d *schema.ResourceData, me
}
log.Printf("[INFO] Received pod: %#v", pod)

err = d.Set("metadata", flattenMetadata(pod.ObjectMeta, d))
err = d.Set("metadata", flattenMetadata(pod.ObjectMeta, d, meta.(ProviderConfig).IgnoredKeys()))
if err != nil {
return diag.FromErr(err)
}
Expand Down
60 changes: 41 additions & 19 deletions kubernetes/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"bytes"
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -135,6 +135,14 @@ func Provider() *schema.Provider {
},
Description: "",
},
"ignored_metadata_keys": {
Type: schema.TypeList,
Optional: true,
Description: "List of metadata annotation and label keys that the provider will ignore for all resources and not manage. Regular expressions can be used.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -200,38 +208,47 @@ type KubeClientsets interface {
AggregatorClientset() (*aggregator.Clientset, error)
}

type kubeClientsets struct {
config *restclient.Config
type ProviderConfig interface {
IgnoredKeys() []string
}

type providerConfig struct {
clientConfig *restclient.Config
mainClientset *kubernetes.Clientset
aggregatorClientset *aggregator.Clientset
ignoredKeys []string
}

func (k kubeClientsets) MainClientset() (*kubernetes.Clientset, error) {
if k.mainClientset != nil {
return k.mainClientset, nil
func (p providerConfig) MainClientset() (*kubernetes.Clientset, error) {
if p.mainClientset != nil {
return p.mainClientset, nil
}
if k.config != nil {
kc, err := kubernetes.NewForConfig(k.config)
if p.clientConfig != nil {
kc, err := kubernetes.NewForConfig(p.clientConfig)
if err != nil {
return nil, fmt.Errorf("Failed to configure client: %s", err)
}
k.mainClientset = kc
p.mainClientset = kc
}
return k.mainClientset, nil
return p.mainClientset, nil
}

func (k kubeClientsets) AggregatorClientset() (*aggregator.Clientset, error) {
if k.aggregatorClientset != nil {
return k.aggregatorClientset, nil
func (p providerConfig) AggregatorClientset() (*aggregator.Clientset, error) {
if p.aggregatorClientset != nil {
return p.aggregatorClientset, nil
}
if k.config != nil {
ac, err := aggregator.NewForConfig(k.config)
if p.clientConfig != nil {
ac, err := aggregator.NewForConfig(p.clientConfig)
if err != nil {
return nil, fmt.Errorf("Failed to configure client: %s", err)
}
k.aggregatorClientset = ac
p.aggregatorClientset = ac
}
return k.aggregatorClientset, nil
return p.aggregatorClientset, nil
}

func (k providerConfig) IgnoredKeys() []string {
return k.ignoredKeys
}

func providerConfigure(ctx context.Context, d *schema.ResourceData, terraformVersion string) (interface{}, diag.Diagnostics) {
Expand All @@ -258,11 +275,16 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, terraformVer
}
}

m := kubeClientsets{
config: cfg,
m := providerConfig{
clientConfig: cfg,
mainClientset: nil,
aggregatorClientset: nil,
}
if v, ok := d.GetOk("ignored_metadata_keys"); ok {
for _, val := range v.([]interface{}) {
m.ignoredKeys = append(m.ignoredKeys, val.(string))
}
}
return m, diag.Diagnostics{}
}

Expand Down
5 changes: 3 additions & 2 deletions kubernetes/resource_kubernetes_api_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package kubernetes

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"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"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -144,7 +145,7 @@ func resourceKubernetesAPIServiceRead(ctx context.Context, d *schema.ResourceDat
return diag.FromErr(err)
}
log.Printf("[INFO] Received API service: %#v", svc)
err = d.Set("metadata", flattenMetadata(svc.ObjectMeta, d))
err = d.Set("metadata", flattenMetadata(svc.ObjectMeta, d, meta.(ProviderConfig).IgnoredKeys()))
if err != nil {
return diag.FromErr(err)
}
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/resource_kubernetes_cluster_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package kubernetes

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
api "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -140,7 +141,7 @@ func resourceKubernetesClusterRoleRead(ctx context.Context, d *schema.ResourceDa
return diag.FromErr(err)
}
log.Printf("[INFO] Received cluster role: %#v", cRole)
err = d.Set("metadata", flattenMetadata(cRole.ObjectMeta, d))
err = d.Set("metadata", flattenMetadata(cRole.ObjectMeta, d, meta.(ProviderConfig).IgnoredKeys()))
if err != nil {
return diag.FromErr(err)
}
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/resource_kubernetes_cluster_role_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package kubernetes

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
api "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -93,7 +94,7 @@ func resourceKubernetesClusterRoleBindingRead(ctx context.Context, d *schema.Res
}

log.Printf("[INFO] Received ClusterRoleBinding: %#v", binding)
err = d.Set("metadata", flattenMetadata(binding.ObjectMeta, d))
err = d.Set("metadata", flattenMetadata(binding.ObjectMeta, d, meta.(ProviderConfig).IgnoredKeys()))
if err != nil {
return diag.FromErr(err)
}
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/resource_kubernetes_config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package kubernetes

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
api "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -86,7 +87,7 @@ func resourceKubernetesConfigMapRead(ctx context.Context, d *schema.ResourceData
return diag.FromErr(err)
}
log.Printf("[INFO] Received config map: %#v", cfgMap)
err = d.Set("metadata", flattenMetadata(cfgMap.ObjectMeta, d))
err = d.Set("metadata", flattenMetadata(cfgMap.ObjectMeta, d, meta.(ProviderConfig).IgnoredKeys()))
if err != nil {
return diag.FromErr(err)
}
Expand Down
7 changes: 4 additions & 3 deletions kubernetes/resource_kubernetes_cron_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package kubernetes
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"k8s.io/api/batch/v1beta1"
Expand Down Expand Up @@ -156,12 +157,12 @@ func resourceKubernetesCronJobRead(ctx context.Context, d *schema.ResourceData,
}
}

err = d.Set("metadata", flattenMetadata(job.ObjectMeta, d))
err = d.Set("metadata", flattenMetadata(job.ObjectMeta, d, meta.(ProviderConfig).IgnoredKeys()))
if err != nil {
return diag.FromErr(err)
}

jobSpec, err := flattenCronJobSpec(job.Spec, d)
jobSpec, err := flattenCronJobSpec(job.Spec, d, meta.(ProviderConfig).IgnoredKeys())
if err != nil {
return diag.FromErr(err)
}
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/resource_kubernetes_csi_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package kubernetes
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -107,7 +108,7 @@ func resourceKubernetesCSIDriverRead(ctx context.Context, d *schema.ResourceData
return diag.FromErr(err)
}
log.Printf("[INFO] Received CSIDriver: %#v", CSIDriver)
err = d.Set("metadata", flattenMetadata(CSIDriver.ObjectMeta, d))
err = d.Set("metadata", flattenMetadata(CSIDriver.ObjectMeta, d, meta.(ProviderConfig).IgnoredKeys()))
if err != nil {
return diag.FromErr(err)
}
Expand Down
4 changes: 2 additions & 2 deletions kubernetes/resource_kubernetes_daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,12 @@ func resourceKubernetesDaemonSetRead(ctx context.Context, d *schema.ResourceData
}
log.Printf("[INFO] Received daemonset: %#v", daemonset)

err = d.Set("metadata", flattenMetadata(daemonset.ObjectMeta, d))
err = d.Set("metadata", flattenMetadata(daemonset.ObjectMeta, d, meta.(ProviderConfig).IgnoredKeys()))
if err != nil {
return diag.FromErr(err)
}

spec, err := flattenDaemonSetSpec(daemonset.Spec, d)
spec, err := flattenDaemonSetSpec(daemonset.Spec, d, meta.(ProviderConfig).IgnoredKeys())
if err != nil {
return diag.FromErr(err)
}
Expand Down
4 changes: 2 additions & 2 deletions kubernetes/resource_kubernetes_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@ func resourceKubernetesDeploymentRead(ctx context.Context, d *schema.ResourceDat
}
log.Printf("[INFO] Received deployment: %#v", deployment)

err = d.Set("metadata", flattenMetadata(deployment.ObjectMeta, d))
err = d.Set("metadata", flattenMetadata(deployment.ObjectMeta, d, meta.(ProviderConfig).IgnoredKeys()))
if err != nil {
return diag.FromErr(err)
}

spec, err := flattenDeploymentSpec(deployment.Spec, d)
spec, err := flattenDeploymentSpec(deployment.Spec, d, meta.(ProviderConfig).IgnoredKeys())
if err != nil {
return diag.FromErr(err)
}
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/resource_kubernetes_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package kubernetes

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
api "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -82,7 +83,7 @@ func resourceKubernetesEndpointsRead(ctx context.Context, d *schema.ResourceData
return diag.Errorf("Failed to read endpoint because: %s", err)
}
log.Printf("[INFO] Received endpoints: %#v", ep)
err = d.Set("metadata", flattenMetadata(ep.ObjectMeta, d))
err = d.Set("metadata", flattenMetadata(ep.ObjectMeta, d, meta.(ProviderConfig).IgnoredKeys()))
if err != nil {
return diag.Errorf("Failed to read endpoints because: %s", err)
}
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/resource_kubernetes_horizontal_pod_autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package kubernetes

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
api "k8s.io/api/autoscaling/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -151,7 +152,7 @@ func resourceKubernetesHorizontalPodAutoscalerRead(ctx context.Context, d *schem
}

log.Printf("[INFO] Received horizontal pod autoscaler: %#v", hpa)
err = d.Set("metadata", flattenMetadata(hpa.ObjectMeta, d))
err = d.Set("metadata", flattenMetadata(hpa.ObjectMeta, d, meta.(ProviderConfig).IgnoredKeys()))
if err != nil {
return diag.FromErr(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package kubernetes

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -64,7 +65,7 @@ func resourceKubernetesHorizontalPodAutoscalerV2Read(ctx context.Context, d *sch
return diag.FromErr(err)
}
log.Printf("[INFO] Received horizontal pod autoscaler: %#v", hpa)
err = d.Set("metadata", flattenMetadata(hpa.ObjectMeta, d))
err = d.Set("metadata", flattenMetadata(hpa.ObjectMeta, d, meta.(ProviderConfig).IgnoredKeys()))
if err != nil {
return diag.FromErr(err)
}
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/resource_kubernetes_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func resourceKubernetesIngressRead(ctx context.Context, d *schema.ResourceData,
return diag.Errorf("Failed to read Ingress '%s' because: %s", buildId(ing.ObjectMeta), err)
}
log.Printf("[INFO] Received ingress: %#v", ing)
err = d.Set("metadata", flattenMetadata(ing.ObjectMeta, d))
err = d.Set("metadata", flattenMetadata(ing.ObjectMeta, d, meta.(ProviderConfig).IgnoredKeys()))
if err != nil {
return diag.FromErr(err)
}
Expand Down
Loading

0 comments on commit 72ee6d4

Please sign in to comment.