Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limitador Service Settings #19

Merged
merged 10 commits into from
Jun 16, 2022
18 changes: 18 additions & 0 deletions api/v1alpha1/limitador_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ type LimitadorSpec struct {

// +optional
Version *string `json:"version,omitempty"`

// +optional
Listener *Listener `json:"listener,omitempty"`
}

// LimitadorStatus defines the observed state of Limitador
type LimitadorStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file

ServiceURL string `json:"service-url,omitempty"`
didierofrivia marked this conversation as resolved.
Show resolved Hide resolved
}

//+kubebuilder:object:root=true
Expand All @@ -62,6 +67,19 @@ type LimitadorList struct {
Items []Limitador `json:"items"`
}

type Listener struct {
// +optional
HTTP TransportProtocol `json:"http,omitempty"`
// +optional
GRPC TransportProtocol `json:"grpc,omitempty"`
}

type TransportProtocol struct {
// +optional
Port *int32 `json:"port,omitempty"`
// We could describe TLS within this type
}

func init() {
SchemeBuilder.Register(&Limitador{}, &LimitadorList{})
}
7 changes: 7 additions & 0 deletions api/v1alpha1/ratelimit_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type RateLimitSpec struct {
Namespace string `json:"namespace"`
Seconds int `json:"seconds"`
Variables []string `json:"variables"`
// +optional
LimitadorRef NamespacedName `json:"limitador-ref,omitempty"`
didierofrivia marked this conversation as resolved.
Show resolved Hide resolved
}

// RateLimitStatus defines the observed state of RateLimit
Expand Down Expand Up @@ -62,6 +64,11 @@ type RateLimitList struct {
Items []RateLimit `json:"items"`
}

type NamespacedName struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Namespace string `json:"namespace,omitempty"`
Name string `json:"name,omitempty"`
}

func init() {
SchemeBuilder.Register(&RateLimit{}, &RateLimitList{})
}
58 changes: 58 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

18 changes: 18 additions & 0 deletions bundle/manifests/limitador.kuadrant.io_limitadors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,31 @@ spec:
spec:
description: LimitadorSpec defines the desired state of Limitador
properties:
listener:
properties:
grpc:
properties:
port:
format: int32
type: integer
type: object
http:
properties:
port:
format: int32
type: integer
type: object
type: object
replicas:
type: integer
version:
type: string
type: object
status:
description: LimitadorStatus defines the observed state of Limitador
properties:
service-url:
type: string
type: object
type: object
served: true
Expand Down
7 changes: 7 additions & 0 deletions bundle/manifests/limitador.kuadrant.io_ratelimits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ spec:
items:
type: string
type: array
limitador-ref:
properties:
name:
type: string
namespace:
type: string
type: object
max_value:
type: integer
namespace:
Expand Down
18 changes: 18 additions & 0 deletions config/crd/bases/limitador.kuadrant.io_limitadors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,31 @@ spec:
spec:
description: LimitadorSpec defines the desired state of Limitador
properties:
listener:
properties:
grpc:
properties:
port:
format: int32
type: integer
type: object
http:
properties:
port:
format: int32
type: integer
type: object
type: object
replicas:
type: integer
version:
type: string
type: object
status:
description: LimitadorStatus defines the observed state of Limitador
properties:
service-url:
type: string
type: object
type: object
served: true
Expand Down
7 changes: 7 additions & 0 deletions config/crd/bases/limitador.kuadrant.io_ratelimits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ spec:
items:
type: string
type: array
limitador-ref:
properties:
name:
type: string
namespace:
type: string
type: object
max_value:
type: integer
namespace:
Expand Down
25 changes: 25 additions & 0 deletions config/deploy/manfiests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,31 @@ spec:
spec:
description: LimitadorSpec defines the desired state of Limitador
properties:
listener:
properties:
grpc:
properties:
port:
format: int32
type: integer
type: object
http:
properties:
port:
format: int32
type: integer
type: object
type: object
replicas:
type: integer
version:
type: string
type: object
status:
description: LimitadorStatus defines the observed state of Limitador
properties:
service-url:
type: string
type: object
type: object
served: true
Expand Down Expand Up @@ -101,6 +119,13 @@ spec:
items:
type: string
type: array
limitador-ref:
properties:
name:
type: string
namespace:
type: string
type: object
max_value:
type: integer
namespace:
Expand Down
25 changes: 25 additions & 0 deletions config/install/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,31 @@ spec:
spec:
description: LimitadorSpec defines the desired state of Limitador
properties:
listener:
properties:
grpc:
properties:
port:
format: int32
type: integer
type: object
http:
properties:
port:
format: int32
type: integer
type: object
type: object
replicas:
type: integer
version:
type: string
type: object
status:
description: LimitadorStatus defines the observed state of Limitador
properties:
service-url:
type: string
type: object
type: object
served: true
Expand Down Expand Up @@ -94,6 +112,13 @@ spec:
items:
type: string
type: array
limitador-ref:
properties:
name:
type: string
namespace:
type: string
type: object
max_value:
type: integer
namespace:
Expand Down
27 changes: 27 additions & 0 deletions controllers/limitador_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package controllers
import (
"context"
"fmt"
"github.com/kuadrant/limitador-operator/pkg/helpers"
"strconv"

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -79,6 +81,11 @@ func (r *LimitadorReconciler) Reconcile(eventCtx context.Context, req ctrl.Reque
return ctrl.Result{}, err
}

// Reconcile Status
if err = r.reconcileStatus(ctx, limitadorObj); err != nil {
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}

Expand All @@ -89,6 +96,26 @@ func (r *LimitadorReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func (r *LimitadorReconciler) reconcileStatus(ctx context.Context, limitadorObj *limitadorv1alpha1.Limitador) error {
logger := logr.FromContext(ctx)
// Simple enough now, we could implement a thorough check like with RateLimit with ObservedGeneration in the future
builtServiceUrl := buildServiceUrl(limitadorObj)
if builtServiceUrl != limitadorObj.Status.ServiceURL {
logger.V(1).Info("Updating the Status", "Name", limitadorObj.Name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need, already includes the name

limitadorObj.Status.ServiceURL = builtServiceUrl
return r.Client().Status().Update(ctx, limitadorObj)

}
return nil
}

func buildServiceUrl(limitadorObj *limitadorv1alpha1.Limitador) string {
didierofrivia marked this conversation as resolved.
Show resolved Hide resolved
return "http://" +
limitadorObj.Name + "." +
limitadorObj.Namespace + ".svc.cluster.local:" +
strconv.Itoa(int(helpers.GetValueOrDefault(*limitadorObj.Spec.Listener.HTTP.Port, limitador.DefaultServiceHTTPPort).(int32)))
}

func mutateLimitadorDeployment(existingObj, desiredObj client.Object) (bool, error) {
existing, ok := existingObj.(*appsv1.Deployment)
if !ok {
Expand Down
Loading