Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support pdb in kube 1.20 #1046

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading