Browse Source

pdb

pull/14/head
Yaroslav Shishnev 1 month ago
parent
commit
fc7c471c08
1 changed files with 66 additions and 23 deletions
  1. 66
    23
      pkg/controller/rabbitmq/rabbitmq_pdb.go

+ 66
- 23
pkg/controller/rabbitmq/rabbitmq_pdb.go View File

@@ -5,7 +5,9 @@ import (
"github.com/go-logr/logr"
v1beta1policy "k8s.io/api/policy/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -13,53 +15,94 @@ import (
rabbitmqv1 "github.com/tekliner/rabbitmq-operator/pkg/apis/rabbitmq/v1"
)

func (r *ReconcileRabbitmq) reconcilePdb(reqLogger logr.Logger,
cr *rabbitmqv1.Rabbitmq,
pdb *v1beta1policy.PodDisruptionBudget) (reconcile.Result, error) {
reqLogger.Info("Started reconciling PodDisruptionBudget", "Pdb.Namespace", pdb.Namespace, "Pdb.Name", pdb.Name)
func (r *ReconcileRabbitmq) reconcilePdb(reqLogger logr.Logger, cr *rabbitmqv1.Rabbitmq) (reconcile.Result, error) {

if err := controllerutil.SetControllerReference(cr, pdb, r.scheme); err != nil {
reqLogger.Info("Error setting controller reference for PodDisruptionBudget", "Pdb.Namespace", pdb.Namespace, "Pdb.Name", pdb.Name)
newPdb := getDisruptionBudget(cr)
reqLogger.Info("Started reconciling PodDisruptionBudget", "Pdb.Namespace", newPdb.Namespace, "Pdb.Name", newPdb.Name)

if err := controllerutil.SetControllerReference(cr, &newPdb, r.scheme); err != nil {
reqLogger.Info("Error setting controller reference for PodDisruptionBudget", "Pdb.Namespace", &newPdb.Namespace, "Pdb.Name", &newPdb.Name)
return reconcile.Result{}, err
}
// Check existing pdb
foundPdb := &v1beta1policy.PodDisruptionBudget{}
reqLogger.Info("Getting PodDisruptionBudget", "Pdb.Namespace", pdb.Namespace, "Pdb.Name", pdb.Name)
err := r.client.Get(context.TODO(), types.NamespacedName{Name: pdb.Name, Namespace: pdb.Namespace}, foundPdb)
reqLogger.Info("Getting PodDisruptionBudget", "Pdb.Namespace", &newPdb.Namespace, "Pdb.Name", &newPdb.Name)
err := r.client.Get(context.TODO(), types.NamespacedName{Name: newPdb.Name, Namespace: newPdb.Namespace}, foundPdb)

if err != nil && apierrors.IsNotFound(err) {
reqLogger.Info("No PodDisruptionBudget found, creating new", "Pdb.Namespace", pdb.Namespace, "Pdb.Name", pdb.Name)
err = r.client.Create(context.TODO(), pdb)
reqLogger.Info("No PodDisruptionBudget found, creating new", "Pdb.Namespace", &newPdb.Namespace, "Pdb.Name", &newPdb.Name)
err = r.client.Create(context.TODO(), &newPdb)

foundPdb = pdb
foundPdb = &newPdb

if err != nil {
reqLogger.Info("Error creating new PodDisruptionBudget", "Pdb.Namespace", pdb.Namespace, "Pdb.Name", pdb.Name)
reqLogger.Info("Error creating new PodDisruptionBudget", "Pdb.Namespace", &newPdb.Namespace, "Pdb.Name", &newPdb.Name)
return reconcile.Result{}, err
}
} else if err != nil {
reqLogger.Info("Error getting PodDisruptionBudget", "Pdb.Namespace", pdb.Namespace, "Pdb.Name", pdb.Name)
reqLogger.Info("Error getting PodDisruptionBudget", "Pdb.Namespace", &newPdb.Namespace, "Pdb.Name", &newPdb.Name)
return reconcile.Result{}, err
}

if !reflect.DeepEqual(foundPdb.Spec.Selector, pdb.Spec.Selector) {
reqLogger.Info("Selectors not deep equal", "Pdb.Namespace", pdb.Namespace, "Pdb.Name", pdb.Name)
foundPdb.Spec.Selector = pdb.Spec.Selector
if !reflect.DeepEqual(foundPdb.Spec.Selector, &newPdb.Spec.Selector) {
reqLogger.Info("Selectors not deep equal", "Pdb.Namespace", &newPdb.Namespace, "Pdb.Name", &newPdb.Name)
foundPdb.Spec.Selector = newPdb.Spec.Selector
}

if !reflect.DeepEqual(foundPdb.Spec.MaxUnavailable, pdb.Spec.MaxUnavailable) {
reqLogger.Info("MaxUnavailable not deep equal", "Pdb.Namespace", pdb.Namespace, "Pdb.Name", pdb.Name)
foundPdb.Spec.MaxUnavailable = pdb.Spec.MaxUnavailable
if !reflect.DeepEqual(foundPdb.Spec.MaxUnavailable, &newPdb.Spec.MaxUnavailable) {
reqLogger.Info("MaxUnavailable not deep equal", "Pdb.Namespace", &newPdb.Namespace, "Pdb.Name", &newPdb.Name)
foundPdb.Spec.MaxUnavailable = newPdb.Spec.MaxUnavailable
}

if !reflect.DeepEqual(foundPdb.Spec.MinAvailable, pdb.Spec.MinAvailable) {
reqLogger.Info("MinAvailable not deep equal", "Pdb.Namespace", pdb.Namespace, "Pdb.Name", pdb.Name)
foundPdb.Spec.MaxUnavailable = pdb.Spec.MaxUnavailable
if !reflect.DeepEqual(foundPdb.Spec.MinAvailable, &newPdb.Spec.MinAvailable) {
reqLogger.Info("MinAvailable not deep equal", "Pdb.Namespace", &newPdb.Namespace, "Pdb.Name", &newPdb.Name)
foundPdb.Spec.MaxUnavailable = newPdb.Spec.MaxUnavailable
}

if err = r.client.Update(context.TODO(), foundPdb); err != nil {
reqLogger.Info("Error updating PodDisruptionBudget", "Pdb.Namespace", pdb.Namespace, "Pdb.Name", pdb.Name)
reqLogger.Info("Error updating PodDisruptionBudget", "Pdb.Namespace", &newPdb.Namespace, "Pdb.Name", &newPdb.Name)
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}

func getDisruptionBudget(cr *rabbitmqv1.Rabbitmq) v1beta1policy.PodDisruptionBudget {
podDisruptionBudget := v1beta1policy.PodDisruptionBudget{}
labelSelector := metav1.LabelSelector{MatchLabels: cr.Labels}
if cr.Spec.RabbitmqReplicas >= 2 {
specPDB := v1beta1policy.PodDisruptionBudgetSpec{Selector: &labelSelector}

if cr.Spec.RabbitmqPdb.Spec.MinAvailable != nil && cr.Spec.RabbitmqPdb.Spec.MaxUnavailable != nil {
specPDB.MinAvailable = cr.Spec.RabbitmqPdb.Spec.MinAvailable
} else if cr.Spec.RabbitmqPdb.Spec.MinAvailable != nil {
specPDB.MinAvailable = cr.Spec.RabbitmqPdb.Spec.MinAvailable
} else if cr.Spec.RabbitmqPdb.Spec.MaxUnavailable != nil {
specPDB.MaxUnavailable = cr.Spec.RabbitmqPdb.Spec.MaxUnavailable
} else {
specPDB.MinAvailable = func() *intstr.IntOrString { v := intstr.FromInt(1); return &v }()
}

podDisruptionBudget = v1beta1policy.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
},
Spec: specPDB,
}
} else {
maxUnavailable := intstr.FromInt(1)
specPDB := v1beta1policy.PodDisruptionBudgetSpec{
Selector: &labelSelector,
MaxUnavailable: &maxUnavailable,
}
podDisruptionBudget = v1beta1policy.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
},
Spec: specPDB,
}
}
return podDisruptionBudget

}

Loading…
Cancel
Save