|
| 1 | +/* |
| 2 | +Copyright 2025 The Fluid Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package compatibility |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/blang/semver/v4" |
| 21 | + |
| 22 | + nativeLog "log" |
| 23 | + "sync" |
| 24 | + |
| 25 | + "k8s.io/apimachinery/pkg/api/errors" |
| 26 | + "k8s.io/client-go/discovery" |
| 27 | + ctrl "sigs.k8s.io/controller-runtime" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + nodeBindingTokenSupported = false |
| 32 | + nodeBindingTokenOnce sync.Once |
| 33 | +) |
| 34 | + |
| 35 | +// Beta release, default enabled. see https://github.com/kubernetes/enhancements/issues/4193 |
| 36 | +const nodeBindingTokenSupportedVersion = "v1.30.0" |
| 37 | + |
| 38 | +// Checks the ServiceAccountTokenPodNodeInfo feature gate, whether the apiserver embeds the node name for the associated node when issuing service account tokens bound to Pod objects. |
| 39 | +func discoverNodeBindingTokenCompatibility() { |
| 40 | + nativeLog.Printf("Discovering k8s version to check NodeBindingToken compatibility...") |
| 41 | + restConfig := ctrl.GetConfigOrDie() |
| 42 | + discoveryClient := discovery.NewDiscoveryClientForConfigOrDie(restConfig) |
| 43 | + |
| 44 | + serverVersion, err := discoveryClient.ServerVersion() |
| 45 | + if err != nil && !errors.IsNotFound(err) { |
| 46 | + nativeLog.Fatalf("failed to discover batch/v1 group version: %v", err) |
| 47 | + } |
| 48 | + // transform to semver.Version and compare |
| 49 | + currentVersion, err := semver.ParseTolerant(serverVersion.GitVersion) |
| 50 | + if err != nil { |
| 51 | + nativeLog.Fatalf("Failed to parse current version: %v", err) |
| 52 | + } |
| 53 | + targetVersion, err := semver.ParseTolerant(nodeBindingTokenSupportedVersion) |
| 54 | + if err != nil { |
| 55 | + nativeLog.Fatalf("Failed to parse target version: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + if currentVersion.GTE(targetVersion) { |
| 59 | + nodeBindingTokenSupported = true |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func IsNodeBindingTokenSupported() bool { |
| 64 | + nodeBindingTokenOnce.Do(func() { |
| 65 | + discoverNodeBindingTokenCompatibility() |
| 66 | + }) |
| 67 | + return nodeBindingTokenSupported |
| 68 | +} |
0 commit comments