From 053b2d0630903894e72aceff1b7d88495bfabe67 Mon Sep 17 00:00:00 2001 From: haarchri Date: Tue, 8 Feb 2022 18:26:03 +0100 Subject: [PATCH] fix(ec2): vpcpeeringconnection fixed fields for resolvers & changed tagger Signed-off-by: haarchri --- apis/ec2/generator-config.yaml | 16 ++--- apis/ec2/v1alpha1/custom_types.go | 4 ++ apis/ec2/v1alpha1/zz_generated.deepcopy.go | 27 ++------ apis/ec2/v1alpha1/zz_types.go | 4 -- .../ec2/v1alpha1/zz_vpc_peering_connection.go | 7 +- ...s.crossplane.io_vpcpeeringconnections.yaml | 10 +++ .../ec2/vpcpeeringconnection/setup.go | 65 ++++++++++++++----- .../vpcpeeringconnection/zz_conversions.go | 40 +++++------- 8 files changed, 96 insertions(+), 77 deletions(-) diff --git a/apis/ec2/generator-config.yaml b/apis/ec2/generator-config.yaml index 3561ac6d0e..0df78948d6 100644 --- a/apis/ec2/generator-config.yaml +++ b/apis/ec2/generator-config.yaml @@ -55,14 +55,14 @@ ignore: - DeleteVpcPeeringConnectionInput.DryRun - RejectVpcPeeringConnectionInput.DryRun - AcceptVpcPeeringConnectionInput.DryRun - - CreateVpcPeeringConnectionInput.VPCID - - DeleteVpcPeeringConnectionInput.VPCID - - RejectVpcPeeringConnectionInput.VPCID - - AcceptVpcPeeringConnectionInput.VPCID - - CreateVpcPeeringConnectionInput.PeerVPCID - - DeleteVpcPeeringConnectionInput.PeerVPCID - - RejectVpcPeeringConnectionInput.PeerVPCID - - AcceptVpcPeeringConnectionInput.PeerVPCID + - CreateVpcPeeringConnectionInput.VpcId + - DeleteVpcPeeringConnectionInput.VpcId + - RejectVpcPeeringConnectionInput.VpcId + - AcceptVpcPeeringConnectionInput.VpcId + - CreateVpcPeeringConnectionInput.PeerVpcId + - DeleteVpcPeeringConnectionInput.PeerVpcId + - RejectVpcPeeringConnectionInput.PeerVpcId + - AcceptVpcPeeringConnectionInput.PeerVpcId - CreateVolumeInput.DryRun - CreateVolumeInput.KmsKeyId - CreateTransitGatewayInput.DryRun diff --git a/apis/ec2/v1alpha1/custom_types.go b/apis/ec2/v1alpha1/custom_types.go index d8401665d0..b58dae7349 100644 --- a/apis/ec2/v1alpha1/custom_types.go +++ b/apis/ec2/v1alpha1/custom_types.go @@ -157,6 +157,10 @@ type CustomVPCPeeringConnectionParameters struct { // will be created, but will be in pending-acceptance state. This will only lead to an active // connection if both VPCs are in the same tenant. AcceptRequest bool `json:"acceptRequest,omitempty"` + + // Metadata tagging key value pairs + // +optional + Tags []Tag `json:"tags,omitempty"` } // CustomTransitGatewayParameters are custom parameters for TransitGateway diff --git a/apis/ec2/v1alpha1/zz_generated.deepcopy.go b/apis/ec2/v1alpha1/zz_generated.deepcopy.go index a94e051354..4d2a396500 100644 --- a/apis/ec2/v1alpha1/zz_generated.deepcopy.go +++ b/apis/ec2/v1alpha1/zz_generated.deepcopy.go @@ -1282,11 +1282,6 @@ func (in *CarrierGateway) DeepCopyInto(out *CarrierGateway) { } } } - if in.VPCID != nil { - in, out := &in.VPCID, &out.VPCID - *out = new(string) - **out = **in - } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CarrierGateway. @@ -1714,11 +1709,6 @@ func (in *ClientVPNEndpoint) DeepCopyInto(out *ClientVPNEndpoint) { } } } - if in.VPCID != nil { - in, out := &in.VPCID, &out.VPCID - *out = new(string) - **out = **in - } if in.VPNPort != nil { in, out := &in.VPNPort, &out.VPNPort *out = new(int64) @@ -2724,6 +2714,13 @@ func (in *CustomVPCPeeringConnectionParameters) DeepCopyInto(out *CustomVPCPeeri *out = new(v1.Selector) (*in).DeepCopyInto(*out) } + if in.Tags != nil { + in, out := &in.Tags, &out.Tags + *out = make([]Tag, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CustomVPCPeeringConnectionParameters. @@ -17892,11 +17889,6 @@ func (in *VPCPeeringConnectionParameters) DeepCopyInto(out *VPCPeeringConnection *out = new(string) **out = **in } - if in.PeerVPCID != nil { - in, out := &in.PeerVPCID, &out.PeerVPCID - *out = new(string) - **out = **in - } if in.TagSpecifications != nil { in, out := &in.TagSpecifications, &out.TagSpecifications *out = make([]*TagSpecification, len(*in)) @@ -17908,11 +17900,6 @@ func (in *VPCPeeringConnectionParameters) DeepCopyInto(out *VPCPeeringConnection } } } - if in.VPCID != nil { - in, out := &in.VPCID, &out.VPCID - *out = new(string) - **out = **in - } in.CustomVPCPeeringConnectionParameters.DeepCopyInto(&out.CustomVPCPeeringConnectionParameters) } diff --git a/apis/ec2/v1alpha1/zz_types.go b/apis/ec2/v1alpha1/zz_types.go index 043083855d..c05d9dcbdb 100644 --- a/apis/ec2/v1alpha1/zz_types.go +++ b/apis/ec2/v1alpha1/zz_types.go @@ -398,8 +398,6 @@ type CarrierGateway struct { OwnerID *string `json:"ownerID,omitempty"` Tags []*Tag `json:"tags,omitempty"` - - VPCID *string `json:"vpcID,omitempty"` } // +kubebuilder:skipversion @@ -524,8 +522,6 @@ type ClientVPNEndpoint struct { Tags []*Tag `json:"tags,omitempty"` - VPCID *string `json:"vpcID,omitempty"` - VPNPort *int64 `json:"vpnPort,omitempty"` } diff --git a/apis/ec2/v1alpha1/zz_vpc_peering_connection.go b/apis/ec2/v1alpha1/zz_vpc_peering_connection.go index e75ad9539d..231c34d63c 100644 --- a/apis/ec2/v1alpha1/zz_vpc_peering_connection.go +++ b/apis/ec2/v1alpha1/zz_vpc_peering_connection.go @@ -38,13 +38,8 @@ type VPCPeeringConnectionParameters struct { // // Default: The Region in which you make the request. PeerRegion *string `json:"peerRegion,omitempty"` - // The ID of the VPC with which you are creating the VPC peering connection. - // You must specify this parameter in the request. - PeerVPCID *string `json:"peerVPCID,omitempty"` // The tags to assign to the peering connection. - TagSpecifications []*TagSpecification `json:"tagSpecifications,omitempty"` - // The ID of the requester VPC. You must specify this parameter in the request. - VPCID *string `json:"vpcID,omitempty"` + TagSpecifications []*TagSpecification `json:"tagSpecifications,omitempty"` CustomVPCPeeringConnectionParameters `json:",inline"` } diff --git a/package/crds/ec2.aws.crossplane.io_vpcpeeringconnections.yaml b/package/crds/ec2.aws.crossplane.io_vpcpeeringconnections.yaml index 96565af4f3..1d2ee83764 100644 --- a/package/crds/ec2.aws.crossplane.io_vpcpeeringconnections.yaml +++ b/package/crds/ec2.aws.crossplane.io_vpcpeeringconnections.yaml @@ -129,6 +129,16 @@ spec: type: array type: object type: array + tags: + description: Metadata tagging key value pairs + items: + properties: + key: + type: string + value: + type: string + type: object + type: array vpcID: description: The ID of the requester VPC. You must specify this parameter in the request. diff --git a/pkg/controller/ec2/vpcpeeringconnection/setup.go b/pkg/controller/ec2/vpcpeeringconnection/setup.go index f371851956..5bb137c70e 100644 --- a/pkg/controller/ec2/vpcpeeringconnection/setup.go +++ b/pkg/controller/ec2/vpcpeeringconnection/setup.go @@ -2,6 +2,7 @@ package vpcpeeringconnection import ( "context" + "sort" "time" "github.com/aws/aws-sdk-go/aws" @@ -14,6 +15,7 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/ratelimiter" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/pkg/resource" + "github.com/pkg/errors" svcapitypes "github.com/crossplane/provider-aws/apis/ec2/v1alpha1" awsclients "github.com/crossplane/provider-aws/pkg/clients" @@ -24,6 +26,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller" ) +const ( + errKubeUpdateFailed = "cannot update VPCPeeringConnection" +) + // SetupVPCPeeringConnection adds a controller that reconciles VPCPeeringConnection. func SetupVPCPeeringConnection(mgr ctrl.Manager, l logging.Logger, rl workqueue.RateLimiter, poll time.Duration) error { name := managed.ControllerName(svcapitypes.VPCPeeringConnectionGroupKind) @@ -48,6 +54,7 @@ func SetupVPCPeeringConnection(mgr ctrl.Manager, l logging.Logger, rl workqueue. managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithCreationGracePeriod(3*time.Minute), managed.WithLogger(l.WithValues("controller", name)), + managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))) } @@ -115,21 +122,8 @@ func (e *custom) isUpToDate(cr *svcapitypes.VPCPeeringConnection, obj *svcsdk.De } func preCreate(ctx context.Context, cr *svcapitypes.VPCPeeringConnection, obj *svcsdk.CreateVpcPeeringConnectionInput) error { - // set external name as tag on the vpc peering connection - resType := "vpc-peering-connection" - key := "crossplane-claim-name" - value := cr.ObjectMeta.Name - - spec := svcsdk.TagSpecification{ - ResourceType: &resType, - Tags: []*svcsdk.Tag{ - { - Key: &key, - Value: &value, - }, - }, - } - obj.TagSpecifications = append(obj.TagSpecifications, &spec) + obj.PeerVpcId = cr.Spec.ForProvider.PeerVPCID + obj.VpcId = cr.Spec.ForProvider.VPCID return nil } @@ -138,8 +132,47 @@ func (e *custom) postCreate(ctx context.Context, cr *svcapitypes.VPCPeeringConne if err != nil { return managed.ExternalCreation{}, err } - // set peering connection id as external name annotation on k8s object after creation + // set peering connection id as external name annotation on k8s object after creation meta.SetExternalName(cr, aws.StringValue(obj.VpcPeeringConnection.VpcPeeringConnectionId)) return cre, nil } + +type tagger struct { + kube client.Client +} + +func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { + cr, ok := mgd.(*svcapitypes.VPCPeeringConnection) + if !ok { + return errors.New(errUnexpectedObject) + } + var vpcPeeringConnectionTags svcapitypes.TagSpecification + for _, tagSpecification := range cr.Spec.ForProvider.TagSpecifications { + if aws.StringValue(tagSpecification.ResourceType) == "vpc-peering-connection" { + vpcPeeringConnectionTags = *tagSpecification + } + } + + tagMap := map[string]string{} + tagMap["Name"] = cr.Name + for _, t := range vpcPeeringConnectionTags.Tags { + tagMap[aws.StringValue(t.Key)] = aws.StringValue(t.Value) + } + for k, v := range resource.GetExternalTags(mgd) { + tagMap[k] = v + } + vpcPeeringConnectionTags.Tags = make([]*svcapitypes.Tag, len(tagMap)) + vpcPeeringConnectionTags.ResourceType = aws.String("vpc-peering-connection") + i := 0 + for k, v := range tagMap { + vpcPeeringConnectionTags.Tags[i] = &svcapitypes.Tag{Key: aws.String(k), Value: aws.String(v)} + i++ + } + sort.Slice(vpcPeeringConnectionTags.Tags, func(i, j int) bool { + return aws.StringValue(vpcPeeringConnectionTags.Tags[i].Key) < aws.StringValue(vpcPeeringConnectionTags.Tags[j].Key) + }) + + cr.Spec.ForProvider.TagSpecifications = []*svcapitypes.TagSpecification{&vpcPeeringConnectionTags} + return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) +} diff --git a/pkg/controller/ec2/vpcpeeringconnection/zz_conversions.go b/pkg/controller/ec2/vpcpeeringconnection/zz_conversions.go index 09109525ac..2dbe68437a 100644 --- a/pkg/controller/ec2/vpcpeeringconnection/zz_conversions.go +++ b/pkg/controller/ec2/vpcpeeringconnection/zz_conversions.go @@ -213,36 +213,30 @@ func GenerateCreateVpcPeeringConnectionInput(cr *svcapitypes.VPCPeeringConnectio if cr.Spec.ForProvider.PeerRegion != nil { res.SetPeerRegion(*cr.Spec.ForProvider.PeerRegion) } - if cr.Spec.ForProvider.PeerVPCID != nil { - res.SetPeerVpcId(*cr.Spec.ForProvider.PeerVPCID) - } if cr.Spec.ForProvider.TagSpecifications != nil { - f3 := []*svcsdk.TagSpecification{} - for _, f3iter := range cr.Spec.ForProvider.TagSpecifications { - f3elem := &svcsdk.TagSpecification{} - if f3iter.ResourceType != nil { - f3elem.SetResourceType(*f3iter.ResourceType) + f2 := []*svcsdk.TagSpecification{} + for _, f2iter := range cr.Spec.ForProvider.TagSpecifications { + f2elem := &svcsdk.TagSpecification{} + if f2iter.ResourceType != nil { + f2elem.SetResourceType(*f2iter.ResourceType) } - if f3iter.Tags != nil { - f3elemf1 := []*svcsdk.Tag{} - for _, f3elemf1iter := range f3iter.Tags { - f3elemf1elem := &svcsdk.Tag{} - if f3elemf1iter.Key != nil { - f3elemf1elem.SetKey(*f3elemf1iter.Key) + if f2iter.Tags != nil { + f2elemf1 := []*svcsdk.Tag{} + for _, f2elemf1iter := range f2iter.Tags { + f2elemf1elem := &svcsdk.Tag{} + if f2elemf1iter.Key != nil { + f2elemf1elem.SetKey(*f2elemf1iter.Key) } - if f3elemf1iter.Value != nil { - f3elemf1elem.SetValue(*f3elemf1iter.Value) + if f2elemf1iter.Value != nil { + f2elemf1elem.SetValue(*f2elemf1iter.Value) } - f3elemf1 = append(f3elemf1, f3elemf1elem) + f2elemf1 = append(f2elemf1, f2elemf1elem) } - f3elem.SetTags(f3elemf1) + f2elem.SetTags(f2elemf1) } - f3 = append(f3, f3elem) + f2 = append(f2, f2elem) } - res.SetTagSpecifications(f3) - } - if cr.Spec.ForProvider.VPCID != nil { - res.SetVpcId(*cr.Spec.ForProvider.VPCID) + res.SetTagSpecifications(f2) } return res