@@ -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