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

Sync policy object only policy spec is changed #861

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func LaunchSubscriptionReportSyncer(ctx context.Context, mgr ctrl.Manager, agent
predicate := predicate.NewPredicateFuncs(func(object client.Object) bool { return true })

// emitter config
emitter := generic.ObjectEmitterWrapper(enum.SubscriptionReportType, nil, nil)
emitter := generic.ObjectEmitterWrapper(enum.SubscriptionReportType, nil, nil, false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add TODO to handle it if needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


// syncer
name := "status.subscription_report"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func LaunchSubscriptionStatusSyncer(ctx context.Context, mgr ctrl.Manager, agent
predicate := predicate.NewPredicateFuncs(func(object client.Object) bool { return true })

// emitter config
emitter := generic.ObjectEmitterWrapper(enum.SubscriptionStatusType, nil, nil)
emitter := generic.ObjectEmitterWrapper(enum.SubscriptionStatusType, nil, nil, false)

// syncer
name := "status.subscription_status"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ func (e *genericObjectEmitter) Delete(object client.Object) bool {
func ObjectEmitterWrapper(eventType enum.EventType,
shouldUpdate func(client.Object) bool,
tweakFunc func(client.Object),
specUpdate bool,
) ObjectEmitter {
eventData := genericpayload.GenericObjectBundle{}
return NewGenericObjectEmitter(
eventType,
&eventData,
NewGenericObjectHandler(&eventData),
NewGenericObjectHandler(&eventData, specUpdate),
WithShouldUpdate(shouldUpdate),
WithTweakFunc(tweakFunc),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (

type genericObjectHandler struct {
eventData *genericpayload.GenericObjectBundle
onlySpec bool
}

func NewGenericObjectHandler(eventData *genericpayload.GenericObjectBundle) Handler {
func NewGenericObjectHandler(eventData *genericpayload.GenericObjectBundle, onlySpec bool) Handler {
return &genericObjectHandler{
eventData: eventData,
onlySpec: onlySpec,
}
}

Expand All @@ -24,6 +26,11 @@ func (h *genericObjectHandler) Update(obj client.Object) bool {
return true
}

old := (*h.eventData)[index]
if h.onlySpec && old.GetGeneration() == obj.GetGeneration() {
return false
}

// if we reached here, object already exists in the bundle. check if we need to update the object
if obj.GetResourceVersion() == (*h.eventData)[index].GetResourceVersion() {
return false // update in bundle only if object changed. check for changes using resourceVersion field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func LaunchManagedClusterSyncer(ctx context.Context, mgr ctrl.Manager, agentConf
constants.ManagedClusterManagedByAnnotation: statusconfig.GetLeafHubName(),
})
}
emitter := generic.ObjectEmitterWrapper(enum.ManagedClusterType, nil, tweakFunc)
emitter := generic.ObjectEmitterWrapper(enum.ManagedClusterType, nil, tweakFunc, false)

return generic.LaunchGenericObjectSyncer(
"status.managed_cluster",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func LaunchPlacementDecisionSyncer(ctx context.Context, mgr ctrl.Manager, agentC
return true // resource
}, func(obj client.Object) {
obj.SetManagedFields(nil)
})
}, false)

// syncer
name := "status.placement_decision"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func LaunchPlacementRuleSyncer(ctx context.Context, mgr ctrl.Manager, agentConfi
func(obj client.Object) bool {
return agentConfig.EnableGlobalResource &&
utils.HasAnnotation(obj, constants.OriginOwnerReferenceAnnotation)
}, tweakFunc)
}, tweakFunc, false)

localPlacementRuleEmitter := generic.ObjectEmitterWrapper(enum.LocalPlacementRuleSpecType,
func(obj client.Object) bool {
// return statusconfig.GetEnableLocalPolicy() == statusconfig.EnableLocalPolicyTrue &&
// !utils.HasAnnotation(obj, constants.OriginOwnerReferenceAnnotation) // local resource
return false // disable the placementrule now
}, tweakFunc)
}, tweakFunc, false)

// syncer
name := "status.placement_decision"
Expand Down
2 changes: 1 addition & 1 deletion agent/pkg/status/controller/placement/placement_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func LaunchPlacementSyncer(ctx context.Context, mgr ctrl.Manager, agentConfig *c
},
func(obj client.Object) {
obj.SetManagedFields(nil)
},
}, false,
)

// syncer
Expand Down
1 change: 1 addition & 0 deletions agent/pkg/status/controller/policies/policy_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func LaunchPolicySyncer(ctx context.Context, mgr ctrl.Manager, agentConfig *conf
!utils.HasLabel(obj, constants.PolicyEventRootPolicyNameLabelKey) // root policy
},
cleanPolicy,
true,
)

// global policy emitters
Expand Down