Skip to content

Commit

Permalink
change logic for source type
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Wadsworth committed Dec 21, 2022
1 parent 034dabf commit 6f0e60c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
1 change: 1 addition & 0 deletions cmd/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/upbound/provider-gcp/internal/clients"
"github.com/upbound/provider-gcp/internal/controller"
"github.com/upbound/provider-gcp/internal/features"

_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)

Expand Down
40 changes: 11 additions & 29 deletions internal/clients/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const (
keyProject = "project"

keyCredentials = "credentials"
accountKey = "Secret"
accessToken = "AccessToken"
accessTokenCredentials = "access_token"
)
Expand Down Expand Up @@ -81,36 +80,19 @@ func TerraformSetupBuilder(version, providerSource, providerVersion string) terr
case xpv1.CredentialsSourceInjectedIdentity:
// We don't need to do anything here, as the TF Provider will take care of workloadIdentity etc.
case accessToken:
return useAccessToken(ctx, pc, client, ps)
data, err := resource.CommonCredentialExtractor(ctx, "Secret", client, pc.Spec.Credentials.CommonCredentialSelectors)
if err != nil {
return ps, errors.Wrap(err, errExtractCredentials)
}
ps.Configuration[accessTokenCredentials] = string(data)
default:
return useSecret(ctx, pc, client, ps)
data, err := resource.CommonCredentialExtractor(ctx, pc.Spec.Credentials.Source, client, pc.Spec.Credentials.CommonCredentialSelectors)
if err != nil {
return ps, errors.Wrap(err, errExtractCredentials)
}
ps.Configuration[keyCredentials] = string(data)
}
return ps, nil
}
}

func useDefault(ctx context.Context, pc *v1beta1.ProviderConfig, client client.Client) ([]byte, error) {
data, err := resource.CommonCredentialExtractor(ctx, pc.Spec.Credentials.Source, client, pc.Spec.Credentials.CommonCredentialSelectors)
if err != nil {
return nil, errors.Wrap(err, errExtractCredentials)
}
return data, nil
}

func useSecret(ctx context.Context, pc *v1beta1.ProviderConfig, client client.Client, ps terraform.Setup) (terraform.Setup, error) {
data, err := useDefault(ctx, pc, client)
if err != nil {
return ps, err
}
ps.Configuration[keyCredentials] = string(data)
return ps, nil
}

func useAccessToken(ctx context.Context, pc *v1beta1.ProviderConfig, client client.Client, ps terraform.Setup) (terraform.Setup, error) {
data, err := useDefault(ctx, pc, client)
if err != nil {
return ps, err
return ps, nil
}
ps.Configuration[accessTokenCredentials] = string(data)
return ps, nil
}

0 comments on commit 6f0e60c

Please sign in to comment.