Skip to content

Commit

Permalink
ratelimitpolicy status has virtualservices and gateways (Kuadrant#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki authored Feb 23, 2022
1 parent 8317588 commit 2a0e406
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 31 deletions.
64 changes: 63 additions & 1 deletion apis/apim/v1alpha1/ratelimitpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ package v1alpha1

import (
limitadorv1alpha1 "github.com/kuadrant/limitador-operator/api/v1alpha1"
istiov1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kuadrant/kuadrant-controller/pkg/common"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down Expand Up @@ -98,14 +101,73 @@ type RateLimitPolicySpec struct {
Limits []limitadorv1alpha1.RateLimitSpec `json:"limits,omitempty"`
}

type ObjectRefStatus struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
}

type VirtualServicesStatus struct {
Name string `json:"name"`

// +optional
Gateways []ObjectRefStatus `json:"gateways,omitempty"`
}

// RateLimitPolicyStatus defines the observed state of RateLimitPolicy
type RateLimitPolicyStatus struct {
// VirtualServices represents the current VirtualService objects with reference to this ratelimitpolicy object
// +optional
VirtualServices []VirtualServicesStatus `json:"virtualservices,omitempty"`
}

// AddVirtualService Adds virtualservice to the list of objects only if it does not exit. Returns true when added.
func (r *RateLimitPolicyStatus) AddVirtualService(vs *istiov1alpha3.VirtualService) bool {
for idx := range r.VirtualServices {
if r.VirtualServices[idx].Name == vs.Name {
return false
}
}

// Not found, add it

newVSStatus := VirtualServicesStatus{
Name: vs.Name,
}

for _, gw := range vs.Spec.Gateways {
gwKey := common.NamespacedNameToObjectKey(gw, vs.Namespace)
newVSStatus.Gateways = append(newVSStatus.Gateways, ObjectRefStatus{
Name: gwKey.Name,
Namespace: gwKey.Namespace,
})
}

r.VirtualServices = append(r.VirtualServices, newVSStatus)
return true
}

// DeleteVirtualService removes virtualservice from the list of objects only if it does exit. Returns true when deleted from the list.
func (r *RateLimitPolicyStatus) DeleteVirtualService(vs *istiov1alpha3.VirtualService) bool {
for idx := range r.VirtualServices {
if r.VirtualServices[idx].Name == vs.Name {
// remove the element at idx
r.VirtualServices = append(r.VirtualServices[:idx], r.VirtualServices[idx+1:]...)
return true
}
}
return false
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// RateLimitPolicy is the Schema for the ratelimitpolicies API
type RateLimitPolicy struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec RateLimitPolicySpec `json:"spec,omitempty"`
Spec RateLimitPolicySpec `json:"spec,omitempty"`
Status RateLimitPolicyStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
58 changes: 58 additions & 0 deletions apis/apim/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions config/crd/bases/apim.kuadrant.io_ratelimitpolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,38 @@ spec:
- name
x-kubernetes-list-type: map
type: object
status:
description: RateLimitPolicyStatus defines the observed state of RateLimitPolicy
properties:
virtualservices:
description: VirtualServices represents the current VirtualService
objects with reference to this ratelimitpolicy object
items:
properties:
gateways:
items:
properties:
name:
type: string
namespace:
type: string
required:
- name
- namespace
type: object
type: array
name:
type: string
required:
- name
type: object
type: array
type: object
type: object
served: true
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
Expand Down
28 changes: 28 additions & 0 deletions config/deploy/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,37 @@ spec:
- name
x-kubernetes-list-type: map
type: object
status:
description: RateLimitPolicyStatus defines the observed state of RateLimitPolicy
properties:
virtualservices:
description: VirtualServices represents the current VirtualService objects with reference to this ratelimitpolicy object
items:
properties:
gateways:
items:
properties:
name:
type: string
namespace:
type: string
required:
- name
- namespace
type: object
type: array
name:
type: string
required:
- name
type: object
type: array
type: object
type: object
served: true
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
Expand Down
Loading

0 comments on commit 2a0e406

Please sign in to comment.