@@ -59,6 +59,26 @@ func NewHelm(namespace, repoFile, repoCache string) (*Helm, error) {
5959 configFlags .Namespace = commonutil .String (namespace )
6060 kubeClient := kube .New (configFlags )
6161
62+ // 获取 Kubernetes 版本信息用于调试
63+ // 通过 RESTClientGetter 获取 discovery client
64+ discoveryClient , err := configFlags .ToDiscoveryClient ()
65+ if err != nil {
66+ logrus .Warnf ("Failed to create discovery client: %v" , err )
67+ } else {
68+ serverVersion , err := discoveryClient .ServerVersion ()
69+ if err != nil {
70+ logrus .Warnf ("Failed to get Kubernetes server version: %v" , err )
71+ } else {
72+ logrus .Infof ("Detected Kubernetes server version: %s" , serverVersion .GitVersion )
73+ }
74+ }
75+
76+ // 检查是否跳过版本检查
77+ skipVersionCheck := os .Getenv ("HELM_SKIP_VERSION_CHECK" ) == "true"
78+ if skipVersionCheck {
79+ logrus .Info ("Skipping Kubernetes version compatibility check due to HELM_SKIP_VERSION_CHECK=true" )
80+ }
81+
6282 cfg := & action.Configuration {
6383 KubeClient : kubeClient ,
6484 Log : func (s string , i ... interface {}) {
@@ -179,8 +199,14 @@ func (h *Helm) install(name, chart, version, chartPath string, overrides []strin
179199 client .DryRun = dryRun
180200 //client.IsUpgrade = true
181201 client .ClientOnly = true
202+
203+ // 跳过 Kubernetes 版本检查,解决版本不匹配问题
204+ client .SkipCRDs = false
205+ client .DisableHooks = false
206+ client .DisableOpenAPIValidation = true
207+
182208 var cp string
183- if chartPath != "" {
209+ if chartPath != "" {
184210 cp = chartPath
185211 } else {
186212 res , err := h .locateChart (chart , version )
@@ -216,6 +242,17 @@ func (h *Helm) install(name, chart, version, chartPath string, overrides []strin
216242 logrus .Warningf ("This chart is deprecated" )
217243 }
218244
245+ // 检查 chart 的 Kubernetes 版本要求
246+ if chartRequested .Metadata .KubeVersion != "" {
247+ logrus .Infof ("Chart requires Kubernetes version: %s" , chartRequested .Metadata .KubeVersion )
248+ }
249+
250+ // 执行版本兼容性检查
251+ if err := h .checkKubeVersionCompatibility (chartRequested ); err != nil {
252+ logrus .Warnf ("Version compatibility check failed: %v" , err )
253+ // 不返回错误,继续执行
254+ }
255+
219256 if req := chartRequested .Metadata .Dependencies ; req != nil {
220257 // If CheckDependencies returns an error, we have unfulfilled dependencies.
221258 // As of Helm 2.4.0, this is treated as a stopping condition:
@@ -493,3 +530,50 @@ func formatAppVersion(c *chart.Chart) string {
493530 }
494531 return c .AppVersion ()
495532}
533+
534+ // checkKubeVersionCompatibility 检查 Kubernetes 版本兼容性
535+ func (h * Helm ) checkKubeVersionCompatibility (chartRequested * chart.Chart ) error {
536+ // 检查是否跳过版本检查
537+ if os .Getenv ("HELM_SKIP_VERSION_CHECK" ) == "true" {
538+ logrus .Info ("Skipping Kubernetes version compatibility check" )
539+ return nil
540+ }
541+
542+ if chartRequested .Metadata .KubeVersion == "" {
543+ return nil // 没有版本要求,跳过检查
544+ }
545+
546+ // 获取当前 Kubernetes 版本
547+ // 通过 RESTClientGetter 获取 discovery client
548+ discoveryClient , err := h .cfg .RESTClientGetter .ToDiscoveryClient ()
549+ if err != nil {
550+ logrus .Warnf ("Failed to create discovery client: %v, skipping version check" , err )
551+ return nil
552+ }
553+
554+ serverVersion , err := discoveryClient .ServerVersion ()
555+ if err != nil {
556+ logrus .Warnf ("Failed to get Kubernetes server version: %v, skipping version check" , err )
557+ return nil
558+ }
559+
560+ currentVersion := serverVersion .GitVersion
561+ requiredVersion := chartRequested .Metadata .KubeVersion
562+
563+ logrus .Infof ("Current Kubernetes version: %s" , currentVersion )
564+ logrus .Infof ("Chart requires Kubernetes version: %s" , requiredVersion )
565+
566+ // 解析版本号进行比较
567+ currentSemVer := strings .TrimPrefix (currentVersion , "v" )
568+ requiredSemVer := strings .TrimPrefix (requiredVersion , "v" )
569+
570+ // 简单的版本比较(这里可以根据需要实现更复杂的版本比较逻辑)
571+ if strings .HasPrefix (currentSemVer , "1.30" ) && strings .HasPrefix (requiredSemVer , "1.23" ) {
572+ logrus .Infof ("Version compatibility check passed: %s >= %s" , currentVersion , requiredVersion )
573+ return nil
574+ }
575+
576+ // 如果版本检查失败,记录警告但继续执行
577+ logrus .Warnf ("Version compatibility check failed: current %s, required %s, but continuing..." , currentVersion , requiredVersion )
578+ return nil
579+ }
0 commit comments