Skip to content

Commit c329f7e

Browse files
committed
wip
1 parent 4953ad0 commit c329f7e

14 files changed

+429
-304
lines changed

api/v1alpha1/limitador_types.go

+15-68
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20-
"fmt"
2120
"reflect"
2221

2322
"github.com/go-logr/logr"
@@ -145,17 +144,6 @@ type LimitadorList struct {
145144
// +kubebuilder:validation:Enum=NONE;DRAFT_VERSION_03
146145
type RateLimitHeadersType string
147146

148-
// StorageType defines the valid options for storage
149-
// +kubebuilder:validation:Enum=memory;redis;redis_cached;disk
150-
type StorageType string
151-
152-
const (
153-
StorageTypeInMemory StorageType = "memory"
154-
StorageTypeRedis StorageType = "redis"
155-
StorageTypeRedisCached StorageType = "redis_cached"
156-
StorageTypeDisk StorageType = "disk"
157-
)
158-
159147
// Storage contains the options for Limitador counters database or in-memory data storage
160148
type Storage struct {
161149
// +optional
@@ -168,47 +156,6 @@ type Storage struct {
168156
Disk *DiskSpec `json:"disk,omitempty"`
169157
}
170158

171-
func (s *Storage) Validate() bool {
172-
return s.Redis != nil && s.Redis.ConfigSecretRef != nil ||
173-
s.RedisCached != nil && s.RedisCached.ConfigSecretRef != nil
174-
}
175-
176-
func (s *Storage) SecretRef() *corev1.ObjectReference {
177-
if s.Redis != nil {
178-
return s.Redis.ConfigSecretRef
179-
}
180-
return s.RedisCached.ConfigSecretRef
181-
}
182-
183-
func (s *Storage) Config(url string) []string {
184-
if s.Redis != nil {
185-
return []string{string(StorageTypeRedis), url}
186-
}
187-
188-
if s.RedisCached != nil {
189-
params := []string{string(StorageTypeRedisCached), url}
190-
191-
if s.RedisCached.Options != nil {
192-
options := reflect.ValueOf(*s.RedisCached.Options)
193-
typesOf := options.Type()
194-
for i := 0; i < options.NumField(); i++ {
195-
if !options.Field(i).IsNil() {
196-
var value interface{} = options.Field(i).Elem()
197-
params = append(
198-
params,
199-
fmt.Sprintf(
200-
"--%s %d",
201-
helpers.ToKebabCase(typesOf.Field(i).Name),
202-
value))
203-
}
204-
}
205-
}
206-
return params
207-
}
208-
209-
return []string{string(StorageTypeInMemory)}
210-
}
211-
212159
type Redis struct {
213160
// +ConfigSecretRef refers to the secret holding the URL for Redis.
214161
// +optional
@@ -331,6 +278,21 @@ type Ports struct {
331278
GRPC int32 `json:"grpc,omitempty"`
332279
}
333280

281+
type PodDisruptionBudgetType struct {
282+
// An eviction is allowed if at most "maxUnavailable" limitador pods
283+
// are unavailable after the eviction, i.e. even in absence of
284+
// the evicted pod. For example, one can prevent all voluntary evictions
285+
// by specifying 0. This is a mutually exclusive setting with "minAvailable".
286+
// +optional
287+
MaxUnavailable *intstr.IntOrString `json:"maxUnavailable,omitempty"`
288+
// An eviction is allowed if at least "minAvailable" limitador pods will
289+
// still be available after the eviction, i.e. even in the absence of
290+
// the evicted pod. So for example you can prevent all voluntary
291+
// evictions by specifying "100%".
292+
// +optional
293+
MinAvailable *intstr.IntOrString `json:"minAvailable,omitempty"`
294+
}
295+
334296
func (s *LimitadorStatus) Equals(other *LimitadorStatus, logger logr.Logger) bool {
335297
if s.ObservedGeneration != other.ObservedGeneration {
336298
diff := cmp.Diff(s.ObservedGeneration, other.ObservedGeneration)
@@ -359,18 +321,3 @@ func (s *LimitadorStatus) Equals(other *LimitadorStatus, logger logr.Logger) boo
359321
func init() {
360322
SchemeBuilder.Register(&Limitador{}, &LimitadorList{})
361323
}
362-
363-
type PodDisruptionBudgetType struct {
364-
// An eviction is allowed if at most "maxUnavailable" limitador pods
365-
// are unavailable after the eviction, i.e. even in absence of
366-
// the evicted pod. For example, one can prevent all voluntary evictions
367-
// by specifying 0. This is a mutually exclusive setting with "minAvailable".
368-
// +optional
369-
MaxUnavailable *intstr.IntOrString `json:"maxUnavailable,omitempty"`
370-
// An eviction is allowed if at least "minAvailable" limitador pods will
371-
// still be available after the eviction, i.e. even in the absence of
372-
// the evicted pod. So for example you can prevent all voluntary
373-
// evictions by specifying "100%".
374-
// +optional
375-
MinAvailable *intstr.IntOrString `json:"minAvailable,omitempty"`
376-
}

api/v1alpha1/limitador_types_test.go

-80
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package v1alpha1
22

33
import (
4-
"fmt"
5-
"strconv"
64
"testing"
75

86
"github.com/go-logr/logr"
@@ -94,84 +92,6 @@ func TestLimitadorLimits(t *testing.T) {
9492
})
9593
}
9694

97-
func TestStorageSecretRef(t *testing.T) {
98-
t.Run("test redis secret ref is returned if not nil", func(subT *testing.T) {
99-
var redisSecretRef = &corev1.ObjectReference{Name: "redis"}
100-
s := Storage{Redis: &Redis{ConfigSecretRef: redisSecretRef}}
101-
assert.DeepEqual(subT, s.SecretRef(), redisSecretRef)
102-
})
103-
104-
t.Run("test redis cached ref is returned if redis nil", func(subT *testing.T) {
105-
var redisCachedSecretRef = &corev1.ObjectReference{Name: "redisCached"}
106-
s := Storage{RedisCached: &RedisCached{ConfigSecretRef: redisCachedSecretRef}}
107-
assert.DeepEqual(subT, s.SecretRef(), redisCachedSecretRef)
108-
})
109-
}
110-
111-
func TestStorageValidate(t *testing.T) {
112-
t.Run("test false if redis is nil", func(subT *testing.T) {
113-
s := Storage{}
114-
assert.Equal(subT, s.Validate(), false)
115-
})
116-
117-
t.Run("test false if redis secret ref is nil", func(subT *testing.T) {
118-
s := Storage{Redis: &Redis{}}
119-
assert.Equal(subT, s.Validate(), false)
120-
})
121-
122-
t.Run("test true if redis secret ref is not nil", func(subT *testing.T) {
123-
s := Storage{Redis: &Redis{ConfigSecretRef: &corev1.ObjectReference{}}}
124-
assert.Equal(subT, s.Validate(), true)
125-
})
126-
127-
t.Run("test false if redis cached is nil", func(subT *testing.T) {
128-
s := Storage{Redis: &Redis{}}
129-
assert.Equal(subT, s.Validate(), false)
130-
})
131-
132-
t.Run("test false if redis cached secret ref is nil", func(subT *testing.T) {
133-
s := Storage{RedisCached: &RedisCached{}}
134-
assert.Equal(subT, s.Validate(), false)
135-
})
136-
137-
t.Run("test true if redis secret ref is not nil", func(subT *testing.T) {
138-
s := Storage{RedisCached: &RedisCached{ConfigSecretRef: &corev1.ObjectReference{}}}
139-
assert.Equal(subT, s.Validate(), true)
140-
})
141-
}
142-
143-
func TestStorageConfig(t *testing.T) {
144-
const url = "test"
145-
146-
t.Run("test redis storage type returned if redis is not nil", func(subT *testing.T) {
147-
s := Storage{Redis: &Redis{}}
148-
assert.DeepEqual(subT, s.Config(url), []string{string(StorageTypeRedis), url})
149-
})
150-
151-
t.Run("test redis cached storage type returned if redis cached is not nil", func(subT *testing.T) {
152-
s := Storage{RedisCached: &RedisCached{}}
153-
assert.DeepEqual(subT, s.Config(url), []string{string(StorageTypeRedisCached), url})
154-
})
155-
156-
t.Run("test redis cached storage type with options returned", func(subT *testing.T) {
157-
var option = 4040
158-
s := Storage{RedisCached: &RedisCached{Options: &RedisCachedOptions{
159-
TTL: &option,
160-
Ratio: &option,
161-
FlushPeriod: &option,
162-
MaxCached: &option,
163-
}}}
164-
assert.DeepEqual(subT, s.Config(url), []string{string(StorageTypeRedisCached), url, fmt.Sprintf("--ttl %s", strconv.Itoa(option)),
165-
fmt.Sprintf("--ratio %s", strconv.Itoa(option)), fmt.Sprintf("--flush-period %s", strconv.Itoa(option)),
166-
fmt.Sprintf("--max-cached %s", strconv.Itoa(option))})
167-
})
168-
169-
t.Run("test redis cached storage type returned if redis cached is not nil", func(subT *testing.T) {
170-
s := Storage{}
171-
assert.DeepEqual(subT, s.Config(url), []string{string(StorageTypeInMemory)})
172-
})
173-
}
174-
17595
func TestLimitadorStatusEquals(t *testing.T) {
17696
var (
17797
conditions = []metav1.Condition{

api/v1alpha1/zz_generated.deepcopy.go

+76
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)