Skip to content

Commit

Permalink
Remove bad package name "util"
Browse files Browse the repository at this point in the history
Address Nic's comments: FWIW packages named util are considered an
anti-pattern. See "bad package names" at https://blog.golang.org/package-names

Signed-off-by: zzxwill <[email protected]>
  • Loading branch information
zzxwill committed Oct 28, 2021
1 parent 6a555ef commit aa3e2f2
Show file tree
Hide file tree
Showing 18 changed files with 237 additions and 337 deletions.
153 changes: 152 additions & 1 deletion pkg/clients/alibaba.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
56 changes: 55 additions & 1 deletion pkg/util/endpoint_test.go → pkg/clients/alibaba_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
})
}
}
4 changes: 2 additions & 2 deletions pkg/controller/database/rdsinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/controller/database/rdsinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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),
},
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/nas/mountpoint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/nas/nas_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/oss/oss_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/slb/slb_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit aa3e2f2

Please sign in to comment.