Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for multiple client options on connect #433

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions apis/v1alpha3/provider_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type ProviderSpec struct {

// ProjectID is the project name (not numerical ID) of this GCP Provider.
ProjectID string `json:"projectID"`
// Endpoint overrides the default endpoint.
Endpoint string `json:"endpoint"`
// WithoutAuthentication - specifies that no authentication should be used. It is suitable only for testing and for accessing public resources.
WithoutAuthentication bool `json:"withoutAuthentication"`
evgenyKharitonov marked this conversation as resolved.
Show resolved Hide resolved
}

// +kubebuilder:object:root=true
Expand Down
4 changes: 4 additions & 0 deletions apis/v1beta1/providerconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type ProviderConfigSpec struct {

// ProjectID is the project name (not numerical ID) of this GCP ProviderConfig.
ProjectID string `json:"projectID"`
// Endpoint overrides the default endpoint.
Endpoint string `json:"endpoint"`
// WithoutAuthentication - specifies that no authentication should be used. It is suitable only for testing and for accessing public resources.
WithoutAuthentication bool `json:"withoutAuthentication"`
evgenyKharitonov marked this conversation as resolved.
Show resolved Hide resolved
}

// ProviderCredentials required to authenticate.
Expand Down
8 changes: 8 additions & 0 deletions package/crds/gcp.crossplane.io_providerconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ spec:
description: ProjectID is the project name (not numerical ID) of this
GCP ProviderConfig.
type: string
endpoint:
description: Endpoint overrides the default endpoint.
type: string
withoutAuthentication:
description: WithoutAuthentication - specifies that no authentication
should be used. It is suitable only for testing and for accessing
public resources.
type: boolean
required:
- credentials
- projectID
Expand Down
8 changes: 8 additions & 0 deletions package/crds/gcp.crossplane.io_providers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ spec:
description: ProjectID is the project name (not numerical ID) of this
GCP Provider.
type: string
endpoint:
description: Endpoint overrides the default endpoint.
type: string
withoutAuthentication:
description: WithoutAuthentication - specifies that no authentication
should be used. It is suitable only for testing and for accessing
public resources.
type: boolean
required:
- credentialsSecretRef
- projectID
Expand Down
38 changes: 31 additions & 7 deletions pkg/clients/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ import (

const scopeCloudPlatform = "https://www.googleapis.com/auth/cloud-platform"

// GetAuthInfo returns the necessary authentication information that is necessary
// GetConnectionInfo returns the necessary connection information that is necessary
// to use when the controller connects to GCP API in order to reconcile the managed
// resource.
func GetAuthInfo(ctx context.Context, c client.Client, mg resource.Managed) (projectID string, opts option.ClientOption, err error) {
func GetConnectionInfo(ctx context.Context, c client.Client, mg resource.Managed) (projectID string, opts []option.ClientOption, err error) {
switch {
case mg.GetProviderConfigReference() != nil:
return UseProviderConfig(ctx, c, mg)
Expand All @@ -59,22 +59,35 @@ func GetAuthInfo(ctx context.Context, c client.Client, mg resource.Managed) (pro

// UseProvider to return GCP authentication information.
// Deprecated: Use UseProviderConfig
func UseProvider(ctx context.Context, c client.Client, mg resource.Managed) (projectID string, opts option.ClientOption, err error) {
func UseProvider(ctx context.Context, c client.Client, mg resource.Managed) (projectID string, opts []option.ClientOption, err error) {
evgenyKharitonov marked this conversation as resolved.
Show resolved Hide resolved
opts = make([]option.ClientOption, 0)

p := &v1alpha3.Provider{}
if err := c.Get(ctx, types.NamespacedName{Name: mg.GetProviderReference().Name}, p); err != nil {
return "", nil, err
}

if len(p.Spec.Endpoint) > 0 {
opts = append(opts, option.WithEndpoint(p.Spec.Endpoint))
}

if p.Spec.WithoutAuthentication {
opts = append(opts, option.WithoutAuthentication())
}

ref := p.Spec.CredentialsSecretRef
s := &v1.Secret{}
if err := c.Get(ctx, types.NamespacedName{Name: ref.Name, Namespace: ref.Namespace}, s); err != nil {
return "", nil, err
}
return p.Spec.ProjectID, option.WithCredentialsJSON(s.Data[ref.Key]), nil
opts = append(opts, option.WithCredentialsJSON(s.Data[ref.Key]))
return p.Spec.ProjectID, opts, nil
}

// UseProviderConfig to return GCP authentication information.
func UseProviderConfig(ctx context.Context, c client.Client, mg resource.Managed) (projectID string, opts option.ClientOption, err error) {
func UseProviderConfig(ctx context.Context, c client.Client, mg resource.Managed) (projectID string, opts []option.ClientOption, err error) {
opts = make([]option.ClientOption, 0)

pc := &v1beta1.ProviderConfig{}
t := resource.NewProviderConfigUsageTracker(c, &v1beta1.ProviderConfigUsage{})
if err := t.Track(ctx, mg); err != nil {
Expand All @@ -83,20 +96,31 @@ func UseProviderConfig(ctx context.Context, c client.Client, mg resource.Managed
if err := c.Get(ctx, types.NamespacedName{Name: mg.GetProviderConfigReference().Name}, pc); err != nil {
return "", nil, err
}

if len(pc.Spec.Endpoint) > 0 {
opts = append(opts, option.WithEndpoint(pc.Spec.Endpoint))
}

if pc.Spec.WithoutAuthentication {
opts = append(opts, option.WithoutAuthentication())
}

switch s := pc.Spec.Credentials.Source; s { //nolint:exhaustive
case xpv1.CredentialsSourceInjectedIdentity:
ts, err := google.DefaultTokenSource(ctx, scopeCloudPlatform)
if err != nil {
return "", nil, errors.Wrap(err, "cannot get application default credentials token")
}
return pc.Spec.ProjectID, option.WithTokenSource(ts), nil
opts = append(opts, option.WithTokenSource(ts))
default:
data, err := resource.CommonCredentialExtractor(ctx, pc.Spec.Credentials.Source, c, pc.Spec.Credentials.CommonCredentialSelectors)
if err != nil {
return "", nil, errors.Wrap(err, "cannot get credentials")
}
return pc.Spec.ProjectID, option.WithCredentialsJSON(data), nil
opts = append(opts, option.WithCredentialsJSON(data))
}

return pc.Spec.ProjectID, opts, nil
}

// IsErrorNotFoundGRPC gets a value indicating whether the given error represents
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/cache/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ type connecter struct {
}

func (c *connecter) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
projectID, opts, err := gcp.GetAuthInfo(ctx, c.client, mg)
projectID, opts, err := gcp.GetConnectionInfo(ctx, c.client, mg)
if err != nil {
return nil, err
}
s, err := redis.NewService(ctx, opts)
s, err := redis.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/compute/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ type addressConnector struct {
}

func (c *addressConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
projectID, opts, err := gcp.GetAuthInfo(ctx, c.kube, mg)
projectID, opts, err := gcp.GetConnectionInfo(ctx, c.kube, mg)
if err != nil {
return nil, err
}
s, err := compute.NewService(ctx, opts)
s, err := compute.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/compute/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ type firewallConnector struct {
}

func (c *firewallConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
projectID, opts, err := gcp.GetAuthInfo(ctx, c.kube, mg)
projectID, opts, err := gcp.GetConnectionInfo(ctx, c.kube, mg)
if err != nil {
return nil, err
}
s, err := compute.NewService(ctx, opts)
s, err := compute.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/compute/globaladdress.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ type gaConnector struct {
}

func (c *gaConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
projectID, opts, err := gcp.GetAuthInfo(ctx, c.kube, mg)
projectID, opts, err := gcp.GetConnectionInfo(ctx, c.kube, mg)
if err != nil {
return nil, err
}
s, err := compute.NewService(ctx, opts)
s, err := compute.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/compute/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ type networkConnector struct {
}

func (c *networkConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
projectID, opts, err := gcp.GetAuthInfo(ctx, c.kube, mg)
projectID, opts, err := gcp.GetConnectionInfo(ctx, c.kube, mg)
if err != nil {
return nil, err
}
s, err := compute.NewService(ctx, opts)
s, err := compute.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/compute/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ type routerConnector struct {
}

func (c *routerConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
projectID, opts, err := gcp.GetAuthInfo(ctx, c.kube, mg)
projectID, opts, err := gcp.GetConnectionInfo(ctx, c.kube, mg)
if err != nil {
return nil, err
}
s, err := compute.NewService(ctx, opts)
s, err := compute.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/compute/subnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ type subnetworkConnector struct {
}

func (c *subnetworkConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
projectID, opts, err := gcp.GetAuthInfo(ctx, c.kube, mg)
projectID, opts, err := gcp.GetConnectionInfo(ctx, c.kube, mg)
if err != nil {
return nil, err
}
s, err := googlecompute.NewService(ctx, opts)
s, err := googlecompute.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/container/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ type clusterConnector struct {
}

func (c *clusterConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
projectID, opts, err := gcp.GetAuthInfo(ctx, c.kube, mg)
projectID, opts, err := gcp.GetConnectionInfo(ctx, c.kube, mg)
if err != nil {
return nil, err
}
s, err := container.NewService(ctx, opts)
s, err := container.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/container/nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ type nodePoolConnector struct {
}

func (c *nodePoolConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
projectID, opts, err := gcp.GetAuthInfo(ctx, c.kube, mg)
projectID, opts, err := gcp.GetConnectionInfo(ctx, c.kube, mg)
if err != nil {
return nil, err
}
s, err := container.NewService(ctx, opts)
s, err := container.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/database/cloudsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ type cloudsqlConnector struct {
}

func (c *cloudsqlConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
projectID, opts, err := gcp.GetAuthInfo(ctx, c.kube, mg)
projectID, opts, err := gcp.GetConnectionInfo(ctx, c.kube, mg)
if err != nil {
return nil, err
}
s, err := sqladmin.NewService(ctx, opts)
s, err := sqladmin.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/dns/resource_record_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ type connector struct {
}

func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
projectID, opts, err := gcp.GetAuthInfo(ctx, c.kube, mg)
projectID, opts, err := gcp.GetConnectionInfo(ctx, c.kube, mg)
if err != nil {
return nil, err
}
d, err := dns.NewService(ctx, opts)
d, err := dns.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/iam/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ type connecter struct {

// Connect sets up iam client using credentials from the provider
func (c *connecter) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
projectID, opts, err := gcp.GetAuthInfo(ctx, c.client, mg)
projectID, opts, err := gcp.GetConnectionInfo(ctx, c.client, mg)
if err != nil {
return nil, err
}
s, err := iamv1.NewService(ctx, opts)
s, err := iamv1.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/iam/serviceaccountkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ type serviceAccountKeyServiceConnector struct {

// Connect sets up SA key external client using credentials from the provider
func (c *serviceAccountKeyServiceConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
_, opts, err := gcp.GetAuthInfo(ctx, c.client, mg)
_, opts, err := gcp.GetConnectionInfo(ctx, c.client, mg)

if err != nil {
return nil, err
}

s, err := iamv1.NewService(ctx, opts)
s, err := iamv1.NewService(ctx, opts...)

if err != nil {
return nil, errors.Wrap(err, errNewClient)
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/iam/serviceaccountpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ type serviceAccountPolicyConnecter struct {

// Connect sets up iam client using credentials from the provider
func (c *serviceAccountPolicyConnecter) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
_, opts, err := gcp.GetAuthInfo(ctx, c.client, mg)
_, opts, err := gcp.GetConnectionInfo(ctx, c.client, mg)
if err != nil {
return nil, err
}
s, err := iamv1.NewService(ctx, opts)
s, err := iamv1.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/kms/cryptokey.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ type cryptoKeyConnecter struct {

// Connect sets up kms client using credentials from the provider
func (c *cryptoKeyConnecter) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
_, opts, err := gcp.GetAuthInfo(ctx, c.client, mg)
_, opts, err := gcp.GetConnectionInfo(ctx, c.client, mg)
if err != nil {
return nil, err
}
s, err := kmsv1.NewService(ctx, opts)
s, err := kmsv1.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/kms/cryptokeypolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ type cryptoKeyPolicyConnecter struct {

// Connect sets up kms client using credentials from the provider
func (c *cryptoKeyPolicyConnecter) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
_, opts, err := gcp.GetAuthInfo(ctx, c.client, mg)
_, opts, err := gcp.GetConnectionInfo(ctx, c.client, mg)
if err != nil {
return nil, err
}
s, err := kmsv1.NewService(ctx, opts)
s, err := kmsv1.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/kms/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ func (c *keyRingConnecter) Connect(ctx context.Context, mg resource.Managed) (ma
return nil, errors.New(errNotKeyRing)
}

projectID, opts, err := gcp.GetAuthInfo(ctx, c.client, mg)
projectID, opts, err := gcp.GetConnectionInfo(ctx, c.client, mg)
if err != nil {
return nil, err
}
s, err := kmsv1.NewService(ctx, opts)
s, err := kmsv1.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/pubsub/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ type subscriptionConnector struct {

// Connect returns an ExternalClient with necessary information to talk to GCP API.
func (c *subscriptionConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
projectID, opts, err := gcp.GetAuthInfo(ctx, c.client, mg)
projectID, opts, err := gcp.GetConnectionInfo(ctx, c.client, mg)
if err != nil {
return nil, err
}

s, err := pubsub.NewService(ctx, opts)
s, err := pubsub.NewService(ctx, opts...)
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand Down
Loading