Skip to content

Commit

Permalink
feat: auth policy enforced condition
Browse files Browse the repository at this point in the history
  • Loading branch information
KevFan committed Jan 31, 2024
1 parent c0c178f commit 7904b26
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
19 changes: 13 additions & 6 deletions controllers/authpolicy_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,28 @@ func (r *AuthPolicyReconciler) calculateStatus(ap *api.AuthPolicy, specErr error
ObservedGeneration: ap.Status.ObservedGeneration,
}

availableCond := r.acceptedCondition(ap, specErr, authConfigReady)

availableCond := r.acceptedCondition(ap, specErr)
meta.SetStatusCondition(&newStatus.Conditions, *availableCond)

enforcedCond := r.enforcedCondition(ap, authConfigReady)
meta.SetStatusCondition(&newStatus.Conditions, *enforcedCond)

return newStatus
}

func (r *AuthPolicyReconciler) acceptedCondition(policy common.KuadrantPolicy, specErr error, authConfigReady bool) *metav1.Condition {
func (r *AuthPolicyReconciler) acceptedCondition(policy common.KuadrantPolicy, specErr error) *metav1.Condition {
cond := common.AcceptedCondition(policy, specErr)

return cond
}

func (r *AuthPolicyReconciler) enforcedCondition(policy common.KuadrantPolicy, authConfigReady bool) *metav1.Condition {
var err common.PolicyError
if !authConfigReady {
cond.Status = metav1.ConditionFalse
cond.Reason = "AuthSchemeNotReady"
cond.Message = "AuthScheme is not ready yet" // TODO(rahul): need to take care if status change is delayed.
err = common.NewErrUnknown(policy.Kind(), fmt.Errorf("AuthScheme is not ready yet"))
}

cond := common.EnforcedCondition(policy, err)

return cond
}
31 changes: 31 additions & 0 deletions pkg/common/apimachinery_status_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
)

const (
PolicyConditionEnforced gatewayapiv1alpha2.PolicyConditionType = "Enforced"

PolicyReasonEnforced gatewayapiv1alpha2.PolicyConditionReason = "Enforced"
)

// ConditionMarshal marshals the set of conditions as a JSON array, sorted by condition type.
func ConditionMarshal(conditions []metav1.Condition) ([]byte, error) {
condCopy := slices.Clone(conditions)
Expand Down Expand Up @@ -44,3 +50,28 @@ func AcceptedCondition(policy KuadrantPolicy, err error) *metav1.Condition {

return cond
}

// EnforcedCondition returns an enforced conditions with common reasons for a kuadrant policy
func EnforcedCondition(policy KuadrantPolicy, err error) *metav1.Condition {
// Enforced
cond := &metav1.Condition{
Type: string(PolicyConditionEnforced),
Status: metav1.ConditionTrue,
Reason: string(PolicyReasonEnforced),
Message: fmt.Sprintf("%s has been successfully enforced", policy.Kind()),
}
if err == nil {
return cond
}

Check warning on line 65 in pkg/common/apimachinery_status_conditions.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/apimachinery_status_conditions.go#L55-L65

Added lines #L55 - L65 were not covered by tests

cond.Status = metav1.ConditionFalse
cond.Message = err.Error()
cond.Reason = "ReconciliationError"

var policyErr PolicyError
if errors.As(err, &policyErr) {
cond.Reason = string(policyErr.Reason())
}

Check warning on line 74 in pkg/common/apimachinery_status_conditions.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/apimachinery_status_conditions.go#L67-L74

Added lines #L67 - L74 were not covered by tests

return cond

Check warning on line 76 in pkg/common/apimachinery_status_conditions.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/apimachinery_status_conditions.go#L76

Added line #L76 was not covered by tests
}
22 changes: 22 additions & 0 deletions pkg/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,25 @@ func NewErrConflict(kind string, nameNamespace string, err error) ErrConflict {
Err: err,
}
}

var _ PolicyError = ErrUnknown{}

type ErrUnknown struct {
Kind string
Err error
}

func (e ErrUnknown) Error() string {
return fmt.Sprintf("%s has encountered some issues: %s", e.Kind, e.Err.Error())

Check warning on line 97 in pkg/common/errors.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/errors.go#L96-L97

Added lines #L96 - L97 were not covered by tests
}

func (e ErrUnknown) Reason() gatewayapiv1alpha2.PolicyConditionReason {
return "Unknown"

Check warning on line 101 in pkg/common/errors.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/errors.go#L100-L101

Added lines #L100 - L101 were not covered by tests
}

func NewErrUnknown(kind string, err error) ErrUnknown {
return ErrUnknown{
Kind: kind,
Err: err,
}

Check warning on line 108 in pkg/common/errors.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/errors.go#L104-L108

Added lines #L104 - L108 were not covered by tests
}

0 comments on commit 7904b26

Please sign in to comment.