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

redis cached: response timeout #128

Merged
merged 2 commits into from
Apr 5, 2024
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
12 changes: 2 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec
Expand Down Expand Up @@ -93,9 +92,6 @@ IMG ?= $(DEFAULT_IMG)
DEFAULT_REPLACES_VERSION = 0.0.0-alpha
REPLACES_VERSION ?= $(DEFAULT_REPLACES_VERSION)

# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.22

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
Expand Down Expand Up @@ -210,9 +206,9 @@ clean-cov: ## Remove coverage reports
.PHONY: test
test: test-unit test-integration ## Run all tests

test-integration: clean-cov generate fmt vet envtest ## Run Integration tests.
test-integration: clean-cov generate fmt vet ## Run Integration tests.
mkdir -p coverage/integration
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) $(ARCH_PARAM) use $(ENVTEST_K8S_VERSION) -p path)" go test ./controllers... -coverprofile $(PROJECT_PATH)/coverage/integration/cover.out -ginkgo.v -v -timeout 0
go test ./controllers... -coverprofile $(PROJECT_PATH)/coverage/integration/cover.out -ginkgo.v -v -timeout 0

ifdef TEST_NAME
test-unit: TEST_PATTERN := --run $(TEST_NAME)
Expand Down Expand Up @@ -266,10 +262,6 @@ install-olm: $(OPERATOR_SDK)
uninstall-olm:
$(OPERATOR_SDK) olm uninstall

ENVTEST = $(shell pwd)/bin/setup-envtest
envtest: ## Download envtest-setup locally if necessary.
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)

# go-install-tool will 'go install' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-install-tool
Expand Down
4 changes: 4 additions & 0 deletions api/v1alpha1/limitador_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ type RedisCachedOptions struct {
// +optional
// MaxCached refers to the maximum amount of counters cached [default: 10000]
MaxCached *int `json:"max-cached,omitempty"`

// +optional
// ResponseTimeout defines the timeout for Redis commands in milliseconds [default: 350]
ResponseTimeout *int `json:"response-timeout,omitempty"`
}

type RedisCached struct {
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: "2024-03-05T09:17:17Z"
createdAt: "2024-04-04T14:34:19Z"
operators.operatorframework.io/builder: operator-sdk-v1.32.0
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
repository: https://github.com/Kuadrant/limitador-operator
Expand Down
4 changes: 4 additions & 0 deletions bundle/manifests/limitador.kuadrant.io_limitadors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,10 @@ spec:
description: 'Ratio to apply to the TTL from Redis on
cached counters [default: 10]'
type: integer
response-timeout:
description: 'ResponseTimeout defines the timeout for
Redis commands in milliseconds [default: 350]'
type: integer
ttl:
description: 'TTL for cached counters in milliseconds
[default: 5000]'
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/limitador.kuadrant.io_limitadors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,10 @@ spec:
description: 'Ratio to apply to the TTL from Redis on
cached counters [default: 10]'
type: integer
response-timeout:
description: 'ResponseTimeout defines the timeout for
Redis commands in milliseconds [default: 350]'
type: integer
ttl:
description: 'TTL for cached counters in milliseconds
[default: 5000]'
Expand Down
52 changes: 45 additions & 7 deletions controllers/limitador_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"

Expand Down Expand Up @@ -632,8 +633,50 @@ var _ = Describe("Limitador controller", func() {
}, timeout, interval).Should(BeTrue())
})

It("command line is correct", func() {
It("with all defaults, the command line is correct", func() {
limitadorObj := limitadorWithRedisCachedStorage(client.ObjectKeyFromObject(redisSecret), testNamespace)
limitadorObj.Spec.Storage.RedisCached.Options = nil
Expect(k8sClient.Create(context.TODO(), limitadorObj)).Should(Succeed())
Eventually(testLimitadorIsReady(limitadorObj), time.Minute, 5*time.Second).Should(BeTrue())

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

return err == nil
}, timeout, interval).Should(BeTrue())

Expect(deploymentObj.Spec.Template.Spec.Containers).To(HaveLen(1))
Expect(deploymentObj.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",
"redis_cached",
"$(LIMITADOR_OPERATOR_REDIS_URL)",
),
)
})

It("with all the optional parameters, the command line is correct", func() {
limitadorObj := limitadorWithRedisCachedStorage(client.ObjectKeyFromObject(redisSecret), testNamespace)
limitadorObj.Spec.Storage.RedisCached.Options = &limitadorv1alpha1.RedisCachedOptions{
TTL: ptr.To(1),
Ratio: ptr.To(2),
FlushPeriod: ptr.To(3),
MaxCached: ptr.To(4),
ResponseTimeout: ptr.To(5),
}

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

Expand Down Expand Up @@ -665,6 +708,7 @@ var _ = Describe("Limitador controller", func() {
"--ratio", "2",
"--flush-period", "3",
"--max-cached", "4",
"--response-timeout", "5",
),
)
})
Expand Down Expand Up @@ -791,12 +835,6 @@ func limitadorWithRedisCachedStorage(key client.ObjectKey, ns string) *limitador
ConfigSecretRef: &v1.LocalObjectReference{
Name: key.Name,
},
Options: &limitadorv1alpha1.RedisCachedOptions{
TTL: &[]int{1}[0],
Ratio: &[]int{2}[0],
FlushPeriod: &[]int{3}[0],
MaxCached: &[]int{4}[0],
},
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
Expand Down Expand Up @@ -55,7 +56,7 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,
UseExistingCluster: &[]bool{true}[0],
UseExistingCluster: ptr.To(true),
}

cfg, err := testEnv.Start()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
k8s.io/apimachinery v0.28.3
k8s.io/client-go v0.28.3
k8s.io/klog/v2 v2.100.1
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
k8s.io/utils v0.0.0-20240310230437-4693a0247e57
sigs.k8s.io/controller-runtime v0.16.3
sigs.k8s.io/yaml v1.3.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5OhxCKlKJy0sHc+PcDwFB24dQ=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9/go.mod h1:wZK2AVp1uHCp4VamDVgBP2COHZjqD1T68Rf0CM3YjSM=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20240310230437-4693a0247e57 h1:gbqbevonBh57eILzModw6mrkbwM0gQBEuevE/AaBsHY=
k8s.io/utils v0.0.0-20240310230437-4693a0247e57/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/controller-runtime v0.16.3 h1:2TuvuokmfXvDUamSx1SuAOO3eTyye+47mJCigwG62c4=
sigs.k8s.io/controller-runtime v0.16.3/go.mod h1:j7bialYoSn142nv9sCOJmQgDXQXxnroFU4VnX/brVJ0=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
Expand Down
3 changes: 3 additions & 0 deletions pkg/limitador/redis_cache_storage_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func RedisCachedDeploymentOptions(ctx context.Context, cl client.Client, defSecr
if redisCachedObj.Options.MaxCached != nil {
command = append(command, "--max-cached", strconv.Itoa(*redisCachedObj.Options.MaxCached))
}
if redisCachedObj.Options.ResponseTimeout != nil {
command = append(command, "--response-timeout", strconv.Itoa(*redisCachedObj.Options.ResponseTimeout))
}
}

return DeploymentStorageOptions{
Expand Down
11 changes: 7 additions & 4 deletions pkg/limitador/redis_cache_storage_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

Expand Down Expand Up @@ -104,10 +105,11 @@ func TestRedisCachedDeploymentOptions(t *testing.T) {
redisObj := limitadorv1alpha1.RedisCached{
ConfigSecretRef: &v1.LocalObjectReference{Name: "redisSecret"},
Options: &limitadorv1alpha1.RedisCachedOptions{
TTL: &[]int{1}[0],
Ratio: &[]int{2}[0],
FlushPeriod: &[]int{3}[0],
MaxCached: &[]int{4}[0],
TTL: ptr.To(1),
Ratio: ptr.To(2),
FlushPeriod: ptr.To(3),
MaxCached: ptr.To(4),
ResponseTimeout: ptr.To(5),
},
}
options, err := RedisCachedDeploymentOptions(ctx, cl, namespace, redisObj)
Expand All @@ -121,6 +123,7 @@ func TestRedisCachedDeploymentOptions(t *testing.T) {
"--ratio", "2",
"--flush-period", "3",
"--max-cached", "4",
"--response-timeout", "5",
},
},
)
Expand Down
Loading