fix: add kubernetes version from config (#2724)

Signed-off-by: redscholar <blacktiledhouse@gmail.com>
This commit is contained in:
liujian 2025-08-26 15:00:20 +08:00 committed by GitHub
parent 79fa0a4d8c
commit d62e56985a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 2 deletions

View File

@ -16,6 +16,11 @@ limitations under the License.
package api
import (
"encoding/json"
"os"
)
const (
// SchemaLabelSubfix is the label key used to indicate which schema a playbook belongs to.
SchemaLabelSubfix = "kubekey.kubesphere.io/schema"
@ -140,7 +145,7 @@ type IPTable struct {
// SchemaFile2Table converts a SchemaFile and its filename into a SchemaTable structure.
// It initializes the SchemaTable fields from the SchemaFile's DataSchema and sets up the Playbook map
// with playbook labels and their corresponding paths. Other playbook fields are left empty for later population.
func SchemaFile2Table(schemaFile SchemaFile, filename string) SchemaTable {
func SchemaFile2Table(schemaFile SchemaFile, configFile string, filename string) SchemaTable {
table := SchemaTable{
Name: filename,
Title: schemaFile.DataSchema.Title,
@ -158,5 +163,26 @@ func SchemaFile2Table(schemaFile SchemaFile, filename string) SchemaTable {
}
}
// If the schema is for kubernetes.json, try to supplement the version field from the config file.
if filename == "kubernetes.json" {
// Attempt to read the config file.
if configFileBytes, err := os.ReadFile(configFile); err == nil {
var config map[string]map[string]any
// Attempt to unmarshal the config file into a map.
if err := json.Unmarshal(configFileBytes, &config); err == nil {
// Check if the config contains an entry for "kubernetes.json".
if v, ok := config[filename]; ok {
// Check if the "kubernetes" key exists and is a map.
if kube, ok := v["kubernetes"].(map[string]any); ok {
// If "kube_version" exists and is a string, set it as the table's version.
if kubeVer, ok := kube["kube_version"].(string); ok {
table.Version = kubeVer
}
}
}
}
}
}
return table
}

View File

@ -322,7 +322,8 @@ func (h ResourceHandler) ListSchema(request *restful.Request, response *restful.
api.HandleError(response, request, err)
return
}
schema := api.SchemaFile2Table(schemaFile, entry.Name())
schema := api.SchemaFile2Table(schemaFile, filepath.Join(h.rootPath, api.SchemaConfigFile), entry.Name())
// For each playbook, if it matches a label in schema.Playbook and the label value equals schema.Name, add its info.
for _, playbook := range playbookList.Items {
for label, schemaName := range playbook.Labels {