Skip to content

Commit

Permalink
upgrade SLS index based on new runtime
Browse files Browse the repository at this point in the history
Signed-off-by: Zheng Xi Zhou <[email protected]>
  • Loading branch information
zzxwill committed Jul 6, 2021
1 parent 604591b commit ba64de0
Showing 1 changed file with 9 additions and 39 deletions.
48 changes: 9 additions & 39 deletions pkg/controller/sls/index_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,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 @@ -58,7 +56,7 @@ func SetupIndex(mgr ctrl.Manager, l logging.Logger) error {
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))),
managed.WithExternalConnecter(&indexConnector{
client: mgr.GetClient(),
usage: resource.NewProviderConfigUsageTracker(mgr.GetClient(), &v1alpha1.ProviderConfigUsage{}),
usage: resource.NewProviderConfigUsageTracker(mgr.GetClient(), &v1beta1.ProviderConfigUsage{}),
NewClientFn: slsclient.NewClient,
})))
}
Expand All @@ -74,45 +72,17 @@ 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(errNotIndex)
return nil, errors.New(errNotLogtail)
}

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)
return &indexExternal{client: slsClient}, nil
slsClient := c.NewClientFn(info.AccessKeyID, info.AccessKeySecret,
info.SecurityToken, info.Region)
return &external{client: slsClient}, nil
}

// indexExternal includes external SLS client
Expand Down

0 comments on commit ba64de0

Please sign in to comment.