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

Verbosity level #119

Merged
merged 2 commits into from
Dec 1, 2023
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
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
22 changes: 22 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,27 @@ 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 = &[]limitadorv1alpha1.VerbosityLevel{tt.VerbosityLevel}[0]
command := DeploymentCommand(limObj, DeploymentStorageOptions{})
assert.Assert(subTest, is.Contains(command, tt.ExpectedArg))
})
}
})
}

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