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

Upgrade ProviderConfig version to v1beta1 #95

Merged
merged 2 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion pkg/controller/sls/index_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type indexConnector struct {
func (c *indexConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
cr, ok := mg.(*aliv1alpha1.LogstoreIndex)
if !ok {
return nil, errors.New(errNotLogtail)
return nil, errors.New(errNotIndex)
}

info, err := util.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name)
Expand Down
44 changes: 7 additions & 37 deletions pkg/controller/sls/machineGroupBinding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

aliv1alpha1 "github.com/crossplane/provider-alibaba/apis/sls/v1alpha1"
"github.com/crossplane/provider-alibaba/apis/v1alpha1"
"github.com/crossplane/provider-alibaba/apis/v1beta1"
slsclient "github.com/crossplane/provider-alibaba/pkg/clients/sls"
"github.com/crossplane/provider-alibaba/pkg/util"
)
Expand All @@ -57,7 +55,7 @@ func SetupMachineGroupBinding(mgr ctrl.Manager, l logging.Logger) error {
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))),
managed.WithExternalConnecter(&machineGroupBindingConnector{
client: mgr.GetClient(),
usage: resource.NewProviderConfigUsageTracker(mgr.GetClient(), &v1alpha1.ProviderConfigUsage{}),
usage: resource.NewProviderConfigUsageTracker(mgr.GetClient(), &v1beta1.ProviderConfigUsage{}),
NewClientFn: slsclient.NewClient,
})))
}
Expand All @@ -76,41 +74,13 @@ func (c *machineGroupBindingConnector) Connect(ctx context.Context, mg resource.
return nil, errors.New(errNotMachineGroupBinding)
}

var (
sel *xpv1.SecretKeySelector
region string
)

switch {
case cr.GetProviderConfigReference() != nil:
if err := c.usage.Track(ctx, mg); err != nil {
return nil, errors.Wrap(err, errTrackUsage)
}

pc := &v1alpha1.ProviderConfig{}
if err := c.client.Get(ctx, types.NamespacedName{Name: cr.Spec.ProviderConfigReference.Name}, pc); err != nil {
return nil, errors.Wrap(err, errGetProviderConfig)
}
if s := pc.Spec.Credentials.Source; s != xpv1.CredentialsSourceSecret {
return nil, errors.Errorf(errFmtUnsupportedCredSource, s)
}
sel = pc.Spec.Credentials.SecretRef
region = pc.Spec.Region
default:
return nil, errors.New(errNoProvider)
}

if sel == nil {
return nil, errors.New(errNoConnectionSecret)
}

s := &corev1.Secret{}
nn := types.NamespacedName{Namespace: sel.Namespace, Name: sel.Name}
if err := c.client.Get(ctx, nn, s); err != nil {
return nil, errors.Wrap(err, errGetConnectionSecret)
info, err := util.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not introduced in this PR, but FWIW packages named util are considered an anti-pattern. See "bad package names" at https://blog.golang.org/package-names.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Fixing this bad package in #96.

if err != nil {
return nil, err
}

slsClient := c.NewClientFn(string(s.Data[util.AccessKeyID]), string(s.Data[util.AccessKeySecret]), string(s.Data[util.SecurityToken]), region)
slsClient := c.NewClientFn(info.AccessKeyID, info.AccessKeySecret,
info.SecurityToken, info.Region)
return &machineGroupBindingExternal{client: slsClient}, nil
}

Expand Down
44 changes: 7 additions & 37 deletions pkg/controller/sls/machineGroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

aliv1alpha1 "github.com/crossplane/provider-alibaba/apis/sls/v1alpha1"
"github.com/crossplane/provider-alibaba/apis/v1alpha1"
"github.com/crossplane/provider-alibaba/apis/v1beta1"
slsclient "github.com/crossplane/provider-alibaba/pkg/clients/sls"
"github.com/crossplane/provider-alibaba/pkg/util"
)
Expand All @@ -56,7 +54,7 @@ func SetupMachineGroup(mgr ctrl.Manager, l logging.Logger) error {
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))),
managed.WithExternalConnecter(&machineGroupConnector{
client: mgr.GetClient(),
usage: resource.NewProviderConfigUsageTracker(mgr.GetClient(), &v1alpha1.ProviderConfigUsage{}),
usage: resource.NewProviderConfigUsageTracker(mgr.GetClient(), &v1beta1.ProviderConfigUsage{}),
NewClientFn: slsclient.NewClient,
})))
}
Expand All @@ -75,41 +73,13 @@ func (c *machineGroupConnector) Connect(ctx context.Context, mg resource.Managed
return nil, errors.New(errNotMachineGroup)
}

var (
sel *xpv1.SecretKeySelector
region string
)

switch {
case cr.GetProviderConfigReference() != nil:
if err := c.usage.Track(ctx, mg); err != nil {
return nil, errors.Wrap(err, errTrackUsage)
}

pc := &v1alpha1.ProviderConfig{}
if err := c.client.Get(ctx, types.NamespacedName{Name: cr.Spec.ProviderConfigReference.Name}, pc); err != nil {
return nil, errors.Wrap(err, errGetProviderConfig)
}
if s := pc.Spec.Credentials.Source; s != xpv1.CredentialsSourceSecret {
return nil, errors.Errorf(errFmtUnsupportedCredSource, s)
}
sel = pc.Spec.Credentials.SecretRef
region = pc.Spec.Region
default:
return nil, errors.New(errNoProvider)
}

if sel == nil {
return nil, errors.New(errNoConnectionSecret)
}

s := &corev1.Secret{}
nn := types.NamespacedName{Namespace: sel.Namespace, Name: sel.Name}
if err := c.client.Get(ctx, nn, s); err != nil {
return nil, errors.Wrap(err, errGetConnectionSecret)
info, err := util.PrepareClient(ctx, mg, cr, c.client, c.usage, cr.Spec.ProviderConfigReference.Name)
if err != nil {
return nil, err
}

slsClient := c.NewClientFn(string(s.Data[util.AccessKeyID]), string(s.Data[util.AccessKeySecret]), string(s.Data[util.SecurityToken]), region)
slsClient := c.NewClientFn(info.AccessKeyID, info.AccessKeySecret,
info.SecurityToken, info.Region)
return &machineGroupExternal{client: slsClient}, nil
}

Expand Down