-
Notifications
You must be signed in to change notification settings - Fork 14
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
First version of the RateLimit reconciler #6
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5684cdd
Add RateLimit API
davidor e7bb20f
config/samples: add example ratelimit
davidor 8f051cf
limitador/k8s_objects: extract ports to constants
davidor 1ad2ad6
go.mod: add testify
davidor ed0387d
limitador: add client with create and delete
davidor d6d46ad
controllers: extract limitador controller tests
davidor 2da7e7e
controllers: add first version of rate limit controller
davidor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
limitadorv1alpha1 "github.com/3scale/limitador-operator/api/v1alpha1" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
appsv1 "k8s.io/api/apps/v1" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"time" | ||
) | ||
|
||
var _ = Describe("Limitador controller", func() { | ||
const ( | ||
LimitadorName = "limitador-test" | ||
LimitadorNamespace = "default" | ||
LimitadorReplicas = 2 | ||
LimitadorImage = "quay.io/3scale/limitador" | ||
LimitadorVersion = "0.3.0" | ||
|
||
timeout = time.Second * 10 | ||
interval = time.Millisecond * 250 | ||
) | ||
|
||
replicas := LimitadorReplicas | ||
version := LimitadorVersion | ||
limitador := limitadorv1alpha1.Limitador{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Limitador", | ||
APIVersion: "limitador.3scale.net/v1alpha1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: LimitadorName, | ||
Namespace: LimitadorNamespace, | ||
}, | ||
Spec: limitadorv1alpha1.LimitadorSpec{ | ||
Replicas: &replicas, | ||
Version: &version, | ||
}, | ||
} | ||
|
||
Context("Creating a new Limitador object", func() { | ||
BeforeEach(func() { | ||
err := k8sClient.Delete(context.TODO(), limitador.DeepCopy()) | ||
Expect(err == nil || errors.IsNotFound(err)) | ||
|
||
Expect(k8sClient.Create(context.TODO(), limitador.DeepCopy())).Should(Succeed()) | ||
}) | ||
|
||
It("Should create a new deployment with the right number of replicas and version", func() { | ||
createdLimitadorDeployment := appsv1.Deployment{} | ||
Eventually(func() bool { | ||
err := k8sClient.Get( | ||
context.TODO(), | ||
types.NamespacedName{ | ||
Namespace: LimitadorNamespace, | ||
Name: LimitadorName, | ||
}, | ||
&createdLimitadorDeployment) | ||
|
||
return err == nil | ||
}, timeout, interval).Should(BeTrue()) | ||
|
||
Expect(*createdLimitadorDeployment.Spec.Replicas).Should( | ||
Equal((int32)(LimitadorReplicas)), | ||
) | ||
Expect(createdLimitadorDeployment.Spec.Template.Spec.Containers[0].Image).Should( | ||
Equal(LimitadorImage + ":" + LimitadorVersion), | ||
) | ||
}) | ||
|
||
It("Should create a Limitador service", func() { | ||
createdLimitadorService := v1.Service{} | ||
Eventually(func() bool { | ||
err := k8sClient.Get( | ||
context.TODO(), | ||
types.NamespacedName{ | ||
Namespace: "default", // Hardcoded for now | ||
Name: "limitador", // Hardcoded for now | ||
}, | ||
&createdLimitadorService) | ||
|
||
return err == nil | ||
}, timeout, interval).Should(BeTrue()) | ||
}) | ||
}) | ||
|
||
Context("Deleting a Limitador object", func() { | ||
BeforeEach(func() { | ||
err := k8sClient.Create(context.TODO(), limitador.DeepCopy()) | ||
Expect(err == nil || errors.IsAlreadyExists(err)) | ||
|
||
Expect(k8sClient.Delete(context.TODO(), limitador.DeepCopy())).Should(Succeed()) | ||
}) | ||
|
||
It("Should delete the limitador deployment", func() { | ||
createdLimitadorDeployment := appsv1.Deployment{} | ||
Eventually(func() bool { | ||
err := k8sClient.Get( | ||
context.TODO(), | ||
types.NamespacedName{ | ||
Namespace: LimitadorNamespace, | ||
Name: LimitadorName, | ||
}, | ||
&createdLimitadorDeployment) | ||
|
||
return errors.IsNotFound(err) | ||
}, timeout, interval).Should(BeTrue()) | ||
}) | ||
|
||
It("Should delete the limitador service", func() { | ||
createdLimitadorService := v1.Service{} | ||
Eventually(func() bool { | ||
err := k8sClient.Get( | ||
context.TODO(), | ||
types.NamespacedName{ | ||
Namespace: "default", // Hardcoded for now | ||
Name: "limitador", // Hardcoded for now | ||
}, | ||
&createdLimitadorService) | ||
|
||
return errors.IsNotFound(err) | ||
}, timeout, interval).Should(BeTrue()) | ||
}) | ||
}) | ||
|
||
Context("Updating a limitador object", func() { | ||
BeforeEach(func() { | ||
err := k8sClient.Delete(context.TODO(), limitador.DeepCopy()) | ||
Expect(err == nil || errors.IsNotFound(err)) | ||
|
||
Expect(k8sClient.Create(context.TODO(), limitador.DeepCopy())).Should(Succeed()) | ||
}) | ||
|
||
It("Should modify the limitador deployment", func() { | ||
updatedLimitador := limitadorv1alpha1.Limitador{} | ||
Eventually(func() bool { | ||
err := k8sClient.Get( | ||
context.TODO(), | ||
types.NamespacedName{ | ||
Namespace: LimitadorNamespace, | ||
Name: LimitadorName, | ||
}, | ||
&updatedLimitador) | ||
|
||
return err == nil | ||
}, timeout, interval).Should(BeTrue()) | ||
|
||
replicas = LimitadorReplicas + 1 | ||
updatedLimitador.Spec.Replicas = &replicas | ||
version = "latest" | ||
updatedLimitador.Spec.Version = &version | ||
|
||
Expect(k8sClient.Update(context.TODO(), &updatedLimitador)).Should(Succeed()) | ||
updatedLimitadorDeployment := appsv1.Deployment{} | ||
Eventually(func() bool { | ||
err := k8sClient.Get( | ||
context.TODO(), | ||
types.NamespacedName{ | ||
Namespace: LimitadorNamespace, | ||
Name: LimitadorName, | ||
}, | ||
&updatedLimitadorDeployment) | ||
|
||
if err != nil { | ||
return false | ||
} | ||
|
||
correctReplicas := *updatedLimitadorDeployment.Spec.Replicas == LimitadorReplicas+1 | ||
correctImage := updatedLimitadorDeployment.Spec.Template.Spec.Containers[0].Image == LimitadorImage+":latest" | ||
|
||
return correctReplicas && correctImage | ||
}, timeout, interval).Should(BeTrue()) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need
Namespace
? Is this is intended to "bind" this rate limit to some Limitador service in another namespace? In that case, I suggest having URL.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All k8s objects have a meta property
namespace
you can use to bind one rate limit object to the limitador service of the same namespace. JFYI.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The naming is confusing, sorry. A limitador namespace doesn't need to be a kubernetes namespace. Limitador also runs outside kubernetes. A limitador namespace is the "context" on which a limit applies, it could be a service, a product, a proxy, a kubernetes namespace or any other thing really. It's meant to be generic to support multiple use cases.
In a future version of Limitador we might want to rename this field so it's less confusing for users running it in Kubernetes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I see, it is an internal namespace.