Skip to content

Commit

Permalink
chore: use mock package for test client. Move secret into selector
Browse files Browse the repository at this point in the history
  • Loading branch information
miketonks-form3 committed Jun 19, 2023
1 parent 147057d commit 2ccea4f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 36 deletions.
8 changes: 4 additions & 4 deletions api/v1alpha1/awschaos_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ type AWSChaosSpec struct {
// +optional
Duration *string `json:"duration,omitempty" webhook:"Duration"`

// SecretName defines the name of kubernetes secret.
// +optional
SecretName *string `json:"secretName,omitempty" webhook:",nilable"`

AWSSelector `json:",inline"`

// RemoteCluster represents the remote cluster where the chaos will be deployed
Expand All @@ -91,6 +87,10 @@ type AWSSelector struct {
// AWSRegion defines the region of aws.
AWSRegion string `json:"awsRegion"`

// SecretName defines the name of kubernetes secret.
// +optional
SecretName *string `json:"secretName,omitempty" webhook:",nilable"`

// Ec2Instance indicates the ID of the ec2 instance.
Ec2Instance string `json:"ec2Instance"`

Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/awschaos_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ var _ = Describe("AWSChaos", func() {
Spec: AWSChaosSpec{
Action: Ec2Stop,
AWSSelector: AWSSelector{
SecretName: &testSecretName,
Ec2Instance: testInstance,
Mode: OneMode,
},
SecretName: &testSecretName,
},
}

Expand Down
10 changes: 5 additions & 5 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 35 additions & 21 deletions pkg/selector/aws/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
"github.com/chaos-mesh/chaos-mesh/controllers/config"
"github.com/chaos-mesh/chaos-mesh/pkg/mock"
"github.com/chaos-mesh/chaos-mesh/pkg/selector/generic"
"go.uber.org/fx"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// EC2Client defines the minimum client interface required for this package
Expand All @@ -33,28 +37,24 @@ type EC2Client interface {
}

type SelectImpl struct {
EC2Client EC2Client
c client.Client
generic.Option
}

func (impl *SelectImpl) Select(ctx context.Context, awsSelector *v1alpha1.AWSSelector) ([]*v1alpha1.AWSSelector, error) {
if len(awsSelector.Filters) == 0 {
return []*v1alpha1.AWSSelector{awsSelector}, nil
}

instances := []*v1alpha1.AWSSelector{}

// we have filters, so we should lookup the cloud resources
instances := []*v1alpha1.AWSSelector{}

// TODO: for now, lazy load the client if not set - I'm unsure how to pass it in the main application
if impl.EC2Client == nil {
ec2client, err := newEc2Client(ctx, awsSelector)
if err != nil {
return nil, fmt.Errorf("failed to create client: %w", err)
}
impl.EC2Client = ec2client
ec2client, err := impl.newEc2Client(ctx, awsSelector)
if err != nil {
return nil, fmt.Errorf("failed to create client: %w", err)
}

result, err := impl.EC2Client.DescribeInstances(ctx, &ec2.DescribeInstancesInput{
result, err := ec2client.DescribeInstances(ctx, &ec2.DescribeInstancesInput{
Filters: buildEc2Filters(awsSelector.Filters),
})
if err != nil {
Expand All @@ -66,6 +66,7 @@ func (impl *SelectImpl) Select(ctx context.Context, awsSelector *v1alpha1.AWSSel
Ec2Instance: *r.Instances[0].InstanceId,
Endpoint: awsSelector.Endpoint,
AWSRegion: awsSelector.AWSRegion,
SecretName: awsSelector.SecretName,
EbsVolume: awsSelector.EbsVolume,
DeviceName: awsSelector.DeviceName,
})
Expand All @@ -81,8 +82,19 @@ func (impl *SelectImpl) Select(ctx context.Context, awsSelector *v1alpha1.AWSSel
return filteredInstances, nil
}

func New() *SelectImpl {
return &SelectImpl{}
type Params struct {
fx.In

Client client.Client
}

func New(params Params) *SelectImpl {
return &SelectImpl{
params.Client,
generic.Option{
TargetNamespace: config.ControllerCfg.TargetNamespace,
},
}
}

func buildEc2Filters(filters []*v1alpha1.AWSFilter) []ec2types.Filter {
Expand All @@ -97,8 +109,11 @@ func buildEc2Filters(filters []*v1alpha1.AWSFilter) []ec2types.Filter {
return ec2Filters
}

func newEc2Client(ctx context.Context, awsSelector *v1alpha1.AWSSelector) (*ec2.Client, error) {
func (impl *SelectImpl) newEc2Client(ctx context.Context, awsSelector *v1alpha1.AWSSelector) (EC2Client, error) {

if ec2client := mock.On("MockCreateEc2Client"); ec2client != nil {
return ec2client.(EC2Client), nil
}
opts := []func(*awscfg.LoadOptions) error{
awscfg.WithRegion(awsSelector.AWSRegion),
}
Expand All @@ -109,16 +124,15 @@ func newEc2Client(ctx context.Context, awsSelector *v1alpha1.AWSSelector) (*ec2.
})))
}

// TODO: no access to secret here, need to solve this
// if awschaos.Spec.SecretName != nil {
// TODO How to get namespace for secret??
// if awsSelector.SecretName != nil {
// secret := &v1.Secret{}
// err := impl.Client.Get(ctx, types.NamespacedName{
// Name: *awschaos.Spec.SecretName,
// Namespace: awschaos.Namespace,
// err := impl.c.Get(ctx, types.NamespacedName{
// Name: *awsSelector.SecretName,
// Namespace: impl.TargetNamespace,
// }, secret)
// if err != nil {
// impl.Log.Error(err, "fail to get cloud secret")
// return v1alpha1.NotInjected, err
// return nil, fmt.Errorf("fail to get cloud secret: %w", err)
// }
// opts = append(opts, awscfg.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
// string(secret.Data["aws_access_key_id"]),
Expand Down
11 changes: 6 additions & 5 deletions pkg/selector/aws/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/smithy-go/ptr"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
"github.com/chaos-mesh/chaos-mesh/pkg/mock"
"github.com/chaos-mesh/chaos-mesh/pkg/selector"
"github.com/chaos-mesh/chaos-mesh/pkg/selector/aws"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -52,15 +53,15 @@ func TestSelect(t *testing.T) {
Mode: v1alpha1.OneMode,
}

ec2Client := StubClient{
ec2client := StubClient{
Input: &ec2.DescribeInstancesInput{},
Output: buildInstancesOutput("1111", "2222", "3333"),
}
defer mock.With("MockCreateEc2Client", ec2client)()

s := selector.New(
selector.SelectorParams{
AWSSelector: &aws.SelectImpl{
EC2Client: ec2Client,
},
AWSSelector: &aws.SelectImpl{},
})

result, err := s.Select(ctx, sel)
Expand All @@ -78,7 +79,7 @@ func TestSelect(t *testing.T) {
Name: ptr.String("tag:Stack"),
Values: []string{"staging"},
}},
}, ec2Client.Input)
}, ec2client.Input)
}

func buildInstancesOutput(instanceIDs ...string) *ec2.DescribeInstancesOutput {
Expand Down

0 comments on commit 2ccea4f

Please sign in to comment.