feat: enhance schema handling with playbook results (#2667)

- Added a new field `Result` to `SchemaTablePlaybook` to store the result of the associated playbook.
- Updated the `allSchema` function to unmarshal and assign the playbook result if available.
- Cleared `PlaybookPath` after processing to prevent unintended references.
- Adjusted the `SchemaTable` struct to include `PlaybookPath` for better schema management.

Signed-off-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
liujian 2025-07-23 11:31:16 +08:00 committed by GitHub
parent 2b8ea3eb46
commit 6b9636d144
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 10 deletions

View File

@ -74,23 +74,26 @@ type InventoryHostGroups struct {
// It includes fields such as name, type, title, description, version, namespace, logo, priority, and associated playbooks.
// The Playbook field is a slice of SchemaTablePlaybook, each representing a playbook reference.
type SchemaTable struct {
Name string `json:"name"` // Name of schema, defined by filename
SchemaType string `json:"schemaType"` // Type of the schema (e.g., CRD, built-in)
Title string `json:"title"` // Title of the schema
Description string `json:"description"` // Description of the schema
Version string `json:"version"` // Version of the schema
Namespace string `json:"namespace"` // Namespace of the schema
Logo string `json:"logo"` // Logo URL or identifier
Priority int `json:"priority"` // Priority for display or ordering
Playbook SchemaTablePlaybook `json:"playbook"` // List of reference playbooks
Name string `json:"name"` // Name of schema, defined by filename
SchemaType string `json:"schemaType"` // Type of the schema (e.g., CRD, built-in)
Title string `json:"title"` // Title of the schema
Description string `json:"description"` // Description of the schema
Version string `json:"version"` // Version of the schema
Namespace string `json:"namespace"` // Namespace of the schema
Logo string `json:"logo"` // Logo URL or identifier
Priority int `json:"priority"` // Priority for display or ordering
Playbook SchemaTablePlaybook `json:"playbook"` // List of reference playbooks
PlaybookPath map[string]string `json:"playbookPath,omitempty"` // PlaybookPath for current schema
}
// SchemaTablePlaybook represents a reference to a playbook associated with a schema.
// It includes the playbook's name, namespace, and phase.
type SchemaTablePlaybook struct {
Path string `json:"path"` // Path of playbook template.
Name string `json:"name"` // Name of the playbook
Namespace string `json:"namespace"` // Namespace of the playbook
Phase string `json:"phase"` // Phase of the playbook
Result any `json:"result"` // Result of the playbook
}
// IPTable represents an IP address entry and its SSH status information.

View File

@ -59,7 +59,7 @@ func NewSchemaService(rootPath string, workdir string, client ctrlclient.Client)
Doc("list all schema as table").
Metadata(restfulspec.KeyOpenAPITags, []string{_const.ResourceTag}).
Param(ws.QueryParameter("schemaType", "the type of schema json").Required(false)).
Param(ws.QueryParameter("playbookLabel", "the reference playbook of schema. eg: \"install.kubekey.kubesphere.io/schema\", \"check.kubekey.kubesphere.io/schema\" \\n"+
Param(ws.QueryParameter("playbookLabel", "the reference playbook of schema. eg: \"install.kubekey.kubesphere.io/schema\", \"check.kubekey.kubesphere.io/schema\" "+
"if empty will not return any reference playbook").Required(false)).
Param(ws.QueryParameter(query.ParameterPage, "page").Required(false).DataFormat("page=%d")).
Param(ws.QueryParameter(query.ParameterLimit, "limit").Required(false)).
@ -232,10 +232,19 @@ func (h schemaHandler) allSchema(request *restful.Request, response *restful.Res
case 0: // skip
case 1:
item := &playbookList.Items[0]
var result any
if len(item.Status.Result.Raw) != 0 {
if err := json.Unmarshal(item.Status.Result.Raw, &result); err != nil {
api.HandleBadRequest(response, request, errors.Errorf("failed to unmarshal result from playbook of schema %q", schema.Name))
return
}
}
schema.Playbook = api.SchemaTablePlaybook{
Path: schema.PlaybookPath[playbookLabel],
Name: item.Name,
Namespace: item.Namespace,
Phase: string(item.Status.Phase),
Result: result,
}
default:
playbookNames := make([]string, 0, len(playbookList.Items))
@ -246,6 +255,8 @@ func (h schemaHandler) allSchema(request *restful.Request, response *restful.Res
return
}
}
// clear PlaybookPath
schema.PlaybookPath = nil
schemaTable = append(schemaTable, schema)
}
// less is a comparison function for sorting SchemaTable items by a given field.