Skip to content

Commit

Permalink
Policy:Bump:v1 (#993)
Browse files Browse the repository at this point in the history
Bump the Auth and Rate Limit policies to v1

Signed-off-by: Jim Fitzpatrick <[email protected]>
  • Loading branch information
Boomatang authored Nov 8, 2024
1 parent c3a16d8 commit d74efd5
Show file tree
Hide file tree
Showing 69 changed files with 1,293 additions and 1,382 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ endef

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) crd paths="./api/v1beta1;./api/v1beta3;./api/v1" output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) crd paths="./api/v1beta1;./api/v1" output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) rbac:roleName=manager-role webhook paths="./..."

.PHONY: dependencies-manifests
Expand Down
61 changes: 30 additions & 31 deletions api/v1beta3/authpolicy_types.go → api/v1/authpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta3
package v1

import (
"fmt"
Expand All @@ -29,7 +29,6 @@ import (
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

kuadrantv1 "github.com/kuadrant/kuadrant-operator/api/v1"
kuadrantgatewayapi "github.com/kuadrant/kuadrant-operator/pkg/library/gatewayapi"
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant"
"github.com/kuadrant/kuadrant-operator/pkg/library/utils"
Expand All @@ -42,8 +41,8 @@ const (
)

var (
AuthPolicyGroupKind = schema.GroupKind{Group: SchemeGroupVersion.Group, Kind: "AuthPolicy"}
AuthPoliciesResource = SchemeGroupVersion.WithResource("authpolicies")
AuthPolicyGroupKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "AuthPolicy"}
AuthPoliciesResource = GroupVersion.WithResource("authpolicies")
)

// +kubebuilder:object:root=true
Expand Down Expand Up @@ -81,7 +80,7 @@ func (p *AuthPolicy) GetLocator() string {

// TODO: remove
func (p *AuthPolicy) IsAtomicOverride() bool {
return p.Spec.Overrides != nil && p.Spec.Overrides.Strategy == kuadrantv1.AtomicMergeStrategy
return p.Spec.Overrides != nil && p.Spec.Overrides.Strategy == AtomicMergeStrategy
}

// DEPRECATED: Use GetTargetRefs instead
Expand All @@ -100,12 +99,12 @@ func (p *AuthPolicy) GetTargetRefs() []machinery.PolicyTargetReference {

func (p *AuthPolicy) GetMergeStrategy() machinery.MergeStrategy {
if spec := p.Spec.Defaults; spec != nil {
return kuadrantv1.DefaultsMergeStrategy(spec.Strategy)
return DefaultsMergeStrategy(spec.Strategy)
}
if spec := p.Spec.Overrides; spec != nil {
return kuadrantv1.OverridesMergeStrategy(spec.Strategy)
return OverridesMergeStrategy(spec.Strategy)
}
return kuadrantv1.AtomicDefaultsMergeStrategy
return AtomicDefaultsMergeStrategy
}

func (p *AuthPolicy) Merge(other machinery.Policy) machinery.Policy {
Expand All @@ -116,24 +115,24 @@ func (p *AuthPolicy) Merge(other machinery.Policy) machinery.Policy {
return source.GetMergeStrategy()(source, p)
}

var _ kuadrantv1.MergeablePolicy = &AuthPolicy{}
var _ MergeablePolicy = &AuthPolicy{}

func (p *AuthPolicy) Empty() bool {
return p.Spec.Proper().AuthScheme == nil
}

func (p *AuthPolicy) Rules() map[string]kuadrantv1.MergeableRule {
rules := make(map[string]kuadrantv1.MergeableRule)
func (p *AuthPolicy) Rules() map[string]MergeableRule {
rules := make(map[string]MergeableRule)
policyLocator := p.GetLocator()
spec := p.Spec.Proper()

for ruleID := range spec.NamedPatterns {
rule := spec.NamedPatterns[ruleID]
rules[fmt.Sprintf("patterns#%s", ruleID)] = kuadrantv1.NewMergeableRule(&rule, policyLocator)
rules[fmt.Sprintf("patterns#%s", ruleID)] = NewMergeableRule(&rule, policyLocator)
}

if whenPredicates := spec.MergeableWhenPredicates; len(whenPredicates.Predicates) > 0 {
rules["conditions#"] = kuadrantv1.NewMergeableRule(&whenPredicates, policyLocator)
rules["conditions#"] = NewMergeableRule(&whenPredicates, policyLocator)
}

if spec.AuthScheme == nil {
Expand All @@ -142,49 +141,49 @@ func (p *AuthPolicy) Rules() map[string]kuadrantv1.MergeableRule {

for ruleID := range spec.AuthScheme.Authentication {
rule := spec.AuthScheme.Authentication[ruleID]
rules[fmt.Sprintf("authentication#%s", ruleID)] = kuadrantv1.NewMergeableRule(&rule, policyLocator)
rules[fmt.Sprintf("authentication#%s", ruleID)] = NewMergeableRule(&rule, policyLocator)
}

for ruleID := range spec.AuthScheme.Metadata {
rule := spec.AuthScheme.Metadata[ruleID]
rules[fmt.Sprintf("metadata#%s", ruleID)] = kuadrantv1.NewMergeableRule(&rule, policyLocator)
rules[fmt.Sprintf("metadata#%s", ruleID)] = NewMergeableRule(&rule, policyLocator)
}

for ruleID := range spec.AuthScheme.Authorization {
rule := spec.AuthScheme.Authorization[ruleID]
rules[fmt.Sprintf("authorization#%s", ruleID)] = kuadrantv1.NewMergeableRule(&rule, policyLocator)
rules[fmt.Sprintf("authorization#%s", ruleID)] = NewMergeableRule(&rule, policyLocator)
}

for ruleID := range spec.AuthScheme.Callbacks {
rule := spec.AuthScheme.Callbacks[ruleID]
rules[fmt.Sprintf("callbacks#%s", ruleID)] = kuadrantv1.NewMergeableRule(&rule, policyLocator)
rules[fmt.Sprintf("callbacks#%s", ruleID)] = NewMergeableRule(&rule, policyLocator)
}

if spec.AuthScheme.Response == nil {
return rules
}

if rule := spec.AuthScheme.Response.Unauthenticated; rule != nil {
rules["response.unauthenticated#"] = kuadrantv1.NewMergeableRule(rule, policyLocator)
rules["response.unauthenticated#"] = NewMergeableRule(rule, policyLocator)
}
if rule := spec.AuthScheme.Response.Unauthorized; rule != nil {
rules["response.unauthorized#"] = kuadrantv1.NewMergeableRule(rule, policyLocator)
rules["response.unauthorized#"] = NewMergeableRule(rule, policyLocator)
}

for ruleID := range spec.AuthScheme.Response.Success.Headers {
rule := spec.AuthScheme.Response.Success.Headers[ruleID]
rules[fmt.Sprintf("response.success.headers#%s", ruleID)] = kuadrantv1.NewMergeableRule(&rule, policyLocator)
rules[fmt.Sprintf("response.success.headers#%s", ruleID)] = NewMergeableRule(&rule, policyLocator)
}

for ruleID := range spec.AuthScheme.Response.Success.DynamicMetadata {
rule := spec.AuthScheme.Response.Success.DynamicMetadata[ruleID]
rules[fmt.Sprintf("response.success.metadata#%s", ruleID)] = kuadrantv1.NewMergeableRule(&rule, policyLocator)
rules[fmt.Sprintf("response.success.metadata#%s", ruleID)] = NewMergeableRule(&rule, policyLocator)
}

return rules
}

func (p *AuthPolicy) SetRules(rules map[string]kuadrantv1.MergeableRule) {
func (p *AuthPolicy) SetRules(rules map[string]MergeableRule) {
// clear all rules of the policy before setting new ones
p.Spec.Proper().NamedPatterns = nil
p.Spec.Proper().Predicates = nil
Expand Down Expand Up @@ -435,7 +434,7 @@ type MergeablePatternExpressions struct {

func (r *MergeablePatternExpressions) GetSpec() any { return r.PatternExpressions }
func (r *MergeablePatternExpressions) GetSource() string { return r.Source }
func (r *MergeablePatternExpressions) WithSource(source string) kuadrantv1.MergeableRule {
func (r *MergeablePatternExpressions) WithSource(source string) MergeableRule {
r.Source = source
return r
}
Expand All @@ -447,7 +446,7 @@ type MergeablePatternExpressionOrRef struct {

func (r *MergeablePatternExpressionOrRef) GetSpec() any { return r.PatternExpressionOrRef }
func (r *MergeablePatternExpressionOrRef) GetSource() string { return r.Source }
func (r *MergeablePatternExpressionOrRef) WithSource(source string) kuadrantv1.MergeableRule {
func (r *MergeablePatternExpressionOrRef) WithSource(source string) MergeableRule {
r.Source = source
return r
}
Expand All @@ -459,7 +458,7 @@ type MergeableAuthenticationSpec struct {

func (r *MergeableAuthenticationSpec) GetSpec() any { return r.AuthenticationSpec }
func (r *MergeableAuthenticationSpec) GetSource() string { return r.Source }
func (r *MergeableAuthenticationSpec) WithSource(source string) kuadrantv1.MergeableRule {
func (r *MergeableAuthenticationSpec) WithSource(source string) MergeableRule {
r.Source = source
return r
}
Expand All @@ -471,7 +470,7 @@ type MergeableMetadataSpec struct {

func (r *MergeableMetadataSpec) GetSpec() any { return r.MetadataSpec }
func (r *MergeableMetadataSpec) GetSource() string { return r.Source }
func (r *MergeableMetadataSpec) WithSource(source string) kuadrantv1.MergeableRule {
func (r *MergeableMetadataSpec) WithSource(source string) MergeableRule {
r.Source = source
return r
}
Expand All @@ -483,7 +482,7 @@ type MergeableAuthorizationSpec struct {

func (r *MergeableAuthorizationSpec) GetSpec() any { return r.AuthorizationSpec }
func (r *MergeableAuthorizationSpec) GetSource() string { return r.Source }
func (r *MergeableAuthorizationSpec) WithSource(source string) kuadrantv1.MergeableRule {
func (r *MergeableAuthorizationSpec) WithSource(source string) MergeableRule {
r.Source = source
return r
}
Expand Down Expand Up @@ -515,7 +514,7 @@ type MergeableDenyWithSpec struct {

func (r *MergeableDenyWithSpec) GetSpec() any { return r.DenyWithSpec }
func (r *MergeableDenyWithSpec) GetSource() string { return r.Source }
func (r *MergeableDenyWithSpec) WithSource(source string) kuadrantv1.MergeableRule {
func (r *MergeableDenyWithSpec) WithSource(source string) MergeableRule {
r.Source = source
return r
}
Expand All @@ -535,7 +534,7 @@ type MergeableHeaderSuccessResponseSpec struct {

func (r *MergeableHeaderSuccessResponseSpec) GetSpec() any { return r.HeaderSuccessResponseSpec }
func (r *MergeableHeaderSuccessResponseSpec) GetSource() string { return r.Source }
func (r *MergeableHeaderSuccessResponseSpec) WithSource(source string) kuadrantv1.MergeableRule {
func (r *MergeableHeaderSuccessResponseSpec) WithSource(source string) MergeableRule {
r.Source = source
return r
}
Expand All @@ -547,7 +546,7 @@ type MergeableSuccessResponseSpec struct {

func (r *MergeableSuccessResponseSpec) GetSpec() any { return r.SuccessResponseSpec }
func (r *MergeableSuccessResponseSpec) GetSource() string { return r.Source }
func (r *MergeableSuccessResponseSpec) WithSource(source string) kuadrantv1.MergeableRule {
func (r *MergeableSuccessResponseSpec) WithSource(source string) MergeableRule {
r.Source = source
return r
}
Expand All @@ -559,7 +558,7 @@ type MergeableCallbackSpec struct {

func (r *MergeableCallbackSpec) GetSpec() any { return r.CallbackSpec }
func (r *MergeableCallbackSpec) GetSource() string { return r.Source }
func (r *MergeableCallbackSpec) WithSource(source string) kuadrantv1.MergeableRule {
func (r *MergeableCallbackSpec) WithSource(source string) MergeableRule {
r.Source = source
return r
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build unit

package v1beta3
package v1

import (
"testing"
Expand Down
8 changes: 3 additions & 5 deletions api/v1beta3/common_types.go → api/v1/common_types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package v1beta3
package v1

import (
"github.com/samber/lo"

kuadrantv1 "github.com/kuadrant/kuadrant-operator/api/v1"
)

func NewPredicate(predicate string) Predicate {
Expand Down Expand Up @@ -50,7 +48,7 @@ type MergeableWhenPredicates struct {
Source string `json:"-"`
}

var _ kuadrantv1.MergeableRule = &MergeableWhenPredicates{}
var _ MergeableRule = &MergeableWhenPredicates{}

func (p *MergeableWhenPredicates) GetSpec() any {
return p.Predicates
Expand All @@ -60,7 +58,7 @@ func (p *MergeableWhenPredicates) GetSource() string {
return p.Source
}

func (p *MergeableWhenPredicates) WithSource(source string) kuadrantv1.MergeableRule {
func (p *MergeableWhenPredicates) WithSource(source string) MergeableRule {
p.Source = source
return p
}
5 changes: 4 additions & 1 deletion api/v1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

// GroupName specifies the group name used to register the objects.
const GroupName = "kuadrant.io"

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "kuadrant.io", Version: "v1"}
GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta3
package v1

import (
"time"
Expand All @@ -25,7 +25,6 @@ import (
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

kuadrantv1 "github.com/kuadrant/kuadrant-operator/api/v1"
kuadrantgatewayapi "github.com/kuadrant/kuadrant-operator/pkg/library/gatewayapi"
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant"
"github.com/kuadrant/kuadrant-operator/pkg/library/utils"
Expand All @@ -38,8 +37,8 @@ const (
)

var (
RateLimitPolicyGroupKind = schema.GroupKind{Group: SchemeGroupVersion.Group, Kind: "RateLimitPolicy"}
RateLimitPoliciesResource = SchemeGroupVersion.WithResource("ratelimitpolicies")
RateLimitPolicyGroupKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "RateLimitPolicy"}
RateLimitPoliciesResource = GroupVersion.WithResource("ratelimitpolicies")
// Top level predicate rules key starting with # to prevent conflict with limit names
// TODO(eastizle): this coupling between limit names and rule IDs is a bad smell. Merging implementation should be enhanced.
RulesKeyTopLevelPredicates = "###_TOP_LEVEL_PREDICATES_###"
Expand Down Expand Up @@ -94,12 +93,12 @@ func (p *RateLimitPolicy) GetTargetRefs() []machinery.PolicyTargetReference {

func (p *RateLimitPolicy) GetMergeStrategy() machinery.MergeStrategy {
if spec := p.Spec.Defaults; spec != nil {
return kuadrantv1.DefaultsMergeStrategy(spec.Strategy)
return DefaultsMergeStrategy(spec.Strategy)
}
if spec := p.Spec.Overrides; spec != nil {
return kuadrantv1.OverridesMergeStrategy(spec.Strategy)
return OverridesMergeStrategy(spec.Strategy)
}
return kuadrantv1.AtomicDefaultsMergeStrategy
return AtomicDefaultsMergeStrategy
}

func (p *RateLimitPolicy) Merge(other machinery.Policy) machinery.Policy {
Expand All @@ -110,30 +109,30 @@ func (p *RateLimitPolicy) Merge(other machinery.Policy) machinery.Policy {
return source.GetMergeStrategy()(source, p)
}

var _ kuadrantv1.MergeablePolicy = &RateLimitPolicy{}
var _ MergeablePolicy = &RateLimitPolicy{}

func (p *RateLimitPolicy) Empty() bool {
return len(p.Spec.Proper().Limits) == 0
}

func (p *RateLimitPolicy) Rules() map[string]kuadrantv1.MergeableRule {
rules := make(map[string]kuadrantv1.MergeableRule)
func (p *RateLimitPolicy) Rules() map[string]MergeableRule {
rules := make(map[string]MergeableRule)
policyLocator := p.GetLocator()
spec := p.Spec.Proper()

if whenPredicates := spec.MergeableWhenPredicates; len(whenPredicates.Predicates) > 0 {
rules[RulesKeyTopLevelPredicates] = kuadrantv1.NewMergeableRule(&whenPredicates, policyLocator)
rules[RulesKeyTopLevelPredicates] = NewMergeableRule(&whenPredicates, policyLocator)
}

for ruleID := range spec.Limits {
limit := spec.Limits[ruleID]
rules[ruleID] = kuadrantv1.NewMergeableRule(&limit, policyLocator)
rules[ruleID] = NewMergeableRule(&limit, policyLocator)
}

return rules
}

func (p *RateLimitPolicy) SetRules(rules map[string]kuadrantv1.MergeableRule) {
func (p *RateLimitPolicy) SetRules(rules map[string]MergeableRule) {
// clear all rules of the policy before setting new ones
p.Spec.Proper().Limits = nil
p.Spec.Proper().Predicates = nil
Expand Down Expand Up @@ -273,7 +272,7 @@ func (l Limit) CountersAsStringList() []string {
return utils.Map(l.Counters, func(counter Counter) string { return string(counter.Expression) })
}

var _ kuadrantv1.MergeableRule = &Limit{}
var _ MergeableRule = &Limit{}

func (l *Limit) GetSpec() any {
return l
Expand All @@ -283,7 +282,7 @@ func (l *Limit) GetSource() string {
return l.Source
}

func (l *Limit) WithSource(source string) kuadrantv1.MergeableRule {
func (l *Limit) WithSource(source string) MergeableRule {
l.Source = source
return l
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build unit

package v1beta3
package v1

import (
"testing"
Expand Down
Loading

0 comments on commit d74efd5

Please sign in to comment.