Skip to content

Commit c639255

Browse files
committed
chore(VpcPeering): remove duplicated code that loads local and remote KCP Network
1 parent ddd6898 commit c639255

File tree

4 files changed

+377
-5
lines changed

4 files changed

+377
-5
lines changed

internal/controller/cloud-control/vpcpeering_azure_test.go

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,4 +1078,238 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
10781078
Should(Succeed())
10791079
})
10801080
})
1081+
1082+
It("Scenario: KCP Azure VpcPeering error if Network not found", func() {
1083+
const (
1084+
kymaName = "81e57df5-8345-480b-ad15-45d5c108941f"
1085+
kcpPeeringName = "fe4cf1de-ebee-47bf-8d97-d117eb731369"
1086+
remoteSubscription = "dc68e4d2-1305-4e20-aded-032774eab076"
1087+
remoteTenant = "ca491e69-d2a5-4026-8384-c948e1601b7a"
1088+
remoteResourceGroup = "MyResourceGroup"
1089+
remoteVnetName = "MyVnet"
1090+
remotePeeringName = "my-peering"
1091+
localPeeringName = "kyma-peering"
1092+
)
1093+
1094+
scope := &cloudcontrolv1beta1.Scope{}
1095+
1096+
By("Given Scope exists", func() {
1097+
// Tell Scope reconciler to ignore this kymaName
1098+
kcpscope.Ignore.AddName(kymaName)
1099+
1100+
Eventually(CreateScopeAzure).
1101+
WithArguments(infra.Ctx(), infra, scope, WithName(kymaName)).
1102+
Should(Succeed())
1103+
})
1104+
1105+
localResourceGroupName := scope.Spec.Scope.Azure.VpcNetwork
1106+
localVirtualNetworkName := scope.Spec.Scope.Azure.VpcNetwork
1107+
1108+
azureMockLocal := infra.AzureMock().MockConfigs(scope.Spec.Scope.Azure.SubscriptionId, scope.Spec.Scope.Azure.TenantId)
1109+
azureMockRemote := infra.AzureMock().MockConfigs(remoteSubscription, remoteTenant)
1110+
1111+
By("And Given local Azure VNET exists", func() {
1112+
err := azureMockLocal.CreateNetwork(infra.Ctx(), localResourceGroupName, localVirtualNetworkName, scope.Spec.Region, "10.200.0.0/25", nil)
1113+
Expect(err).ToNot(HaveOccurred())
1114+
})
1115+
1116+
By("And Given remote Azure VNet exists with Kyma tag", func() {
1117+
err := azureMockRemote.CreateNetwork(infra.Ctx(), remoteResourceGroup, remoteVnetName, scope.Spec.Region, "10.100.0.0/25", map[string]string{kymaName: kymaName})
1118+
Expect(err).ToNot(HaveOccurred())
1119+
})
1120+
1121+
localKcpNetworkName := common.KcpNetworkKymaCommonName(scope.Name)
1122+
remoteKcpNetworkName := scope.Name + "--remote"
1123+
1124+
var kcpPeering *cloudcontrolv1beta1.VpcPeering
1125+
1126+
By("And Given KCP VpcPeering is created", func() {
1127+
kcpPeering = (&cloudcontrolv1beta1.VpcPeeringBuilder{}).
1128+
WithScope(kymaName).
1129+
WithRemoteRef("skr-namespace", "skr-azure-vpcpeering").
1130+
WithDetails(localKcpNetworkName, infra.KCP().Namespace(), remoteKcpNetworkName, infra.KCP().Namespace(), remotePeeringName, true, true).
1131+
WithLocalPeeringName(localPeeringName).
1132+
WithUseRemoteGateway(true).
1133+
Build()
1134+
1135+
Eventually(CreateObj).
1136+
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering,
1137+
WithName(kcpPeeringName),
1138+
).
1139+
Should(Succeed())
1140+
})
1141+
1142+
By("Then KCP VpcPeering has Error condition", func() {
1143+
Eventually(LoadAndCheck).
1144+
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering,
1145+
NewObjActions(),
1146+
HavingConditionTrue(cloudcontrolv1beta1.ConditionTypeError),
1147+
HavingState(string(cloudcontrolv1beta1.StateError)),
1148+
).
1149+
Should(Succeed())
1150+
})
1151+
1152+
// DELETE
1153+
1154+
By("When KCP VpcPeering is deleted", func() {
1155+
Eventually(Delete).
1156+
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering).
1157+
Should(Succeed(), "failed deleting VpcPeering")
1158+
})
1159+
1160+
By("Then KCP VpcPeering does not exist", func() {
1161+
Eventually(IsDeleted).
1162+
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering).
1163+
Should(Succeed(), "expected VpcPeering not to exist (be deleted), but it still exists")
1164+
})
1165+
1166+
By("And Then local Azure peering does not exist", func() {
1167+
peering, err := azureMockLocal.GetPeering(infra.Ctx(), localResourceGroupName, localVirtualNetworkName, localPeeringName)
1168+
Expect(err).To(HaveOccurred())
1169+
Expect(azuremeta.IsNotFound(err)).To(BeTrue())
1170+
Expect(peering).To(BeNil())
1171+
})
1172+
1173+
By("And Then remote Azure peering does not exists", func() {
1174+
peering, err := azureMockRemote.GetPeering(infra.Ctx(), remoteResourceGroup, remoteVnetName, remotePeeringName)
1175+
Expect(err).To(HaveOccurred())
1176+
Expect(azuremeta.IsNotFound(err)).To(BeTrue())
1177+
Expect(peering).To(BeNil())
1178+
})
1179+
1180+
By("// cleanup: Scope", func() {
1181+
Eventually(Delete).
1182+
WithArguments(infra.Ctx(), infra.KCP().Client(), scope).
1183+
Should(Succeed())
1184+
})
1185+
})
1186+
1187+
It("Scenario: KCP Azure VpcPeering error if Network error", func() {
1188+
const (
1189+
kymaName = "ca398c25-31b6-4611-99d4-91ffaa1db433"
1190+
kcpPeeringName = "77844695-662f-4757-bdc5-5010231f6629"
1191+
remoteSubscription = "82ade9ba-a9df-4f63-bcff-8fc123cf7c0e"
1192+
remoteTenant = "a5f768a1-dcf8-485e-9728-eea90a71b57b"
1193+
remoteResourceGroup = "MyResourceGroup"
1194+
remoteVnetName = "MyVnet"
1195+
remotePeeringName = "my-peering"
1196+
localPeeringName = "kyma-peering"
1197+
)
1198+
1199+
scope := &cloudcontrolv1beta1.Scope{}
1200+
1201+
By("Given Scope exists", func() {
1202+
// Tell Scope reconciler to ignore this kymaName
1203+
kcpscope.Ignore.AddName(kymaName)
1204+
1205+
Eventually(CreateScopeAzure).
1206+
WithArguments(infra.Ctx(), infra, scope, WithName(kymaName)).
1207+
Should(Succeed())
1208+
})
1209+
1210+
localResourceGroupName := scope.Spec.Scope.Azure.VpcNetwork
1211+
localVirtualNetworkName := scope.Spec.Scope.Azure.VpcNetwork
1212+
1213+
azureMockLocal := infra.AzureMock().MockConfigs(scope.Spec.Scope.Azure.SubscriptionId, scope.Spec.Scope.Azure.TenantId)
1214+
azureMockRemote := infra.AzureMock().MockConfigs(remoteSubscription, remoteTenant)
1215+
1216+
By("And Given local Azure VNET exists", func() {
1217+
err := azureMockLocal.CreateNetwork(infra.Ctx(), localResourceGroupName, localVirtualNetworkName, scope.Spec.Region, "10.200.0.0/25", nil)
1218+
Expect(err).ToNot(HaveOccurred())
1219+
})
1220+
1221+
By("And Given remote Azure VNet exists with Kyma tag", func() {
1222+
err := azureMockRemote.CreateNetwork(infra.Ctx(), remoteResourceGroup, remoteVnetName, scope.Spec.Region, "10.100.0.0/25", map[string]string{kymaName: kymaName})
1223+
Expect(err).ToNot(HaveOccurred())
1224+
})
1225+
1226+
localKcpNetworkName := common.KcpNetworkKymaCommonName(scope.Name)
1227+
remoteKcpNetworkName := scope.Name + "--remote"
1228+
1229+
var localKcpNet *cloudcontrolv1beta1.Network
1230+
1231+
By("And Given local KCP Network is created", func() {
1232+
kcpnetwork.Ignore.AddName(localKcpNetworkName)
1233+
localKcpNet = (&cloudcontrolv1beta1.NetworkBuilder{}).
1234+
WithScope(scope.Name).
1235+
WithAzureRef(scope.Spec.Scope.Azure.TenantId, scope.Spec.Scope.Azure.SubscriptionId, scope.Spec.Scope.Azure.VpcNetwork, scope.Spec.Scope.Azure.VpcNetwork).
1236+
Build()
1237+
Eventually(CreateObj).
1238+
WithArguments(infra.Ctx(), infra.KCP().Client(), localKcpNet, WithName(localKcpNetworkName)).
1239+
Should(Succeed())
1240+
})
1241+
1242+
By("And Given KCP KymaNetwork has Error state", func() {
1243+
Eventually(UpdateStatus).
1244+
WithArguments(infra.Ctx(),
1245+
infra.KCP().Client(),
1246+
localKcpNet,
1247+
WithoutConditions("Ready"),
1248+
WithState("Error"),
1249+
WithConditions(KcpErrorCondition())).
1250+
Should(Succeed())
1251+
})
1252+
1253+
var kcpPeering *cloudcontrolv1beta1.VpcPeering
1254+
1255+
By("And Given KCP VpcPeering is created", func() {
1256+
kcpPeering = (&cloudcontrolv1beta1.VpcPeeringBuilder{}).
1257+
WithScope(kymaName).
1258+
WithRemoteRef("skr-namespace", "skr-azure-vpcpeering").
1259+
WithDetails(localKcpNetworkName, infra.KCP().Namespace(), remoteKcpNetworkName, infra.KCP().Namespace(), remotePeeringName, true, true).
1260+
WithLocalPeeringName(localPeeringName).
1261+
WithUseRemoteGateway(true).
1262+
Build()
1263+
1264+
Eventually(CreateObj).
1265+
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering,
1266+
WithName(kcpPeeringName),
1267+
).
1268+
Should(Succeed())
1269+
})
1270+
1271+
By("Then KCP VpcPeering has Error condition", func() {
1272+
Eventually(LoadAndCheck).
1273+
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering,
1274+
NewObjActions(),
1275+
HavingConditionTrue(cloudcontrolv1beta1.ConditionTypeError),
1276+
HavingState(string(cloudcontrolv1beta1.StateError)),
1277+
).
1278+
Should(Succeed())
1279+
})
1280+
1281+
// DELETE
1282+
1283+
By("When KCP VpcPeering is deleted", func() {
1284+
Eventually(Delete).
1285+
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering).
1286+
Should(Succeed(), "failed deleting VpcPeering")
1287+
})
1288+
1289+
By("Then KCP VpcPeering does not exist", func() {
1290+
Eventually(IsDeleted).
1291+
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering).
1292+
Should(Succeed(), "expected VpcPeering not to exist (be deleted), but it still exists")
1293+
})
1294+
1295+
By("And Then local Azure peering does not exist", func() {
1296+
peering, err := azureMockLocal.GetPeering(infra.Ctx(), localResourceGroupName, localVirtualNetworkName, localPeeringName)
1297+
Expect(err).To(HaveOccurred())
1298+
Expect(azuremeta.IsNotFound(err)).To(BeTrue())
1299+
Expect(peering).To(BeNil())
1300+
})
1301+
1302+
By("And Then remote Azure peering does not exists", func() {
1303+
peering, err := azureMockRemote.GetPeering(infra.Ctx(), remoteResourceGroup, remoteVnetName, remotePeeringName)
1304+
Expect(err).To(HaveOccurred())
1305+
Expect(azuremeta.IsNotFound(err)).To(BeTrue())
1306+
Expect(peering).To(BeNil())
1307+
})
1308+
1309+
By("// cleanup: Scope", func() {
1310+
Eventually(Delete).
1311+
WithArguments(infra.Ctx(), infra.KCP().Client(), scope).
1312+
Should(Succeed())
1313+
})
1314+
})
10811315
})

pkg/kcp/vpcpeering/networkLocalLoad.go

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,74 @@ func kcpNetworkLocalLoad(ctx context.Context, st composed.State) (error, context
3636
}
3737

3838
if apierrors.IsNotFound(err) {
39-
return composed.StopWithRequeueDelay(util.Timing.T10000ms()), ctx
39+
changed := false
40+
41+
if meta.RemoveStatusCondition(state.ObjAsVpcPeering().Conditions(), cloudcontrolv1beta1.ConditionTypeReady) {
42+
changed = true
43+
}
44+
45+
if meta.SetStatusCondition(state.ObjAsVpcPeering().Conditions(), metav1.Condition{
46+
Type: cloudcontrolv1beta1.ConditionTypeError,
47+
Status: metav1.ConditionTrue,
48+
Reason: cloudcontrolv1beta1.ReasonWaitingDependency,
49+
Message: "Local network not found",
50+
}) {
51+
changed = true
52+
}
53+
54+
if state.ObjAsVpcPeering().Status.State != string(cloudcontrolv1beta1.StateError) {
55+
state.ObjAsVpcPeering().Status.State = string(cloudcontrolv1beta1.StateError)
56+
changed = true
57+
}
58+
59+
if !changed {
60+
return composed.StopWithRequeueDelay(util.Timing.T10000ms()), ctx
61+
}
62+
63+
return composed.PatchStatus(state.ObjAsVpcPeering()).
64+
ErrorLogMessage("Error patching KCP VpcPeering status with local network not found").
65+
SuccessError(composed.StopWithRequeueDelay(util.Timing.T10000ms())).
66+
SuccessLogMsg("KCP VpcPeering local KCP Network not found").
67+
Run(ctx, state)
4068
}
4169

4270
state.localNetwork = net
4371

44-
// Ignore state check if marked for deletion
4572
if composed.IsMarkedForDeletion(state.Obj()) {
46-
return composed.LogErrorAndReturn(err, "KCP VpcPeering marked for deletion, continue", nil, ctx)
73+
74+
if net.Status.Network != nil {
75+
return nil, ctx
76+
}
77+
78+
changed := false
79+
80+
if meta.RemoveStatusCondition(state.ObjAsVpcPeering().Conditions(), cloudcontrolv1beta1.ConditionTypeReady) {
81+
changed = true
82+
}
83+
84+
if meta.SetStatusCondition(state.ObjAsVpcPeering().Conditions(), metav1.Condition{
85+
Type: cloudcontrolv1beta1.ConditionTypeError,
86+
Status: metav1.ConditionTrue,
87+
Reason: cloudcontrolv1beta1.ReasonMissingDependency,
88+
Message: "Local network reference missing",
89+
}) {
90+
changed = true
91+
}
92+
93+
if state.ObjAsVpcPeering().Status.State != string(cloudcontrolv1beta1.StateError) {
94+
state.ObjAsVpcPeering().Status.State = string(cloudcontrolv1beta1.StateError)
95+
changed = true
96+
}
97+
98+
if !changed {
99+
return composed.StopWithRequeueDelay(util.Timing.T300000ms()), ctx
100+
}
101+
102+
return composed.PatchStatus(state.ObjAsVpcPeering()).
103+
ErrorLogMessage("Error patching KCP VpcPeering status with local network reference missing").
104+
SuccessError(composed.StopWithRequeueDelay(util.Timing.T300000ms())).
105+
SuccessLogMsg("KCP VpcPeering local network reference missing").
106+
Run(ctx, state)
47107
}
48108

49109
if net.Status.State == string(cloudcontrolv1beta1.StateError) {
@@ -62,6 +122,11 @@ func kcpNetworkLocalLoad(ctx context.Context, st composed.State) (error, context
62122
changed = true
63123
}
64124

125+
if state.ObjAsVpcPeering().Status.State != string(cloudcontrolv1beta1.StateError) {
126+
state.ObjAsVpcPeering().Status.State = string(cloudcontrolv1beta1.StateError)
127+
changed = true
128+
}
129+
65130
if !changed {
66131
return composed.StopAndForget, ctx
67132
}

0 commit comments

Comments
 (0)