Skip to content

Commit

Permalink
[controller] Reconciling storage options
Browse files Browse the repository at this point in the history
  • Loading branch information
didierofrivia committed Sep 21, 2022
1 parent 5e634b5 commit 9a55813
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
33 changes: 32 additions & 1 deletion controllers/limitador_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/types"
"reflect"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -78,7 +79,18 @@ func (r *LimitadorReconciler) Reconcile(eventCtx context.Context, req ctrl.Reque
return ctrl.Result{}, err
}

deployment := limitador.LimitadorDeployment(limitadorObj)
limitadorStorage := limitadorObj.Spec.Storage
var storageConfigSecret *v1.Secret
if limitadorStorage != nil && limitadorStorage.Type != limitadorv1alpha1.StorageTypeInMemory {
if !limitadorStorage.Validate() {
return ctrl.Result{}, fmt.Errorf("there's no ConfigSecretRef set")
}
if storageConfigSecret, err = getStorageConfigSecret(ctx, r.Client(), limitadorStorage.ConfigSecretRef); err != nil {
return ctrl.Result{}, err
}
}

deployment := limitador.LimitadorDeployment(limitadorObj, storageConfigSecret)
err = r.ReconcileDeployment(ctx, deployment, mutateLimitadorDeployment)
logger.V(1).Info("reconcile deployment", "error", err)
if err != nil {
Expand Down Expand Up @@ -278,3 +290,22 @@ func mutateLimitadorDeployment(existingObj, desiredObj client.Object) (bool, err

return updated, nil
}

func getStorageConfigSecret(ctx context.Context, client client.Client, secretRef *v1.ObjectReference) (*v1.Secret, error) {
storageConfigSecret := &v1.Secret{}
if err := client.Get(
ctx,
types.NamespacedName{
Name: secretRef.Name,
Namespace: secretRef.Namespace,
},
storageConfigSecret,
); err != nil {
return nil, err
}

if len(storageConfigSecret.Data) > 0 && storageConfigSecret.Data["URL"] != nil {
return storageConfigSecret, nil
}
return nil, fmt.Errorf("the storage config Secret doesn't have the `URL` field")
}
16 changes: 15 additions & 1 deletion controllers/limitador_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ var _ = Describe("Limitador controller", func() {
GRPC: grpcPort,
},
Limits: limits,
Storage: &limitadorv1alpha1.Storage{
Type: limitadorv1alpha1.StorageTypeInMemory,
ConfigSecretRef: nil,
},
},
}
}
Expand Down Expand Up @@ -127,7 +131,7 @@ var _ = Describe("Limitador controller", func() {
Expect(k8sClient.Create(context.TODO(), limitadorObj)).Should(Succeed())
})

It("Should create a new deployment with the right number of replicas, version and config file", func() {
It("Should create a new deployment with the right settings", func() {
createdLimitadorDeployment := appsv1.Deployment{}
Eventually(func() bool {
err := k8sClient.Get(
Expand Down Expand Up @@ -158,6 +162,16 @@ var _ = Describe("Limitador controller", func() {
Expect(createdLimitadorDeployment.Spec.Template.Spec.Volumes[0].VolumeSource.ConfigMap.Name).Should(
Equal(limitador.LimitsCMNamePrefix + limitadorObj.Name),
)
Expect(createdLimitadorDeployment.Spec.Template.Spec.Containers[0].Command).Should(
Equal(
[]string{
"limitador-server",
"/home/limitador/etc/limitador-config.yaml",
"in-memory",
"",
},
),
)
})

It("Should create a Limitador service", func() {
Expand Down
18 changes: 17 additions & 1 deletion pkg/limitador/k8s_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func LimitadorService(limitador *limitadorv1alpha1.Limitador) *v1.Service {
}
}

func LimitadorDeployment(limitador *limitadorv1alpha1.Limitador) *appsv1.Deployment {
func LimitadorDeployment(limitador *limitadorv1alpha1.Limitador, storageConfigSecret *v1.Secret) *appsv1.Deployment {
var replicas int32 = DefaultReplicas
if limitador.Spec.Replicas != nil {
replicas = int32(*limitador.Spec.Replicas)
Expand Down Expand Up @@ -94,6 +94,8 @@ func LimitadorDeployment(limitador *limitadorv1alpha1.Limitador) *appsv1.Deploym
Command: []string{
"limitador-server",
fmt.Sprintf("%s%s", LimitadorCMMountPath, LimitadorConfigFileName),
storageType(limitador.Spec.Storage),
storageConfig(limitador.Spec.Storage, storageConfigSecret),
},
Ports: []v1.ContainerPort{
{
Expand Down Expand Up @@ -196,3 +198,17 @@ func ownerRefToLimitador(limitador *limitadorv1alpha1.Limitador) metav1.OwnerRef
UID: limitador.UID,
}
}

func storageType(storage *limitadorv1alpha1.Storage) string {
if storage != nil {
return string(storage.Type)
}
return string(limitadorv1alpha1.StorageTypeInMemory)
}

func storageConfig(storage *limitadorv1alpha1.Storage, storageConfigSecret *v1.Secret) string {
if storage != nil && storage.Type != limitadorv1alpha1.StorageTypeInMemory {
return string(storageConfigSecret.Data["URL"])
}
return ""
}

0 comments on commit 9a55813

Please sign in to comment.