Skip to content

Commit

Permalink
verbosity level
Browse files Browse the repository at this point in the history
Signed-off-by: Eguzki Astiz Lezaun <[email protected]>
  • Loading branch information
eguzki committed Nov 21, 2023
1 parent f6a01e8 commit e7b65ab
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 1 deletion.
8 changes: 8 additions & 0 deletions api/v1alpha1/limitador_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ var (
}
)

// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Maximum=4
type VerbosityLevel int

// 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 @@ -87,6 +91,10 @@ type LimitadorSpec struct {

// +optional
ResourceRequirements *corev1.ResourceRequirements `json:"resourceRequirements,omitempty"`

// Sets the level of verbosity
// +optional
Verbosity *VerbosityLevel `json:"verbosity,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
5 changes: 5 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.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ metadata:
capabilities: Basic Install
categories: Integration & Delivery
containerImage: quay.io/kuadrant/limitador-operator:latest
createdAt: "2023-11-21T15:45:03Z"
createdAt: "2023-11-21T22:28:31Z"
operators.operatorframework.io/builder: operator-sdk-v1.28.1
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
repository: https://github.com/Kuadrant/limitador-operator
Expand Down
5 changes: 5 additions & 0 deletions bundle/manifests/limitador.kuadrant.io_limitadors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,11 @@ spec:
- basic
- exhaustive
type: string
verbosity:
description: Sets the level of verbosity
maximum: 4
minimum: 1
type: integer
version:
type: string
type: object
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/limitador.kuadrant.io_limitadors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,11 @@ spec:
- basic
- exhaustive
type: string
verbosity:
description: Sets the level of verbosity
maximum: 4
minimum: 1
type: integer
version:
type: string
type: object
Expand Down
126 changes: 126 additions & 0 deletions controllers/limitador_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,132 @@ var _ = Describe("Limitador controller", func() {
})
})

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

BeforeEach(func() {
limitadorObj = basicLimitador(testNamespace)
limitadorObj.Spec.Verbosity = &[]limitadorv1alpha1.VerbosityLevel{3}[0]

Expect(k8sClient.Create(context.TODO(), limitadorObj)).Should(Succeed())
Eventually(testLimitadorIsReady(limitadorObj), time.Minute, 5*time.Second).Should(BeTrue())
})

It("Should create a new deployment with verbosity level command line arg", func() {
deployment := &appsv1.Deployment{}
Eventually(func() bool {
err := k8sClient.Get(context.TODO(),
types.NamespacedName{
Namespace: testNamespace,
Name: limitador.DeploymentName(limitadorObj),
}, deployment)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(deployment.Spec.Template.Spec.Containers[0].Command).To(
HaveExactElements(
"limitador-server",
"-vvv",
"--http-port",
strconv.Itoa(int(limitadorv1alpha1.DefaultServiceHTTPPort)),
"--rls-port",
strconv.Itoa(int(limitadorv1alpha1.DefaultServiceGRPCPort)),
"/home/limitador/etc/limitador-config.yaml",
"memory",
),
)
})
})

Context("Creating a new Limitador object with too high verbosity level", func() {
var limitadorObj *limitadorv1alpha1.Limitador

It("Should be rejected by k8s", func() {
limitadorObj = basicLimitador(testNamespace)
limitadorObj.Spec.Verbosity = &[]limitadorv1alpha1.VerbosityLevel{6}[0]

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

Context("Reconciling command line args for verbosity", func() {
var limitadorObj *limitadorv1alpha1.Limitador

BeforeEach(func() {
limitadorObj = basicLimitador(testNamespace)

Expect(k8sClient.Create(context.TODO(), limitadorObj)).Should(Succeed())
Eventually(testLimitadorIsReady(limitadorObj), time.Minute, 5*time.Second).Should(BeTrue())
})

It("Should modify the limitador deployment command line args", func() {
deployment := &appsv1.Deployment{}
Eventually(func() bool {
err := k8sClient.Get(context.TODO(),
types.NamespacedName{
Namespace: testNamespace,
Name: limitador.DeploymentName(limitadorObj),
}, deployment)
return err == nil
}, timeout, interval).Should(BeTrue())

// verbosity level command line arg should be missing
Expect(deployment.Spec.Template.Spec.Containers[0].Command).To(
HaveExactElements(
"limitador-server",
"--http-port",
strconv.Itoa(int(limitadorv1alpha1.DefaultServiceHTTPPort)),
"--rls-port",
strconv.Itoa(int(limitadorv1alpha1.DefaultServiceGRPCPort)),
"/home/limitador/etc/limitador-config.yaml",
"memory",
),
)

// Let's add verbosity level
updatedLimitador := limitadorv1alpha1.Limitador{}
Eventually(func() bool {
err := k8sClient.Get(context.TODO(),
types.NamespacedName{
Namespace: testNamespace,
Name: limitadorObj.Name,
}, &updatedLimitador)

if err != nil {
return false
}

updatedLimitador.Spec.Verbosity = &[]limitadorv1alpha1.VerbosityLevel{3}[0]
return k8sClient.Update(context.TODO(), &updatedLimitador) == nil
}, timeout, interval).Should(BeTrue())

Eventually(func() bool {
newDeployment := &appsv1.Deployment{}
err := k8sClient.Get(context.TODO(),
types.NamespacedName{
Namespace: testNamespace,
Name: limitador.DeploymentName(limitadorObj),
}, newDeployment)

if err != nil {
return false
}

return reflect.DeepEqual(newDeployment.Spec.Template.Spec.Containers[0].Command,
[]string{
"limitador-server",
"-vvv",
"--http-port",
strconv.Itoa(int(limitadorv1alpha1.DefaultServiceHTTPPort)),
"--rls-port",
strconv.Itoa(int(limitadorv1alpha1.DefaultServiceGRPCPort)),
"/home/limitador/etc/limitador-config.yaml",
"memory",
})
}, timeout, interval).Should(BeTrue())
})
})

Context("Modifying limitador deployment objects", func() {
var limitadorObj *limitadorv1alpha1.Limitador

Expand Down
6 changes: 6 additions & 0 deletions pkg/limitador/deployment_options.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package limitador

import (
"fmt"
"path/filepath"
"strconv"
"strings"

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -43,6 +45,10 @@ func DeploymentCommand(limObj *limitadorv1alpha1.Limitador, storageOptions Deplo
command = append(command, "--limit-name-in-labels")
}

if limObj.Spec.Verbosity != nil {
command = append(command, fmt.Sprintf("-%s", strings.Repeat("v", int(*limObj.Spec.Verbosity))))
}

// let's set explicitly the HTTP port,
// as it is being set in the readiness and liveness probe and in the service
command = append(command, "--http-port", strconv.Itoa(int(limObj.HTTPPort())))
Expand Down
23 changes: 23 additions & 0 deletions pkg/limitador/deployment_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"gotest.tools/assert"
is "gotest.tools/assert/cmp"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -84,6 +85,28 @@ func TestDeploymentCommand(t *testing.T) {
"a", "b", "c",
})
})

t.Run("when verbosity is set in the spec command line args includes -v*", func(subT *testing.T) {
tests := []struct {
Name string
VerbosityLevel limitadorv1alpha1.VerbosityLevel
ExpectedArg string
}{
{"log level 1", 1, "-v"},
{"log level 2", 2, "-vv"},
{"log level 3", 3, "-vvv"},
{"log level 4", 4, "-vvvv"},
}
for _, tt := range tests {
subT.Run(tt.Name, func(subTest *testing.T) {
limObj := basicLimitador()
limObj.Spec.Verbosity = &tt.VerbosityLevel

Check failure on line 103 in pkg/limitador/deployment_options_test.go

View workflow job for this annotation

GitHub Actions / Lint

G601: Implicit memory aliasing in for loop. (gosec)
command := DeploymentCommand(limObj, DeploymentStorageOptions{})
assert.Assert(subTest, is.Contains(command, tt.ExpectedArg))
})
}
})

}

func TestDeploymentVolumeMounts(t *testing.T) {
Expand Down

0 comments on commit e7b65ab

Please sign in to comment.