Skip to content

Commit

Permalink
Merge pull request #108 from grzpiotrowski/resource-names
Browse files Browse the repository at this point in the history
Resource names alignment
  • Loading branch information
grzpiotrowski authored Nov 20, 2023
2 parents a0a88c2 + d22d51f commit 49a4073
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 27 deletions.
48 changes: 34 additions & 14 deletions controllers/limitador_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
limitadorv1alpha1 "github.com/kuadrant/limitador-operator/api/v1alpha1"
"github.com/kuadrant/limitador-operator/pkg/limitador"
"github.com/kuadrant/limitador-operator/pkg/reconcilers"
upgrades "github.com/kuadrant/limitador-operator/pkg/upgrades"
)

// LimitadorReconciler reconciles a Limitador object
Expand Down Expand Up @@ -79,7 +80,7 @@ func (r *LimitadorReconciler) Reconcile(eventCtx context.Context, req ctrl.Reque
return ctrl.Result{}, nil
}

specErr := r.reconcileSpec(ctx, limitadorObj)
specResult, specErr := r.reconcileSpec(ctx, limitadorObj)

statusResult, statusErr := r.reconcileStatus(ctx, limitadorObj, specErr)

Expand All @@ -91,6 +92,11 @@ func (r *LimitadorReconciler) Reconcile(eventCtx context.Context, req ctrl.Reque
return ctrl.Result{}, statusErr
}

if specResult.Requeue {
logger.V(1).Info("Reconciling spec not finished. Requeueing.")
return specResult, nil
}

if statusResult.Requeue {
logger.V(1).Info("Reconciling status not finished. Requeueing.")
return statusResult, nil
Expand All @@ -100,24 +106,31 @@ func (r *LimitadorReconciler) Reconcile(eventCtx context.Context, req ctrl.Reque
return ctrl.Result{}, nil
}

func (r *LimitadorReconciler) reconcileSpec(ctx context.Context, limitadorObj *limitadorv1alpha1.Limitador) error {
func (r *LimitadorReconciler) reconcileSpec(ctx context.Context, limitadorObj *limitadorv1alpha1.Limitador) (ctrl.Result, error) {
if err := r.reconcileService(ctx, limitadorObj); err != nil {
return err
return ctrl.Result{}, err
}

if err := r.reconcilePVC(ctx, limitadorObj); err != nil {
return err
return ctrl.Result{}, err
}

if err := r.reconcileDeployment(ctx, limitadorObj); err != nil {
return err
result, err := r.reconcileDeployment(ctx, limitadorObj)
if result.Requeue {
return result, nil
}
if err != nil {
return ctrl.Result{}, err
}

if err := r.reconcileLimitsConfigMap(ctx, limitadorObj); err != nil {
return err
return ctrl.Result{}, err
}

return r.reconcilePdb(ctx, limitadorObj)
if err := r.reconcilePdb(ctx, limitadorObj); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}

func (r *LimitadorReconciler) reconcilePdb(ctx context.Context, limitadorObj *limitadorv1alpha1.Limitador) error {
Expand Down Expand Up @@ -162,15 +175,15 @@ func (r *LimitadorReconciler) reconcilePdb(ctx context.Context, limitadorObj *li
return nil
}

func (r *LimitadorReconciler) reconcileDeployment(ctx context.Context, limitadorObj *limitadorv1alpha1.Limitador) error {
func (r *LimitadorReconciler) reconcileDeployment(ctx context.Context, limitadorObj *limitadorv1alpha1.Limitador) (ctrl.Result, error) {
logger, err := logr.FromContext(ctx)
if err != nil {
return err
return ctrl.Result{}, err
}

deploymentOptions, err := r.getDeploymentOptions(ctx, limitadorObj)
if err != nil {
return err
return ctrl.Result{}, err
}

deploymentMutators := make([]reconcilers.DeploymentMutateFn, 0)
Expand All @@ -192,15 +205,16 @@ func (r *LimitadorReconciler) reconcileDeployment(ctx context.Context, limitador
deployment := limitador.Deployment(limitadorObj, deploymentOptions)
// controller reference
if err := r.SetOwnerReference(limitadorObj, deployment); err != nil {
return err
return ctrl.Result{}, err
}
err = r.ReconcileDeployment(ctx, deployment, reconcilers.DeploymentMutator(deploymentMutators...))
logger.V(1).Info("reconcile deployment", "error", err)
if err != nil {
return err
return ctrl.Result{}, err
}

return nil
// TODO: To be deleted when the upgrade path is no longer needed.
return upgrades.UpgradeDeploymentTov070(ctx, r.Client(), limitadorObj, client.ObjectKeyFromObject(deployment))
}

func (r *LimitadorReconciler) reconcileService(ctx context.Context, limitadorObj *limitadorv1alpha1.Limitador) error {
Expand Down Expand Up @@ -267,6 +281,12 @@ func (r *LimitadorReconciler) reconcileLimitsConfigMap(ctx context.Context, limi
return err
}

// TODO: To be deleted when the upgrade path is no longer needed.
err = upgrades.UpgradeConfigMapTov070(ctx, r.Client(), limitadorObj)
if err != nil {
return err
}

return nil
}

Expand Down
22 changes: 11 additions & 11 deletions controllers/limitador_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ var _ = Describe("Limitador controller", func() {
context.TODO(),
types.NamespacedName{
Namespace: LimitadorNamespace,
Name: limitadorObj.Name,
Name: limitador.DeploymentName(limitadorObj),
},
&createdLimitadorDeployment)

Expand Down Expand Up @@ -353,7 +353,7 @@ var _ = Describe("Limitador controller", func() {
context.TODO(),
types.NamespacedName{
Namespace: LimitadorNamespace,
Name: limitadorObj.Name,
Name: limitador.DeploymentName(limitadorObj),
},
&updatedLimitadorDeployment)

Expand Down Expand Up @@ -396,7 +396,7 @@ var _ = Describe("Limitador controller", func() {
context.TODO(),
types.NamespacedName{
Namespace: LimitadorNamespace,
Name: limitadorObj.Name,
Name: limitador.DeploymentName(limitadorObj),
},
&updatedLimitadorDeployment)

Expand Down Expand Up @@ -544,7 +544,7 @@ var _ = Describe("Limitador controller", func() {
context.TODO(),
types.NamespacedName{
Namespace: LimitadorNamespace,
Name: limitadorObj.Name,
Name: limitador.DeploymentName(limitadorObj),
},
&createdLimitadorDeployment)

Expand Down Expand Up @@ -609,7 +609,7 @@ var _ = Describe("Limitador controller", func() {
context.TODO(),
types.NamespacedName{
Namespace: LimitadorNamespace,
Name: limitadorObj.Name,
Name: limitador.DeploymentName(limitadorObj),
},
&updatedLimitadorDeployment)

Expand Down Expand Up @@ -672,7 +672,7 @@ var _ = Describe("Limitador controller", func() {
context.TODO(),
types.NamespacedName{
Namespace: LimitadorNamespace,
Name: limitadorObj.Name,
Name: limitador.DeploymentName(limitadorObj),
},
&updatedLimitadorDeployment)

Expand Down Expand Up @@ -711,7 +711,7 @@ var _ = Describe("Limitador controller", func() {
context.TODO(),
types.NamespacedName{
Namespace: LimitadorNamespace,
Name: limitadorObj.Name,
Name: limitador.DeploymentName(limitadorObj),
},
&deploymentObj)

Expand All @@ -730,7 +730,7 @@ var _ = Describe("Limitador controller", func() {
context.TODO(),
types.NamespacedName{
Namespace: LimitadorNamespace,
Name: limitadorObj.Name,
Name: limitador.DeploymentName(limitadorObj),
},
&updateDeploymentObj)

Expand Down Expand Up @@ -825,7 +825,7 @@ var _ = Describe("Limitador controller", func() {
context.TODO(),
types.NamespacedName{
Namespace: LimitadorNamespace,
Name: limitadorObj.Name,
Name: limitador.DeploymentName(limitadorObj),
},
&deploymentObj)

Expand Down Expand Up @@ -908,7 +908,7 @@ var _ = Describe("Limitador controller", func() {
context.TODO(),
types.NamespacedName{
Namespace: LimitadorNamespace,
Name: limitadorObj.Name,
Name: limitador.DeploymentName(limitadorObj),
},
&deploymentObj)

Expand Down Expand Up @@ -955,7 +955,7 @@ var _ = Describe("Limitador controller", func() {
context.TODO(),
types.NamespacedName{
Namespace: LimitadorNamespace,
Name: limitadorObj.Name,
Name: limitador.DeploymentName(limitadorObj),
},
&deploymentObj)

Expand Down
8 changes: 6 additions & 2 deletions pkg/limitador/k8s_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func Deployment(limitador *limitadorv1alpha1.Limitador, deploymentOptions Deploy
APIVersion: "apps/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: limitador.ObjectMeta.Name, // TODO: revisit later. For now assume same.
Name: DeploymentName(limitador),
Namespace: limitador.ObjectMeta.Namespace, // TODO: revisit later. For now assume same.
Labels: Labels(limitador),
},
Expand Down Expand Up @@ -164,7 +164,7 @@ func LimitsConfigMap(limitadorObj *limitadorv1alpha1.Limitador) (*v1.ConfigMap,
}

func LimitsConfigMapName(limitadorObj *limitadorv1alpha1.Limitador) string {
return fmt.Sprintf("limits-config-%s", limitadorObj.Name)
return fmt.Sprintf("limitador-limits-config-%s", limitadorObj.Name)
}

func ServiceName(limitadorObj *limitadorv1alpha1.Limitador) string {
Expand All @@ -175,6 +175,10 @@ func PVCName(limitadorObj *limitadorv1alpha1.Limitador) string {
return fmt.Sprintf("limitador-%s", limitadorObj.Name)
}

func DeploymentName(limitadorObj *limitadorv1alpha1.Limitador) string {
return fmt.Sprintf("limitador-%s", limitadorObj.Name)
}

func PodDisruptionBudget(limitadorObj *limitadorv1alpha1.Limitador) *policyv1.PodDisruptionBudget {
return &policyv1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Expand Down
77 changes: 77 additions & 0 deletions pkg/upgrades/v0_7_0.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package upgrades

import (
"context"
"fmt"

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"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

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

"github.com/go-logr/logr"
)

func UpgradeDeploymentTov070(ctx context.Context, cli client.Client, limitadorObj *limitadorv1alpha1.Limitador, newDeploymentKey client.ObjectKey) (ctrl.Result, error) {
logger := logr.FromContextOrDiscard(ctx)
logger.V(1).Info("Upgrading Deployment to v0.7.0", "deployment", newDeploymentKey.Name)

newDeployment := &appsv1.Deployment{}
if err := cli.Get(ctx, newDeploymentKey, newDeployment); err != nil {
if errors.IsNotFound(err) {
logger.V(1).Info("New deployment not found")
return ctrl.Result{Requeue: true}, nil
}
return ctrl.Result{}, err
}

availableCondition := helpers.FindDeploymentStatusCondition(newDeployment.Status.Conditions, "Available")

if availableCondition == nil {
return ctrl.Result{Requeue: true}, nil
}

if availableCondition.Status == v1.ConditionTrue {
oldDeployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: ReleaseV060DeploymentName(limitadorObj),
Namespace: limitadorObj.Namespace,
},
}
if err := cli.Delete(ctx, oldDeployment); err != nil {
if errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}
}
return ctrl.Result{}, nil
}

func UpgradeConfigMapTov070(ctx context.Context, cli client.Client, limitadorObj *limitadorv1alpha1.Limitador) error {
oldConfigMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: ReleaseV060LimitsConfigMapName(limitadorObj),
Namespace: limitadorObj.Namespace,
},
}

if err := cli.Delete(ctx, oldConfigMap); err != nil && !errors.IsNotFound(err) {
return err
}

return nil
}

func ReleaseV060DeploymentName(limitadorObj *limitadorv1alpha1.Limitador) string {
return limitadorObj.Name
}

func ReleaseV060LimitsConfigMapName(limitadorObj *limitadorv1alpha1.Limitador) string {
return fmt.Sprintf("limits-config-%s", limitadorObj.Name)
}
Loading

0 comments on commit 49a4073

Please sign in to comment.