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

feat: add logs for policy ignores #4816

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,6 @@ func (p *Plan) Calculate() *Plan {
}
}

for _, pol := range p.Policies {
changes = pol.Apply(changes)
}

// filter out updates this external dns does not have ownership claim over
if p.OwnerID != "" {
changes.Delete = endpoint.FilterEndpointsByOwnerID(p.OwnerID, changes.Delete)
Expand All @@ -257,6 +253,10 @@ func (p *Plan) Calculate() *Plan {
changes.UpdateNew = endpoint.FilterEndpointsByOwnerID(p.OwnerID, changes.UpdateNew)
}

for _, pol := range p.Policies {
Copy link
Contributor

Choose a reason for hiding this comment

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

is order matter?

changes = pol.Apply(changes)
}

plan := &Plan{
Current: p.Current,
Desired: p.Desired,
Expand Down
20 changes: 20 additions & 0 deletions plan/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ limitations under the License.

package plan

import (
log "github.com/sirupsen/logrus"
)

// Policy allows to apply different rules to a set of changes.
type Policy interface {
Apply(changes *Changes) *Changes
Expand All @@ -41,6 +45,11 @@ type UpsertOnlyPolicy struct{}

// Apply applies the upsert-only policy which strips out any deletions.
func (p *UpsertOnlyPolicy) Apply(changes *Changes) *Changes {
if log.GetLevel() == log.DebugLevel {
Copy link
Contributor

Choose a reason for hiding this comment

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

Any chance to cover all this changes with tests?

for _, ep := range changes.Delete {
log.Debugf(`Skipping deletion of endpoint %v due to "upsert-only" policy`, ep)
}
}
return &Changes{
Create: changes.Create,
UpdateOld: changes.UpdateOld,
Expand All @@ -53,6 +62,17 @@ type CreateOnlyPolicy struct{}

// Apply applies the create-only policy which strips out updates and deletions.
func (p *CreateOnlyPolicy) Apply(changes *Changes) *Changes {
if log.GetLevel() == log.DebugLevel {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shell we consider something like that. To avoid code duplication and improve readability, you can extract the logging logic to a separate function.

if log.GetLevel() == log.DebugLevel {
	logSkippedEndpoints("deletion", changes.Delete)
	logSkippedEndpoints("update-old", changes.UpdateOld)
	logSkippedEndpoints("update-new", changes.UpdateNew)
}

func logSkippedEndpoints(action string, endpoints []*endpoint.Endpoint) {
	for _, ep := range endpoints {
		log.Debugf(`Skipping %s of endpoint %v due to "create-only" policy`, action, ep)
	}
}

for _, ep := range changes.Delete {
log.Debugf(`Skipping deletion of endpoint %v due to "create-only" policy`, ep)
}
for _, ep := range changes.UpdateOld {
log.Debugf(`Skipping update-old of endpoint %v due to "create-only" policy`, ep)
}
for _, ep := range changes.UpdateNew {
log.Debugf(`Skipping update-new of endpoint %v due to "create-only" policy`, ep)
}
}
return &Changes{
Create: changes.Create,
}
Expand Down
Loading