From 566fae73bedb59db9aff39f18199cb439bc33260 Mon Sep 17 00:00:00 2001 From: John Houston Date: Thu, 30 Nov 2023 00:31:44 -0500 Subject: [PATCH] move KubernetsClientGetter into its own package --- framework/.gitignore | 2 +- .../tools/generator/templates/resource.go.tpl | 15 +++------------ framework/provider/autocrud/crud_delete.go | 2 +- framework/provider/autocrud/crud_read.go | 2 +- framework/provider/client/clientgetter.go | 12 ++++++++++++ .../provider/{ => client}/kubernetes_clients.go | 14 ++++++++++---- framework/provider/provider_configure.go | 3 ++- 7 files changed, 30 insertions(+), 20 deletions(-) create mode 100644 framework/provider/client/clientgetter.go rename framework/provider/{ => client}/kubernetes_clients.go (73%) diff --git a/framework/.gitignore b/framework/.gitignore index e9f5565bbd..903d488697 100644 --- a/framework/.gitignore +++ b/framework/.gitignore @@ -1 +1 @@ -codegen-tmp \ No newline at end of file +.codegen-tmp \ No newline at end of file diff --git a/framework/codegen/tools/generator/templates/resource.go.tpl b/framework/codegen/tools/generator/templates/resource.go.tpl index 94f4ba25b2..191e00d46a 100644 --- a/framework/codegen/tools/generator/templates/resource.go.tpl +++ b/framework/codegen/tools/generator/templates/resource.go.tpl @@ -5,9 +5,7 @@ import ( "fmt" "github.com/hashicorp/terraform-plugin-framework/resource" - - "k8s.io/client-go/discovery" - "k8s.io/client-go/dynamic" + "github.com/hashicorp/terraform-provider-kubernetes/framework/provider/client" ) // Ensure provider defined types fully satisfy framework interfaces. @@ -25,14 +23,7 @@ type {{ .ResourceConfig.Kind }} struct { APIVersion string Kind string - clientGetter KubernetesClientGetter -} - - -// FIXME move this -type KubernetesClientGetter interface { - DynamicClient() (dynamic.Interface, error) - DiscoveryClient() (discovery.DiscoveryInterface, error) + clientGetter client.KubernetesClientGetter } func (r *{{ .ResourceConfig.Kind }}) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { @@ -45,7 +36,7 @@ func (r *{{ .ResourceConfig.Kind }}) Configure(ctx context.Context, req resource return } - clientGetter, ok := req.ProviderData.(KubernetesClientGetter) + clientGetter, ok := req.ProviderData.(client.KubernetesClientGetter) if !ok { resp.Diagnostics.AddError( "Unexpected Resource Configure Type", diff --git a/framework/provider/autocrud/crud_delete.go b/framework/provider/autocrud/crud_delete.go index f2e3a18d01..25b4d37d1f 100644 --- a/framework/provider/autocrud/crud_delete.go +++ b/framework/provider/autocrud/crud_delete.go @@ -38,7 +38,7 @@ func Delete(ctx context.Context, clientGetter KubernetesClientGetter, kind, apiV var resourceInterface dynamic.ResourceInterface if mapping.Scope.Name() == meta.RESTScopeNameNamespace { - if namespace != "" { + if namespace == "" { namespace = "default" } resourceInterface = client.Resource(mapping.Resource).Namespace(namespace) diff --git a/framework/provider/autocrud/crud_read.go b/framework/provider/autocrud/crud_read.go index b9730884c5..08ec92e959 100644 --- a/framework/provider/autocrud/crud_read.go +++ b/framework/provider/autocrud/crud_read.go @@ -38,7 +38,7 @@ func Read(ctx context.Context, clientGetter KubernetesClientGetter, kind, apiVer var resourceInterface dynamic.ResourceInterface if mapping.Scope.Name() == meta.RESTScopeNameNamespace { - if namespace != "" { + if namespace == "" { namespace = "default" } resourceInterface = client.Resource(mapping.Resource).Namespace(namespace) diff --git a/framework/provider/client/clientgetter.go b/framework/provider/client/clientgetter.go new file mode 100644 index 0000000000..950413248d --- /dev/null +++ b/framework/provider/client/clientgetter.go @@ -0,0 +1,12 @@ +package client + +import ( + "k8s.io/client-go/discovery" + "k8s.io/client-go/dynamic" +) + +// KubernetesClientGetter is an interface for getting client-go interfaces +type KubernetesClientGetter interface { + DynamicClient() (dynamic.Interface, error) + DiscoveryClient() (discovery.DiscoveryInterface, error) +} diff --git a/framework/provider/kubernetes_clients.go b/framework/provider/client/kubernetes_clients.go similarity index 73% rename from framework/provider/kubernetes_clients.go rename to framework/provider/client/kubernetes_clients.go index da4c06ce0e..1c761bc452 100644 --- a/framework/provider/kubernetes_clients.go +++ b/framework/provider/client/kubernetes_clients.go @@ -1,4 +1,4 @@ -package provider +package client import ( "fmt" @@ -10,13 +10,19 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth" ) -type KubernetesClientGetter struct { +type kubernetesClientGetter struct { config *rest.Config dynamicClient dynamic.Interface discoveryClient discovery.DiscoveryInterface } -func (k KubernetesClientGetter) DynamicClient() (dynamic.Interface, error) { +func NewKubernetesClientGetter(config *rest.Config) KubernetesClientGetter { + return &kubernetesClientGetter{ + config: config, + } +} + +func (k kubernetesClientGetter) DynamicClient() (dynamic.Interface, error) { if k.dynamicClient != nil { return k.dynamicClient, nil } @@ -31,7 +37,7 @@ func (k KubernetesClientGetter) DynamicClient() (dynamic.Interface, error) { return k.dynamicClient, nil } -func (k KubernetesClientGetter) DiscoveryClient() (discovery.DiscoveryInterface, error) { +func (k kubernetesClientGetter) DiscoveryClient() (discovery.DiscoveryInterface, error) { if k.discoveryClient != nil { return k.discoveryClient, nil } diff --git a/framework/provider/provider_configure.go b/framework/provider/provider_configure.go index 4fb1041f06..655a679603 100644 --- a/framework/provider/provider_configure.go +++ b/framework/provider/provider_configure.go @@ -13,6 +13,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/provider" "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-provider-kubernetes/framework/provider/client" "github.com/hashicorp/terraform-plugin-log/tflog" @@ -37,7 +38,7 @@ func (p *KubernetesProvider) Configure(ctx context.Context, req provider.Configu resp.Diagnostics.Append(diag.NewErrorDiagnostic("failed to initilize Kubernetes client configuration", err.Error())) } - resp.ResourceData = &KubernetesClientGetter{config: cfg} + resp.ResourceData = client.NewKubernetesClientGetter(cfg) } func newKubernetesClientConfig(ctx context.Context, data KubernetesProviderModel) (*restclient.Config, error) {