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

Support for empty Limtador CR #30

Merged
merged 2 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions api/v1alpha1/limitador_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
DefaultServiceHTTPPort int32 = 8080
DefaultServiceGRPCPort int32 = 8081
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

Expand Down Expand Up @@ -61,6 +66,34 @@ type Limitador struct {
Status LimitadorStatus `json:"status,omitempty"`
}

func (l *Limitador) GRPCPort() int32 {
Copy link
Member

Choose a reason for hiding this comment

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

I see, so instead of defining default values in the "code" we'll get them defined in "types"

if l.Spec.Listener == nil ||
l.Spec.Listener.GRPC == nil ||
l.Spec.Listener.GRPC.Port == nil {
return DefaultServiceGRPCPort
}

return *l.Spec.Listener.GRPC.Port
}

func (l *Limitador) HTTPPort() int32 {
if l.Spec.Listener == nil ||
l.Spec.Listener.HTTP == nil ||
l.Spec.Listener.HTTP.Port == nil {
return DefaultServiceHTTPPort
}

return *l.Spec.Listener.HTTP.Port
}

func (l *Limitador) Limits() []RateLimit {
if l.Spec.Limits == nil {
return make([]RateLimit, 0)
}

return l.Spec.Limits
}

//+kubebuilder:object:root=true

// LimitadorList contains a list of Limitador
Expand All @@ -72,9 +105,9 @@ type LimitadorList struct {

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

type TransportProtocol struct {
Expand Down
12 changes: 10 additions & 2 deletions api/v1alpha1/zz_generated.deepcopy.go

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

6 changes: 3 additions & 3 deletions controllers/limitador_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package controllers
import (
"context"
"fmt"
"github.com/kuadrant/limitador-operator/pkg/helpers"
v1 "k8s.io/api/core/v1"
"strconv"

v1 "k8s.io/api/core/v1"

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -126,7 +126,7 @@ func buildServiceUrl(limitadorObj *limitadorv1alpha1.Limitador) string {
return "http://" +
limitadorObj.Name + "." +
limitadorObj.Namespace + ".svc.cluster.local:" +
strconv.Itoa(int(helpers.GetValueOrDefault(*limitadorObj.Spec.Listener.HTTP.Port, limitador.DefaultServiceHTTPPort).(int32)))
strconv.Itoa(int(limitadorObj.HTTPPort()))
}

func mutateLimitsConfigMap(existingObj, desiredObj client.Object) (bool, error) {
Expand Down
39 changes: 36 additions & 3 deletions controllers/limitador_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package controllers

import (
"context"
"github.com/kuadrant/limitador-operator/pkg/limitador"
"time"

"github.com/kuadrant/limitador-operator/pkg/limitador"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -36,8 +37,8 @@ var _ = Describe("Limitador controller", func() {

replicas := LimitadorReplicas
version := LimitadorVersion
httpPort := limitadorv1alpha1.TransportProtocol{Port: &httpPortNumber}
grpcPort := limitadorv1alpha1.TransportProtocol{Port: &grpcPortNumber}
httpPort := &limitadorv1alpha1.TransportProtocol{Port: &httpPortNumber}
grpcPort := &limitadorv1alpha1.TransportProtocol{Port: &grpcPortNumber}

limits := []limitadorv1alpha1.RateLimit{
{
Expand Down Expand Up @@ -83,6 +84,38 @@ var _ = Describe("Limitador controller", func() {

deletePropagationPolicy := client.PropagationPolicy(metav1.DeletePropagationForeground)

Context("Creating a new empty Limitador object", func() {
var limitadorObj *limitadorv1alpha1.Limitador

BeforeEach(func() {
limitadorObj = newLimitador()
limitadorObj.Spec = limitadorv1alpha1.LimitadorSpec{}
err := k8sClient.Delete(context.TODO(), limitadorObj, deletePropagationPolicy)
Expect(err == nil || errors.IsNotFound(err))

Expect(k8sClient.Create(context.TODO(), limitadorObj)).Should(Succeed())
})

It("Should create a Limitador service with default ports", func() {
createdLimitadorService := v1.Service{}
Eventually(func() bool {
err := k8sClient.Get(
context.TODO(),
types.NamespacedName{
Namespace: LimitadorNamespace,
Name: limitadorObj.Name,
},
&createdLimitadorService)
return err == nil
}, timeout, interval).Should(BeTrue())
Expect(len(createdLimitadorService.Spec.Ports)).Should(Equal(2))
Expect(createdLimitadorService.Spec.Ports[0].Name).Should(Equal("http"))
Expect(createdLimitadorService.Spec.Ports[0].Port).Should(Equal(limitadorv1alpha1.DefaultServiceHTTPPort))
Expect(createdLimitadorService.Spec.Ports[1].Name).Should(Equal("grpc"))
Expect(createdLimitadorService.Spec.Ports[1].Port).Should(Equal(limitadorv1alpha1.DefaultServiceGRPCPort))
})
})

Context("Creating a new Limitador object", func() {
var limitadorObj *limitadorv1alpha1.Limitador

Expand Down
7 changes: 0 additions & 7 deletions pkg/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,3 @@ func FetchEnv(key string, def string) string {

return val
}

func GetValueOrDefault(expectedValue interface{}, defaultValue interface{}) interface{} {
if expectedValue != nil {
return expectedValue
}
return defaultValue
}
18 changes: 8 additions & 10 deletions pkg/limitador/k8s_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package limitador
import (
"crypto/md5"
"fmt"

limitadorv1alpha1 "github.com/kuadrant/limitador-operator/api/v1alpha1"
"github.com/kuadrant/limitador-operator/pkg/helpers"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -17,8 +17,6 @@ const (
DefaultReplicas = 1
Image = "quay.io/3scale/limitador"
StatusEndpoint = "/status"
DefaultServiceHTTPPort = 8080
DefaultServiceGRPCPort = 8081
LimitadorConfigFileName = "limitador-config.yaml"
LimitadorCMHash = "hash"
LimitsCMNamePrefix = "limits-config-"
Expand All @@ -43,13 +41,13 @@ func LimitadorService(limitador *limitadorv1alpha1.Limitador) *v1.Service {
{
Name: "http",
Protocol: v1.ProtocolTCP,
Port: helpers.GetValueOrDefault(*limitador.Spec.Listener.HTTP.Port, DefaultServiceHTTPPort).(int32),
Port: limitador.HTTPPort(),
TargetPort: intstr.FromString("http"),
},
{
Name: "grpc",
Protocol: v1.ProtocolTCP,
Port: helpers.GetValueOrDefault(*limitador.Spec.Listener.GRPC.Port, DefaultServiceGRPCPort).(int32),
Port: limitador.GRPCPort(),
TargetPort: intstr.FromString("grpc"),
},
},
Expand Down Expand Up @@ -99,12 +97,12 @@ func LimitadorDeployment(limitador *limitadorv1alpha1.Limitador) *appsv1.Deploym
Ports: []v1.ContainerPort{
{
Name: "http",
ContainerPort: helpers.GetValueOrDefault(*limitador.Spec.Listener.HTTP.Port, DefaultServiceHTTPPort).(int32),
ContainerPort: limitador.HTTPPort(),
Protocol: v1.ProtocolTCP,
},
{
Name: "grpc",
ContainerPort: helpers.GetValueOrDefault(*limitador.Spec.Listener.GRPC.Port, DefaultServiceGRPCPort).(int32),
ContainerPort: limitador.GRPCPort(),
Protocol: v1.ProtocolTCP,
},
},
Expand All @@ -122,7 +120,7 @@ func LimitadorDeployment(limitador *limitadorv1alpha1.Limitador) *appsv1.Deploym
Handler: v1.Handler{
HTTPGet: &v1.HTTPGetAction{
Path: StatusEndpoint,
Port: intstr.FromInt(int(helpers.GetValueOrDefault(*limitador.Spec.Listener.HTTP.Port, DefaultServiceHTTPPort).(int32))),
Port: intstr.FromInt(int(limitador.HTTPPort())),
Scheme: v1.URISchemeHTTP,
},
},
Expand All @@ -136,7 +134,7 @@ func LimitadorDeployment(limitador *limitadorv1alpha1.Limitador) *appsv1.Deploym
Handler: v1.Handler{
HTTPGet: &v1.HTTPGetAction{
Path: StatusEndpoint,
Port: intstr.FromInt(int(helpers.GetValueOrDefault(*limitador.Spec.Listener.HTTP.Port, DefaultServiceHTTPPort).(int32))),
Port: intstr.FromInt(int(limitador.HTTPPort())),
Scheme: v1.URISchemeHTTP,
},
},
Expand Down Expand Up @@ -174,7 +172,7 @@ func LimitadorDeployment(limitador *limitadorv1alpha1.Limitador) *appsv1.Deploym
}

func LimitsConfigMap(limitador *limitadorv1alpha1.Limitador) (*v1.ConfigMap, error) {
limitsMarshalled, marshallErr := yaml.Marshal(limitador.Spec.Limits)
limitsMarshalled, marshallErr := yaml.Marshal(limitador.Limits())
if marshallErr != nil {
return nil, marshallErr
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/limitador/k8s_objects_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package limitador

import (
"gotest.tools/assert"
"testing"

"gotest.tools/assert"
)

func TestConstants(t *testing.T) {
assert.Check(t, "latest" == DefaultVersion)
assert.Check(t, 1 == DefaultReplicas)
assert.Check(t, "quay.io/3scale/limitador" == Image)
assert.Check(t, "/status" == StatusEndpoint)
assert.Check(t, 8080 == DefaultServiceHTTPPort)
assert.Check(t, 8081 == DefaultServiceGRPCPort)
assert.Check(t, "limitador-config.yaml" == LimitadorConfigFileName)
assert.Check(t, "hash" == LimitadorCMHash)
assert.Check(t, "limits-config-" == LimitsCMNamePrefix)
Expand Down