Skip to content

Commit 277736d

Browse files
Merge pull request #2042 from lunarwhite/np-spec-reconcile
CM-758: (resourceapply) fix NetworkPolicy spec not reconciling on update and potential infinite loops
2 parents 1a3af44 + 4735935 commit 277736d

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

pkg/operator/resource/resourceapply/generic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func ApplyDirectly(ctx context.Context, clients *ClientHolder, recorder events.R
147147
if clients.kubeClient == nil {
148148
result.Error = fmt.Errorf("missing kubeClient")
149149
} else {
150-
result.Result, result.Changed, result.Error = ApplyNetworkPolicy(ctx, clients.kubeClient.NetworkingV1(), recorder, t)
150+
result.Result, result.Changed, result.Error = ApplyNetworkPolicy(ctx, clients.kubeClient.NetworkingV1(), recorder, t, cache)
151151
}
152152
case *rbacv1.ClusterRole:
153153
if clients.kubeClient == nil {

pkg/operator/resource/resourceapply/networking.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,44 @@ import (
1515
"github.com/openshift/library-go/pkg/operator/resource/resourcemerge"
1616
)
1717

18-
// ApplyClusterRole merges objectmeta, does not worry about anything else
19-
func ApplyNetworkPolicy(ctx context.Context, client networkingclientv1.NetworkPoliciesGetter, recorder events.Recorder, required *networkingv1.NetworkPolicy) (*networkingv1.NetworkPolicy, bool, error) {
18+
// ApplyNetworkPolicy merges objectmeta and requires spec
19+
func ApplyNetworkPolicy(ctx context.Context, client networkingclientv1.NetworkPoliciesGetter, recorder events.Recorder, required *networkingv1.NetworkPolicy, cache ResourceCache) (*networkingv1.NetworkPolicy, bool, error) {
2020
existing, err := client.NetworkPolicies(required.Namespace).Get(ctx, required.Name, metav1.GetOptions{})
2121
if apierrors.IsNotFound(err) {
2222
requiredCopy := required.DeepCopy()
2323
actual, err := client.NetworkPolicies(required.Namespace).Create(
2424
ctx, resourcemerge.WithCleanLabelsAndAnnotations(requiredCopy).(*networkingv1.NetworkPolicy), metav1.CreateOptions{})
2525
resourcehelper.ReportCreateEvent(recorder, required, err)
26+
cache.UpdateCachedResourceMetadata(required, actual)
2627
return actual, true, err
2728
}
2829
if err != nil {
2930
return nil, false, err
3031
}
3132

33+
if cache.SafeToSkipApply(required, existing) {
34+
return existing, false, nil
35+
}
36+
3237
modified := false
3338
existingCopy := existing.DeepCopy()
3439

3540
resourcemerge.EnsureObjectMeta(&modified, &existingCopy.ObjectMeta, required.ObjectMeta)
36-
if equality.Semantic.DeepEqual(existingCopy.Spec, required.Spec) && !modified {
41+
specContentSame := equality.Semantic.DeepEqual(existingCopy.Spec, required.Spec)
42+
if specContentSame && !modified {
43+
cache.UpdateCachedResourceMetadata(required, existingCopy)
3744
return existingCopy, false, nil
3845
}
3946

47+
existingCopy.Spec = required.Spec
48+
4049
if klog.V(2).Enabled() {
4150
klog.Infof("NetworkPolicy %q changes: %v", required.Name, JSONPatchNoError(existing, existingCopy))
4251
}
4352

4453
actual, err := client.NetworkPolicies(existingCopy.Namespace).Update(ctx, existingCopy, metav1.UpdateOptions{})
4554
resourcehelper.ReportUpdateEvent(recorder, required, err)
55+
cache.UpdateCachedResourceMetadata(required, actual)
4656
return actual, true, err
4757
}
4858

0 commit comments

Comments
 (0)