diff --git a/pkg/clients/alibaba.go b/pkg/clients/alibaba.go index 5a9d43e..b03be33 100644 --- a/pkg/clients/alibaba.go +++ b/pkg/clients/alibaba.go @@ -14,4 +14,155 @@ See the License for the specific language governing permissions and limitations under the License. */ -package clients +package alibabacloud + +import ( + "fmt" + + "github.com/crossplane/crossplane-runtime/pkg/resource" + "github.com/pkg/errors" + "golang.org/x/net/context" + "gopkg.in/yaml.v2" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" + + nas "github.com/crossplane/provider-alibaba/apis/nas/v1alpha1" + oss "github.com/crossplane/provider-alibaba/apis/oss/v1alpha1" + slb "github.com/crossplane/provider-alibaba/apis/slb/v1alpha1" + sls "github.com/crossplane/provider-alibaba/apis/sls/v1alpha1" + aliv1beta1 "github.com/crossplane/provider-alibaba/apis/v1beta1" +) + +// Domain is Alibaba Cloud Domain +const Domain = "aliyuncs.com" + +const ( + // ErrPrepareClientEstablishmentInfo is the error of failing to prepare all the information for establishing an SDK client + ErrPrepareClientEstablishmentInfo = "failed to prepare all the information for establishing an SDK client" + errTrackUsage = "cannot track provider config usage" + + errRegionNotValid = "region is not valid" + errCloudResourceNotSupported = "cloud resource is not supported" + + // ErrGetProviderConfig is the error of getting provider config + ErrGetProviderConfig = "failed to get ProviderConfig" + // ErrGetCredentials is the error of getting credentials + ErrGetCredentials = "cannot get credentials" + errFailedToExtractCredentials = "failed to extract Alibaba credentials" + // ErrAccessKeyNotComplete is the error of not existing of AccessKeyID or AccessKeySecret + ErrAccessKeyNotComplete = "AccessKeyID or AccessKeySecret not existed" +) + +// AlibabaCredentials represents ak/sk, stsToken(maybe) information +type AlibabaCredentials struct { + AccessKeyID string `yaml:"accessKeyId"` + AccessKeySecret string `yaml:"accessKeySecret"` + SecurityToken string `yaml:"securityToken"` +} + +// ClientEstablishmentInfo represents all the information for establishing an SDK client +type ClientEstablishmentInfo struct { + AlibabaCredentials `json:",inline"` + Region string `json:"region"` + Endpoint string `json:"endpoint"` +} + +// PrepareClient will prepare all information to establish an Alibaba Cloud resource SDK client +func PrepareClient(ctx context.Context, mg resource.Managed, res runtime.Object, c client.Client, usage resource.Tracker, providerConfigName string) (*ClientEstablishmentInfo, error) { + info := &ClientEstablishmentInfo{} + + if err := usage.Track(ctx, mg); err != nil { + return nil, errors.Wrap(err, errTrackUsage) + } + + cred, err := GetCredentials(ctx, c, providerConfigName) + if err != nil { + return nil, errors.Wrap(err, ErrPrepareClientEstablishmentInfo) + } + info.AccessKeyID = cred.AccessKeyID + info.AccessKeySecret = cred.AccessKeySecret + info.SecurityToken = cred.SecurityToken + + region, err := GetRegion(ctx, c, providerConfigName) + if err != nil { + return nil, errors.Wrap(err, ErrPrepareClientEstablishmentInfo) + } + info.Region = region + + endpoint, err := GetEndpoint(res, region) + if err != nil { + return nil, err + } + info.Endpoint = endpoint + + return info, nil +} + +// GetEndpoint gets endpoints for all cloud resources +func GetEndpoint(res runtime.Object, region string) (string, error) { + if res == nil || res.GetObjectKind() == nil { + return "", errors.New(errCloudResourceNotSupported) + } + + if region == "" && res.GetObjectKind().GroupVersionKind().Kind != slb.CLBKind { + return "", errors.New(errRegionNotValid) + } + + var endpoint string + switch res.GetObjectKind().GroupVersionKind().Kind { + case oss.BucketKind: + endpoint = fmt.Sprintf("http://oss-%s.%s", region, Domain) + case nas.NASFileSystemKind, nas.NASMountTargetKind: + endpoint = fmt.Sprintf("nas.%s.%s", region, Domain) + case slb.CLBKind: + endpoint = fmt.Sprintf("slb.%s", Domain) + case sls.ProjectGroupKind: + endpoint = fmt.Sprintf("%s.log.%s", region, Domain) + default: + return "", errors.New(errCloudResourceNotSupported) + } + return endpoint, nil +} + +// GetProviderConfig gets ProviderConfig +func GetProviderConfig(ctx context.Context, k8sClient client.Client, providerConfigName string) (*aliv1beta1.ProviderConfig, error) { + providerConfig := &aliv1beta1.ProviderConfig{} + if err := k8sClient.Get(ctx, types.NamespacedName{Name: providerConfigName}, providerConfig); err != nil { + return nil, errors.Wrap(err, ErrGetProviderConfig) + } + return providerConfig, nil +} + +// GetCredentials gets Alibaba credentials from ProviderConfig +func GetCredentials(ctx context.Context, client client.Client, providerConfigName string) (*AlibabaCredentials, error) { + pc, err := GetProviderConfig(ctx, client, providerConfigName) + if err != nil { + return nil, err + } + + cd := pc.Spec.Credentials + data, err := resource.CommonCredentialExtractor(ctx, cd.Source, client, cd.CommonCredentialSelectors) + if err != nil { + return nil, errors.Wrap(err, ErrGetCredentials) + } + + var cred AlibabaCredentials + if err := yaml.Unmarshal(data, &cred); err != nil { + return nil, errors.Wrap(err, errFailedToExtractCredentials) + } + if cred.AccessKeyID == "" || cred.AccessKeySecret == "" { + return nil, errors.New(ErrAccessKeyNotComplete) + } + + return &cred, nil +} + +// GetRegion gets regions from ProviderConfig +func GetRegion(ctx context.Context, client client.Client, providerConfigName string) (string, error) { + pc, err := GetProviderConfig(ctx, client, providerConfigName) + if err != nil { + return "", err + } + return pc.Spec.Region, nil +} diff --git a/pkg/util/endpoint_test.go b/pkg/clients/alibaba_test.go similarity index 59% rename from pkg/util/endpoint_test.go rename to pkg/clients/alibaba_test.go index af0cea3..29de8a4 100644 --- a/pkg/util/endpoint_test.go +++ b/pkg/clients/alibaba_test.go @@ -14,19 +14,23 @@ See the License for the specific language governing permissions and limitations under the License. */ -package util +package alibabacloud import ( + "context" "fmt" "testing" + xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/client" "github.com/crossplane/provider-alibaba/apis/oss/v1alpha1" + "github.com/crossplane/provider-alibaba/apis/v1beta1" ) func TestGetEndpoint(t *testing.T) { @@ -80,3 +84,53 @@ func TestGetEndpoint(t *testing.T) { }) } } + +func TestGetCredentials(t *testing.T) { + ctx := context.TODO() + type args struct { + client client.Client + name string + } + var pc v1beta1.ProviderConfig + pc.Spec.Credentials.Source = "Secret" + pc.Spec.Credentials.SecretRef = &xpv1.SecretKeySelector{ + Key: "credentials", + } + pc.Spec.Credentials.SecretRef.Name = "default" + + type want struct { + cred *AlibabaCredentials + err error + } + cases := map[string]struct { + args args + want want + }{ + "FailedToGetProviderConfig": { + args: args{ + client: &test.MockClient{ + MockGet: test.NewMockGetFn(nil, func(obj client.Object) error { + return errors.New("E1") + }), + }, + name: "abc", + }, + want: want{ + cred: nil, + err: errors.Wrap(errors.New("E1"), ErrGetProviderConfig), + }, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + cred, err := GetCredentials(ctx, tc.args.client, tc.args.name) + if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { + t.Errorf("\nGetCredentials(...) -want error, +got error:\n%s\n", diff) + } + if diff := cmp.Diff(tc.want.cred, cred, test.EquateConditions()); diff != "" { + t.Errorf("\nGetEndpoint(...) %s\n", diff) + } + }) + } +} diff --git a/pkg/controller/database/rdsinstance.go b/pkg/controller/database/rdsinstance.go index f5c04cd..2b0b0be 100644 --- a/pkg/controller/database/rdsinstance.go +++ b/pkg/controller/database/rdsinstance.go @@ -33,8 +33,8 @@ import ( "github.com/crossplane/provider-alibaba/apis/database/v1alpha1" aliv1beta1 "github.com/crossplane/provider-alibaba/apis/v1beta1" + alibabacloud "github.com/crossplane/provider-alibaba/pkg/clients" "github.com/crossplane/provider-alibaba/pkg/clients/rds" - "github.com/crossplane/provider-alibaba/pkg/util" ) const ( @@ -81,7 +81,7 @@ func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.E return nil, errors.New(errNotRDSInstance) } - clientEstablishmentInfo, err := util.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name) + clientEstablishmentInfo, err := alibabacloud.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name) if err != nil { return nil, err } diff --git a/pkg/controller/database/rdsinstance_test.go b/pkg/controller/database/rdsinstance_test.go index f340fbf..7096661 100644 --- a/pkg/controller/database/rdsinstance_test.go +++ b/pkg/controller/database/rdsinstance_test.go @@ -17,8 +17,8 @@ import ( "github.com/crossplane/provider-alibaba/apis/database/v1alpha1" aliv1beta1 "github.com/crossplane/provider-alibaba/apis/v1beta1" + alibabacloud "github.com/crossplane/provider-alibaba/pkg/clients" "github.com/crossplane/provider-alibaba/pkg/clients/rds" - "github.com/crossplane/provider-alibaba/pkg/util" ) const ( @@ -94,7 +94,7 @@ func TestConnector(t *testing.T) { }, }, }, - want: errors.Wrap(errors.Wrap(errBoom, util.ErrGetProviderConfig), util.ErrPrepareClientEstablishmentInfo), + want: errors.Wrap(errors.Wrap(errBoom, alibabacloud.ErrGetProviderConfig), alibabacloud.ErrPrepareClientEstablishmentInfo), }, "UnsupportedCredentialsError": { reason: "An error should be returned if the selected credentials source is unsupported", @@ -124,7 +124,7 @@ func TestConnector(t *testing.T) { }, }, }, - want: errors.Wrap(errors.Wrap(errors.Errorf(errFmtUnsupportedCredSource, "wat"), errGetCredentials), util.ErrPrepareClientEstablishmentInfo), + want: errors.Wrap(errors.Wrap(errors.Errorf(errFmtUnsupportedCredSource, "wat"), errGetCredentials), alibabacloud.ErrPrepareClientEstablishmentInfo), }, "GetProviderError": { reason: "Errors getting a Provider should be returned", @@ -143,7 +143,7 @@ func TestConnector(t *testing.T) { }, }, }, - want: errors.Wrap(errors.Wrap(errBoom, util.ErrGetProviderConfig), util.ErrPrepareClientEstablishmentInfo), + want: errors.Wrap(errors.Wrap(errBoom, alibabacloud.ErrGetProviderConfig), alibabacloud.ErrPrepareClientEstablishmentInfo), }, "NoConnectionSecretError": { reason: "An error should be returned if no connection secret was specified", @@ -172,7 +172,7 @@ func TestConnector(t *testing.T) { }, }, }, - want: errors.Wrap(errors.Wrap(errors.New(errExtractSecretKey), errGetCredentials), util.ErrPrepareClientEstablishmentInfo), + want: errors.Wrap(errors.Wrap(errors.New(errExtractSecretKey), errGetCredentials), alibabacloud.ErrPrepareClientEstablishmentInfo), }, "GetConnectionSecretError": { reason: "Errors getting a secret should be returned", @@ -203,7 +203,7 @@ func TestConnector(t *testing.T) { }, }, }, - want: errors.Wrap(errors.Wrap(errors.Wrap(errBoom, errGetCredentialsSecret), errGetCredentials), util.ErrPrepareClientEstablishmentInfo), + want: errors.Wrap(errors.Wrap(errors.Wrap(errBoom, errGetCredentialsSecret), errGetCredentials), alibabacloud.ErrPrepareClientEstablishmentInfo), }, "NewRDSClientError": { reason: "Errors getting a secret should be returned", @@ -234,7 +234,7 @@ func TestConnector(t *testing.T) { }, }, }, - want: errors.Wrap(errors.New(util.ErrAccessKeyNotComplete), util.ErrPrepareClientEstablishmentInfo), + want: errors.Wrap(errors.New(alibabacloud.ErrAccessKeyNotComplete), alibabacloud.ErrPrepareClientEstablishmentInfo), }, } diff --git a/pkg/controller/nas/mountpoint_controller.go b/pkg/controller/nas/mountpoint_controller.go index 18a5157..42e1e10 100644 --- a/pkg/controller/nas/mountpoint_controller.go +++ b/pkg/controller/nas/mountpoint_controller.go @@ -31,8 +31,8 @@ import ( "github.com/crossplane/provider-alibaba/apis/nas/v1alpha1" aliv1beta1 "github.com/crossplane/provider-alibaba/apis/v1beta1" + alibabacloud "github.com/crossplane/provider-alibaba/pkg/clients" nasclient "github.com/crossplane/provider-alibaba/pkg/clients/nas" - "github.com/crossplane/provider-alibaba/pkg/util" ) const ( @@ -73,7 +73,7 @@ func (c *mtConnector) Connect(ctx context.Context, mg resource.Managed) (managed return nil, errors.New(errNotNASMountTarget) } - info, err := util.PrepareClient(ctx, mg, cr.DeepCopyObject(), c.Client, c.Usage, cr.Spec.ProviderConfigReference.Name) + info, err := alibabacloud.PrepareClient(ctx, mg, cr.DeepCopyObject(), c.Client, c.Usage, cr.Spec.ProviderConfigReference.Name) if err != nil { return nil, err } diff --git a/pkg/controller/nas/nas_controller.go b/pkg/controller/nas/nas_controller.go index 4d0a778..4b7a5d9 100644 --- a/pkg/controller/nas/nas_controller.go +++ b/pkg/controller/nas/nas_controller.go @@ -31,8 +31,8 @@ import ( "github.com/crossplane/provider-alibaba/apis/nas/v1alpha1" aliv1beta1 "github.com/crossplane/provider-alibaba/apis/v1beta1" + alibabacloud "github.com/crossplane/provider-alibaba/pkg/clients" nasclient "github.com/crossplane/provider-alibaba/pkg/clients/nas" - "github.com/crossplane/provider-alibaba/pkg/util" ) const ( @@ -74,7 +74,7 @@ func (c *Connector) Connect(ctx context.Context, mg resource.Managed) (managed.E return nil, errors.New(errNotNASFileSystem) } - info, err := util.PrepareClient(ctx, mg, cr, c.Client, c.Usage, cr.Spec.ProviderConfigReference.Name) + info, err := alibabacloud.PrepareClient(ctx, mg, cr, c.Client, c.Usage, cr.Spec.ProviderConfigReference.Name) if err != nil { return nil, err } diff --git a/pkg/controller/oss/oss_controller.go b/pkg/controller/oss/oss_controller.go index b5b8cc8..1193153 100644 --- a/pkg/controller/oss/oss_controller.go +++ b/pkg/controller/oss/oss_controller.go @@ -31,8 +31,8 @@ import ( "github.com/crossplane/provider-alibaba/apis/oss/v1alpha1" aliv1beta1 "github.com/crossplane/provider-alibaba/apis/v1beta1" + alibabacloud "github.com/crossplane/provider-alibaba/pkg/clients" ossclient "github.com/crossplane/provider-alibaba/pkg/clients/oss" - "github.com/crossplane/provider-alibaba/pkg/util" ) const ( @@ -75,7 +75,7 @@ func (c *Connector) Connect(ctx context.Context, mg resource.Managed) (managed.E return nil, errors.New(errNotBucket) } - info, err := util.PrepareClient(ctx, mg, cr, c.Client, c.Usage, cr.Spec.ProviderConfigReference.Name) + info, err := alibabacloud.PrepareClient(ctx, mg, cr, c.Client, c.Usage, cr.Spec.ProviderConfigReference.Name) if err != nil { return nil, err } diff --git a/pkg/controller/slb/slb_controller.go b/pkg/controller/slb/slb_controller.go index daf8449..c2bc5b8 100644 --- a/pkg/controller/slb/slb_controller.go +++ b/pkg/controller/slb/slb_controller.go @@ -31,8 +31,8 @@ import ( "github.com/crossplane/provider-alibaba/apis/slb/v1alpha1" aliv1beta1 "github.com/crossplane/provider-alibaba/apis/v1beta1" + alibabacloud "github.com/crossplane/provider-alibaba/pkg/clients" slbclient "github.com/crossplane/provider-alibaba/pkg/clients/slb" - "github.com/crossplane/provider-alibaba/pkg/util" ) const ( @@ -74,7 +74,7 @@ func (c *Connector) Connect(ctx context.Context, mg resource.Managed) (managed.E return nil, errors.New(errNotCLB) } - info, err := util.PrepareClient(ctx, mg, cr, c.Client, c.Usage, cr.Spec.ProviderConfigReference.Name) + info, err := alibabacloud.PrepareClient(ctx, mg, cr, c.Client, c.Usage, cr.Spec.ProviderConfigReference.Name) if err != nil { return nil, err } diff --git a/pkg/controller/sls/index_controller.go b/pkg/controller/sls/index_controller.go index af9141d..a2cecbd 100644 --- a/pkg/controller/sls/index_controller.go +++ b/pkg/controller/sls/index_controller.go @@ -33,8 +33,8 @@ import ( aliv1alpha1 "github.com/crossplane/provider-alibaba/apis/sls/v1alpha1" "github.com/crossplane/provider-alibaba/apis/v1beta1" + alibabacloud "github.com/crossplane/provider-alibaba/pkg/clients" slsclient "github.com/crossplane/provider-alibaba/pkg/clients/sls" - "github.com/crossplane/provider-alibaba/pkg/util" ) const ( @@ -75,7 +75,7 @@ func (c *indexConnector) Connect(ctx context.Context, mg resource.Managed) (mana return nil, errors.New(errNotIndex) } - info, err := util.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name) + info, err := alibabacloud.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name) if err != nil { return nil, err } diff --git a/pkg/controller/sls/logtail_controller.go b/pkg/controller/sls/logtail_controller.go index 4151be2..5db4788 100644 --- a/pkg/controller/sls/logtail_controller.go +++ b/pkg/controller/sls/logtail_controller.go @@ -31,8 +31,8 @@ import ( aliv1alpha1 "github.com/crossplane/provider-alibaba/apis/sls/v1alpha1" "github.com/crossplane/provider-alibaba/apis/v1beta1" + alibabacloud "github.com/crossplane/provider-alibaba/pkg/clients" slsclient "github.com/crossplane/provider-alibaba/pkg/clients/sls" - "github.com/crossplane/provider-alibaba/pkg/util" ) const ( @@ -73,7 +73,7 @@ func (c *logtailConnector) Connect(ctx context.Context, mg resource.Managed) (ma return nil, errors.New(errNotLogtail) } - info, err := util.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name) + info, err := alibabacloud.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name) if err != nil { return nil, err } diff --git a/pkg/controller/sls/machineGroupBinding_controller.go b/pkg/controller/sls/machineGroupBinding_controller.go index ec4a819..ee3a428 100644 --- a/pkg/controller/sls/machineGroupBinding_controller.go +++ b/pkg/controller/sls/machineGroupBinding_controller.go @@ -32,8 +32,8 @@ import ( aliv1alpha1 "github.com/crossplane/provider-alibaba/apis/sls/v1alpha1" "github.com/crossplane/provider-alibaba/apis/v1beta1" + alibabacloud "github.com/crossplane/provider-alibaba/pkg/clients" slsclient "github.com/crossplane/provider-alibaba/pkg/clients/sls" - "github.com/crossplane/provider-alibaba/pkg/util" ) const ( @@ -74,13 +74,12 @@ func (c *machineGroupBindingConnector) Connect(ctx context.Context, mg resource. return nil, errors.New(errNotMachineGroupBinding) } - info, err := util.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name) + info, err := alibabacloud.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name) if err != nil { return nil, err } - slsClient := c.NewClientFn(info.AccessKeyID, info.AccessKeySecret, - info.SecurityToken, info.Region) + slsClient := c.NewClientFn(info.AccessKeyID, info.AccessKeySecret, info.SecurityToken, info.Region) return &machineGroupBindingExternal{client: slsClient}, nil } diff --git a/pkg/controller/sls/machineGroup_controller.go b/pkg/controller/sls/machineGroup_controller.go index 082c429..32f9355 100644 --- a/pkg/controller/sls/machineGroup_controller.go +++ b/pkg/controller/sls/machineGroup_controller.go @@ -31,8 +31,8 @@ import ( aliv1alpha1 "github.com/crossplane/provider-alibaba/apis/sls/v1alpha1" "github.com/crossplane/provider-alibaba/apis/v1beta1" + alibabacloud "github.com/crossplane/provider-alibaba/pkg/clients" slsclient "github.com/crossplane/provider-alibaba/pkg/clients/sls" - "github.com/crossplane/provider-alibaba/pkg/util" ) const ( @@ -73,7 +73,7 @@ func (c *machineGroupConnector) Connect(ctx context.Context, mg resource.Managed return nil, errors.New(errNotMachineGroup) } - info, err := util.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name) + info, err := alibabacloud.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name) if err != nil { return nil, err } diff --git a/pkg/controller/sls/project_controller.go b/pkg/controller/sls/project_controller.go index 5a73424..94fdb19 100644 --- a/pkg/controller/sls/project_controller.go +++ b/pkg/controller/sls/project_controller.go @@ -34,8 +34,8 @@ import ( slsv1alpha1 "github.com/crossplane/provider-alibaba/apis/sls/v1alpha1" "github.com/crossplane/provider-alibaba/apis/v1beta1" + alibabacloud "github.com/crossplane/provider-alibaba/pkg/clients" slsclient "github.com/crossplane/provider-alibaba/pkg/clients/sls" - "github.com/crossplane/provider-alibaba/pkg/util" ) const errNotProject = "managed resource is not a SLS project custom resource" @@ -71,7 +71,7 @@ func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.E return nil, errors.New(errNotProject) } - clientEstablishmentInfo, err := util.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name) + clientEstablishmentInfo, err := alibabacloud.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name) if err != nil { return nil, err } diff --git a/pkg/controller/sls/store_controller.go b/pkg/controller/sls/store_controller.go index 7aa2859..bf7bf1d 100644 --- a/pkg/controller/sls/store_controller.go +++ b/pkg/controller/sls/store_controller.go @@ -34,8 +34,8 @@ import ( slsv1alpha1 "github.com/crossplane/provider-alibaba/apis/sls/v1alpha1" "github.com/crossplane/provider-alibaba/apis/v1beta1" + alibabacloud "github.com/crossplane/provider-alibaba/pkg/clients" slsclient "github.com/crossplane/provider-alibaba/pkg/clients/sls" - "github.com/crossplane/provider-alibaba/pkg/util" ) const ( @@ -74,7 +74,7 @@ func (c *logStoreConnector) Connect(ctx context.Context, mg resource.Managed) (m return nil, errors.New(errNotStore) } - info, err := util.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name) + info, err := alibabacloud.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name) if err != nil { return nil, err } diff --git a/pkg/util/client.go b/pkg/util/client.go deleted file mode 100644 index 6b2e499..0000000 --- a/pkg/util/client.go +++ /dev/null @@ -1,71 +0,0 @@ -/* - - Copyright 2021 The Crossplane Authors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -package util - -import ( - "github.com/crossplane/crossplane-runtime/pkg/resource" - "github.com/pkg/errors" - "golang.org/x/net/context" - "k8s.io/apimachinery/pkg/runtime" - "sigs.k8s.io/controller-runtime/pkg/client" -) - -const ( - // ErrPrepareClientEstablishmentInfo is the error of failing to prepare all the information for establishing an SDK client - ErrPrepareClientEstablishmentInfo string = "failed to prepare all the information for establishing an SDK client" - errTrackUsage string = "cannot track provider config usage" -) - -// ClientEstablishmentInfo represents all the information for establishing an SDK client -type ClientEstablishmentInfo struct { - AlibabaCredentials `json:",inline"` - Region string `json:"region"` - Endpoint string `json:"endpoint"` -} - -// PrepareClient will prepare all information to establish an Alibaba Cloud resource SDK client -func PrepareClient(ctx context.Context, mg resource.Managed, res runtime.Object, c client.Client, usage resource.Tracker, providerConfigName string) (*ClientEstablishmentInfo, error) { - info := &ClientEstablishmentInfo{} - - if err := usage.Track(ctx, mg); err != nil { - return nil, errors.Wrap(err, errTrackUsage) - } - - cred, err := GetCredentials(ctx, c, providerConfigName) - if err != nil { - return nil, errors.Wrap(err, ErrPrepareClientEstablishmentInfo) - } - info.AccessKeyID = cred.AccessKeyID - info.AccessKeySecret = cred.AccessKeySecret - info.SecurityToken = cred.SecurityToken - - region, err := GetRegion(ctx, c, providerConfigName) - if err != nil { - return nil, errors.Wrap(err, ErrPrepareClientEstablishmentInfo) - } - info.Region = region - - endpoint, err := GetEndpoint(res, region) - if err != nil { - return nil, err - } - info.Endpoint = endpoint - - return info, nil -} diff --git a/pkg/util/endpoint.go b/pkg/util/endpoint.go deleted file mode 100644 index b72b700..0000000 --- a/pkg/util/endpoint.go +++ /dev/null @@ -1,63 +0,0 @@ -/* -Copyright 2021 The Crossplane Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package util - -import ( - "fmt" - - "github.com/pkg/errors" - "k8s.io/apimachinery/pkg/runtime" - - nas "github.com/crossplane/provider-alibaba/apis/nas/v1alpha1" - oss "github.com/crossplane/provider-alibaba/apis/oss/v1alpha1" - slb "github.com/crossplane/provider-alibaba/apis/slb/v1alpha1" - sls "github.com/crossplane/provider-alibaba/apis/sls/v1alpha1" -) - -// Domain is Alibaba Cloud Domain -var Domain = "aliyuncs.com" - -var ( - errRegionNotValid = "region is not valid" - errCloudResourceNotSupported = "cloud resource is not supported" -) - -// GetEndpoint gets endpoints for all cloud resources -func GetEndpoint(res runtime.Object, region string) (string, error) { - if res == nil || res.GetObjectKind() == nil { - return "", errors.New(errCloudResourceNotSupported) - } - - if region == "" && res.GetObjectKind().GroupVersionKind().Kind != slb.CLBKind { - return "", errors.New(errRegionNotValid) - } - - var endpoint string - switch res.GetObjectKind().GroupVersionKind().Kind { - case oss.BucketKind: - endpoint = fmt.Sprintf("http://oss-%s.%s", region, Domain) - case nas.NASFileSystemKind, nas.NASMountTargetKind: - endpoint = fmt.Sprintf("nas.%s.%s", region, Domain) - case slb.CLBKind: - endpoint = fmt.Sprintf("slb.%s", Domain) - case sls.ProjectGroupKind: - endpoint = fmt.Sprintf("%s.log.%s", region, Domain) - default: - return "", errors.New(errCloudResourceNotSupported) - } - return endpoint, nil -} diff --git a/pkg/util/provider.go b/pkg/util/provider.go deleted file mode 100644 index 7abcf0e..0000000 --- a/pkg/util/provider.go +++ /dev/null @@ -1,88 +0,0 @@ -/* -Copyright 2021 The Crossplane Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package util - -import ( - "context" - - "github.com/crossplane/crossplane-runtime/pkg/resource" - "github.com/pkg/errors" - "gopkg.in/yaml.v2" - "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" - - aliv1beta1 "github.com/crossplane/provider-alibaba/apis/v1beta1" -) - -const ( - // ErrGetProviderConfig is the error of getting provider config - ErrGetProviderConfig = "failed to get ProviderConfig" - // ErrGetCredentials is the error of getting credentials - ErrGetCredentials = "cannot get credentials" - errFailedToExtractCredentials = "failed to extract Alibaba credentials" - // ErrAccessKeyNotComplete is the error of not existing of AccessKeyID or AccessKeySecret - ErrAccessKeyNotComplete = "AccessKeyID or AccessKeySecret not existed" -) - -// AlibabaCredentials represents ak/sk, stsToken(maybe) information -type AlibabaCredentials struct { - AccessKeyID string `yaml:"accessKeyId"` - AccessKeySecret string `yaml:"accessKeySecret"` - SecurityToken string `yaml:"securityToken"` -} - -// GetProviderConfig gets ProviderConfig -func GetProviderConfig(ctx context.Context, k8sClient client.Client, providerConfigName string) (*aliv1beta1.ProviderConfig, error) { - providerConfig := &aliv1beta1.ProviderConfig{} - if err := k8sClient.Get(ctx, types.NamespacedName{Name: providerConfigName}, providerConfig); err != nil { - return nil, errors.Wrap(err, ErrGetProviderConfig) - } - return providerConfig, nil -} - -// GetCredentials gets Alibaba credentials from ProviderConfig -func GetCredentials(ctx context.Context, client client.Client, providerConfigName string) (*AlibabaCredentials, error) { - pc, err := GetProviderConfig(ctx, client, providerConfigName) - if err != nil { - return nil, err - } - - cd := pc.Spec.Credentials - data, err := resource.CommonCredentialExtractor(ctx, cd.Source, client, cd.CommonCredentialSelectors) - if err != nil { - return nil, errors.Wrap(err, ErrGetCredentials) - } - - var cred AlibabaCredentials - if err := yaml.Unmarshal(data, &cred); err != nil { - return nil, errors.Wrap(err, errFailedToExtractCredentials) - } - if cred.AccessKeyID == "" || cred.AccessKeySecret == "" { - return nil, errors.New(ErrAccessKeyNotComplete) - } - - return &cred, nil -} - -// GetRegion gets regions from ProviderConfig -func GetRegion(ctx context.Context, client client.Client, providerConfigName string) (string, error) { - pc, err := GetProviderConfig(ctx, client, providerConfigName) - if err != nil { - return "", err - } - return pc.Spec.Region, nil -} diff --git a/pkg/util/provider_test.go b/pkg/util/provider_test.go deleted file mode 100644 index 3df2bb5..0000000 --- a/pkg/util/provider_test.go +++ /dev/null @@ -1,82 +0,0 @@ -/* - - Copyright 2021 The Crossplane Authors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -package util - -import ( - "context" - "testing" - - xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" - "github.com/crossplane/crossplane-runtime/pkg/test" - "github.com/google/go-cmp/cmp" - "github.com/pkg/errors" - "sigs.k8s.io/controller-runtime/pkg/client" - - "github.com/crossplane/provider-alibaba/apis/v1beta1" -) - -func TestGetCredentials(t *testing.T) { - ctx := context.TODO() - type args struct { - client client.Client - name string - } - var pc v1beta1.ProviderConfig - pc.Spec.Credentials.Source = "Secret" - pc.Spec.Credentials.SecretRef = &xpv1.SecretKeySelector{ - Key: "credentials", - } - pc.Spec.Credentials.SecretRef.Name = "default" - - type want struct { - cred *AlibabaCredentials - err error - } - cases := map[string]struct { - args args - want want - }{ - "FailedToGetProviderConfig": { - args: args{ - client: &test.MockClient{ - MockGet: test.NewMockGetFn(nil, func(obj client.Object) error { - return errors.New("E1") - }), - }, - name: "abc", - }, - want: want{ - cred: nil, - err: errors.Wrap(errors.New("E1"), ErrGetProviderConfig), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - cred, err := GetCredentials(ctx, tc.args.client, tc.args.name) - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("\nGetCredentials(...) -want error, +got error:\n%s\n", diff) - } - if diff := cmp.Diff(tc.want.cred, cred, test.EquateConditions()); diff != "" { - t.Errorf("\nGetEndpoint(...) %s\n", diff) - } - }) - } -}