Skip to content

Commit

Permalink
Use dynamic lazy rest mapper to resolve new crds instantly (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepov committed Mar 27, 2020
1 parent d01755b commit cea58b9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
10 changes: 10 additions & 0 deletions examples/0.12/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,13 @@ resource "k8s_manifest" "nginx-pvc" {
content = data.template_file.nginx-pvc.rendered
namespace = "nginx"
}

resource "k8s_manifest" "crontab-crd" {
content = file("${path.module}/../manifests/crontab-crd.yaml")
}

resource "k8s_manifest" "crontab-resource" {
content = file("${path.module}/../manifests/crontab-resource.yaml")
namespace = "nginx"
depends_on = [k8s_manifest.crontab-crd]
}
42 changes: 42 additions & 0 deletions examples/manifests/crontab-crd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Deprecated in v1.16 in favor of apiextensions.k8s.io/v1
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
# name must match the spec fields below, and be in the form: <plural>.<group>
name: crontabs.stable.example.com
spec:
# group name to use for REST API: /apis/<group>/<version>
group: stable.example.com
# list of versions supported by this CustomResourceDefinition
versions:
- name: v1
# Each version can be enabled/disabled by Served flag.
served: true
# One and only one version must be marked as the storage version.
storage: true
# either Namespaced or Cluster
scope: Namespaced
names:
# plural name to be used in the URL: /apis/<group>/<version>/<plural>
plural: crontabs
# singular name to be used as an alias on the CLI and for display
singular: crontab
# kind is normally the CamelCased singular type. Your resource manifests use this.
kind: CronTab
# shortNames allow shorter string to match your resource on the CLI
shortNames:
- ct
preserveUnknownFields: false
validation:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer
7 changes: 7 additions & 0 deletions examples/manifests/crontab-resource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: "stable.example.com/v1"
kind: CronTab
metadata:
name: my-new-cron-object
spec:
cronSpec: "* * * * */5"
image: my-awesome-cron-image
11 changes: 9 additions & 2 deletions k8s/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/mitchellh/go-homedir"

_ "k8s.io/client-go/plugin/pkg/client/auth" // to import all provider auths
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)

func Provider() terraform.ResourceProvider {
Expand Down Expand Up @@ -230,7 +230,14 @@ func providerConfigure(d *schema.ResourceData, terraformVersion string) (interfa
}
}

c, err := client.New(cfg, client.Options{})
mapper, err := apiutil.NewDynamicRESTMapper(cfg, apiutil.WithLazyDiscovery)
if err != nil {
return nil, err
}

c, err := client.New(cfg, client.Options{
Mapper: mapper,
})
if err != nil {
return nil, fmt.Errorf("Failed to configure: %s", err)
}
Expand Down

0 comments on commit cea58b9

Please sign in to comment.