Skip to content

Commit

Permalink
fix: applied linter fixes
Browse files Browse the repository at this point in the history
Signed-off-by: André Kesser <[email protected]>
  • Loading branch information
André Kesser committed Aug 4, 2022
1 parent 6a1afd5 commit 5ffd9de
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 96 deletions.
2 changes: 2 additions & 0 deletions apis/ec2/manualv1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ var (
VPCCIDRBlockKindAPIVersion = VPCCIDRBlockKind + "." + SchemeGroupVersion.String()
VPCCIDRBlockGroupVersionKind = SchemeGroupVersion.WithKind(VPCCIDRBlockKind)
)

// SecurityGroupRule type metadata.
var (
SecurityGroupRuleKind = reflect.TypeOf(SecurityGroupRule{}).Name()
SecurityGroupRuleGroupKind = schema.GroupKind{Group: Group, Kind: SecurityGroupRuleKind}.String()
SecurityGroupRuleKindAPIVersion = SecurityGroupRuleKind + "." + SchemeGroupVersion.String()
SecurityGroupRuleGroupVersionKind = SchemeGroupVersion.WithKind(SecurityGroupRuleKind)
)

// Instance type metadata.
var (
InstanceKind = reflect.TypeOf(Instance{}).Name()
Expand Down
2 changes: 1 addition & 1 deletion apis/ec2/manualv1alpha1/securitygrouprule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type SecurityGroupRuleParameters struct {
Description *string `json:"description,omitempty"`

// +kubebuilder:validation:Optional
PrefixListId *string `json:"prefixListId,omitempty"`
PrefixListID *string `json:"prefixListId,omitempty"`

// Region is the region you'd like your resource to be created in.
// +kubebuilder:validation:Required
Expand Down
4 changes: 2 additions & 2 deletions apis/ec2/manualv1alpha1/zz_generated.deepcopy.go

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

4 changes: 3 additions & 1 deletion package/crds/ec2.aws.crossplane.io_securitygrouprules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ spec:
securityGroupId:
type: string
securityGroupIdRef:
description: A Reference to a named object.
description: If using a SecurittyGroup managed by crossplane as
reference, enable ignoreIngress or ignoreEgress on the sg to
prevent the roules to be constantly created and deleted
properties:
name:
description: Name of the referenced object.
Expand Down
4 changes: 2 additions & 2 deletions pkg/clients/ec2/fake/securitygrouprule.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ type MockSecurityGroupRuleClient struct {
MockRevokeEgress func(ctx context.Context, input *ec2.RevokeSecurityGroupEgressInput, opts []func(*ec2.Options)) (*ec2.RevokeSecurityGroupEgressOutput, error)
}

// DeleteSecurityGroup mocks DeleteSecurityGroup method
// DescribeSecurityGroupRules mocks DescribeSecurityGroupRules method
func (m *MockSecurityGroupRuleClient) DescribeSecurityGroupRules(ctx context.Context, input *ec2.DescribeSecurityGroupRulesInput, opts ...func(*ec2.Options)) (*ec2.DescribeSecurityGroupRulesOutput, error) {
return m.MockDescribe(ctx, input, opts)
}

// DescribeSecurityGroups mocks DescribeSecurityGroups method
// ModifySecurityGroupRules mocks ModifySecurityGroupRules method
func (m *MockSecurityGroupRuleClient) ModifySecurityGroupRules(ctx context.Context, input *ec2.ModifySecurityGroupRulesInput, opts ...func(*ec2.Options)) (*ec2.ModifySecurityGroupRulesOutput, error) {
return m.MockModify(ctx, input, opts)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/clients/ec2/securitygrouprule.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ec2"
)

// SecurityGroupRuleClient is the external client used for SecurityGroupRule Custom Resource
type SecurityGroupRuleClient interface {
AuthorizeSecurityGroupIngress(ctx context.Context, params *ec2.AuthorizeSecurityGroupIngressInput, optFns ...func(*ec2.Options)) (*ec2.AuthorizeSecurityGroupIngressOutput, error)
AuthorizeSecurityGroupEgress(ctx context.Context, params *ec2.AuthorizeSecurityGroupEgressInput, optFns ...func(*ec2.Options)) (*ec2.AuthorizeSecurityGroupEgressOutput, error)
Expand All @@ -16,6 +17,7 @@ type SecurityGroupRuleClient interface {
RevokeSecurityGroupEgress(ctx context.Context, params *ec2.RevokeSecurityGroupEgressInput, optFns ...func(*ec2.Options)) (*ec2.RevokeSecurityGroupEgressOutput, error)
}

// NewSecurityGroupRuleClient generates client for AWS Security Group Rule API
func NewSecurityGroupRuleClient(cfg aws.Config) SecurityGroupRuleClient {
return ec2.NewFromConfig(cfg)
}
4 changes: 2 additions & 2 deletions pkg/controller/ec2/securitygroup/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (e *external) Update(ctx context.Context, mgd resource.Managed) (managed.Ex
}
}

if awsclient.BoolValue(cr.Spec.ForProvider.IgnorIngress) != true {
if !awsclient.BoolValue(cr.Spec.ForProvider.IgnorIngress) {
add, remove := ec2.DiffPermissions(ec2.GenerateEC2Permissions(cr.Spec.ForProvider.Ingress), response.SecurityGroups[0].IpPermissions)
if len(remove) > 0 {
if _, err := e.sg.RevokeSecurityGroupIngress(ctx, &awsec2.RevokeSecurityGroupIngressInput{
Expand All @@ -261,7 +261,7 @@ func (e *external) Update(ctx context.Context, mgd resource.Managed) (managed.Ex
}
}

if awsclient.BoolValue(cr.Spec.ForProvider.IgnorEgress) != true {
if !awsclient.BoolValue(cr.Spec.ForProvider.IgnorEgress) {
add, remove := ec2.DiffPermissions(ec2.GenerateEC2Permissions(cr.Spec.ForProvider.Egress), response.SecurityGroups[0].IpPermissionsEgress)
if len(remove) > 0 {
if _, err = e.sg.RevokeSecurityGroupEgress(ctx, &awsec2.RevokeSecurityGroupEgressInput{
Expand Down
48 changes: 24 additions & 24 deletions pkg/controller/ec2/securitygrouprule/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
awsec2 "github.com/aws/aws-sdk-go-v2/service/ec2"
awsec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"

"github.com/crossplane-contrib/provider-aws/apis/ec2/manualv1alpha1"
"github.com/crossplane-contrib/provider-aws/apis/v1alpha1"

awsclient "github.com/crossplane-contrib/provider-aws/pkg/clients"
"github.com/crossplane-contrib/provider-aws/pkg/clients/ec2"
"github.com/crossplane-contrib/provider-aws/pkg/features"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/connection"
"github.com/crossplane/crossplane-runtime/pkg/controller"
Expand All @@ -25,11 +28,12 @@ import (

const (
errUnexpectedObject = "The managed resource is not an SecurityGroupRule resource"
errDescribe = "failed to describe SecurityGroupRule with id"
errCreate = "failed to create the SecurityGroupRule resource"
errDelete = "failed to delete the SecurityGroupRule resource"
ingressType = "ingress"
egressType = "egress"
)

// SetupSecurityGroupRule adds a controller that reconciles SecurityGroupRules.
func SetupSecurityGroupRule(mgr ctrl.Manager, o controller.Options) error {
name := managed.ControllerName(manualv1alpha1.SecurityGroupRuleKind)

Expand Down Expand Up @@ -58,10 +62,6 @@ type connector struct {
newClientFn func(config aws.Config) ec2.SecurityGroupRuleClient
}

type tagger struct {
kube client.Client
}

func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
cr, ok := mg.(*manualv1alpha1.SecurityGroupRule)
if !ok {
Expand Down Expand Up @@ -105,7 +105,7 @@ func (e *external) Observe(ctx context.Context, mgd resource.Managed) (managed.E
SecurityGroupRuleID: &externalName,
}
// Check if the two sgr are in sync
needsUpdate, _, _, _ := compareSgr(&cr.Spec.ForProvider, existingSgrP)
needsUpdate, _, _ := compareSgr(&cr.Spec.ForProvider, existingSgrP)
cr.SetConditions(xpv1.Available())
return managed.ExternalObservation{
ResourceExists: true,
Expand All @@ -124,8 +124,8 @@ func (e *external) Create(ctx context.Context, mgd resource.Managed) (managed.Ex
return managed.ExternalCreation{}, err
}

func (e *external) createSgr(ctx context.Context, sgr *manualv1alpha1.SecurityGroupRule) error {
if *sgr.Spec.ForProvider.Type == "ingress" {
func (e *external) createSgr(ctx context.Context, sgr *manualv1alpha1.SecurityGroupRule) error { //nolint: gocyclo
if *sgr.Spec.ForProvider.Type == ingressType {
providerValues := sgr.Spec.ForProvider
input := &awsec2.AuthorizeSecurityGroupIngressInput{
GroupId: providerValues.SecurityGroupID,
Expand Down Expand Up @@ -163,11 +163,11 @@ func (e *external) createSgr(ctx context.Context, sgr *manualv1alpha1.SecurityGr

if result != nil {
if len(result.SecurityGroupRules) > 0 && result.SecurityGroupRules[0].SecurityGroupRuleId != nil {
sgrId := result.SecurityGroupRules[0].SecurityGroupRuleId
meta.SetExternalName(sgr, awsclient.StringValue(sgrId))
sgrID := result.SecurityGroupRules[0].SecurityGroupRuleId
meta.SetExternalName(sgr, awsclient.StringValue(sgrID))
}
}
} else if *sgr.Spec.ForProvider.Type == "egress" {
} else if *sgr.Spec.ForProvider.Type == egressType {
providerValues := sgr.Spec.ForProvider
input := &awsec2.AuthorizeSecurityGroupEgressInput{
GroupId: providerValues.SecurityGroupID,
Expand Down Expand Up @@ -205,8 +205,8 @@ func (e *external) createSgr(ctx context.Context, sgr *manualv1alpha1.SecurityGr

if result != nil {
if len(result.SecurityGroupRules) > 0 && result.SecurityGroupRules[0].SecurityGroupRuleId != nil {
sgrId := result.SecurityGroupRules[0].SecurityGroupRuleId
meta.SetExternalName(sgr, awsclient.StringValue(sgrId))
sgrID := result.SecurityGroupRules[0].SecurityGroupRuleId
meta.SetExternalName(sgr, awsclient.StringValue(sgrID))
}
}
}
Expand All @@ -229,14 +229,14 @@ func (e *external) deleteSgr(ctx context.Context, sgr *manualv1alpha1.SecurityGr
func (e *external) deleteSgrForType(ctx context.Context, sgr *manualv1alpha1.SecurityGroupRule, sgrType string) error {
// We cant use the type of the sgr, because in case of an update of the type property of
// an existing sgr, we need to delete the actual sgr with the old type
if sgrType == "ingress" {
if sgrType == ingressType {
_, err := e.client.RevokeSecurityGroupIngress(ctx, &awsec2.RevokeSecurityGroupIngressInput{
SecurityGroupRuleIds: []string{meta.GetExternalName(sgr)},
GroupId: sgr.Spec.ForProvider.SecurityGroupID,
})

return awsclient.Wrap(resource.Ignore(ec2.IsCIDRNotFound, err), errDelete)
} else if sgrType == "egress" {
} else if sgrType == egressType {
_, err := e.client.RevokeSecurityGroupEgress(ctx, &awsec2.RevokeSecurityGroupEgressInput{
SecurityGroupRuleIds: []string{meta.GetExternalName(sgr)},
GroupId: sgr.Spec.ForProvider.SecurityGroupID,
Expand All @@ -252,10 +252,10 @@ func getTypeForDeletion(sgr *manualv1alpha1.SecurityGroupRule, switchType bool)
// the sgr that needs to be deleted has the old, that is opposite, type
returnType := *sgr.Spec.ForProvider.Type
if switchType {
if *&returnType == "ingress" {
return "egress"
if returnType == ingressType {
return egressType
}
return "ingress"
return ingressType
}
return returnType

Expand All @@ -272,7 +272,7 @@ func (e *external) Update(ctx context.Context, mgd resource.Managed) (managed.Ex
if err != nil {
return managed.ExternalUpdate{}, err
}
needsUpdate, recreate, typechange, err := compareSgr(&cr.Spec.ForProvider, existingSgr)
needsUpdate, recreate, typechange := compareSgr(&cr.Spec.ForProvider, existingSgr)

if needsUpdate {
if recreate {
Expand Down Expand Up @@ -313,7 +313,7 @@ func (e *external) Update(ctx context.Context, mgd resource.Managed) (managed.Ex
return managed.ExternalUpdate{}, nil
}

func compareSgr(desired *manualv1alpha1.SecurityGroupRuleParameters, actual *manualv1alpha1.SecurityGroupRuleParameters) (needsUpdate bool, recreate bool, typechange bool, err error) {
func compareSgr(desired *manualv1alpha1.SecurityGroupRuleParameters, actual *manualv1alpha1.SecurityGroupRuleParameters) (needsUpdate bool, recreate bool, typechange bool) {

needsUpdate = false
recreate = false
Expand Down Expand Up @@ -345,7 +345,7 @@ func compareSgr(desired *manualv1alpha1.SecurityGroupRuleParameters, actual *man
typechange = true
}

return needsUpdate, recreate, typechange, nil
return needsUpdate, recreate, typechange
}

func (e *external) getExternalSgr(ctx context.Context, externalName string) (*manualv1alpha1.SecurityGroupRuleParameters, error) {
Expand All @@ -358,9 +358,9 @@ func (e *external) getExternalSgr(ctx context.Context, externalName string) (*ma
return nil, err
}
existingSgr := response.SecurityGroupRules[0]
crType := "ingress"
crType := ingressType
if awsclient.BoolValue(existingSgr.IsEgress) {
crType = "egress"
crType = egressType
}
cr := &manualv1alpha1.SecurityGroupRuleParameters{
Description: existingSgr.Description,
Expand Down
Loading

0 comments on commit 5ffd9de

Please sign in to comment.