mirror of
https://github.com/openshift/openshift-mcp-server.git
synced 2025-10-17 14:27:48 +03:00
A new configuration options is available: `--list-output` There are two modes available: - `yaml`: current default (will be changed in subsequent PR), which returns a multi-document YAML - `table`: returns a plain-text table as created by the kube-api server when requested with `Accept: application/json;as=Table;v=v1;g=meta.k8s.io` Additional logic has been added to the table format to include the apiVersion and kind. This is not returned by the server, kubectl doesn't include this either. However, this is extremely handy for the LLM when using the generic resource tools.
52 lines
1.6 KiB
Go
52 lines
1.6 KiB
Go
package kubernetes
|
|
|
|
import (
|
|
"context"
|
|
v1 "k8s.io/api/core/v1"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
"strings"
|
|
)
|
|
|
|
func (k *Kubernetes) EventsList(ctx context.Context, namespace string) ([]map[string]any, error) {
|
|
var eventMap []map[string]any
|
|
raw, err := k.ResourcesList(ctx, &schema.GroupVersionKind{
|
|
Group: "", Version: "v1", Kind: "Event",
|
|
}, namespace, ResourceListOptions{})
|
|
if err != nil {
|
|
return eventMap, err
|
|
}
|
|
unstructuredList := raw.(*unstructured.UnstructuredList)
|
|
if len(unstructuredList.Items) == 0 {
|
|
return eventMap, nil
|
|
}
|
|
for _, item := range unstructuredList.Items {
|
|
event := &v1.Event{}
|
|
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(item.Object, event); err != nil {
|
|
return eventMap, err
|
|
}
|
|
timestamp := event.EventTime.Time
|
|
if timestamp.IsZero() && event.Series != nil {
|
|
timestamp = event.Series.LastObservedTime.Time
|
|
} else if timestamp.IsZero() && event.Count > 1 {
|
|
timestamp = event.LastTimestamp.Time
|
|
} else if timestamp.IsZero() {
|
|
timestamp = event.FirstTimestamp.Time
|
|
}
|
|
eventMap = append(eventMap, map[string]any{
|
|
"Namespace": event.Namespace,
|
|
"Timestamp": timestamp.String(),
|
|
"Type": event.Type,
|
|
"Reason": event.Reason,
|
|
"InvolvedObject": map[string]string{
|
|
"apiVersion": event.InvolvedObject.APIVersion,
|
|
"Kind": event.InvolvedObject.Kind,
|
|
"Name": event.InvolvedObject.Name,
|
|
},
|
|
"Message": strings.TrimSpace(event.Message),
|
|
})
|
|
}
|
|
return eventMap, nil
|
|
}
|