Skip to content

Commit

Permalink
Use golang.org/x/sync/errgroup
Browse files Browse the repository at this point in the history
Signed-off-by: Max Melentyev <[email protected]>
  • Loading branch information
max-melentyev committed Apr 29, 2024
1 parent 97c6fab commit 6482b89
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.16.0
go.uber.org/zap v1.26.0
golang.org/x/sync v0.7.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
k8s.io/api v0.28.3
k8s.io/apiextensions-apiserver v0.28.3
Expand Down Expand Up @@ -125,7 +126,6 @@ require (
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
58 changes: 26 additions & 32 deletions pkg/controller/ec2/instance/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package instance

import (
"context"
"sync"
"sync/atomic"

"github.com/aws/aws-sdk-go-v2/aws"
awsec2 "github.com/aws/aws-sdk-go-v2/service/ec2"
Expand All @@ -33,6 +31,7 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -181,58 +180,53 @@ func (e *external) describeInstance(ctx context.Context, instanceId string) (
awsec2.DescribeInstanceAttributeOutput,
error,
) {
wg := sync.WaitGroup{}
eg := errgroup.Group{}

var describeOutput *awsec2.DescribeInstancesOutput
var describeError error
wg.Add(1)
go func() {
eg.Go(func() error {
describeOutput, describeError = e.client.DescribeInstances(ctx, &awsec2.DescribeInstancesInput{
InstanceIds: []string{instanceId},
})
wg.Done()
}()
return nil
})

attrs := awsec2.DescribeInstanceAttributeOutput{}
attrsErr := atomic.Pointer[error]{}
descAttr := func(attr types.InstanceAttributeName) (*awsec2.DescribeInstanceAttributeOutput, error) {
return e.client.DescribeInstanceAttribute(ctx, &awsec2.DescribeInstanceAttributeInput{
InstanceId: &instanceId,
Attribute: attr,
})
}

wg.Add(1)
go func() {
if r, err := descAttr(types.InstanceAttributeNameDisableApiTermination); err != nil {
attrsErr.Store(&err)
eg.Go(func() error {
if res, err := descAttr(types.InstanceAttributeNameDisableApiTermination); err != nil {
return errorutils.Wrap(err, "fetching DisableApiTermination")
} else {
attrs.DisableApiTermination = r.DisableApiTermination
attrs.DisableApiTermination = res.DisableApiTermination
return nil
}
wg.Done()
}()
})

wg.Add(1)
go func() {
if r, err := descAttr(types.InstanceAttributeNameInstanceInitiatedShutdownBehavior); err != nil {
attrsErr.Store(&err)
eg.Go(func() error {
if res, err := descAttr(types.InstanceAttributeNameInstanceInitiatedShutdownBehavior); err != nil {
return errorutils.Wrap(err, "fetching InstanceInitiatedShutdownBehavior")
} else {
attrs.InstanceInitiatedShutdownBehavior = r.InstanceInitiatedShutdownBehavior
attrs.InstanceInitiatedShutdownBehavior = res.InstanceInitiatedShutdownBehavior
return nil
}
wg.Done()
}()
})

wg.Add(1)
go func() {
if r, err := descAttr(types.InstanceAttributeNameUserData); err != nil {
attrsErr.Store(&err)
eg.Go(func() error {
if res, err := descAttr(types.InstanceAttributeNameUserData); err != nil {
return errorutils.Wrap(err, "fetching UserData")
} else {
attrs.UserData = r.UserData
attrs.UserData = res.UserData
return nil
}
wg.Done()
}()
})

wg.Wait()
attrsErr := eg.Wait()

if describeError != nil {
return nil, attrs,
Expand All @@ -250,8 +244,8 @@ func (e *external) describeInstance(ctx context.Context, instanceId string) (
return nil, attrs, errors.New(errMultipleItems)
}

if err := attrsErr.Load(); err != nil {
return nil, attrs, errorutils.Wrap(*err, errDescribe)
if attrsErr != nil {
return nil, attrs, errorutils.Wrap(attrsErr, errDescribe)
}
return &describeOutput.Reservations[0].Instances[0], attrs, nil
}
Expand Down

0 comments on commit 6482b89

Please sign in to comment.