Skip to content

Commit

Permalink
move KubernetsClientGetter into its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhouston committed Nov 30, 2023
1 parent 5d8d988 commit 566fae7
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion framework/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
codegen-tmp
.codegen-tmp
15 changes: 3 additions & 12 deletions framework/codegen/tools/generator/templates/resource.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion framework/provider/autocrud/crud_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion framework/provider/autocrud/crud_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions framework/provider/client/clientgetter.go
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider
package client

import (
"fmt"
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion framework/provider/provider_configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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) {
Expand Down

0 comments on commit 566fae7

Please sign in to comment.