Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(AzureVpcPeering): delete remote peering #676

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/cloud-control/v1beta1/vpcpeering_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (b *VpcPeeringBuilder) WithAwsPeering(remoteVpcId, remoteRegion, remoteAcco
return b
}

func (b *VpcPeeringBuilder) WithDetails(localName, localNamespace, remoteName, remoteNamespace, peeringName string, importCustomRoutes bool) *VpcPeeringBuilder {
func (b *VpcPeeringBuilder) WithDetails(localName, localNamespace, remoteName, remoteNamespace, peeringName string, importCustomRoutes, deleteRemotePeering bool) *VpcPeeringBuilder {
if localName == "" {
if b.Obj.Spec.Details == nil {
return b
Expand All @@ -104,6 +104,7 @@ func (b *VpcPeeringBuilder) WithDetails(localName, localNamespace, remoteName, r
b.Obj.Spec.Details.RemoteNetwork.Namespace = remoteNamespace
b.Obj.Spec.Details.PeeringName = peeringName
b.Obj.Spec.Details.ImportCustomRoutes = importCustomRoutes
b.Obj.Spec.Details.DeleteRemotePeering = deleteRemotePeering
return b
}

Expand Down
2 changes: 2 additions & 0 deletions api/cloud-control/v1beta1/vpcpeering_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type VpcPeeringDetails struct {
LocalPeeringName string `json:"localPeeringName,omitempty"`

ImportCustomRoutes bool `json:"importCustomRoutes,omitempty"`

DeleteRemotePeering bool `json:"deleteRemotePeering,omitempty"`
}

// +kubebuilder:validation:MinProperties=1
Expand Down
2 changes: 2 additions & 0 deletions api/cloud-resources/v1beta1/azurevpcpeering_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type AzureVpcPeeringSpec struct {
// +kubebuilder:validation:Required
// +kubebuilder:validation:XValidation:rule=(self == oldSelf), message="RemoteVnet is immutable."
RemoteVnet string `json:"remoteVnet,omitempty"`

DeleteRemotePeering bool `json:"deleteRemotePeering,omitempty"`
}

// AzureVpcPeeringStatus defines the observed state of AzureVpcPeering
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ spec:
properties:
details:
properties:
deleteRemotePeering:
type: boolean
importCustomRoutes:
type: boolean
localNetwork:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ spec:
spec:
description: AzureVpcPeeringSpec defines the desired state of AzureVpcPeering
properties:
deleteRemotePeering:
type: boolean
remotePeeringName:
type: string
x-kubernetes-validations:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ spec:
properties:
details:
properties:
deleteRemotePeering:
type: boolean
importCustomRoutes:
type: boolean
localNetwork:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ spec:
spec:
description: AzureVpcPeeringSpec defines the desired state of AzureVpcPeering
properties:
deleteRemotePeering:
type: boolean
remotePeeringName:
type: string
x-kubernetes-validations:
Expand Down
8 changes: 4 additions & 4 deletions internal/api-tests/vpcpeering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
canCreate(
"VpcPeering with network details",
nb().WithScope("s").WithRemoteRef("ns", "n").
WithDetails("loc", "loc-ns", "rem", "rem-ns", "name", true),
WithDetails("loc", "loc-ns", "rem", "rem-ns", "name", true, false),
)

canNotCreate(
"VpcPeering with both network details and GPC info can not be created",
nb().WithScope("s").WithRemoteRef("ns", "n").
WithGcpPeering("peering", "project", "vpc", true).
WithDetails("loc", "loc-ns", "rem", "rem-ns", "name", true),
WithDetails("loc", "loc-ns", "rem", "rem-ns", "name", true, false),
"Only one of details or vpcPeering can be specified",
)

Expand All @@ -48,10 +48,10 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
canNotChange(
"VpcPeering network reference can not change",
nb().WithScope("s").WithRemoteRef("ns", "n").
WithDetails("loc", "loc-ns", "rem", "rem-ns", "name", true),
WithDetails("loc", "loc-ns", "rem", "rem-ns", "name", true, false),
func(b Builder[*cloudcontrolv1beta1.VpcPeering]) {
bb(b).
WithDetails("loc2", "loc-ns2", "rem2", "rem-ns2", "name2", false)
WithDetails("loc2", "loc-ns2", "rem2", "rem-ns2", "name2", false, false)
},
"Peering details are immutable",
)
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/cloud-control/vpcpeering_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
kcpPeering = (&cloudcontrolv1beta1.VpcPeeringBuilder{}).
WithScope(kymaName).
WithRemoteRef("skr-namespace", "skr-aws-ip-range").
WithDetails(localKcpNetworkName, infra.KCP().Namespace(), remoteKcpNetworkName, infra.KCP().Namespace(), "", false).
WithDetails(localKcpNetworkName, infra.KCP().Namespace(), remoteKcpNetworkName, infra.KCP().Namespace(), "", false, false).
Build()

Eventually(CreateObj).
Expand Down
11 changes: 9 additions & 2 deletions internal/controller/cloud-control/vpcpeering_azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
remoteSubscription = "afdbc79f-de19-4df4-94cd-6be2739dc0e0"
remoteResourceGroup = "MyResourceGroup"
remoteVnetName = "MyVnet"
remotePeeringName = "MyPeering"
remotePeeringName = "my-peering"
)

scope := &cloudcontrolv1beta1.Scope{}
Expand Down Expand Up @@ -66,7 +66,7 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
kcpPeering = (&cloudcontrolv1beta1.VpcPeeringBuilder{}).
WithScope(kymaName).
WithRemoteRef("skr-namespace", "skr-azure-vpcpeering").
WithDetails(localKcpNetworkName, infra.KCP().Namespace(), remoteKcpNetworkName, infra.KCP().Namespace(), remotePeeringName, true).
WithDetails(localKcpNetworkName, infra.KCP().Namespace(), remoteKcpNetworkName, infra.KCP().Namespace(), remotePeeringName, true, true).
Build()

Eventually(CreateObj).
Expand Down Expand Up @@ -270,6 +270,13 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
Expect(peering).To(BeNil())
})

By("And Then remote Azure peering exists", func() {
peering, err := azureMockRemote.GetPeering(infra.Ctx(), remoteResourceGroup, remoteVnetName, remotePeeringName)
Expect(err).To(HaveOccurred())
Expect(azuremeta.IsNotFound(err)).To(BeTrue())
Expect(peering).To(BeNil())
})

By("// cleanup: Local KCP Network", func() {
Eventually(Delete).
WithArguments(infra.Ctx(), infra.KCP().Client(), localKcpNet).
Expand Down
11 changes: 5 additions & 6 deletions pkg/kcp/provider/azure/vpcpeering/deleteVpcPeering.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ func deleteVpcPeering(ctx context.Context, st composed.State) (error, context.Co
logger := composed.LoggerFromCtx(ctx)
obj := state.ObjAsVpcPeering()

lll := logger.WithValues("vpcPeeringName", obj.Name)

if len(obj.Status.Id) == 0 {
lll.Info("VpcPeering deleted before Azure peering is created")
logger.Info("VpcPeering deleted before Azure peering is created")
return nil, nil
}

resourceGroupName := state.Scope().Spec.Scope.Azure.VpcNetwork

lll = lll.WithValues("vpcPeeringId", obj.Status.Id)
lll.Info("Deleting VpcPeering")
logger.Info("Deleting VpcPeering")

err := state.localClient.DeletePeering(
ctx,
Expand All @@ -31,8 +28,10 @@ func deleteVpcPeering(ctx context.Context, st composed.State) (error, context.Co
)

if err != nil {
return azuremeta.LogErrorAndReturn(err, "Error deleting vpc peering", composed.LoggerIntoCtx(ctx, lll))
return azuremeta.LogErrorAndReturn(err, "Error deleting vpc peering", ctx)
}

logger.Info("VpcPeering deleted")

return nil, nil
}
5 changes: 3 additions & 2 deletions pkg/kcp/provider/azure/vpcpeering/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@ func New(stateFactory StateFactory) composed.Action {
kcpNetworkRemoteLoad,
statusInitiated,
peeringLocalLoad,
remoteClientCreate,
peeringRemoteLoad,
composed.IfElse(
composed.MarkedForDeletionPredicate,
composed.ComposeActions(
"azureVpcPeering-delete",
deleteVpcPeering,
peeringRemoteDelete,
actions.PatchRemoveFinalizer,
),
composed.ComposeActions(
"azureVpcPeering-non-delete",
actions.PatchAddFinalizer,
remoteClientCreate,
peeringRemoteRequireSpecifiedName,
peeringRemoteLoad,
composed.If(
predicateRequireVNetShootTag,
vpcRemoteLoad,
Expand Down
39 changes: 39 additions & 0 deletions pkg/kcp/provider/azure/vpcpeering/peeringRemoteDelete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package vpcpeering

import (
"context"
"github.com/kyma-project/cloud-manager/pkg/composed"
azuremeta "github.com/kyma-project/cloud-manager/pkg/kcp/provider/azure/meta"
)

func peeringRemoteDelete(ctx context.Context, st composed.State) (error, context.Context) {
state := st.(*State)
logger := composed.LoggerFromCtx(ctx)

if !state.ObjAsVpcPeering().Spec.Details.DeleteRemotePeering {
return nil, nil
}

if len(state.ObjAsVpcPeering().Status.RemoteId) == 0 {
logger.Info("Remote VpcPeering deleted before Azure peering is created")
return nil, nil
}

// params must be the same as in peeringRemoteCreate()
err := state.remoteClient.DeletePeering(
ctx,
state.remoteNetworkId.ResourceGroup,
state.remoteNetworkId.NetworkName(),
state.ObjAsVpcPeering().Spec.Details.PeeringName,
)

logger.Info("Deleting remote VpcPeering")

if err != nil {
return azuremeta.LogErrorAndReturn(err, "Error deleting vpc peering", ctx)
}

logger.Info("Remote VpcPeering deleted")

return nil, nil
}
3 changes: 2 additions & 1 deletion pkg/skr/azurevpcpeering/createKpcVpcPeering.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func createKcpVpcPeering(ctx context.Context, st composed.State) (error, context
Name: state.KymaRef.Name,
},
Details: &cloudcontrolv1beta1.VpcPeeringDetails{
PeeringName: obj.Spec.RemotePeeringName,
DeleteRemotePeering: obj.Spec.DeleteRemotePeering,
PeeringName: obj.Spec.RemotePeeringName,
RemoteNetwork: klog.ObjectRef{
Name: state.RemoteNetwork.Name,
Namespace: state.RemoteNetwork.Namespace,
Expand Down
Loading