Skip to content

Commit

Permalink
fix: support pdb in kube 1.20
Browse files Browse the repository at this point in the history
Signed-off-by: Rory Z <[email protected]>
  • Loading branch information
Rory-Z committed Apr 19, 2024
1 parent 2cf7e02 commit 264823a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .github/workflows/cts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
- minikube
- k3s
kubernetes-version:
- "v1.20.0"
- "v1.21.0"
- "v1.22.0"
- "v1.23.0"
- "v1.24.0"
Expand Down Expand Up @@ -100,6 +102,8 @@ jobs:
- minikube
- k3s
kubernetes-version:
- "v1.20.0"
- "v1.21.0"
- "v1.22.0"
- "v1.23.0"
- "v1.24.0"
Expand Down
22 changes: 21 additions & 1 deletion controllers/apps/v2beta1/add_pdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,42 @@ import (
"context"

emperror "emperror.dev/errors"
semver "github.com/Masterminds/semver/v3"
appsv2beta1 "github.com/emqx/emqx-operator/apis/apps/v2beta1"
innerReq "github.com/emqx/emqx-operator/internal/requester"
"github.com/go-logr/logr"
policyv1 "k8s.io/api/policy/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/discovery"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
kubeConfig "sigs.k8s.io/controller-runtime/pkg/client/config"
)

type addPdb struct {
*EMQXReconciler
}

func (a *addPdb) reconcile(ctx context.Context, logger logr.Logger, instance *appsv2beta1.EMQX, _ innerReq.RequesterInterface) subResult {
pdbList := generatePodDisruptionBudget(instance)
cfg := kubeConfig.GetConfigOrDie()
discoveryClient, _ := discovery.NewDiscoveryClientForConfig(cfg)
kubeVersion, _ := discoveryClient.ServerVersion()
v, _ := semver.NewVersion(kubeVersion.String())

pdbList := []client.Object{}
for _, p := range generatePodDisruptionBudget(instance) {
if v.LessThan(semver.MustParse("1.20")) {
beta := policyv1beta1.PodDisruptionBudget{}
structAssign(&beta, p.DeepCopy())
pdbList = append(pdbList, &beta)
continue
}
pdbList = append(pdbList, p)
}

for _, pdb := range pdbList {
if err := ctrl.SetControllerReference(instance, pdb, a.Scheme); err != nil {
return subResult{err: emperror.Wrap(err, "failed to set controller reference")}
Expand Down
23 changes: 23 additions & 0 deletions controllers/apps/v2beta1/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"hash"
"hash/fnv"
"reflect"
"sort"
"time"

Expand All @@ -24,6 +25,28 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

// structAssign copy the value of struct from src to dist.
func structAssign(dist, src interface{}) {
distVal := reflect.ValueOf(dist).Elem()
srcVal := reflect.ValueOf(src).Elem()
srcType := srcVal.Type()

for i := 0; i < srcVal.NumField(); i++ {
if srcVal.Field(i).IsZero() {
continue
}

// Check if the dist has the same field.
name := srcType.Field(i).Name
distValField := distVal.FieldByName(name)
if ok := distValField.IsValid(); !ok {
continue
}

distValField.Set(srcVal.Field(i))
}
}

func getRsPodMap(ctx context.Context, k8sClient client.Client, instance *appsv2beta1.EMQX) map[types.UID][]*corev1.Pod {
labels := appsv2beta1.DefaultReplicantLabels(instance)

Expand Down

0 comments on commit 264823a

Please sign in to comment.