Skip to content

Commit

Permalink
[refactor] Making it more flexible to define service params
Browse files Browse the repository at this point in the history
* Listener instead of Service, to be aligned with other projects
* Defining a TransferProtocol for Ports, allowing for future TLS
  features
* Getting the name of the Limitador Obj for the Service Name
  • Loading branch information
didierofrivia committed Jun 16, 2022
1 parent 8ca84c5 commit 1986fd6
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 89 deletions.
15 changes: 7 additions & 8 deletions api/v1alpha1/limitador_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type LimitadorSpec struct {
Version *string `json:"version,omitempty"`

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

// LimitadorStatus defines the observed state of Limitador
Expand Down Expand Up @@ -67,18 +67,17 @@ type LimitadorList struct {
Items []Limitador `json:"items"`
}

type Service struct {
type Listener struct {
// +optional
Name *string `json:"name,omitempty"`
HTTP TransportProtocol `json:"http,omitempty"`
// +optional
Ports Ports `json:"ports,omitempty"`
GRPC TransportProtocol `json:"grpc,omitempty"`
}

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

func init() {
Expand Down
45 changes: 18 additions & 27 deletions api/v1alpha1/zz_generated.deepcopy.go

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

17 changes: 9 additions & 8 deletions bundle/manifests/limitador.kuadrant.io_limitadors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,23 @@ spec:
spec:
description: LimitadorSpec defines the desired state of Limitador
properties:
replicas:
type: integer
service:
listener:
properties:
name:
type: string
ports:
grpc:
properties:
grpc:
port:
format: int32
type: integer
http:
type: object
http:
properties:
port:
format: int32
type: integer
type: object
type: object
replicas:
type: integer
version:
type: string
type: object
Expand Down
17 changes: 9 additions & 8 deletions config/crd/bases/limitador.kuadrant.io_limitadors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,23 @@ spec:
spec:
description: LimitadorSpec defines the desired state of Limitador
properties:
replicas:
type: integer
service:
listener:
properties:
name:
type: string
ports:
grpc:
properties:
grpc:
port:
format: int32
type: integer
http:
type: object
http:
properties:
port:
format: int32
type: integer
type: object
type: object
replicas:
type: integer
version:
type: string
type: object
Expand Down
17 changes: 9 additions & 8 deletions config/deploy/manfiests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,23 @@ spec:
spec:
description: LimitadorSpec defines the desired state of Limitador
properties:
replicas:
type: integer
service:
listener:
properties:
name:
type: string
ports:
grpc:
properties:
grpc:
port:
format: int32
type: integer
http:
type: object
http:
properties:
port:
format: int32
type: integer
type: object
type: object
replicas:
type: integer
version:
type: string
type: object
Expand Down
17 changes: 9 additions & 8 deletions config/install/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,23 @@ spec:
spec:
description: LimitadorSpec defines the desired state of Limitador
properties:
replicas:
type: integer
service:
listener:
properties:
name:
type: string
ports:
grpc:
properties:
grpc:
port:
format: int32
type: integer
http:
type: object
http:
properties:
port:
format: int32
type: integer
type: object
type: object
replicas:
type: integer
version:
type: string
type: object
Expand Down
4 changes: 2 additions & 2 deletions controllers/limitador_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ func (r *LimitadorReconciler) reconcileStatus(ctx context.Context, limitadorObj

func buildServiceUrl(limitadorObj *limitadorv1alpha1.Limitador) string {
return "http://" +
helpers.GetValueOrDefault(*limitadorObj.Spec.Service.Name, limitador.DefaultServiceName).(string) + "." +
limitadorObj.Name + "." +
limitadorObj.Namespace + ".svc.cluster.local:" +
strconv.Itoa(int(helpers.GetValueOrDefault(*limitadorObj.Spec.Service.Ports.HTTP, limitador.DefaultServiceName).(int32)))
strconv.Itoa(int(helpers.GetValueOrDefault(*limitadorObj.Spec.Listener.HTTP.Port, limitador.DefaultServiceHTTPPort).(int32)))
}

func mutateLimitadorDeployment(existingObj, desiredObj client.Object) (bool, error) {
Expand Down
21 changes: 9 additions & 12 deletions controllers/limitador_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,13 @@ var _ = Describe("Limitador controller", func() {
interval = time.Millisecond * 250
)

serviceName := LimitadorServiceName
httpPort := int32(LimitadorHttpPort)
grpcPort := int32(LimitadorGttpPort)
httpPortNumber := int32(LimitadorHttpPort)
grpcPortNumber := int32(LimitadorGttpPort)

replicas := LimitadorReplicas
version := LimitadorVersion
ports := limitadorv1alpha1.Ports{
GRPC: &grpcPort,
HTTP: &httpPort,
}
httpPort := limitadorv1alpha1.TransportProtocol{Port: &httpPortNumber}
grpcPort := limitadorv1alpha1.TransportProtocol{Port: &grpcPortNumber}
newLimitador := func() *limitadorv1alpha1.Limitador {
// The name can't start with a number.
name := "a" + string(uuid.NewUUID())
Expand All @@ -57,9 +54,9 @@ var _ = Describe("Limitador controller", func() {
Spec: limitadorv1alpha1.LimitadorSpec{
Replicas: &replicas,
Version: &version,
Service: limitadorv1alpha1.Service{
Name: &serviceName,
Ports: ports,
Listener: limitadorv1alpha1.Listener{
HTTP: httpPort,
GRPC: grpcPort,
},
},
}
Expand Down Expand Up @@ -107,7 +104,7 @@ var _ = Describe("Limitador controller", func() {
context.TODO(),
types.NamespacedName{
Namespace: LimitadorNamespace,
Name: LimitadorServiceName,
Name: limitadorObj.Name,
},
&createdLimitadorService)
return err == nil
Expand All @@ -125,7 +122,7 @@ var _ = Describe("Limitador controller", func() {
},
&createdLimitador)
return createdLimitador.Status.ServiceURL
}, timeout, interval).Should(Equal("http://limitador-service.default.svc.cluster.local:8000"))
}, timeout, interval).Should(Equal("http://" + limitadorObj.Name + ".default.svc.cluster.local:8000"))

})
})
Expand Down
15 changes: 7 additions & 8 deletions pkg/limitador/k8s_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
const (
DefaultVersion = "latest"
DefaultReplicas = 1
DefaultServiceName = "limitador"
Image = "quay.io/3scale/limitador"
StatusEndpoint = "/status"
DefaultServiceHTTPPort = 8080
Expand All @@ -26,7 +25,7 @@ func LimitadorService(limitador *limitadorv1alpha1.Limitador) *v1.Service {
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: helpers.GetValueOrDefault(*limitador.Spec.Service.Name, DefaultServiceName).(string),
Name: limitador.Name,
Namespace: limitador.ObjectMeta.Namespace, // TODO: revisit later. For now assume same.
Labels: labels(),
OwnerReferences: []metav1.OwnerReference{ownerRefToLimitador(limitador)},
Expand All @@ -36,13 +35,13 @@ func LimitadorService(limitador *limitadorv1alpha1.Limitador) *v1.Service {
{
Name: "http",
Protocol: v1.ProtocolTCP,
Port: helpers.GetValueOrDefault(*limitador.Spec.Service.Ports.HTTP, DefaultServiceHTTPPort).(int32),
Port: helpers.GetValueOrDefault(*limitador.Spec.Listener.HTTP.Port, DefaultServiceHTTPPort).(int32),
TargetPort: intstr.FromString("http"),
},
{
Name: "grpc",
Protocol: v1.ProtocolTCP,
Port: helpers.GetValueOrDefault(*limitador.Spec.Service.Ports.GRPC, DefaultServiceGRPCPort).(int32),
Port: helpers.GetValueOrDefault(*limitador.Spec.Listener.GRPC.Port, DefaultServiceGRPCPort).(int32),
TargetPort: intstr.FromString("grpc"),
},
},
Expand Down Expand Up @@ -92,12 +91,12 @@ func LimitadorDeployment(limitador *limitadorv1alpha1.Limitador) *appsv1.Deploym
Ports: []v1.ContainerPort{
{
Name: "http",
ContainerPort: helpers.GetValueOrDefault(*limitador.Spec.Service.Ports.HTTP, DefaultServiceHTTPPort).(int32),
ContainerPort: helpers.GetValueOrDefault(*limitador.Spec.Listener.HTTP.Port, DefaultServiceHTTPPort).(int32),
Protocol: v1.ProtocolTCP,
},
{
Name: "grpc",
ContainerPort: helpers.GetValueOrDefault(*limitador.Spec.Service.Ports.GRPC, DefaultServiceGRPCPort).(int32),
ContainerPort: helpers.GetValueOrDefault(*limitador.Spec.Listener.GRPC.Port, DefaultServiceGRPCPort).(int32),
Protocol: v1.ProtocolTCP,
},
},
Expand All @@ -111,7 +110,7 @@ func LimitadorDeployment(limitador *limitadorv1alpha1.Limitador) *appsv1.Deploym
Handler: v1.Handler{
HTTPGet: &v1.HTTPGetAction{
Path: StatusEndpoint,
Port: intstr.FromInt(int(helpers.GetValueOrDefault(*limitador.Spec.Service.Ports.HTTP, DefaultServiceHTTPPort).(int32))),
Port: intstr.FromInt(int(helpers.GetValueOrDefault(*limitador.Spec.Listener.HTTP.Port, DefaultServiceHTTPPort).(int32))),
Scheme: v1.URISchemeHTTP,
},
},
Expand All @@ -125,7 +124,7 @@ func LimitadorDeployment(limitador *limitadorv1alpha1.Limitador) *appsv1.Deploym
Handler: v1.Handler{
HTTPGet: &v1.HTTPGetAction{
Path: StatusEndpoint,
Port: intstr.FromInt(int(helpers.GetValueOrDefault(*limitador.Spec.Service.Ports.HTTP, DefaultServiceHTTPPort).(int32))),
Port: intstr.FromInt(int(helpers.GetValueOrDefault(*limitador.Spec.Listener.HTTP.Port, DefaultServiceHTTPPort).(int32))),
Scheme: v1.URISchemeHTTP,
},
},
Expand Down

0 comments on commit 1986fd6

Please sign in to comment.