Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
GH-324 pollicyaffected condition for gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
maksymvavilov committed Aug 17, 2023
1 parent 448d63f commit f540886
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
10 changes: 9 additions & 1 deletion pkg/_internal/conditions/conditions.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package conditions

type ConditionType string
type ConditionReason string

const (
ConditionTypeReady string = "Ready"
ConditionTypeReady ConditionType = "Ready"

DNSPolicyAffected ConditionType = "DNSPolicyAffected"
DNSPolicyReasonAccepted ConditionReason = "Accepted"
DNSPolicyReasonInvalid ConditionReason = "Invalid"
DNSPolicyReasonConflicted ConditionReason = "Conflicted"
)
59 changes: 55 additions & 4 deletions pkg/controllers/dnspolicy/dnspolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package dnspolicy
import (
"context"
"fmt"
"reflect"

clusterv1 "open-cluster-management.io/api/cluster/v1"

Expand Down Expand Up @@ -141,6 +142,8 @@ func (r *DNSPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}

func (r *DNSPolicyReconciler) reconcileResources(ctx context.Context, dnsPolicy *v1alpha1.DNSPolicy, targetNetworkObject client.Object) error {
gatewayCondition := buildGatewayCondition(conditions.DNSPolicyReasonAccepted, nil)

// validate
err := dnsPolicy.Validate()
if err != nil {
Expand All @@ -153,19 +156,24 @@ func (r *DNSPolicyReconciler) reconcileResources(ctx context.Context, dnsPolicy
return err
}

if err := r.reconcileDNSRecords(ctx, dnsPolicy, gatewayDiffObj); err != nil {
if err = r.reconcileDNSRecords(ctx, dnsPolicy, gatewayDiffObj); err != nil {
gatewayCondition = buildGatewayCondition(conditions.DNSPolicyReasonInvalid, err)
return err
}

if err := r.reconcileHealthChecks(ctx, dnsPolicy, gatewayDiffObj); err != nil {
if err = r.reconcileHealthChecks(ctx, dnsPolicy, gatewayDiffObj); err != nil {
gatewayCondition = buildGatewayCondition(conditions.DNSPolicyReasonInvalid, err)
return err
}

// set direct back ref - i.e. claim the target network object as taken asap
if err := r.ReconcileTargetBackReference(ctx, client.ObjectKeyFromObject(dnsPolicy), targetNetworkObject, DNSPolicyBackRefAnnotation); err != nil {
if err = r.ReconcileTargetBackReference(ctx, client.ObjectKeyFromObject(dnsPolicy), targetNetworkObject, DNSPolicyBackRefAnnotation); err != nil {
gatewayCondition = buildGatewayCondition(conditions.DNSPolicyReasonConflicted, err)
return err
}

_ = r.updateGatewayCondition(ctx, gatewayCondition, gatewayDiffObj)

// set annotation of policies affecting the gateway - should be the last step, only when all the reconciliation steps succeed
return r.ReconcileGatewayPolicyReferences(ctx, dnsPolicy, gatewayDiffObj)
}
Expand Down Expand Up @@ -209,7 +217,7 @@ func (r *DNSPolicyReconciler) calculateStatus(dnsPolicy *v1alpha1.DNSPolicy, spe

func (r *DNSPolicyReconciler) readyCondition(targetNetworkObjectectKind string, specErr error) *metav1.Condition {
cond := &metav1.Condition{
Type: conditions.ConditionTypeReady,
Type: string(conditions.ConditionTypeReady),
Status: metav1.ConditionTrue,
Reason: fmt.Sprintf("%sDNSEnabled", targetNetworkObjectectKind),
Message: fmt.Sprintf("%s is DNS Enabled", targetNetworkObjectectKind),
Expand All @@ -224,6 +232,49 @@ func (r *DNSPolicyReconciler) readyCondition(targetNetworkObjectectKind string,
return cond
}

func buildGatewayCondition(reason conditions.ConditionReason, err error) *metav1.Condition {
condition := &metav1.Condition{
Type: string(conditions.DNSPolicyAffected),
Status: metav1.ConditionTrue,
Reason: string(reason),
Message: "DNS Policy accepted",
}

if err != nil {
condition.Status = metav1.ConditionFalse
condition.Message = err.Error()
}

return condition
}

func (r *DNSPolicyReconciler) updateGatewayCondition(ctx context.Context, condition *metav1.Condition, gatewayDiff *reconcilers.GatewayDiff) error {

// update condition if needed
for _, gw := range append(gatewayDiff.GatewaysWithValidPolicyRef, gatewayDiff.GatewaysMissingPolicyRef...) {
previous := gw.DeepCopy()
meta.SetStatusCondition(&gw.Status.Conditions, *condition)
if !reflect.DeepEqual(previous.Status.Conditions, gw.Status.Conditions) {
if err := r.Client().Status().Update(ctx, gw.Gateway); err != nil {
return err
}
}
}

// remove condition from gateway that is no longer referenced
for _, gw := range gatewayDiff.GatewaysWithInvalidPolicyRef {
previous := gw.DeepCopy()
meta.RemoveStatusCondition(&gw.Status.Conditions, condition.Type)
if !reflect.DeepEqual(previous.Status.Conditions, gw.Status.Conditions) {
if err := r.Client().Status().Update(ctx, gw.Gateway); err != nil {
return err
}
}
}

return nil
}

func (r *DNSPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error {
gatewayEventMapper := &GatewayEventMapper{
Logger: r.Logger().WithName("gatewayEventMapper"),
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/dnsrecord/dnsrecord_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (r *DNSRecordReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
dnsRecord.Status.ObservedGeneration = dnsRecord.Generation
dnsRecord.Status.Endpoints = dnsRecord.Spec.Endpoints
}
setDNSRecordCondition(dnsRecord, conditions.ConditionTypeReady, status, reason, message)
setDNSRecordCondition(dnsRecord, string(conditions.ConditionTypeReady), status, reason, message)

if !equality.Semantic.DeepEqual(previous.Status, dnsRecord.Status) {
updateErr := r.Status().Update(ctx, dnsRecord)
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/managedzone/managedzone_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (r *ManagedZoneReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

managedZone.Status.ObservedGeneration = managedZone.Generation
setManagedZoneCondition(managedZone, conditions.ConditionTypeReady, status, reason, message)
setManagedZoneCondition(managedZone, string(conditions.ConditionTypeReady), status, reason, message)
err = r.Status().Update(ctx, managedZone)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -192,7 +192,7 @@ func (r *ManagedZoneReconciler) deleteManagedZone(ctx context.Context, managedZo
reason = "ProviderError"
message = fmt.Sprintf("The DNS provider creation failed: %v", err)
managedZone.Status.ObservedGeneration = managedZone.Generation
setManagedZoneCondition(managedZone, conditions.ConditionTypeReady, status, reason, message)
setManagedZoneCondition(managedZone, string(conditions.ConditionTypeReady), status, reason, message)
return err
}
err = dnsProvider.DeleteManagedZone(managedZone)
Expand Down Expand Up @@ -336,7 +336,7 @@ func (r *ManagedZoneReconciler) parentZoneNSRecordReady(ctx context.Context, man
}
}

nsRecordReady := meta.IsStatusConditionTrue(nsRecord.Status.Conditions, conditions.ConditionTypeReady)
nsRecordReady := meta.IsStatusConditionTrue(nsRecord.Status.Conditions, string(conditions.ConditionTypeReady))
if !nsRecordReady {
return fmt.Errorf("the ns record is not in a ready state : %s", nsRecord.Name)
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/dnspolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ var _ = Describe("DNSPolicy", Ordered, func() {
return false
}

return meta.IsStatusConditionTrue(dnsPolicy.Status.Conditions, conditions.ConditionTypeReady)
return meta.IsStatusConditionTrue(dnsPolicy.Status.Conditions, string(conditions.ConditionTypeReady))
}, time.Second*15, time.Second).Should(BeTrue())
})

Expand Down Expand Up @@ -687,7 +687,7 @@ var _ = Describe("DNSPolicy", Ordered, func() {
return false
}

return meta.IsStatusConditionTrue(dnsPolicy.Status.Conditions, conditions.ConditionTypeReady)
return meta.IsStatusConditionTrue(dnsPolicy.Status.Conditions, string(conditions.ConditionTypeReady))
}, time.Second*15, time.Second).Should(BeTrue())
})

Expand Down

0 comments on commit f540886

Please sign in to comment.