Skip to content
Merged
Changes from all 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
47 changes: 44 additions & 3 deletions internal/clients/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package clients
import (
"context"
"encoding/json"
"sync"
"time"

"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -137,8 +139,26 @@ func terraformProviderConfigurationBuilder(creds githubConfig) (terraform.Provid

}

// The terraform provider currently doesn't refresh installation tokens automatically
// Therefore, the terraform provider config needs to be refreshed at least every hour
// Once this PR is merged to terraform provider, the cache expiry can be removed
// https://github.com/integrations/terraform-provider-github/pull/2695

type CachedTerraformSetup struct {
setup *terraform.Setup
expiry time.Time
}

const (
tfSetupCacheTTL = time.Minute * 55
)

// TerraformSetupBuilder builds Terraform a terraform.SetupFn function which returns Terraform provider setup configuration
//
//gocyclo:ignore
func TerraformSetupBuilder(tfProvider *schema.Provider) terraform.SetupFn {
var tfSetupLock sync.RWMutex
tfSetups := make(map[string]CachedTerraformSetup)
return func(ctx context.Context, client client.Client, mg resource.Managed) (terraform.Setup, error) {
ps := terraform.Setup{}

Expand All @@ -147,6 +167,15 @@ func TerraformSetupBuilder(tfProvider *schema.Provider) terraform.SetupFn {
return ps, errors.New(errNoProviderConfig)
}

tfSetupLock.Lock()
defer tfSetupLock.Unlock()

tfSetup, ok := tfSetups[configRef.Name]
if ok && tfSetup.expiry.After(time.Now()) {
return *tfSetup.setup, nil

}

pc := &v1beta1.ProviderConfig{}
if err := client.Get(ctx, types.NamespacedName{Name: configRef.Name}, pc); err != nil {
return ps, errors.Wrap(err, errGetProviderConfig)
Expand All @@ -163,16 +192,28 @@ func TerraformSetupBuilder(tfProvider *schema.Provider) terraform.SetupFn {
}

creds := githubConfig{}
if err := json.Unmarshal(data, &creds); err != nil {
return ps, errors.Wrap(err, errUnmarshalCredentials)
if data != nil {
if err := json.Unmarshal(data, &creds); err != nil {
return ps, errors.Wrap(err, errUnmarshalCredentials)
}
}

ps.Configuration, err = terraformProviderConfigurationBuilder(creds)
if err != nil {
return ps, errors.Wrap(err, errProviderConfigurationBuilder)
}

return ps, errors.Wrap(configureNoForkGithubClient(ctx, &ps, *tfProvider), "failed to configure the Terraform Github provider meta")
err = configureNoForkGithubClient(ctx, &ps, *tfProvider)
if err != nil {
return ps, errors.Wrap(err, "failed to configure the Terraform Github provider meta")
}

tfSetups[configRef.Name] = CachedTerraformSetup{
setup: &ps,
expiry: time.Now().Add(tfSetupCacheTTL),
}

return ps, nil

}
}
Expand Down
Loading