Repository: spotahome/redis-operator
Pull request #405 (2022-05-09 12:50:24)

upgrade poddisruptionbudget apiversion to v1
Fixes apiversion for poddisruptionbudgets

Changes proposed on the PR:
- stop using policy/v1beta1 api for poddisruptionbudgets
- use instead policy/v1


Patch:
diff --git a/api/redisfailover/v1/zz_generated.deepcopy.go b/api/redisfailover/v1/zz_generated.deepcopy.go
index 2f4a900e..48c9681c 100644
--- a/api/redisfailover/v1/zz_generated.deepcopy.go
+++ b/api/redisfailover/v1/zz_generated.deepcopy.go
@@ -126,6 +126,11 @@ func (in *RedisCommandRename) DeepCopy() *RedisCommandRename {
 // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
 func (in *RedisExporter) DeepCopyInto(out *RedisExporter) {
 	*out = *in
+	if in.ContainerSecurityContext != nil {
+		in, out := &in.ContainerSecurityContext, &out.ContainerSecurityContext
+		*out = new(corev1.SecurityContext)
+		(*in).DeepCopyInto(*out)
+	}
 	if in.Args != nil {
 		in, out := &in.Args, &out.Args
 		*out = make([]string, len(*in))
@@ -276,6 +281,11 @@ func (in *RedisSettings) DeepCopyInto(out *RedisSettings) {
 		*out = new(corev1.PodSecurityContext)
 		(*in).DeepCopyInto(*out)
 	}
+	if in.ContainerSecurityContext != nil {
+		in, out := &in.ContainerSecurityContext, &out.ContainerSecurityContext
+		*out = new(corev1.SecurityContext)
+		(*in).DeepCopyInto(*out)
+	}
 	if in.ImagePullSecrets != nil {
 		in, out := &in.ImagePullSecrets, &out.ImagePullSecrets
 		*out = make([]corev1.LocalObjectReference, len(*in))
@@ -348,9 +358,35 @@ func (in *RedisStorage) DeepCopy() *RedisStorage {
 	return out
 }
 
+// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
+func (in *SentinelConfigCopy) DeepCopyInto(out *SentinelConfigCopy) {
+	*out = *in
+	if in.ContainerSecurityContext != nil {
+		in, out := &in.ContainerSecurityContext, &out.ContainerSecurityContext
+		*out = new(corev1.SecurityContext)
+		(*in).DeepCopyInto(*out)
+	}
+	return
+}
+
+// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SentinelConfigCopy.
+func (in *SentinelConfigCopy) DeepCopy() *SentinelConfigCopy {
+	if in == nil {
+		return nil
+	}
+	out := new(SentinelConfigCopy)
+	in.DeepCopyInto(out)
+	return out
+}
+
 // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
 func (in *SentinelExporter) DeepCopyInto(out *SentinelExporter) {
 	*out = *in
+	if in.ContainerSecurityContext != nil {
+		in, out := &in.ContainerSecurityContext, &out.ContainerSecurityContext
+		*out = new(corev1.SecurityContext)
+		(*in).DeepCopyInto(*out)
+	}
 	if in.Args != nil {
 		in, out := &in.Args, &out.Args
 		*out = make([]string, len(*in))
@@ -405,6 +441,11 @@ func (in *SentinelSettings) DeepCopyInto(out *SentinelSettings) {
 		*out = new(corev1.PodSecurityContext)
 		(*in).DeepCopyInto(*out)
 	}
+	if in.ContainerSecurityContext != nil {
+		in, out := &in.ContainerSecurityContext, &out.ContainerSecurityContext
+		*out = new(corev1.SecurityContext)
+		(*in).DeepCopyInto(*out)
+	}
 	if in.ImagePullSecrets != nil {
 		in, out := &in.ImagePullSecrets, &out.ImagePullSecrets
 		*out = make([]corev1.LocalObjectReference, len(*in))
@@ -439,6 +480,7 @@ func (in *SentinelSettings) DeepCopyInto(out *SentinelSettings) {
 		}
 	}
 	in.Exporter.DeepCopyInto(&out.Exporter)
+	in.ConfigCopy.DeepCopyInto(&out.ConfigCopy)
 	return
 }
 
diff --git a/mocks/service/k8s/Services.go b/mocks/service/k8s/Services.go
index b7ab801c..c27ddab1 100644
--- a/mocks/service/k8s/Services.go
+++ b/mocks/service/k8s/Services.go
@@ -11,14 +11,14 @@ import (
 
 	mock "github.com/stretchr/testify/mock"
 
+	policyv1 "k8s.io/api/policy/v1"
+
 	rbacv1 "k8s.io/api/rbac/v1"
 
 	redisfailoverv1 "github.com/spotahome/redis-operator/api/redisfailover/v1"
 
 	v1 "k8s.io/api/core/v1"
 
-	v1beta1 "k8s.io/api/policy/v1beta1"
-
 	watch "k8s.io/apimachinery/pkg/watch"
 )
 
@@ -112,11 +112,11 @@ func (_m *Services) CreateOrUpdatePod(namespace string, pod *v1.Pod) error {
 }
 
 // CreateOrUpdatePodDisruptionBudget provides a mock function with given fields: namespace, podDisruptionBudget
-func (_m *Services) CreateOrUpdatePodDisruptionBudget(namespace string, podDisruptionBudget *v1beta1.PodDisruptionBudget) error {
+func (_m *Services) CreateOrUpdatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1.PodDisruptionBudget) error {
 	ret := _m.Called(namespace, podDisruptionBudget)
 
 	var r0 error
-	if rf, ok := ret.Get(0).(func(string, *v1beta1.PodDisruptionBudget) error); ok {
+	if rf, ok := ret.Get(0).(func(string, *policyv1.PodDisruptionBudget) error); ok {
 		r0 = rf(namespace, podDisruptionBudget)
 	} else {
 		r0 = ret.Error(0)
@@ -196,11 +196,11 @@ func (_m *Services) CreatePod(namespace string, pod *v1.Pod) error {
 }
 
 // CreatePodDisruptionBudget provides a mock function with given fields: namespace, podDisruptionBudget
-func (_m *Services) CreatePodDisruptionBudget(namespace string, podDisruptionBudget *v1beta1.PodDisruptionBudget) error {
+func (_m *Services) CreatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1.PodDisruptionBudget) error {
 	ret := _m.Called(namespace, podDisruptionBudget)
 
 	var r0 error
-	if rf, ok := ret.Get(0).(func(string, *v1beta1.PodDisruptionBudget) error); ok {
+	if rf, ok := ret.Get(0).(func(string, *policyv1.PodDisruptionBudget) error); ok {
 		r0 = rf(namespace, podDisruptionBudget)
 	} else {
 		r0 = ret.Error(0)
@@ -465,15 +465,15 @@ func (_m *Services) GetPod(namespace string, name string) (*v1.Pod, error) {
 }
 
 // GetPodDisruptionBudget provides a mock function with given fields: namespace, name
-func (_m *Services) GetPodDisruptionBudget(namespace string, name string) (*v1beta1.PodDisruptionBudget, error) {
+func (_m *Services) GetPodDisruptionBudget(namespace string, name string) (*policyv1.PodDisruptionBudget, error) {
 	ret := _m.Called(namespace, name)
 
-	var r0 *v1beta1.PodDisruptionBudget
-	if rf, ok := ret.Get(0).(func(string, string) *v1beta1.PodDisruptionBudget); ok {
+	var r0 *policyv1.PodDisruptionBudget
+	if rf, ok := ret.Get(0).(func(string, string) *policyv1.PodDisruptionBudget); ok {
 		r0 = rf(namespace, name)
 	} else {
 		if ret.Get(0) != nil {
-			r0 = ret.Get(0).(*v1beta1.PodDisruptionBudget)
+			r0 = ret.Get(0).(*policyv1.PodDisruptionBudget)
 		}
 	}
 
@@ -806,11 +806,11 @@ func (_m *Services) UpdatePod(namespace string, pod *v1.Pod) error {
 }
 
 // UpdatePodDisruptionBudget provides a mock function with given fields: namespace, podDisruptionBudget
-func (_m *Services) UpdatePodDisruptionBudget(namespace string, podDisruptionBudget *v1beta1.PodDisruptionBudget) error {
+func (_m *Services) UpdatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1.PodDisruptionBudget) error {
 	ret := _m.Called(namespace, podDisruptionBudget)
 
 	var r0 error
-	if rf, ok := ret.Get(0).(func(string, *v1beta1.PodDisruptionBudget) error); ok {
+	if rf, ok := ret.Get(0).(func(string, *policyv1.PodDisruptionBudget) error); ok {
 		r0 = rf(namespace, podDisruptionBudget)
 	} else {
 		r0 = ret.Error(0)
diff --git a/operator/redisfailover/service/generator.go b/operator/redisfailover/service/generator.go
index 9beab34e..c364c4d3 100644
--- a/operator/redisfailover/service/generator.go
+++ b/operator/redisfailover/service/generator.go
@@ -1,20 +1,18 @@
 package service
 
 import (
+	"bytes"
 	"fmt"
 	"strings"
-
-	"bytes"
+	"text/template"
 
 	appsv1 "k8s.io/api/apps/v1"
 	corev1 "k8s.io/api/core/v1"
-	policyv1beta1 "k8s.io/api/policy/v1beta1"
+	policyv1 "k8s.io/api/policy/v1"
 	"k8s.io/apimachinery/pkg/api/resource"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/util/intstr"
 
-	"text/template"
-
 	redisfailoverv1 "github.com/spotahome/redis-operator/api/redisfailover/v1"
 	"github.com/spotahome/redis-operator/operator/redisfailover/util"
 )
@@ -183,6 +181,7 @@ fi`, rfName)
 		},
 	}
 }
+
 func generateRedisReadinessConfigMap(rf *redisfailoverv1.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) *corev1.ConfigMap {
 	name := GetRedisReadinessName(rf)
 	namespace := rf.Namespace
@@ -534,15 +533,15 @@ func generateSentinelDeployment(rf *redisfailoverv1.RedisFailover, labels map[st
 	return sd
 }
 
-func generatePodDisruptionBudget(name string, namespace string, labels map[string]string, ownerRefs []metav1.OwnerReference, minAvailable intstr.IntOrString) *policyv1beta1.PodDisruptionBudget {
-	return &policyv1beta1.PodDisruptionBudget{
+func generatePodDisruptionBudget(name string, namespace string, labels map[string]string, ownerRefs []metav1.OwnerReference, minAvailable intstr.IntOrString) *policyv1.PodDisruptionBudget {
+	return &policyv1.PodDisruptionBudget{
 		ObjectMeta: metav1.ObjectMeta{
 			Name:            name,
 			Namespace:       namespace,
 			Labels:          labels,
 			OwnerReferences: ownerRefs,
 		},
-		Spec: policyv1beta1.PodDisruptionBudgetSpec{
+		Spec: policyv1.PodDisruptionBudgetSpec{
 			MinAvailable: &minAvailable,
 			Selector: &metav1.LabelSelector{
 				MatchLabels: labels,
@@ -604,7 +603,6 @@ func createRedisExporterContainer(rf *redisfailoverv1.RedisFailover) corev1.Cont
 				},
 			},
 		})
-
 	}
 
 	return container
diff --git a/service/k8s/poddisruptionbudget.go b/service/k8s/poddisruptionbudget.go
index cc658b5a..32a1d217 100644
--- a/service/k8s/poddisruptionbudget.go
+++ b/service/k8s/poddisruptionbudget.go
@@ -3,7 +3,7 @@ package k8s
 import (
 	"context"
 
-	policyv1beta1 "k8s.io/api/policy/v1beta1"
+	policyv1 "k8s.io/api/policy/v1"
 	"k8s.io/apimachinery/pkg/api/errors"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/client-go/kubernetes"
@@ -13,10 +13,10 @@ import (
 
 // PodDisruptionBudget the ServiceAccount service that knows how to interact with k8s to manage them
 type PodDisruptionBudget interface {
-	GetPodDisruptionBudget(namespace string, name string) (*policyv1beta1.PodDisruptionBudget, error)
-	CreatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1beta1.PodDisruptionBudget) error
-	UpdatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1beta1.PodDisruptionBudget) error
-	CreateOrUpdatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1beta1.PodDisruptionBudget) error
+	GetPodDisruptionBudget(namespace string, name string) (*policyv1.PodDisruptionBudget, error)
+	CreatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1.PodDisruptionBudget) error
+	UpdatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1.PodDisruptionBudget) error
+	CreateOrUpdatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1.PodDisruptionBudget) error
 	DeletePodDisruptionBudget(namespace string, name string) error
 }
 
@@ -35,16 +35,16 @@ func NewPodDisruptionBudgetService(kubeClient kubernetes.Interface, logger log.L
 	}
 }
 
-func (p *PodDisruptionBudgetService) GetPodDisruptionBudget(namespace string, name string) (*policyv1beta1.PodDisruptionBudget, error) {
-	podDisruptionBudget, err := p.kubeClient.PolicyV1beta1().PodDisruptionBudgets(namespace).Get(context.TODO(), name, metav1.GetOptions{})
+func (p *PodDisruptionBudgetService) GetPodDisruptionBudget(namespace string, name string) (*policyv1.PodDisruptionBudget, error) {
+	podDisruptionBudget, err := p.kubeClient.PolicyV1().PodDisruptionBudgets(namespace).Get(context.TODO(), name, metav1.GetOptions{})
 	if err != nil {
 		return nil, err
 	}
 	return podDisruptionBudget, nil
 }
 
-func (p *PodDisruptionBudgetService) CreatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1beta1.PodDisruptionBudget) error {
-	_, err := p.kubeClient.PolicyV1beta1().PodDisruptionBudgets(namespace).Create(context.TODO(), podDisruptionBudget, metav1.CreateOptions{})
+func (p *PodDisruptionBudgetService) CreatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1.PodDisruptionBudget) error {
+	_, err := p.kubeClient.PolicyV1().PodDisruptionBudgets(namespace).Create(context.TODO(), podDisruptionBudget, metav1.CreateOptions{})
 	if err != nil {
 		return err
 	}
@@ -52,8 +52,8 @@ func (p *PodDisruptionBudgetService) CreatePodDisruptionBudget(namespace string,
 	return nil
 }
 
-func (p *PodDisruptionBudgetService) UpdatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1beta1.PodDisruptionBudget) error {
-	_, err := p.kubeClient.PolicyV1beta1().PodDisruptionBudgets(namespace).Update(context.TODO(), podDisruptionBudget, metav1.UpdateOptions{})
+func (p *PodDisruptionBudgetService) UpdatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1.PodDisruptionBudget) error {
+	_, err := p.kubeClient.PolicyV1().PodDisruptionBudgets(namespace).Update(context.TODO(), podDisruptionBudget, metav1.UpdateOptions{})
 	if err != nil {
 		return err
 	}
@@ -61,7 +61,7 @@ func (p *PodDisruptionBudgetService) UpdatePodDisruptionBudget(namespace string,
 	return nil
 }
 
-func (p *PodDisruptionBudgetService) CreateOrUpdatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1beta1.PodDisruptionBudget) error {
+func (p *PodDisruptionBudgetService) CreateOrUpdatePodDisruptionBudget(namespace string, podDisruptionBudget *policyv1.PodDisruptionBudget) error {
 	storedPodDisruptionBudget, err := p.GetPodDisruptionBudget(namespace, podDisruptionBudget.Name)
 	if err != nil {
 		// If no resource we need to create.
@@ -80,5 +80,5 @@ func (p *PodDisruptionBudgetService) CreateOrUpdatePodDisruptionBudget(namespace
 }
 
 func (p *PodDisruptionBudgetService) DeletePodDisruptionBudget(namespace string, name string) error {
-	return p.kubeClient.PolicyV1beta1().PodDisruptionBudgets(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
+	return p.kubeClient.PolicyV1().PodDisruptionBudgets(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
 }


Test patch:
diff --git a/service/k8s/poddisruptionbudget_test.go b/service/k8s/poddisruptionbudget_test.go
index 1bf8ed43..c9bf9747 100644
--- a/service/k8s/poddisruptionbudget_test.go
+++ b/service/k8s/poddisruptionbudget_test.go
@@ -5,7 +5,7 @@ import (
 	"testing"
 
 	"github.com/stretchr/testify/assert"
-	policyv1beta1 "k8s.io/api/policy/v1beta1"
+	policyv1 "k8s.io/api/policy/v1"
 	kubeerrors "k8s.io/apimachinery/pkg/api/errors"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/runtime"
@@ -17,11 +17,9 @@ import (
 	"github.com/spotahome/redis-operator/service/k8s"
 )
 
-var (
-	podDisruptionBudgetsGroup = schema.GroupVersionResource{Group: "policy", Version: "v1beta1", Resource: "poddisruptionbudgets"}
-)
+var podDisruptionBudgetsGroup = schema.GroupVersionResource{Group: "policy", Version: "v1", Resource: "poddisruptionbudgets"}
 
-func newPodDisruptionBudgetUpdateAction(ns string, podDisruptionBudget *policyv1beta1.PodDisruptionBudget) kubetesting.UpdateActionImpl {
+func newPodDisruptionBudgetUpdateAction(ns string, podDisruptionBudget *policyv1.PodDisruptionBudget) kubetesting.UpdateActionImpl {
 	return kubetesting.NewUpdateAction(podDisruptionBudgetsGroup, ns, podDisruptionBudget)
 }
 
@@ -29,12 +27,12 @@ func newPodDisruptionBudgetGetAction(ns, name string) kubetesting.GetActionImpl
 	return kubetesting.NewGetAction(podDisruptionBudgetsGroup, ns, name)
 }
 
-func newPodDisruptionBudgetCreateAction(ns string, podDisruptionBudget *policyv1beta1.PodDisruptionBudget) kubetesting.CreateActionImpl {
+func newPodDisruptionBudgetCreateAction(ns string, podDisruptionBudget *policyv1.PodDisruptionBudget) kubetesting.CreateActionImpl {
 	return kubetesting.NewCreateAction(podDisruptionBudgetsGroup, ns, podDisruptionBudget)
 }
 
 func TestPodDisruptionBudgetServiceGetCreateOrUpdate(t *testing.T) {
-	testPodDisruptionBudget := &policyv1beta1.PodDisruptionBudget{
+	testPodDisruptionBudget := &policyv1.PodDisruptionBudget{
 		ObjectMeta: metav1.ObjectMeta{
 			Name:            "testpodDisruptionBudget1",
 			ResourceVersion: "10",
@@ -45,8 +43,8 @@ func TestPodDisruptionBudgetServiceGetCreateOrUpdate(t *testing.T) {
 
 	tests := []struct {
 		name                         string
-		podDisruptionBudget          *policyv1beta1.PodDisruptionBudget
-		getPodDisruptionBudgetResult *policyv1beta1.PodDisruptionBudget
+		podDisruptionBudget          *policyv1.PodDisruptionBudget
+		getPodDisruptionBudgetResult *policyv1.PodDisruptionBudget
 		errorOnGet                   error
 		errorOnCreation              error
 		expActions                   []kubetesting.Action
diff --git a/test/integration/redisfailover/creation_test.go b/test/integration/redisfailover/creation_test.go
index 46ae8cbe..524d3a4b 100644
--- a/test/integration/redisfailover/creation_test.go
+++ b/test/integration/redisfailover/creation_test.go
@@ -152,7 +152,6 @@ func TestRedisFailover(t *testing.T) {
 	// check that all of them are connected to the same Redis node, and also that that node
 	// is the master.
 	t.Run("Check Sentinels Checking the Redis Master", clients.testSentinelMonitoring)
-
 }
 
 func (c *clients) testCRCreation(t *testing.T) {
@@ -253,7 +252,6 @@ func (c *clients) testSentinelMonitoring(t *testing.T) {
 }
 
 func (c *clients) testAuth(t *testing.T) {
-
 	assert := assert.New(t)
 
 	redisCfg, err := c.k8sClient.CoreV1().ConfigMaps(namespace).Get(context.Background(), fmt.Sprintf("rfr-%s", name), metav1.GetOptions{})