Skip to content

Commit a575def

Browse files
authored
fix: unit test issue, k8s 1.20-1.22 compatibility (#311)
* fix: unit test issue, k8s 1.20-1.22 compatibility * fix: lint issue
1 parent 8d314ce commit a575def

13 files changed

+192
-2
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ prompts/*
3535

3636
tmp*
3737

38-
__debug*
38+
__debug*
39+
40+
vendor

cmd/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2929
// to ensure that exec-entrypoint and run can make use of them.
3030

31+
"k8s.io/client-go/kubernetes"
3132
_ "k8s.io/client-go/plugin/pkg/client/auth"
3233
"k8s.io/client-go/rest"
3334
"k8s.io/klog/v2"
@@ -187,6 +188,21 @@ func main() {
187188
os.Exit(1)
188189
}
189190

191+
k8sClient, err := kubernetes.NewForConfig(kc)
192+
if err != nil {
193+
setupLog.Error(err, "unable to create k8s client")
194+
os.Exit(1)
195+
}
196+
version, err := k8sClient.Discovery().ServerVersion()
197+
if err != nil {
198+
setupLog.Error(err, "unable to get k8s version")
199+
os.Exit(1)
200+
}
201+
// set env for feature gating, so that to be compatible with different k8s version
202+
setupLog.Info("detected API server version for feature gating", "version", version.String())
203+
_ = os.Setenv(constants.KubeApiVersionMajorEnv, version.Major)
204+
_ = os.Setenv(constants.KubeApiVersionMinorEnv, version.Minor)
205+
190206
setupTimeSeriesAndWatchGlobalConfigChanges(ctx, mgr)
191207

192208
if autoScaleCanBeEnabled && enableAutoScale {

dockerfile/operator.Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ COPY go.mod go.mod
1010
COPY go.sum go.sum
1111
# cache deps before building and copying source so that we don't need to re-download as much
1212
# and so that source changes don't invalidate our downloaded layer
13-
RUN go mod download
13+
RUN go mod vendor
1414

1515
# Copy the go source
1616
COPY cmd/ cmd/
@@ -19,6 +19,9 @@ COPY internal/ internal/
1919
# Copy .git directory to enable VCS info in build
2020
COPY .git/ .git/
2121

22+
COPY scripts/ scripts/
23+
COPY patches/ patches/
24+
RUN bash ./scripts/patch-scheduler.sh
2225

2326
# Build
2427
# the GOARCH has not a default value to allow the binary be built according to the host where the command

internal/constants/env.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,8 @@ const (
142142
NodeDiscoveryReportGPUNodeEnvName = "NODE_DISCOVERY_REPORT_GPU_NODE"
143143
NodeDiscoveryHostNameEnv = "HOSTNAME"
144144
)
145+
146+
const (
147+
KubeApiVersionMajorEnv = "KUBE_API_VERSION_MAJOR"
148+
KubeApiVersionMinorEnv = "KUBE_API_VERSION_MINOR"
149+
)

internal/controller/pod_controller_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,17 @@ var _ = Describe("Pod Controller", func() {
8282
Image: "test-image",
8383
},
8484
},
85+
TerminationGracePeriodSeconds: ptr.To(int64(0)),
8586
},
8687
}
8788
})
8889

8990
AfterEach(func() {
9091
if workerPod != nil {
9192
_ = k8sClient.Delete(ctx, workerPod)
93+
Eventually(func() error {
94+
return k8sClient.Get(ctx, client.ObjectKeyFromObject(workerPod), workerPod)
95+
}).Should(Satisfy(errors.IsNotFound))
9296
}
9397
})
9498

@@ -394,9 +398,15 @@ var _ = Describe("Pod Controller", func() {
394398
AfterEach(func() {
395399
if workload != nil {
396400
_ = k8sClient.Delete(ctx, workload)
401+
Eventually(func() error {
402+
return k8sClient.Get(ctx, client.ObjectKeyFromObject(workload), workload)
403+
}).Should(Satisfy(errors.IsNotFound))
397404
}
398405
if pod != nil {
399406
_ = k8sClient.Delete(ctx, pod)
407+
Eventually(func() error {
408+
return k8sClient.Get(ctx, client.ObjectKeyFromObject(pod), pod)
409+
}).Should(Satisfy(errors.IsNotFound))
400410
}
401411
})
402412

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--- ../vendor/k8s.io/kubernetes/pkg/scheduler/framework/plugins/volumebinding/volume_binding.go 2025-08-06 16:50:24
2+
+++ ../vendor/k8s.io/kubernetes/pkg/scheduler/framework/plugins/volumebinding/volume_binding.go 2025-08-06 16:51:16
3+
@@ -20,6 +20,8 @@
4+
"context"
5+
"errors"
6+
"fmt"
7+
+ "os"
8+
+ "strconv"
9+
"sync"
10+
"time"
11+
12+
@@ -603,9 +605,18 @@
13+
storageClassInformer := fh.SharedInformerFactory().Storage().V1().StorageClasses()
14+
csiNodeInformer := fh.SharedInformerFactory().Storage().V1().CSINodes()
15+
capacityCheck := CapacityCheck{
16+
- CSIDriverInformer: fh.SharedInformerFactory().Storage().V1().CSIDrivers(),
17+
- CSIStorageCapacityInformer: fh.SharedInformerFactory().Storage().V1().CSIStorageCapacities(),
18+
+ CSIDriverInformer: fh.SharedInformerFactory().Storage().V1().CSIDrivers(),
19+
}
20+
+
21+
+ // FIX kubernetes 1.24 and lower version API missing issue
22+
+ minorVersionStr := os.Getenv("KUBE_API_VERSION_MINOR")
23+
+ if minorVersionStr != "" {
24+
+ minorVersion, err := strconv.Atoi(minorVersionStr)
25+
+ if err == nil && minorVersion >= 24 {
26+
+ capacityCheck.CSIStorageCapacityInformer = fh.SharedInformerFactory().Storage().V1().CSIStorageCapacities()
27+
+ }
28+
+ }
29+
+
30+
binder := NewVolumeBinder(klog.FromContext(ctx), fh.ClientSet(), fts, podInformer, nodeInformer, csiNodeInformer, pvcInformer, pvInformer, storageClassInformer, capacityCheck, time.Duration(args.BindTimeoutSeconds)*time.Second)
31+
32+
// build score function
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--- ../vendor/k8s.io/kubernetes/pkg/scheduler/framework/plugins/volumebinding/binder.go 2025-08-06 17:00:36
2+
+++ ../vendor/k8s.io/kubernetes/pkg/scheduler/framework/plugins/volumebinding/binder.go 2025-08-06 17:00:27
3+
@@ -269,7 +269,11 @@
4+
}
5+
6+
b.csiDriverLister = capacityCheck.CSIDriverInformer.Lister()
7+
- b.csiStorageCapacityLister = capacityCheck.CSIStorageCapacityInformer.Lister()
8+
+
9+
+ // FIX kubernetes 1.24 and lower version API missing issue
10+
+ if capacityCheck.CSIStorageCapacityInformer != nil {
11+
+ b.csiStorageCapacityLister = capacityCheck.CSIStorageCapacityInformer.Lister()
12+
+ }
13+
14+
return b
15+
}
16+
@@ -988,6 +992,11 @@
17+
return true, nil, nil
18+
}
19+
20+
+ // FIX kubernetes 1.24 and lower version API missing issue
21+
+ if b.csiStorageCapacityLister == nil {
22+
+ return true, nil, nil
23+
+ }
24+
+
25+
// Look for a matching CSIStorageCapacity object(s).
26+
// TODO (for beta): benchmark this and potentially introduce some kind of lookup structure (https://github.com/kubernetes/enhancements/issues/1698#issuecomment-654356718).
27+
capacities, err := b.csiStorageCapacityLister.List(labels.Everything())
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--- ../vendor/k8s.io/kubernetes/pkg/scheduler/eventhandlers.go 2025-08-06 17:55:42
2+
+++ ../vendor/k8s.io/kubernetes/pkg/scheduler/eventhandlers.go 2025-08-06 17:56:13
3+
@@ -19,6 +19,8 @@
4+
import (
5+
"context"
6+
"fmt"
7+
+ "os"
8+
+ "strconv"
9+
"strings"
10+
"time"
11+
12+
@@ -514,6 +516,14 @@
13+
}
14+
handlers = append(handlers, handlerRegistration)
15+
case framework.CSIStorageCapacity:
16+
+ // FIX kubernetes 1.24 and lower version API missing issue
17+
+ minorVersionStr := os.Getenv("KUBE_API_VERSION_MINOR")
18+
+ if minorVersionStr != "" {
19+
+ minorVersion, err := strconv.Atoi(minorVersionStr)
20+
+ if err != nil || minorVersion < 24 {
21+
+ continue
22+
+ }
23+
+ }
24+
if handlerRegistration, err = informerFactory.Storage().V1().CSIStorageCapacities().Informer().AddEventHandler(
25+
buildEvtResHandler(at, framework.CSIStorageCapacity),
26+
); err != nil {

patches/scheduler-pdb-1.patch

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--- ../vendor/k8s.io/kubernetes/pkg/scheduler/framework/plugins/defaultpreemption/default_preemption.go 2025-08-06 17:45:27
2+
+++ ../vendor/k8s.io/kubernetes/pkg/scheduler/framework/plugins/defaultpreemption/default_preemption.go 2025-08-06 17:45:19
3+
@@ -20,7 +20,9 @@
4+
"context"
5+
"fmt"
6+
"math/rand"
7+
+ "os"
8+
"sort"
9+
+ "strconv"
10+
11+
v1 "k8s.io/api/core/v1"
12+
policy "k8s.io/api/policy/v1"
13+
@@ -364,5 +366,13 @@
14+
}
15+
16+
func getPDBLister(informerFactory informers.SharedInformerFactory) policylisters.PodDisruptionBudgetLister {
17+
+ // FIX kubernetes 1.21 and lower version API missing issue
18+
+ minorVersionStr := os.Getenv("KUBE_API_VERSION_MINOR")
19+
+ if minorVersionStr != "" {
20+
+ minorVersion, err := strconv.Atoi(minorVersionStr)
21+
+ if err != nil || minorVersion < 21 {
22+
+ return nil
23+
+ }
24+
+ }
25+
return informerFactory.Policy().V1().PodDisruptionBudgets().Lister()
26+
}

patches/scheduler-pdb-2.patch

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--- ../vendor/k8s.io/kubernetes/pkg/scheduler/framework/preemption/preemption.go 2025-08-06 17:48:23
2+
+++ ../vendor/k8s.io/kubernetes/pkg/scheduler/framework/preemption/preemption.go 2025-08-06 17:48:13
3+
@@ -21,6 +21,8 @@
4+
"errors"
5+
"fmt"
6+
"math"
7+
+ "os"
8+
+ "strconv"
9+
"sync"
10+
"sync/atomic"
11+
"time"
12+
@@ -34,6 +36,7 @@
13+
"k8s.io/apimachinery/pkg/util/sets"
14+
corelisters "k8s.io/client-go/listers/core/v1"
15+
policylisters "k8s.io/client-go/listers/policy/v1"
16+
+ policyv1 "k8s.io/client-go/listers/policy/v1"
17+
corev1helpers "k8s.io/component-helpers/scheduling/corev1"
18+
"k8s.io/klog/v2"
19+
extenderv1 "k8s.io/kube-scheduler/extender/v1"
20+
@@ -145,7 +148,16 @@
21+
22+
func NewEvaluator(pluginName string, fh framework.Handle, i Interface, enableAsyncPreemption bool) *Evaluator {
23+
podLister := fh.SharedInformerFactory().Core().V1().Pods().Lister()
24+
- pdbLister := fh.SharedInformerFactory().Policy().V1().PodDisruptionBudgets().Lister()
25+
+
26+
+ // FIX kubernetes 1.21 and lower version API missing issue
27+
+ var pdbLister policyv1.PodDisruptionBudgetLister
28+
+ minorVersionStr := os.Getenv("KUBE_API_VERSION_MINOR")
29+
+ if minorVersionStr != "" {
30+
+ minorVersion, err := strconv.Atoi(minorVersionStr)
31+
+ if err == nil && minorVersion >= 21 {
32+
+ pdbLister = fh.SharedInformerFactory().Policy().V1().PodDisruptionBudgets().Lister()
33+
+ }
34+
+ }
35+
36+
ev := &Evaluator{
37+
PluginName: pluginName,

0 commit comments

Comments
 (0)