Skip to content

Commit

Permalink
refactor patrially enforced condition for dnspolicy
Browse files Browse the repository at this point in the history
  • Loading branch information
maksymvavilov committed Jul 11, 2024
1 parent 233f8a8 commit 1b7b44f
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 256 deletions.
70 changes: 40 additions & 30 deletions controllers/dnspolicy_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"errors"
"fmt"
"slices"
"strings"

Expand All @@ -34,6 +35,7 @@ import (

"github.com/kuadrant/kuadrant-operator/api/v1alpha1"
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant"
"github.com/kuadrant/kuadrant-operator/pkg/library/utils"
)

var NegativePolarityConditions []string
Expand Down Expand Up @@ -82,55 +84,63 @@ func (r *DNSPolicyReconciler) calculateStatus(ctx context.Context, dnsPolicy *v1
}

recordsList := &kuadrantdnsv1alpha1.DNSRecordList{}
controlledRecords := &kuadrantdnsv1alpha1.DNSRecordList{}

var enforcedCondition *metav1.Condition
if err := r.Client().List(ctx, recordsList); err != nil {
enforcedCondition = kuadrant.EnforcedCondition(dnsPolicy, kuadrant.NewErrUnknown(dnsPolicy.Kind(), err), false)
} else {
// leave only records controlled by the policy
recordsList.Items = utils.Filter(recordsList.Items, func(record kuadrantdnsv1alpha1.DNSRecord) bool {
for _, reference := range record.GetOwnerReferences() {
if reference.Controller != nil && *reference.Controller && reference.Name == dnsPolicy.Name && reference.UID == dnsPolicy.UID {
return true

Check warning on line 96 in controllers/dnspolicy_status.go

View check run for this annotation

Codecov / codecov/patch

controllers/dnspolicy_status.go#L93-L96

Added lines #L93 - L96 were not covered by tests
}
}
return false

Check warning on line 99 in controllers/dnspolicy_status.go

View check run for this annotation

Codecov / codecov/patch

controllers/dnspolicy_status.go#L99

Added line #L99 was not covered by tests
})

enforcedCondition = r.enforcedCondition(recordsList, dnsPolicy)
}

meta.SetStatusCondition(&newStatus.Conditions, *enforcedCondition)

for _, record := range recordsList.Items {
for _, reference := range record.GetOwnerReferences() {
if reference.Controller != nil && *reference.Controller && reference.Name == dnsPolicy.Name && reference.UID == dnsPolicy.UID {
controlledRecords.Items = append(controlledRecords.Items, record)
}
}
}

propagateRecordConditions(controlledRecords, newStatus)
propagateRecordConditions(recordsList, newStatus)

Check warning on line 106 in controllers/dnspolicy_status.go

View check run for this annotation

Codecov / codecov/patch

controllers/dnspolicy_status.go#L106

Added line #L106 was not covered by tests

return newStatus
}

func (r *DNSPolicyReconciler) enforcedCondition(recordsList *kuadrantdnsv1alpha1.DNSRecordList, dnsPolicy *v1alpha1.DNSPolicy) *metav1.Condition {
var controlled bool
for _, record := range recordsList.Items {
// check that DNS record is controller by this policy
for _, reference := range record.GetOwnerReferences() {
if reference.Controller != nil && *reference.Controller && reference.Name == dnsPolicy.Name && reference.UID == dnsPolicy.UID {
controlled = true
// if at least one record not ready the policy is not enforced
for _, condition := range record.Status.Conditions {
if condition.Type == string(kuadrantdnsv1alpha1.ConditionTypeReady) && condition.Status == metav1.ConditionFalse {
return kuadrant.EnforcedCondition(dnsPolicy, nil, false)
}
}
break
// there are no controlled DNS records present
if len(recordsList.Items) == 0 {
return kuadrant.EnforcedCondition(dnsPolicy, kuadrant.NewErrUnknown(dnsPolicy.Kind(), errors.New("policy is not enforced on any DNSRecord: no routes attached for listeners")), false)

Check warning on line 114 in controllers/dnspolicy_status.go

View check run for this annotation

Codecov / codecov/patch

controllers/dnspolicy_status.go#L113-L114

Added lines #L113 - L114 were not covered by tests
}

// filter not ready records
notReadyRecords := utils.Filter(recordsList.Items, func(record kuadrantdnsv1alpha1.DNSRecord) bool {
for _, condition := range record.Status.Conditions {
if condition.Type == string(kuadrantdnsv1alpha1.ConditionTypeReady) && condition.Status == metav1.ConditionFalse {
return true

Check warning on line 121 in controllers/dnspolicy_status.go

View check run for this annotation

Codecov / codecov/patch

controllers/dnspolicy_status.go#L118-L121

Added lines #L118 - L121 were not covered by tests
}
}
return false

Check warning on line 124 in controllers/dnspolicy_status.go

View check run for this annotation

Codecov / codecov/patch

controllers/dnspolicy_status.go#L124

Added line #L124 was not covered by tests
})

// none of the records are ready
if len(notReadyRecords) == len(recordsList.Items) {
return kuadrant.EnforcedCondition(dnsPolicy, kuadrant.NewErrUnknown(dnsPolicy.Kind(), errors.New("policy is not enforced on any DNSRecord: not a single DNSRecord is ready")), false)

Check warning on line 129 in controllers/dnspolicy_status.go

View check run for this annotation

Codecov / codecov/patch

controllers/dnspolicy_status.go#L128-L129

Added lines #L128 - L129 were not covered by tests
}

// at least one DNS record is controlled by the policy
// and all controlled records are accepted
if controlled {
return kuadrant.EnforcedCondition(dnsPolicy, nil, true)
// some of the records are not ready
if len(notReadyRecords) > 0 {
additionalMessage := ". Not ready DNSRecords are: "
for _, record := range notReadyRecords {
additionalMessage += fmt.Sprintf("%s ", record.Name)

Check warning on line 136 in controllers/dnspolicy_status.go

View check run for this annotation

Codecov / codecov/patch

controllers/dnspolicy_status.go#L133-L136

Added lines #L133 - L136 were not covered by tests
}
cond := kuadrant.EnforcedCondition(dnsPolicy, nil, false)
cond.Message += additionalMessage
return cond

Check warning on line 140 in controllers/dnspolicy_status.go

View check run for this annotation

Codecov / codecov/patch

controllers/dnspolicy_status.go#L138-L140

Added lines #L138 - L140 were not covered by tests
}
// there are no controlled DNS records present
return kuadrant.EnforcedCondition(dnsPolicy, kuadrant.NewErrUnknown(dnsPolicy.Kind(), errors.New("policy is not enforced on any dns record: no routes attached for listeners")), false)
// all records are ready
return kuadrant.EnforcedCondition(dnsPolicy, nil, true)

Check warning on line 143 in controllers/dnspolicy_status.go

View check run for this annotation

Codecov / codecov/patch

controllers/dnspolicy_status.go#L143

Added line #L143 was not covered by tests
}

func propagateRecordConditions(records *kuadrantdnsv1alpha1.DNSRecordList, policyStatus *v1alpha1.DNSPolicyStatus) {
Expand Down
16 changes: 0 additions & 16 deletions tests/common/dnspolicy/dnspolicy_controller_single_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/kuadrant/kuadrant-operator/api/v1alpha1"
"github.com/kuadrant/kuadrant-operator/pkg/common"
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant"
"github.com/kuadrant/kuadrant-operator/pkg/library/utils"
"github.com/kuadrant/kuadrant-operator/tests"
)
Expand Down Expand Up @@ -170,21 +169,6 @@ var _ = Describe("DNSPolicy Single Cluster", func() {
})

It("should create dns records", func(ctx SpecContext) {

Eventually(func(g Gomega, ctx context.Context) {

g.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(dnsPolicy), dnsPolicy)).To(Succeed())
g.Expect(dnsPolicy.Status.Conditions).To(
ContainElement(MatchFields(IgnoreExtras, Fields{
"Type": Equal(string(kuadrant.PolicyConditionEnforced)),
"Status": Equal(metav1.ConditionTrue),
"Reason": Equal(string(kuadrant.PolicyReasonEnforced)),
"Message": Equal("DNSPolicy has been partially enforced"),
})),
)

}, tests.TimeoutMedium, tests.RetryIntervalMedium, ctx).Should(Succeed())

Eventually(func(g Gomega, ctx context.Context) {
recordList := &kuadrantdnsv1alpha1.DNSRecordList{}
err := k8sClient.List(ctx, recordList, &client.ListOptions{Namespace: testNamespace})
Expand Down
Loading

0 comments on commit 1b7b44f

Please sign in to comment.