Skip to content

Commit

Permalink
Revert "[cleanup] remove unused v1beta1 version of PDB (#1468)" (#1580)…
Browse files Browse the repository at this point in the history
… (#1582)

* Revert "[cleanup] remove unused v1beta1 version of PDB (#1468)"

This reverts commit 614b10f.

* use go convention

* support v1beta1 for pdbs

* go convetion

* Revert "support v1beta1 for pdbs"

This reverts commit 13b2cab.

* Reapply "support v1beta1 for pdbs"

This reverts commit 59715f1.

---------

Co-authored-by: Sarah Wang <[email protected]>
  • Loading branch information
tbavelier and swang392 authored Dec 19, 2024
1 parent e3bd1a3 commit 79d9d52
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/assert"

corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down Expand Up @@ -46,7 +47,7 @@ func Test_getPodDisruptionBudget(t *testing.T) {
Namespace: "some-namespace",
},
}
testpdb := GetClusterAgentPodDisruptionBudget(&dda)
testpdb := GetClusterAgentPodDisruptionBudget(&dda, false).(*policyv1.PodDisruptionBudget)
assert.Equal(t, "my-datadog-agent-cluster-agent-pdb", testpdb.Name)
assert.Equal(t, intstr.FromInt(pdbMinAvailableInstances), *testpdb.Spec.MinAvailable)
assert.Nil(t, testpdb.Spec.MaxUnavailable)
Expand Down
21 changes: 18 additions & 3 deletions internal/controller/datadogagent/component/clusteragent/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
"github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1"
"github.com/DataDog/datadog-operator/internal/controller/datadogagent/object"
"github.com/DataDog/datadog-operator/pkg/controller/utils/comparison"
"sigs.k8s.io/controller-runtime/pkg/client"

corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/version"
Expand Down Expand Up @@ -58,13 +60,27 @@ func GetClusterAgentService(dda metav1.Object) *corev1.Service {
return service
}

func GetClusterAgentPodDisruptionBudget(dda metav1.Object) *policyv1.PodDisruptionBudget {
func GetClusterAgentPodDisruptionBudget(dda metav1.Object, useV1BetaPDB bool) client.Object {
// labels and annotations
minAvailableStr := intstr.FromInt(pdbMinAvailableInstances)
matchLabels := map[string]string{
apicommon.AgentDeploymentNameLabelKey: dda.GetName(),
apicommon.AgentDeploymentComponentLabelKey: v2alpha1.DefaultClusterAgentResourceSuffix}
pdb := &policyv1.PodDisruptionBudget{
if useV1BetaPDB {
return &policyv1beta1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: GetClusterAgentPodDisruptionBudgetName(dda),
Namespace: dda.GetNamespace(),
},
Spec: policyv1beta1.PodDisruptionBudgetSpec{
MinAvailable: &minAvailableStr,
Selector: &metav1.LabelSelector{
MatchLabels: matchLabels,
},
},
}
}
return &policyv1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: GetClusterAgentPodDisruptionBudgetName(dda),
Namespace: dda.GetNamespace(),
Expand All @@ -76,7 +92,6 @@ func GetClusterAgentPodDisruptionBudget(dda metav1.Object) *policyv1.PodDisrupti
},
},
}
return pdb
}

// GetMetricsServerServiceName returns the external metrics provider service name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"

apicommon "github.com/DataDog/datadog-operator/api/datadoghq/common"
"github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1"
Expand Down Expand Up @@ -92,12 +94,26 @@ func GetClusterChecksRunnerPodDisruptionBudgetName(dda metav1.Object) string {
return fmt.Sprintf("%s-%s-pdb", dda.GetName(), v2alpha1.DefaultClusterChecksRunnerResourceSuffix)
}

func GetClusterChecksRunnerPodDisruptionBudget(dda metav1.Object) *policyv1.PodDisruptionBudget {
func GetClusterChecksRunnerPodDisruptionBudget(dda metav1.Object, useV1BetaPDB bool) client.Object {
maxUnavailableStr := intstr.FromInt(pdbMaxUnavailableInstances)
matchLabels := map[string]string{
apicommon.AgentDeploymentNameLabelKey: dda.GetName(),
apicommon.AgentDeploymentComponentLabelKey: v2alpha1.DefaultClusterChecksRunnerResourceSuffix}
pdb := &policyv1.PodDisruptionBudget{
if useV1BetaPDB {
return &policyv1beta1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: GetClusterChecksRunnerPodDisruptionBudgetName(dda),
Namespace: dda.GetNamespace(),
},
Spec: policyv1beta1.PodDisruptionBudgetSpec{
MaxUnavailable: &maxUnavailableStr,
Selector: &metav1.LabelSelector{
MatchLabels: matchLabels,
},
},
}
}
return &policyv1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: GetClusterChecksRunnerPodDisruptionBudgetName(dda),
Namespace: dda.GetNamespace(),
Expand All @@ -109,7 +125,6 @@ func GetClusterChecksRunnerPodDisruptionBudget(dda metav1.Object) *policyv1.PodD
},
},
}
return pdb
}

// getDefaultServiceAccountName return the default Cluster-Agent ServiceAccountName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1"
"github.com/stretchr/testify/assert"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand All @@ -32,7 +33,7 @@ func Test_getPodDisruptionBudget(t *testing.T) {
Namespace: "some-namespace",
},
}
testpdb := GetClusterChecksRunnerPodDisruptionBudget(&dda)
testpdb := GetClusterChecksRunnerPodDisruptionBudget(&dda, false).(*policyv1.PodDisruptionBudget)
assert.Equal(t, "my-datadog-agent-cluster-checks-runner-pdb", testpdb.Name)
assert.Equal(t, intstr.FromInt(pdbMaxUnavailableInstances), *testpdb.Spec.MaxUnavailable)
assert.Nil(t, testpdb.Spec.MinAvailable)
Expand Down
6 changes: 4 additions & 2 deletions internal/controller/datadogagent/override/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ func Dependencies(logger logr.Logger, manager feature.ResourceManagers, dda *v2a

func overridePodDisruptionBudget(logger logr.Logger, manager feature.ResourceManagers, dda *v2alpha1.DatadogAgent, createPdb *bool, component v2alpha1.ComponentName) (errs []error) {
if createPdb != nil && *createPdb {
platformInfo := manager.Store().GetPlatformInfo()
useV1BetaPDB := platformInfo.UseV1Beta1PDB()
if component == v2alpha1.ClusterAgentComponentName {
pdb := componentdca.GetClusterAgentPodDisruptionBudget(dda)
pdb := componentdca.GetClusterAgentPodDisruptionBudget(dda, useV1BetaPDB)
if err := manager.Store().AddOrUpdate(kubernetes.PodDisruptionBudgetsKind, pdb); err != nil {
errs = append(errs, err)
}
} else if component == v2alpha1.ClusterChecksRunnerComponentName &&
(dda.Spec.Features.ClusterChecks.UseClusterChecksRunners == nil ||
*dda.Spec.Features.ClusterChecks.UseClusterChecksRunners) {
pdb := componentccr.GetClusterChecksRunnerPodDisruptionBudget(dda)
pdb := componentccr.GetClusterChecksRunnerPodDisruptionBudget(dda, useV1BetaPDB)
if err := manager.Store().AddOrUpdate(kubernetes.PodDisruptionBudgetsKind, pdb); err != nil {
errs = append(errs, err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/datadogagent/testutils/client_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
policyv1 "k8s.io/api/policy/v1"
policyv1 "k8s.io/api/policy/v1beta1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/datadogagent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
policyv1 "k8s.io/api/policy/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -195,7 +194,8 @@ func (r *DatadogAgentReconciler) SetupWithManager(mgr ctrl.Manager, metricForwar
Owns(&rbacv1.Role{}).
Owns(&rbacv1.RoleBinding{}).
Owns(&corev1.ServiceAccount{}).
Owns(&policyv1.PodDisruptionBudget{}).
// We let PlatformInfo supply PDB object based on the current API version
Owns(r.PlatformInfo.CreatePDBObject()).
Owns(&networkingv1.NetworkPolicy{})

if r.Options.DatadogAgentProfileEnabled {
Expand Down
8 changes: 8 additions & 0 deletions pkg/equality/equality.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
policyv1 "k8s.io/api/policy/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
rbacv1 "k8s.io/api/rbac/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -181,9 +182,16 @@ func IsEqualServiceAccounts(objA, objB client.Object) bool {
func IsEqualPodDisruptionBudgets(objA, objB client.Object) bool {
a, okA := objA.(*policyv1.PodDisruptionBudget)
b, okB := objB.(*policyv1.PodDisruptionBudget)

if okA && okB && a != nil && b != nil {
return apiequality.Semantic.DeepEqual(a.Spec, b.Spec)
}
ax, okA := objA.(*policyv1beta1.PodDisruptionBudget)
bx, okB := objB.(*policyv1beta1.PodDisruptionBudget)
if okA && okB && ax != nil && bx != nil {
return apiequality.Semantic.DeepEqual(ax.Spec, bx.Spec)
}

return false
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/kubernetes/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
policyv1 "k8s.io/api/policy/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -43,7 +42,7 @@ func ObjectFromKind(kind ObjectKind, platformInfo PlatformInfo) client.Object {
case ServiceAccountsKind:
return &corev1.ServiceAccount{}
case PodDisruptionBudgetsKind:
return &policyv1.PodDisruptionBudget{}
return platformInfo.CreatePDBObject()
case NetworkPoliciesKind:
return &networkingv1.NetworkPolicy{}
case CiliumNetworkPoliciesKind:
Expand Down
3 changes: 1 addition & 2 deletions pkg/kubernetes/objectslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
policyv1 "k8s.io/api/policy/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -43,7 +42,7 @@ func ObjectListFromKind(kind ObjectKind, platformInfo PlatformInfo) client.Objec
case ServiceAccountsKind:
return &corev1.ServiceAccountList{}
case PodDisruptionBudgetsKind:
return &policyv1.PodDisruptionBudgetList{}
return platformInfo.CreatePDBObjectList()
case NetworkPoliciesKind:
return &networkingv1.NetworkPolicyList{}
case CiliumNetworkPoliciesKind:
Expand Down
24 changes: 24 additions & 0 deletions pkg/kubernetes/platforminfo.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package kubernetes

import (
policyv1 "k8s.io/api/policy/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/version"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type PlatformInfo struct {
Expand Down Expand Up @@ -58,6 +61,27 @@ func NewPlatformInfoFromVersionMaps(versionInfo *version.Info, apiPreferredVersi
}
}

func (platformInfo *PlatformInfo) UseV1Beta1PDB() bool {
preferredVersion := platformInfo.apiPreferredVersions["PodDisruptionBudget"]

// If policy isn't v1beta1 version, we default to v1.
return preferredVersion == "policy/v1beta1"
}

func (platformInfo *PlatformInfo) CreatePDBObject() client.Object {
if platformInfo.UseV1Beta1PDB() {
return &policyv1beta1.PodDisruptionBudget{}
}
return &policyv1.PodDisruptionBudget{}
}

func (platformInfo *PlatformInfo) CreatePDBObjectList() client.ObjectList {
if platformInfo.UseV1Beta1PDB() {
return &policyv1beta1.PodDisruptionBudgetList{}
}
return &policyv1.PodDisruptionBudgetList{}
}

func (platformInfo *PlatformInfo) GetAgentResourcesKind(withCiliumResources bool) []ObjectKind {
return getResourcesKind(withCiliumResources)
}
Expand Down
Loading

0 comments on commit 79d9d52

Please sign in to comment.