Skip to content

Commit

Permalink
Removed support for v1beta1 PDB
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekdwivedi3060 committed Dec 6, 2023
1 parent 7fd810f commit 9e089ef
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 55 deletions.
30 changes: 0 additions & 30 deletions controllers/aerospikecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import (

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
policyv1 "k8s.io/api/policy/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
k8sRuntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -40,8 +37,6 @@ var (
}
)

var PDBbGvk = policyv1beta1.SchemeGroupVersion.WithKind("PodDisruptionBudget")

// AerospikeClusterReconciler reconciles AerospikeClusters
type AerospikeClusterReconciler struct {
client.Client
Expand All @@ -54,10 +49,6 @@ type AerospikeClusterReconciler struct {

// SetupWithManager sets up the controller with the Manager
func (r *AerospikeClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
if err := r.setupPdbAPI(r.KubeConfig); err != nil {
return err
}

return ctrl.NewControllerManagedBy(mgr).
For(&asdbv1.AerospikeCluster{}).
Owns(
Expand All @@ -81,27 +72,6 @@ func (r *AerospikeClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

// setupPdbAPI sets up the pdb api version to use as per the k8s version.
// TODO: Move to v1 when minimum supported k8s version is 1.21
func (r *AerospikeClusterReconciler) setupPdbAPI(config *rest.Config) error {
discoveryClient := discovery.NewDiscoveryClientForConfigOrDie(config)

resources, err := discoveryClient.ServerResourcesForGroupVersion("policy/v1")
if err != nil {
r.Log.Info("Could not get ServerResourcesForGroupVersion for policy/v1, falling back to policy/v1beta1")
return nil
}

for i := range resources.APIResources {
if resources.APIResources[i].Kind == "PodDisruptionBudget" {
PDBbGvk = policyv1.SchemeGroupVersion.WithKind("PodDisruptionBudget")
return nil
}
}

return nil
}

// RackState contains the rack configuration and rack size.
type RackState struct {
Rack *asdbv1.Rack
Expand Down
32 changes: 7 additions & 25 deletions controllers/poddistruptionbudget.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package controllers
import (
"context"
"fmt"
"strconv"

v1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

Expand Down Expand Up @@ -38,9 +37,7 @@ func (r *SingleClusterReconciler) createOrUpdatePDB() error {
}

ls := utils.LabelsForAerospikeCluster(r.aeroCluster.Name)
// TODO: Move to concrete object when minimum supported k8s version is 1.21
pdb := &unstructured.Unstructured{}
pdb.SetGroupVersionKind(PDBbGvk)
pdb := &v1.PodDisruptionBudget{}

if err := r.Client.Get(
context.TODO(), types.NamespacedName{
Expand All @@ -56,11 +53,9 @@ func (r *SingleClusterReconciler) createOrUpdatePDB() error {
pdb.SetName(r.aeroCluster.Name)
pdb.SetNamespace(r.aeroCluster.Namespace)
pdb.SetLabels(ls)
pdb.Object["spec"] = map[string]interface{}{
"maxUnavailable": r.aeroCluster.Spec.MaxUnavailable,
"selector": &metav1.LabelSelector{
MatchLabels: ls,
},
pdb.Spec.MaxUnavailable = r.aeroCluster.Spec.MaxUnavailable
pdb.Spec.Selector = &metav1.LabelSelector{
MatchLabels: ls,
}

// Set AerospikeCluster instance as the owner and controller
Expand Down Expand Up @@ -90,21 +85,8 @@ func (r *SingleClusterReconciler) createOrUpdatePDB() error {
utils.NamespacedName(r.aeroCluster.Namespace, r.aeroCluster.Name),
)

var value string

maxUnavailable := pdb.Object["spec"].(map[string]interface{})["maxUnavailable"]

// Type casting is required because of unstructured object
if val, ok := maxUnavailable.(string); ok {
value = val
} else {
value = strconv.Itoa(int(maxUnavailable.(int64)))
}

if value != r.aeroCluster.Spec.MaxUnavailable.String() {
spec := pdb.Object["spec"].(map[string]interface{})
spec["maxUnavailable"] = r.aeroCluster.Spec.MaxUnavailable
pdb.Object["spec"] = spec
if pdb.Spec.MaxUnavailable.String() != r.aeroCluster.Spec.MaxUnavailable.String() {
pdb.Spec.MaxUnavailable = r.aeroCluster.Spec.MaxUnavailable

if err := r.Client.Update(
context.TODO(), pdb, updateOption,
Expand Down

0 comments on commit 9e089ef

Please sign in to comment.