Skip to content

Commit 3f730bd

Browse files
committed
chore(VpcPeering): remove duplicated code that loads local and remote KCP Network
1 parent 8ed3e16 commit 3f730bd

File tree

4 files changed

+72
-102
lines changed

4 files changed

+72
-102
lines changed

internal/controller/cloud-control/vpcpeering_aws_test.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,6 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
182182

183183
})
184184

185-
By("Then KCP VpcPeering is in missing local network error state", func() {
186-
Eventually(LoadAndCheck).
187-
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering, NewObjActions(),
188-
HavingCondition(cloudcontrolv1beta1.ConditionTypeError, metav1.ConditionTrue, cloudcontrolv1beta1.ReasonMissingDependency, "Local network not found"),
189-
).Should(Succeed())
190-
})
191-
192185
var localKcpNet *cloudcontrolv1beta1.Network
193186

194187
By("When local KCP Network is created", func() {
@@ -202,13 +195,6 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
202195
Should(Succeed())
203196
})
204197

205-
By("Then KCP VpcPeering is in waiting local network to be ready", func() {
206-
Eventually(LoadAndCheck).
207-
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering, NewObjActions(),
208-
HavingCondition(cloudcontrolv1beta1.ConditionTypeError, metav1.ConditionTrue, cloudcontrolv1beta1.ReasonWaitingDependency, "Local network not ready"),
209-
).Should(Succeed())
210-
})
211-
212198
By("When local KCP Network is ready", func() {
213199
kcpnetwork.Ignore.RemoveName(localKcpNetworkName)
214200
// trigger the reconciliation
@@ -219,13 +205,6 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
219205
Should(Succeed(), "expected local kcp network to become ready but it didn't")
220206
})
221207

222-
By("Then KCP VpcPeering is in missing remote network", func() {
223-
Eventually(LoadAndCheck).
224-
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering, NewObjActions(),
225-
HavingCondition(cloudcontrolv1beta1.ConditionTypeError, metav1.ConditionTrue, cloudcontrolv1beta1.ReasonMissingDependency, "Remote network not found"),
226-
).Should(Succeed())
227-
})
228-
229208
var remoteKcpNet *cloudcontrolv1beta1.Network
230209

231210
By("When remote KCP Network is created", func() {
@@ -239,13 +218,6 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
239218
Should(Succeed())
240219
})
241220

242-
By("Then KCP VpcPeering is in waiting remote network to be ready", func() {
243-
Eventually(LoadAndCheck).
244-
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering, NewObjActions(),
245-
HavingCondition(cloudcontrolv1beta1.ConditionTypeError, metav1.ConditionTrue, cloudcontrolv1beta1.ReasonWaitingDependency, "Remote network not ready"),
246-
).Should(Succeed())
247-
})
248-
249221
By("When remote KCP Network is ready", func() {
250222
kcpnetwork.Ignore.RemoveName(remoteKcpNetworkName)
251223
// trigger the reconciliation

internal/controller/cloud-control/vpcpeering_azure_test.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,6 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
8181
Should(Succeed())
8282
})
8383

84-
By("Then KCP VpcPeering is in missing local network error state", func() {
85-
Eventually(LoadAndCheck).
86-
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering, NewObjActions(),
87-
HavingCondition(cloudcontrolv1beta1.ConditionTypeError, metav1.ConditionTrue, cloudcontrolv1beta1.ReasonMissingDependency, "Local network not found"),
88-
).Should(Succeed())
89-
})
90-
9184
var localKcpNet *cloudcontrolv1beta1.Network
9285

9386
By("When local KCP Network is created", func() {
@@ -101,13 +94,6 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
10194
Should(Succeed())
10295
})
10396

104-
By("Then KCP VpcPeering is in waiting local network to be ready error state", func() {
105-
Eventually(LoadAndCheck).
106-
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering, NewObjActions(),
107-
HavingCondition(cloudcontrolv1beta1.ConditionTypeError, metav1.ConditionTrue, cloudcontrolv1beta1.ReasonWaitingDependency, "Local network not ready"),
108-
).Should(Succeed())
109-
})
110-
11197
By("When local KCP Network is Ready", func() {
11298
kcpnetwork.Ignore.RemoveName(localKcpNetworkName)
11399
// trigger the reconciliation
@@ -118,13 +104,6 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
118104
Should(Succeed(), "expected local kcp network to become ready but it didn't")
119105
})
120106

121-
By("Then KCP VpcPeering is in missing remote network error state", func() {
122-
Eventually(LoadAndCheck).
123-
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering, NewObjActions(),
124-
HavingCondition(cloudcontrolv1beta1.ConditionTypeError, metav1.ConditionTrue, cloudcontrolv1beta1.ReasonMissingDependency, "Remote network not found"),
125-
).Should(Succeed())
126-
})
127-
128107
var remoteKcpNet *cloudcontrolv1beta1.Network
129108

130109
By("When remote KCP Network is created", func() {
@@ -138,13 +117,6 @@ var _ = Describe("Feature: KCP VpcPeering", func() {
138117
Should(Succeed())
139118
})
140119

141-
By("Then KCP VpcPeering is in waiting remote network to be ready error state", func() {
142-
Eventually(LoadAndCheck).
143-
WithArguments(infra.Ctx(), infra.KCP().Client(), kcpPeering, NewObjActions(),
144-
HavingCondition(cloudcontrolv1beta1.ConditionTypeError, metav1.ConditionTrue, cloudcontrolv1beta1.ReasonWaitingDependency, "Remote network not ready"),
145-
).Should(Succeed())
146-
})
147-
148120
By("When remote KCP Network is Ready", func() {
149121
kcpnetwork.Ignore.RemoveName(remoteKcpNetworkName)
150122
// trigger the reconciliation

pkg/kcp/vpcpeering/networkLocalLoad.go

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vpcpeering
33
import (
44
"context"
55
"fmt"
6+
"k8s.io/apimachinery/pkg/api/meta"
67

78
cloudcontrolv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-control/v1beta1"
89
"github.com/kyma-project/cloud-manager/pkg/composed"
@@ -36,36 +37,48 @@ func kcpNetworkLocalLoad(ctx context.Context, st composed.State) (error, context
3637
if composed.IsMarkedForDeletion(state.Obj()) {
3738
return composed.LogErrorAndReturn(err, "KCP VpcPeering marked for deletion but, local KCP Network not found", nil, ctx)
3839
}
39-
return composed.PatchStatus(state.ObjAsVpcPeering()).
40-
SetExclusiveConditions(metav1.Condition{
41-
Type: cloudcontrolv1beta1.ConditionTypeError,
42-
Status: metav1.ConditionTrue,
43-
Reason: cloudcontrolv1beta1.ReasonMissingDependency,
44-
Message: "Local network not found",
45-
}).
46-
ErrorLogMessage("Error patching KCP VpcPeering status with missing local network dependency").
47-
SuccessError(composed.StopWithRequeueDelay(util.Timing.T10000ms())).
48-
SuccessLogMsg("KCP VpcPeering local KCP Network not found").
49-
Run(ctx, state)
40+
41+
// Patch status was triggered reconciliation immediately. Changing delay to 1 second to reduce wait time.
42+
return composed.StopWithRequeueDelay(util.Timing.T1000ms()), ctx
5043
}
5144

52-
if net.Status.Network == nil {
45+
state.localNetwork = net
46+
47+
// Ignore state check if marked for deletion
48+
if composed.IsMarkedForDeletion(state.Obj()) {
49+
return composed.LogErrorAndReturn(err, "KCP VpcPeering marked for deletion, continue", nil, ctx)
50+
}
51+
52+
if net.Status.State == string(cloudcontrolv1beta1.StateError) {
53+
changed := false
54+
55+
if meta.RemoveStatusCondition(state.ObjAsVpcPeering().Conditions(), cloudcontrolv1beta1.ConditionTypeReady) {
56+
changed = true
57+
}
58+
59+
if meta.SetStatusCondition(state.ObjAsVpcPeering().Conditions(), metav1.Condition{
60+
Type: cloudcontrolv1beta1.ConditionTypeError,
61+
Status: metav1.ConditionTrue,
62+
Reason: cloudcontrolv1beta1.ReasonWaitingDependency,
63+
Message: "Local network not ready",
64+
}) {
65+
changed = true
66+
}
67+
68+
if !changed {
69+
return composed.StopAndForget, ctx
70+
}
71+
5372
return composed.PatchStatus(state.ObjAsVpcPeering()).
54-
SetExclusiveConditions(metav1.Condition{
55-
Type: cloudcontrolv1beta1.ConditionTypeError,
56-
Status: metav1.ConditionTrue,
57-
Reason: cloudcontrolv1beta1.ReasonWaitingDependency,
58-
Message: "Local network not ready",
59-
}).
6073
ErrorLogMessage("Error patching KCP VpcPeering status with local network not ready").
61-
SuccessError(composed.StopWithRequeue).
74+
SuccessError(composed.StopAndForget).
6275
SuccessLogMsg("KCP VpcPeering local KCP Network not ready").
6376
Run(ctx, state)
6477
}
6578

66-
state.localNetwork = net
67-
68-
logger.Info("KCP VpcPeering local network loaded")
79+
if net.Status.State != string(cloudcontrolv1beta1.StateReady) {
80+
return composed.StopWithRequeueDelay(util.Timing.T1000ms()), ctx
81+
}
6982

70-
return nil, composed.LoggerIntoCtx(ctx, logger)
83+
return nil, ctx
7184
}

pkg/kcp/vpcpeering/networkRemoteLoad.go

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package vpcpeering
22

33
import (
44
"context"
5+
"fmt"
6+
"k8s.io/apimachinery/pkg/api/meta"
57

68
cloudcontrolv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-control/v1beta1"
79
"github.com/kyma-project/cloud-manager/pkg/composed"
@@ -13,13 +15,17 @@ import (
1315

1416
func kcpNetworkRemoteLoad(ctx context.Context, st composed.State) (error, context.Context) {
1517
state := st.(*State)
18+
logger := composed.LoggerFromCtx(ctx)
1619

1720
net := &cloudcontrolv1beta1.Network{}
1821
namespace := state.ObjAsVpcPeering().Spec.Details.RemoteNetwork.Namespace
1922
if namespace == "" {
2023
namespace = state.ObjAsVpcPeering().Namespace
2124
}
2225

26+
logger = logger.
27+
WithValues("remoteKcpNetwork", fmt.Sprintf("%s/%s", namespace, state.ObjAsVpcPeering().Spec.Details.LocalNetwork.Name))
28+
2329
err := state.Cluster().K8sClient().Get(ctx, client.ObjectKey{
2430
Namespace: namespace,
2531
Name: state.ObjAsVpcPeering().Spec.Details.RemoteNetwork.Name,
@@ -34,37 +40,44 @@ func kcpNetworkRemoteLoad(ctx context.Context, st composed.State) (error, contex
3440
return composed.LogErrorAndReturn(err, "KCP VpcPeering marked for deletion but, remote KCP Network not found", nil, ctx)
3541
}
3642

37-
return composed.PatchStatus(state.ObjAsVpcPeering()).
38-
SetExclusiveConditions(metav1.Condition{
39-
Type: cloudcontrolv1beta1.ConditionTypeError,
40-
Status: metav1.ConditionTrue,
41-
Reason: cloudcontrolv1beta1.ReasonMissingDependency,
42-
Message: "Remote network not found",
43-
}).
44-
ErrorLogMessage("Error patching KCP VpcPeering status with missing remote network dependency").
45-
SuccessError(composed.StopWithRequeueDelay(util.Timing.T10000ms())).
46-
SuccessLogMsg("KCP VpcPeering remote KCP Network not found").
47-
Run(ctx, state)
43+
// Patch status was triggered reconciliation immediately. Changing delay to 1 second to reduce wait time.
44+
return composed.StopWithRequeueDelay(util.Timing.T1000ms()), ctx
4845
}
4946

50-
if net.Status.Network == nil {
47+
state.remoteNetwork = net
48+
49+
if composed.IsMarkedForDeletion(state.Obj()) {
50+
return composed.LogErrorAndReturn(err, "KCP VpcPeering marked for deletion, continue", nil, ctx)
51+
}
52+
53+
if net.Status.State == string(cloudcontrolv1beta1.StateError) {
54+
changed := false
55+
if meta.RemoveStatusCondition(state.ObjAsVpcPeering().Conditions(), cloudcontrolv1beta1.ConditionTypeReady) {
56+
changed = true
57+
}
58+
if meta.SetStatusCondition(state.ObjAsVpcPeering().Conditions(), metav1.Condition{
59+
Type: cloudcontrolv1beta1.ConditionTypeError,
60+
Status: metav1.ConditionTrue,
61+
Reason: cloudcontrolv1beta1.ReasonWaitingDependency,
62+
Message: "Remote network not ready",
63+
}) {
64+
changed = true
65+
}
66+
67+
if !changed {
68+
return composed.StopAndForget, ctx
69+
}
70+
5171
return composed.PatchStatus(state.ObjAsVpcPeering()).
52-
SetExclusiveConditions(metav1.Condition{
53-
Type: cloudcontrolv1beta1.ConditionTypeError,
54-
Status: metav1.ConditionTrue,
55-
Reason: cloudcontrolv1beta1.ReasonWaitingDependency,
56-
Message: "Remote network not ready",
57-
}).
5872
ErrorLogMessage("Error patching KCP VpcPeering status with remote network not ready").
59-
SuccessError(composed.StopWithRequeue).
73+
SuccessError(composed.StopAndForget).
6074
SuccessLogMsg("KCP VpcPeering remote KCP Network not ready").
6175
Run(ctx, state)
6276
}
6377

64-
logger := composed.LoggerFromCtx(ctx)
65-
state.remoteNetwork = net
66-
67-
logger.Info("KCP VpcPeering remote network loaded")
78+
if net.Status.State != string(cloudcontrolv1beta1.StateReady) {
79+
return composed.StopWithRequeueDelay(util.Timing.T1000ms()), ctx
80+
}
6881

6982
return nil, ctx
7083
}

0 commit comments

Comments
 (0)