|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "k8s.io/apimachinery/pkg/api/meta" |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 9 | + "k8s.io/apimachinery/pkg/runtime" |
| 10 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 11 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
| 12 | + "k8s.io/client-go/discovery" |
| 13 | + "k8s.io/client-go/dynamic" |
| 14 | + "k8s.io/client-go/kubernetes" |
| 15 | + "k8s.io/client-go/rest" |
| 16 | + "k8s.io/metrics/pkg/apis/metrics" |
| 17 | + metricsv1beta1 "k8s.io/metrics/pkg/client/clientset/versioned/typed/metrics/v1beta1" |
| 18 | +) |
| 19 | + |
| 20 | +// Openshift provides OpenShift-specific detection capabilities. |
| 21 | +// This interface is used by toolsets to conditionally enable OpenShift-specific tools. |
| 22 | +type Openshift interface { |
| 23 | + IsOpenShift(context.Context) bool |
| 24 | +} |
| 25 | + |
| 26 | +// ListOptions contains options for listing Kubernetes resources. |
| 27 | +type ListOptions struct { |
| 28 | + metav1.ListOptions |
| 29 | + AsTable bool |
| 30 | +} |
| 31 | + |
| 32 | +// PodsTopOptions contains options for getting pod metrics. |
| 33 | +type PodsTopOptions struct { |
| 34 | + metav1.ListOptions |
| 35 | + AllNamespaces bool |
| 36 | + Namespace string |
| 37 | + Name string |
| 38 | +} |
| 39 | + |
| 40 | +// NodesTopOptions contains options for getting node metrics. |
| 41 | +type NodesTopOptions struct { |
| 42 | + metav1.ListOptions |
| 43 | + Name string |
| 44 | +} |
| 45 | + |
| 46 | +type KubernetesClientSet interface { |
| 47 | + genericclioptions.RESTClientGetter |
| 48 | + kubernetes.Interface |
| 49 | + // NamespaceOrDefault returns the provided namespace or the default configured namespace if empty |
| 50 | + NamespaceOrDefault(namespace string) string |
| 51 | + RESTConfig() *rest.Config |
| 52 | + RESTMapper() meta.ResettableRESTMapper |
| 53 | + DiscoveryClient() discovery.CachedDiscoveryInterface |
| 54 | + DynamicClient() dynamic.Interface |
| 55 | + MetricsV1beta1Client() *metricsv1beta1.MetricsV1beta1Client |
| 56 | +} |
| 57 | + |
| 58 | +// KubernetesClient defines the interface for Kubernetes operations that tool and prompt handlers need. |
| 59 | +// This interface abstracts the concrete Kubernetes implementation to allow for better decoupling |
| 60 | +// and testability. The pkg/kubernetes.Kubernetes type implements this interface. |
| 61 | +// |
| 62 | +// For toolsets that need direct access to the Kubernetes clientset (e.g., for DynamicClient), |
| 63 | +// they can type-assert to ClientsetProvider. |
| 64 | +type KubernetesClient interface { |
| 65 | + // AccessControlClientset provides access to the underlying Kubernetes clientset with access control enforced |
| 66 | + AccessControlClientset() KubernetesClientSet |
| 67 | + |
| 68 | + // --- Resource Operations --- |
| 69 | + |
| 70 | + // ResourcesList lists resources of the specified GroupVersionKind |
| 71 | + ResourcesList(ctx context.Context, gvk *schema.GroupVersionKind, namespace string, options ListOptions) (runtime.Unstructured, error) |
| 72 | + // ResourcesGet retrieves a single resource by name |
| 73 | + ResourcesGet(ctx context.Context, gvk *schema.GroupVersionKind, namespace, name string) (*unstructured.Unstructured, error) |
| 74 | + // ResourcesCreateOrUpdate creates or updates resources from a YAML/JSON string |
| 75 | + ResourcesCreateOrUpdate(ctx context.Context, resource string) ([]*unstructured.Unstructured, error) |
| 76 | + // ResourcesDelete deletes a resource by name |
| 77 | + ResourcesDelete(ctx context.Context, gvk *schema.GroupVersionKind, namespace, name string) error |
| 78 | + // ResourcesScale gets or sets the scale of a resource |
| 79 | + ResourcesScale(ctx context.Context, gvk *schema.GroupVersionKind, namespace, name string, desiredScale int64, shouldScale bool) (*unstructured.Unstructured, error) |
| 80 | + |
| 81 | + // --- Namespace Operations --- |
| 82 | + |
| 83 | + // NamespacesList lists all namespaces |
| 84 | + NamespacesList(ctx context.Context, options ListOptions) (runtime.Unstructured, error) |
| 85 | + // ProjectsList lists all OpenShift projects |
| 86 | + ProjectsList(ctx context.Context, options ListOptions) (runtime.Unstructured, error) |
| 87 | + |
| 88 | + // --- Pod Operations --- |
| 89 | + |
| 90 | + // PodsListInAllNamespaces lists pods across all namespaces |
| 91 | + PodsListInAllNamespaces(ctx context.Context, options ListOptions) (runtime.Unstructured, error) |
| 92 | + // PodsListInNamespace lists pods in a specific namespace |
| 93 | + PodsListInNamespace(ctx context.Context, namespace string, options ListOptions) (runtime.Unstructured, error) |
| 94 | + // PodsGet retrieves a single pod by name |
| 95 | + PodsGet(ctx context.Context, namespace, name string) (*unstructured.Unstructured, error) |
| 96 | + // PodsDelete deletes a pod and its managed resources |
| 97 | + PodsDelete(ctx context.Context, namespace, name string) (string, error) |
| 98 | + // PodsLog retrieves logs from a pod container |
| 99 | + PodsLog(ctx context.Context, namespace, name, container string, previous bool, tail int64) (string, error) |
| 100 | + // PodsRun creates and runs a new pod with optional service and route |
| 101 | + PodsRun(ctx context.Context, namespace, name, image string, port int32) ([]*unstructured.Unstructured, error) |
| 102 | + // PodsTop retrieves pod metrics |
| 103 | + PodsTop(ctx context.Context, options PodsTopOptions) (*metrics.PodMetricsList, error) |
| 104 | + // PodsExec executes a command in a pod container |
| 105 | + PodsExec(ctx context.Context, namespace, name, container string, command []string) (string, error) |
| 106 | + |
| 107 | + // --- Node Operations --- |
| 108 | + |
| 109 | + // NodesLog retrieves logs from a node |
| 110 | + NodesLog(ctx context.Context, name string, query string, tailLines int64) (string, error) |
| 111 | + // NodesStatsSummary retrieves stats summary from a node |
| 112 | + NodesStatsSummary(ctx context.Context, name string) (string, error) |
| 113 | + // NodesTop retrieves node metrics |
| 114 | + NodesTop(ctx context.Context, options NodesTopOptions) (*metrics.NodeMetricsList, error) |
| 115 | + |
| 116 | + // --- Event Operations --- |
| 117 | + |
| 118 | + // EventsList lists events in a namespace |
| 119 | + EventsList(ctx context.Context, namespace string) ([]map[string]any, error) |
| 120 | + |
| 121 | + // --- Configuration Operations --- |
| 122 | + |
| 123 | + // ConfigurationContextsList returns the list of available context names |
| 124 | + ConfigurationContextsList() (map[string]string, error) |
| 125 | + // ConfigurationContextsDefault returns the current context name |
| 126 | + ConfigurationContextsDefault() (string, error) |
| 127 | + // ConfigurationView returns the kubeconfig content |
| 128 | + ConfigurationView(minify bool) (runtime.Object, error) |
| 129 | +} |
0 commit comments