diff --git a/api/v1alpha1/limitador_types.go b/api/v1alpha1/limitador_types.go index 29f4268b..b1a9bd3c 100644 --- a/api/v1alpha1/limitador_types.go +++ b/api/v1alpha1/limitador_types.go @@ -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 @@ -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() { diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 40ad02a4..df2e2b75 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -97,7 +97,7 @@ func (in *LimitadorSpec) DeepCopyInto(out *LimitadorSpec) { *out = new(string) **out = **in } - in.Service.DeepCopyInto(&out.Service) + in.Listener.DeepCopyInto(&out.Listener) } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new LimitadorSpec. @@ -126,41 +126,33 @@ func (in *LimitadorStatus) DeepCopy() *LimitadorStatus { } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *NamespacedName) DeepCopyInto(out *NamespacedName) { +func (in *Listener) DeepCopyInto(out *Listener) { *out = *in + in.HTTP.DeepCopyInto(&out.HTTP) + in.GRPC.DeepCopyInto(&out.GRPC) } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NamespacedName. -func (in *NamespacedName) DeepCopy() *NamespacedName { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Listener. +func (in *Listener) DeepCopy() *Listener { if in == nil { return nil } - out := new(NamespacedName) + out := new(Listener) in.DeepCopyInto(out) return out } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Ports) DeepCopyInto(out *Ports) { +func (in *NamespacedName) DeepCopyInto(out *NamespacedName) { *out = *in - if in.GRPC != nil { - in, out := &in.GRPC, &out.GRPC - *out = new(int32) - **out = **in - } - if in.HTTP != nil { - in, out := &in.HTTP, &out.HTTP - *out = new(int32) - **out = **in - } } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Ports. -func (in *Ports) DeepCopy() *Ports { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NamespacedName. +func (in *NamespacedName) DeepCopy() *NamespacedName { if in == nil { return nil } - out := new(Ports) + out := new(NamespacedName) in.DeepCopyInto(out) return out } @@ -266,22 +258,21 @@ func (in *RateLimitStatus) DeepCopy() *RateLimitStatus { } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Service) DeepCopyInto(out *Service) { +func (in *TransportProtocol) DeepCopyInto(out *TransportProtocol) { *out = *in - if in.Name != nil { - in, out := &in.Name, &out.Name - *out = new(string) + if in.Port != nil { + in, out := &in.Port, &out.Port + *out = new(int32) **out = **in } - in.Ports.DeepCopyInto(&out.Ports) } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Service. -func (in *Service) DeepCopy() *Service { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TransportProtocol. +func (in *TransportProtocol) DeepCopy() *TransportProtocol { if in == nil { return nil } - out := new(Service) + out := new(TransportProtocol) in.DeepCopyInto(out) return out } diff --git a/bundle/manifests/limitador.kuadrant.io_limitadors.yaml b/bundle/manifests/limitador.kuadrant.io_limitadors.yaml index 8baac9b9..ea9bf35c 100644 --- a/bundle/manifests/limitador.kuadrant.io_limitadors.yaml +++ b/bundle/manifests/limitador.kuadrant.io_limitadors.yaml @@ -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 diff --git a/config/crd/bases/limitador.kuadrant.io_limitadors.yaml b/config/crd/bases/limitador.kuadrant.io_limitadors.yaml index 5a1bbf9c..33cd02fb 100644 --- a/config/crd/bases/limitador.kuadrant.io_limitadors.yaml +++ b/config/crd/bases/limitador.kuadrant.io_limitadors.yaml @@ -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 diff --git a/config/deploy/manfiests.yaml b/config/deploy/manfiests.yaml index e16eeebc..d810ab8d 100644 --- a/config/deploy/manfiests.yaml +++ b/config/deploy/manfiests.yaml @@ -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 diff --git a/config/install/manifests.yaml b/config/install/manifests.yaml index 008c9c8b..87f624f3 100644 --- a/config/install/manifests.yaml +++ b/config/install/manifests.yaml @@ -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 diff --git a/controllers/limitador_controller.go b/controllers/limitador_controller.go index 99f8dcd3..83a9236c 100644 --- a/controllers/limitador_controller.go +++ b/controllers/limitador_controller.go @@ -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) { diff --git a/controllers/limitador_controller_test.go b/controllers/limitador_controller_test.go index 9fdd48b0..6e687eab 100644 --- a/controllers/limitador_controller_test.go +++ b/controllers/limitador_controller_test.go @@ -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()) @@ -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, }, }, } @@ -107,7 +104,7 @@ var _ = Describe("Limitador controller", func() { context.TODO(), types.NamespacedName{ Namespace: LimitadorNamespace, - Name: LimitadorServiceName, + Name: limitadorObj.Name, }, &createdLimitadorService) return err == nil @@ -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")) }) }) diff --git a/pkg/limitador/k8s_objects.go b/pkg/limitador/k8s_objects.go index eb5907c6..02107c72 100644 --- a/pkg/limitador/k8s_objects.go +++ b/pkg/limitador/k8s_objects.go @@ -12,7 +12,6 @@ import ( const ( DefaultVersion = "latest" DefaultReplicas = 1 - DefaultServiceName = "limitador" Image = "quay.io/3scale/limitador" StatusEndpoint = "/status" DefaultServiceHTTPPort = 8080 @@ -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)}, @@ -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"), }, }, @@ -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, }, }, @@ -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, }, }, @@ -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, }, },