Repository: pravega/zookeeper-operator
Pull request #523 (2023-01-05 21:12:49)

feat: Replace deprecated policy/v1beta1 PDB API
### Change log description
Replaces the deprecated PodDistruptionBudget policy/v1beta1 API with the new policy/v1 API.

### Purpose of the change

Closes https://github.com/pravega/zookeeper-operator/issues/428

### What the code does

Replaces the policy/v1beta1 API by simply changing the API version, there are no schema changes to the API, only a behavioral change - see https://kubernetes.io/docs/reference/using-api/deprecation-guide/#poddisruptionbudget-v125.

### How to verify it

A simple verification would be to manually create a ZK cluster on a 1.25 Kubernetes cluster and verify that the PDB created is using the new API. The e2e tests seem to cover this though so those seem to be sufficient. 


codecov[bot]: # [Codecov](https://codecov.io/gh/pravega/zookeeper-operator/pull/523?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pravega) Report
Base: **85.05**% // Head: **85.05**% // No change to project coverage :thumbsup:
> Coverage data is based on head [(`79ac06f`)](https://codecov.io/gh/pravega/zookeeper-operator/pull/523?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pravega) compared to base [(`c29d475`)](https://codecov.io/gh/pravega/zookeeper-operator/commit/c29d475794226ce38de021d72430ccde75a38d2a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pravega).
> Patch coverage: 100.00% of modified lines in pull request are covered.

> :exclamation: Current head 79ac06f differs from pull request most recent head db776d3. Consider uploading reports for the commit db776d3 to get more accurate results

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##           master     #523   +/-   ##
=======================================
  Coverage   85.05%   85.05%           
=======================================
  Files          12       12           
  Lines        1606     1606           
=======================================
  Hits         1366     1366           
  Misses        155      155           
  Partials       85       85           
```


| [Impacted Files](https://codecov.io/gh/pravega/zookeeper-operator/pull/523?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pravega) | Coverage Δ | |
|---|---|---|
| [controllers/zookeepercluster\_controller.go](https://codecov.io/gh/pravega/zookeeper-operator/pull/523?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pravega#diff-Y29udHJvbGxlcnMvem9va2VlcGVyY2x1c3Rlcl9jb250cm9sbGVyLmdv) | `65.46% <100.00%> (ø)` | |
| [pkg/zk/generators.go](https://codecov.io/gh/pravega/zookeeper-operator/pull/523?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pravega#diff-cGtnL3prL2dlbmVyYXRvcnMuZ28=) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pravega). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pravega)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/pravega/zookeeper-operator/pull/523?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pravega).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pravega).

cbuto: @anishakj would you be able to take a look at this PR in order to support Kubernetes 1.25+? 
anishakj: @cbuto Thanks for the PR. Could you please verify that operator upgrade is working fine?
cbuto: > @cbuto Thanks for the PR. Could you please verify that operator upgrade is working fine?

@anishakj yep! I tested a simple upgrade path and everything worked fine. Since there isn't any difference between the APIs schemas, the `policy/v1beta1` are actually served as `policy/v1` and the only thing that changes is the `apiVersion`. 

One more thing related to upgrades is the `policy/v1` API is only available in Kubernetes 1.21+ so that will be the min version support. 

Patch:
diff --git a/controllers/zookeepercluster_controller.go b/controllers/zookeepercluster_controller.go
index 934f405..eb62413 100644
--- a/controllers/zookeepercluster_controller.go
+++ b/controllers/zookeepercluster_controller.go
@@ -15,19 +15,19 @@ import (
 	"strconv"
 	"time"
 
+	"k8s.io/client-go/kubernetes/scheme"
+	"sigs.k8s.io/controller-runtime/pkg/client/fake"
+	"sigs.k8s.io/controller-runtime/pkg/predicate"
+
 	"github.com/pravega/zookeeper-operator/pkg/controller/config"
 	"github.com/pravega/zookeeper-operator/pkg/utils"
 	"github.com/pravega/zookeeper-operator/pkg/yamlexporter"
 	"github.com/pravega/zookeeper-operator/pkg/zk"
-	"k8s.io/client-go/kubernetes/scheme"
-	"sigs.k8s.io/controller-runtime/pkg/client/fake"
-	"sigs.k8s.io/controller-runtime/pkg/predicate"
 
 	"github.com/go-logr/logr"
-	zookeeperv1beta1 "github.com/pravega/zookeeper-operator/api/v1beta1"
 	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/errors"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/labels"
@@ -38,6 +38,8 @@ import (
 	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
 	logf "sigs.k8s.io/controller-runtime/pkg/log"
 	"sigs.k8s.io/controller-runtime/pkg/reconcile"
+
+	zookeeperv1beta1 "github.com/pravega/zookeeper-operator/api/v1beta1"
 )
 
 // ReconcileTime is the delay between reconciliations
@@ -467,7 +469,7 @@ func (r *ZookeeperClusterReconciler) reconcilePodDisruptionBudget(instance *zook
 	if err = controllerutil.SetControllerReference(instance, pdb, r.Scheme); err != nil {
 		return err
 	}
-	foundPdb := &policyv1beta1.PodDisruptionBudget{}
+	foundPdb := &policyv1.PodDisruptionBudget{}
 	err = r.Client.Get(context.TODO(), types.NamespacedName{
 		Name:      pdb.Name,
 		Namespace: pdb.Namespace,
diff --git a/pkg/zk/generators.go b/pkg/zk/generators.go
index 25da7f9..07720c1 100644
--- a/pkg/zk/generators.go
+++ b/pkg/zk/generators.go
@@ -16,12 +16,13 @@ import (
 	"strconv"
 	"strings"
 
-	"github.com/pravega/zookeeper-operator/api/v1beta1"
 	appsv1 "k8s.io/api/apps/v1"
 	v1 "k8s.io/api/core/v1"
-	policyv1beta1 "k8s.io/api/policy/v1beta1"
+	policyv1 "k8s.io/api/policy/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/util/intstr"
+
+	"github.com/pravega/zookeeper-operator/api/v1beta1"
 )
 
 const (
@@ -352,19 +353,19 @@ func makeService(name string, ports []v1.ServicePort, clusterIP bool, external b
 }
 
 // MakePodDisruptionBudget returns a pdb for the zookeeper cluster
-func MakePodDisruptionBudget(z *v1beta1.ZookeeperCluster) *policyv1beta1.PodDisruptionBudget {
+func MakePodDisruptionBudget(z *v1beta1.ZookeeperCluster) *policyv1.PodDisruptionBudget {
 	pdbCount := intstr.FromInt(int(z.Spec.MaxUnavailableReplicas))
-	return &policyv1beta1.PodDisruptionBudget{
+	return &policyv1.PodDisruptionBudget{
 		TypeMeta: metav1.TypeMeta{
 			Kind:       "PodDisruptionBudget",
-			APIVersion: "policy/v1beta1",
+			APIVersion: "policy/v1",
 		},
 		ObjectMeta: metav1.ObjectMeta{
 			Name:      z.GetName(),
 			Namespace: z.Namespace,
 			Labels:    z.Spec.Labels,
 		},
-		Spec: policyv1beta1.PodDisruptionBudgetSpec{
+		Spec: policyv1.PodDisruptionBudgetSpec{
 			MaxUnavailable: &pdbCount,
 			Selector: &metav1.LabelSelector{
 				MatchLabels: map[string]string{


Test patch:
diff --git a/controllers/zookeepercluster_controller_test.go b/controllers/zookeepercluster_controller_test.go
index fe530db..712cccf 100644
--- a/controllers/zookeepercluster_controller_test.go
+++ b/controllers/zookeepercluster_controller_test.go
@@ -16,12 +16,9 @@ import (
 	"testing"
 	"time"
 
-	"github.com/pravega/zookeeper-operator/api/v1beta1"
-	"github.com/pravega/zookeeper-operator/pkg/controller/config"
-	"github.com/pravega/zookeeper-operator/pkg/zk"
 	appsv1 "k8s.io/api/apps/v1"
 	corev1 "k8s.io/api/core/v1"
-	policyv1beta1 "k8s.io/api/policy/v1beta1"
+	policyv1 "k8s.io/api/policy/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/types"
 	"k8s.io/client-go/kubernetes/scheme"
@@ -29,6 +26,10 @@ import (
 	"sigs.k8s.io/controller-runtime/pkg/client/fake"
 	"sigs.k8s.io/controller-runtime/pkg/reconcile"
 
+	"github.com/pravega/zookeeper-operator/api/v1beta1"
+	"github.com/pravega/zookeeper-operator/pkg/controller/config"
+	"github.com/pravega/zookeeper-operator/pkg/zk"
+
 	. "github.com/onsi/ginkgo"
 	. "github.com/onsi/gomega"
 )
@@ -185,7 +186,7 @@ var _ = Describe("ZookeeperCluster Controller", func() {
 			})
 
 			It("should create a pdb", func() {
-				foundPdb := &policyv1beta1.PodDisruptionBudget{}
+				foundPdb := &policyv1.PodDisruptionBudget{}
 				err = cl.Get(context.TODO(), req.NamespacedName, foundPdb)
 				Ω(err).To(BeNil())
 			})
diff --git a/pkg/zk/generators_test.go b/pkg/zk/generators_test.go
index 7fdda6a..a077bb9 100644
--- a/pkg/zk/generators_test.go
+++ b/pkg/zk/generators_test.go
@@ -12,18 +12,18 @@ package zk_test
 
 import (
 	"fmt"
+	"strings"
 
 	log "github.com/sirupsen/logrus"
+	policyv1 "k8s.io/api/policy/v1"
 
-	"strings"
+	appsv1 "k8s.io/api/apps/v1"
+	v1 "k8s.io/api/core/v1"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 
 	"github.com/pravega/zookeeper-operator/api/v1beta1"
 	"github.com/pravega/zookeeper-operator/pkg/utils"
 	"github.com/pravega/zookeeper-operator/pkg/zk"
-	appsv1 "k8s.io/api/apps/v1"
-	v1 "k8s.io/api/core/v1"
-	policyv1beta1 "k8s.io/api/policy/v1beta1"
-	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 
 	. "github.com/onsi/ginkgo"
 	. "github.com/onsi/gomega"
@@ -560,7 +560,7 @@ var _ = Describe("Generators Spec", func() {
 	})
 
 	Context("#MakePodDisruptionBudget", func() {
-		var pdb *policyv1beta1.PodDisruptionBudget
+		var pdb *policyv1.PodDisruptionBudget
 		var domainName string
 		var zkClusterName string