Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a limited generic YAML resource to the provider #195

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ website/vendor
# Test exclusions
!command/test-fixtures/**/*.tfstate
!command/test-fixtures/**/.terraform/
**/terraform-provider-kubernetes
21 changes: 21 additions & 0 deletions _examples/yaml/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
provider "kubernetes" {}

resource "kubernetes_yaml" "test" {
yaml_body = <<YAML
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
azure/frontdoor: enabled
spec:
rules:
- http:
paths:
- path: /testpath
backend:
serviceName: test
servicePort: 80
YAML
}
15 changes: 15 additions & 0 deletions _examples/yaml/yamltest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
azure/frontdoor: enabled
spec:
rules:
- http:
paths:
- path: /testpath
backend:
serviceName: test
servicePort: 80
13 changes: 12 additions & 1 deletion kubernetes/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,15 @@ func Provider() terraform.ResourceProvider {
"kubernetes_service_account": resourceKubernetesServiceAccount(),
"kubernetes_storage_class": resourceKubernetesStorageClass(),
"kubernetes_cluster_role_binding": resourceKubernetesClusterRoleBinding(),
"kubernetes_yaml": resourceKubernetesYAML(),
},
ConfigureFunc: providerConfigure,
}
}

// KubeProvider func to return client and config to work with K8s API
type KubeProvider func() (*kubernetes.Clientset, restclient.Config)

func providerConfigure(d *schema.ResourceData) (interface{}, error) {

var cfg *restclient.Config
Expand Down Expand Up @@ -178,7 +182,14 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return nil, fmt.Errorf("Failed to configure: %s", err)
}

return k, nil
var meta KubeProvider
meta = func() (*kubernetes.Clientset, restclient.Config) {
// Defref config to create a shallow copy, allowing each func
// to manipulate the state without affecting another func
return k, *cfg
}

return meta, nil
}

func tryLoadingConfigFile(d *schema.ResourceData) (*restclient.Config, error) {
Expand Down
3 changes: 1 addition & 2 deletions kubernetes/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/terraform-providers/terraform-provider-google/google"
api "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubernetes "k8s.io/client-go/kubernetes"
)

var testAccProviders map[string]terraform.ResourceProvider
Expand Down Expand Up @@ -247,7 +246,7 @@ func getFirstNode() (api.Node, error) {
if meta == nil {
return api.Node{}, errors.New("Provider not initialized, unable to get cluster node")
}
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()
resp, err := conn.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
return api.Node{}, err
Expand Down
11 changes: 5 additions & 6 deletions kubernetes/resource_kubernetes_cluster_role_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pkgApi "k8s.io/apimachinery/pkg/types"
kubernetes "k8s.io/client-go/kubernetes"
)

func resourceKubernetesClusterRoleBinding() *schema.Resource {
Expand Down Expand Up @@ -48,7 +47,7 @@ func resourceKubernetesClusterRoleBinding() *schema.Resource {
}

func resourceKubernetesClusterRoleBindingCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

metadata := expandMetadata(d.Get("metadata").([]interface{}))
binding := &api.ClusterRoleBinding{
Expand All @@ -69,7 +68,7 @@ func resourceKubernetesClusterRoleBindingCreate(d *schema.ResourceData, meta int
}

func resourceKubernetesClusterRoleBindingRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

name := d.Id()
log.Printf("[INFO] Reading ClusterRoleBinding %s", name)
Expand Down Expand Up @@ -103,7 +102,7 @@ func resourceKubernetesClusterRoleBindingRead(d *schema.ResourceData, meta inter
}

func resourceKubernetesClusterRoleBindingUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

name := d.Id()

Expand All @@ -128,7 +127,7 @@ func resourceKubernetesClusterRoleBindingUpdate(d *schema.ResourceData, meta int
}

func resourceKubernetesClusterRoleBindingDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

name := d.Id()
log.Printf("[INFO] Deleting ClusterRoleBinding: %#v", name)
Expand All @@ -143,7 +142,7 @@ func resourceKubernetesClusterRoleBindingDelete(d *schema.ResourceData, meta int
}

func resourceKubernetesClusterRoleBindingExists(d *schema.ResourceData, meta interface{}) (bool, error) {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

name := d.Id()
log.Printf("[INFO] Checking ClusterRoleBinding %s", name)
Expand Down
11 changes: 5 additions & 6 deletions kubernetes/resource_kubernetes_config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pkgApi "k8s.io/apimachinery/pkg/types"
kubernetes "k8s.io/client-go/kubernetes"
)

func resourceKubernetesConfigMap() *schema.Resource {
Expand All @@ -35,7 +34,7 @@ func resourceKubernetesConfigMap() *schema.Resource {
}

func resourceKubernetesConfigMapCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

metadata := expandMetadata(d.Get("metadata").([]interface{}))
cfgMap := api.ConfigMap{
Expand All @@ -54,7 +53,7 @@ func resourceKubernetesConfigMapCreate(d *schema.ResourceData, meta interface{})
}

func resourceKubernetesConfigMapRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand All @@ -77,7 +76,7 @@ func resourceKubernetesConfigMapRead(d *schema.ResourceData, meta interface{}) e
}

func resourceKubernetesConfigMapUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand Down Expand Up @@ -106,7 +105,7 @@ func resourceKubernetesConfigMapUpdate(d *schema.ResourceData, meta interface{})
}

func resourceKubernetesConfigMapDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand All @@ -125,7 +124,7 @@ func resourceKubernetesConfigMapDelete(d *schema.ResourceData, meta interface{})
}

func resourceKubernetesConfigMapExists(d *schema.ResourceData, meta interface{}) (bool, error) {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions kubernetes/resource_kubernetes_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func resourceKubernetesDeployment() *schema.Resource {
}

func resourceKubernetesDeploymentCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

metadata := expandMetadata(d.Get("metadata").([]interface{}))
spec, err := expandDeploymentSpec(d.Get("spec").([]interface{}))
Expand Down Expand Up @@ -224,7 +224,7 @@ func resourceKubernetesDeploymentCreate(d *schema.ResourceData, meta interface{}
}

func resourceKubernetesDeploymentUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand Down Expand Up @@ -265,7 +265,7 @@ func resourceKubernetesDeploymentUpdate(d *schema.ResourceData, meta interface{}
}

func resourceKubernetesDeploymentRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand Down Expand Up @@ -299,7 +299,7 @@ func resourceKubernetesDeploymentRead(d *schema.ResourceData, meta interface{})
}

func resourceKubernetesDeploymentDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand All @@ -320,7 +320,7 @@ func resourceKubernetesDeploymentDelete(d *schema.ResourceData, meta interface{}
}

func resourceKubernetesDeploymentExists(d *schema.ResourceData, meta interface{}) (bool, error) {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions kubernetes/resource_kubernetes_horizontal_pod_autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pkgApi "k8s.io/apimachinery/pkg/types"
kubernetes "k8s.io/client-go/kubernetes"
)

func resourceKubernetesHorizontalPodAutoscaler() *schema.Resource {
Expand Down Expand Up @@ -82,7 +81,7 @@ func resourceKubernetesHorizontalPodAutoscaler() *schema.Resource {
}

func resourceKubernetesHorizontalPodAutoscalerCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

metadata := expandMetadata(d.Get("metadata").([]interface{}))
svc := api.HorizontalPodAutoscaler{
Expand All @@ -102,7 +101,7 @@ func resourceKubernetesHorizontalPodAutoscalerCreate(d *schema.ResourceData, met
}

func resourceKubernetesHorizontalPodAutoscalerRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand Down Expand Up @@ -131,7 +130,7 @@ func resourceKubernetesHorizontalPodAutoscalerRead(d *schema.ResourceData, meta
}

func resourceKubernetesHorizontalPodAutoscalerUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand Down Expand Up @@ -159,7 +158,7 @@ func resourceKubernetesHorizontalPodAutoscalerUpdate(d *schema.ResourceData, met
}

func resourceKubernetesHorizontalPodAutoscalerDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand All @@ -178,7 +177,7 @@ func resourceKubernetesHorizontalPodAutoscalerDelete(d *schema.ResourceData, met
}

func resourceKubernetesHorizontalPodAutoscalerExists(d *schema.ResourceData, meta interface{}) (bool, error) {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions kubernetes/resource_kubernetes_limit_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pkgApi "k8s.io/apimachinery/pkg/types"
kubernetes "k8s.io/client-go/kubernetes"
)

func resourceKubernetesLimitRange() *schema.Resource {
Expand Down Expand Up @@ -80,7 +79,7 @@ func resourceKubernetesLimitRange() *schema.Resource {
}

func resourceKubernetesLimitRangeCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

metadata := expandMetadata(d.Get("metadata").([]interface{}))
spec, err := expandLimitRangeSpec(d.Get("spec").([]interface{}), d.IsNewResource())
Expand All @@ -103,7 +102,7 @@ func resourceKubernetesLimitRangeCreate(d *schema.ResourceData, meta interface{}
}

func resourceKubernetesLimitRangeRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand All @@ -130,7 +129,7 @@ func resourceKubernetesLimitRangeRead(d *schema.ResourceData, meta interface{})
}

func resourceKubernetesLimitRangeUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand Down Expand Up @@ -164,7 +163,7 @@ func resourceKubernetesLimitRangeUpdate(d *schema.ResourceData, meta interface{}
}

func resourceKubernetesLimitRangeDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand All @@ -184,7 +183,7 @@ func resourceKubernetesLimitRangeDelete(d *schema.ResourceData, meta interface{}
}

func resourceKubernetesLimitRangeExists(d *schema.ResourceData, meta interface{}) (bool, error) {
conn := meta.(*kubernetes.Clientset)
conn, _ := meta.(KubeProvider)()

namespace, name, err := idParts(d.Id())
if err != nil {
Expand Down
Loading