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

fix status check #117

Merged
merged 10 commits into from
Nov 21, 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
2 changes: 1 addition & 1 deletion api/v1alpha1/limitador_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ type LimitadorStatus struct {
ObservedGeneration int64 `json:"observedGeneration,omitempty"`

// Represents the observations of a foo's current state.
// Known .status.conditions.type are: "Available"
// Known .status.conditions.type are: "Ready"
// +patchMergeKey=type
// +patchStrategy=merge
// +listType=map
Expand Down
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-15T10:55:48Z"
createdAt: "2023-11-21T15:45:03Z"
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
2 changes: 1 addition & 1 deletion bundle/manifests/limitador.kuadrant.io_limitadors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ spec:
properties:
conditions:
description: 'Represents the observations of a foo''s current state.
Known .status.conditions.type are: "Available"'
Known .status.conditions.type are: "Ready"'
items:
description: "Condition contains details for one aspect of the current
state of this API Resource. --- This struct is intended for direct
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/limitador.kuadrant.io_limitadors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ spec:
properties:
conditions:
description: 'Represents the observations of a foo''s current state.
Known .status.conditions.type are: "Available"'
Known .status.conditions.type are: "Ready"'
items:
description: "Condition contains details for one aspect of the current
state of this API Resource. --- This struct is intended for direct
Expand Down
7 changes: 6 additions & 1 deletion controllers/limitador_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ func (r *LimitadorReconciler) reconcileDeployment(ctx context.Context, limitador
reconcilers.DeploymentVolumesMutator,
reconcilers.DeploymentVolumeMountsMutator,
reconcilers.DeploymentEnvMutator,
reconcilers.DeploymentPortsMutator,
reconcilers.DeploymentLivenessProbeMutator,
reconcilers.DeploymentReadinessProbeMutator,
)

deployment := limitador.Deployment(limitadorObj, deploymentOptions)
Expand Down Expand Up @@ -229,7 +232,9 @@ func (r *LimitadorReconciler) reconcileService(ctx context.Context, limitadorObj
return err
}

err = r.ReconcileService(ctx, limitadorService, reconcilers.CreateOnlyMutator)
serviceMutator := reconcilers.ServiceMutator(reconcilers.ServicePortsMutator)

err = r.ReconcileService(ctx, limitadorService, serviceMutator)
logger.V(1).Info("reconcile service", "error", err)
if err != nil {
return err
Expand Down
146 changes: 146 additions & 0 deletions controllers/limitador_controller_affinity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package controllers

import (
"context"
"reflect"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

limitadorv1alpha1 "github.com/kuadrant/limitador-operator/api/v1alpha1"
"github.com/kuadrant/limitador-operator/pkg/limitador"
)

var _ = Describe("Limitador controller manages affinity", func() {

var testNamespace string

BeforeEach(func() {
CreateNamespace(&testNamespace)
})

AfterEach(DeleteNamespaceCallback(&testNamespace))

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

affinity := &v1.Affinity{
PodAntiAffinity: &v1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []v1.WeightedPodAffinityTerm{
{
Weight: 100,
PodAffinityTerm: v1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"pod": "label",
},
},
TopologyKey: "kubernetes.io/hostname",
},
},
},
},
}

BeforeEach(func() {
limitadorObj = basicLimitador(testNamespace)
limitadorObj.Spec.Affinity = affinity
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 the custom affinity", 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.Affinity).To(Equal(affinity))
})
})

Context("Updating limitador object with new affinity settings", func() {
var limitadorObj *limitadorv1alpha1.Limitador

affinity := &v1.Affinity{
PodAntiAffinity: &v1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []v1.WeightedPodAffinityTerm{
{
Weight: 100,
PodAffinityTerm: v1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"pod": "label",
},
},
TopologyKey: "kubernetes.io/hostname",
},
},
},
},
}

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 deployment with the affinity custom settings", 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.Affinity).To(BeNil())

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.Affinity = affinity.DeepCopy()

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.Affinity, affinity)
}, timeout, interval).Should(BeTrue())
})
})
})
157 changes: 157 additions & 0 deletions controllers/limitador_controller_limits_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package controllers

import (
"context"
"reflect"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/yaml"

limitadorv1alpha1 "github.com/kuadrant/limitador-operator/api/v1alpha1"
"github.com/kuadrant/limitador-operator/pkg/limitador"
)

var _ = Describe("Limitador controller manages limits", func() {

var testNamespace string

BeforeEach(func() {
CreateNamespace(&testNamespace)
})

AfterEach(DeleteNamespaceCallback(&testNamespace))

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

limits := []limitadorv1alpha1.RateLimit{
{
Conditions: []string{"req.method == 'GET'"},
MaxValue: 10,
Namespace: "test-namespace",
Seconds: 60,
Variables: []string{"user_id"},
Name: "useless",
},
{
Conditions: []string{"req.method == 'POST'"},
MaxValue: 5,
Namespace: "test-namespace",
Seconds: 60,
Variables: []string{"user_id"},
},
}

BeforeEach(func() {
limitadorObj = basicLimitador(testNamespace)
limitadorObj.Spec.Limits = limits
Expect(k8sClient.Create(context.TODO(), limitadorObj)).Should(Succeed())
Eventually(testLimitadorIsReady(limitadorObj), time.Minute, 5*time.Second).Should(BeTrue())
})

It("Should create configmap with the custom limits", func() {
cm := &v1.ConfigMap{}
Eventually(func() bool {
err := k8sClient.Get(context.TODO(),
types.NamespacedName{
Namespace: testNamespace,
Name: limitador.LimitsConfigMapName(limitadorObj),
}, cm)

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

var cmLimits []limitadorv1alpha1.RateLimit
err := yaml.Unmarshal([]byte(cm.Data[limitador.LimitadorConfigFileName]), &cmLimits)
Expect(err).To(BeNil())
Expect(cmLimits).To(Equal(limits))
})
})

Context("Updating limitador object with new limits", func() {
var limitadorObj *limitadorv1alpha1.Limitador

limits := []limitadorv1alpha1.RateLimit{
{
Conditions: []string{"req.method == 'GET'"},
MaxValue: 10,
Namespace: "test-namespace",
Seconds: 60,
Variables: []string{"user_id"},
Name: "useless",
},
{
Conditions: []string{"req.method == 'POST'"},
MaxValue: 5,
Namespace: "test-namespace",
Seconds: 60,
Variables: []string{"user_id"},
},
}

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 configmap with the new limits", func() {
cm := &v1.ConfigMap{}
Eventually(func() bool {
err := k8sClient.Get(context.TODO(),
types.NamespacedName{
Namespace: testNamespace,
Name: limitador.LimitsConfigMapName(limitadorObj),
}, cm)

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

var cmLimits []limitadorv1alpha1.RateLimit
err := yaml.Unmarshal([]byte(cm.Data[limitador.LimitadorConfigFileName]), &cmLimits)
Expect(err).To(BeNil())
Expect(cmLimits).To(BeEmpty())

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.Limits = limits

return k8sClient.Update(context.TODO(), &updatedLimitador) == nil
}, timeout, interval).Should(BeTrue())

Eventually(func() bool {
newCM := &v1.ConfigMap{}
err := k8sClient.Get(context.TODO(),
types.NamespacedName{
Namespace: testNamespace,
Name: limitador.LimitsConfigMapName(limitadorObj),
}, newCM)

if err != nil {
return false
}

var cmLimits []limitadorv1alpha1.RateLimit
err = yaml.Unmarshal([]byte(newCM.Data[limitador.LimitadorConfigFileName]), &cmLimits)
if err != nil {
return false
}

return reflect.DeepEqual(cmLimits, limits)
}, timeout, interval).Should(BeTrue())
})
})
})
Loading
Loading