Skip to content

Commit

Permalink
feat: accepted policy status condition (#347)
Browse files Browse the repository at this point in the history
* feat: rlp accepted condition

* feat: rlp target not found reason

* feat: rlp invalid reason

* feat: rlp conflicted reason

* feat: auth policy accepted condition

* feat: auth policy invalid reason

* feat: auth policy conflict reason

* test: rlp accepted reason conditions

* feat: auth policy target not found reason

* test: auth policy accepted condition reasons integration tests

* refactor: use common acception condition function instead

* refactor: integration test policy factory mutateFn

* refactor: interface for standard policy errors
  • Loading branch information
KevFan authored Jan 9, 2024
1 parent 94683a2 commit ed590ec
Show file tree
Hide file tree
Showing 17 changed files with 926 additions and 864 deletions.
6 changes: 6 additions & 0 deletions api/v1beta2/authpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ func (s *AuthPolicyStatus) Equals(other *AuthPolicyStatus, logger logr.Logger) b
return true
}

var _ common.KuadrantPolicy = &AuthPolicy{}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:metadata:labels="gateway.networking.k8s.io/policy=direct"
Expand Down Expand Up @@ -277,6 +279,10 @@ func (ap *AuthPolicy) GetRulesHostnames() (ruleHosts []string) {
return
}

func (ap *AuthPolicy) Kind() string {
return ap.TypeMeta.Kind
}

//+kubebuilder:object:root=true

// AuthPolicyList contains a list of AuthPolicy
Expand Down
6 changes: 6 additions & 0 deletions api/v1beta2/ratelimitpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ func (s *RateLimitPolicyStatus) Equals(other *RateLimitPolicyStatus, logger logr
return true
}

var _ common.KuadrantPolicy = &RateLimitPolicy{}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:metadata:labels="gateway.networking.k8s.io/policy=direct"
Expand Down Expand Up @@ -240,6 +242,10 @@ func (r *RateLimitPolicy) GetRulesHostnames() (ruleHosts []string) {
return
}

func (r *RateLimitPolicy) Kind() string {
return r.TypeMeta.Kind
}

func init() {
SchemeBuilder.Register(&RateLimitPolicy{}, &RateLimitPolicyList{})
}
24 changes: 16 additions & 8 deletions controllers/authpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (r *AuthPolicyReconciler) Reconcile(eventCtx context.Context, req ctrl.Requ
if delResErr == nil {
delResErr = err
}
return r.reconcileStatus(ctx, ap, delResErr)
return r.reconcileStatus(ctx, ap, common.NewErrTargetNotFound(ap.Kind(), ap.GetTargetRef(), delResErr))
}
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -130,13 +130,21 @@ func (r *AuthPolicyReconciler) Reconcile(eventCtx context.Context, req ctrl.Requ
return ctrl.Result{}, nil
}

func (r *AuthPolicyReconciler) reconcileResources(ctx context.Context, ap *api.AuthPolicy, targetNetworkObject client.Object) error {
// validate
// validate performs validation before proceeding with the reconcile loop, returning a common.ErrInvalid on any failing validation
func (r *AuthPolicyReconciler) validate(ap *api.AuthPolicy, targetNetworkObject client.Object) error {
if err := ap.Validate(); err != nil {
return err
return common.NewErrInvalid(ap.Kind(), err)
}

if err := common.ValidateHierarchicalRules(ap, targetNetworkObject); err != nil {
return common.NewErrInvalid(ap.Kind(), err)
}

return nil
}

func (r *AuthPolicyReconciler) reconcileResources(ctx context.Context, ap *api.AuthPolicy, targetNetworkObject client.Object) error {
if err := r.validate(ap, targetNetworkObject); err != nil {
return err
}

Expand All @@ -159,7 +167,7 @@ func (r *AuthPolicyReconciler) reconcileResources(ctx context.Context, ap *api.A
return err
}

// set annotation of policies afftecting the gateway - should be the last step, only when all the reconciliation steps succeed
// set annotation of policies affecting the gateway - should be the last step, only when all the reconciliation steps succeed
return r.ReconcileGatewayPolicyReferences(ctx, ap, gatewayDiffObj)
}

Expand All @@ -181,13 +189,13 @@ func (r *AuthPolicyReconciler) deleteResources(ctx context.Context, ap *api.Auth
}
}

// update annotation of policies afftecting the gateway
// update annotation of policies affecting the gateway
return r.ReconcileGatewayPolicyReferences(ctx, ap, gatewayDiffObj)
}

// Ensures only one RLP targets the network resource
func (r *AuthPolicyReconciler) reconcileNetworkResourceDirectBackReference(ctx context.Context, ap *api.AuthPolicy, targetNetworkObject client.Object) error {
return r.ReconcileTargetBackReference(ctx, client.ObjectKeyFromObject(ap), targetNetworkObject, common.AuthPolicyBackRefAnnotation)
func (r *AuthPolicyReconciler) reconcileNetworkResourceDirectBackReference(ctx context.Context, ap common.KuadrantPolicy, targetNetworkObject client.Object) error {
return r.ReconcileTargetBackReference(ctx, ap, targetNetworkObject, common.AuthPolicyBackRefAnnotation)
}

func (r *AuthPolicyReconciler) deleteNetworkResourceDirectBackReference(ctx context.Context, targetNetworkObject client.Object) error {
Expand Down
Loading

0 comments on commit ed590ec

Please sign in to comment.