-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathformatters.go
More file actions
241 lines (203 loc) · 7.69 KB
/
formatters.go
File metadata and controls
241 lines (203 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package mcp
import (
"encoding/json"
"fmt"
"strings"
"time"
)
// ResourceFormatter provides AI-friendly formatting for Kubernetes resources
type ResourceFormatter struct{}
func NewResourceFormatter() *ResourceFormatter {
return &ResourceFormatter{}
}
// FormatPodForAI creates an AI-optimized view of pod information
func (f *ResourceFormatter) FormatPodForAI(podData string) (string, error) {
var pod map[string]interface{}
if err := json.Unmarshal([]byte(podData), &pod); err != nil {
return "", err
}
summary := &strings.Builder{}
summary.WriteString("# Pod Summary\n\n")
// Basic information
summary.WriteString(fmt.Sprintf("**Name**: %s\n", pod["name"]))
summary.WriteString(fmt.Sprintf("**Namespace**: %s\n", pod["namespace"]))
summary.WriteString(fmt.Sprintf("**Status**: %s\n", pod["status"]))
summary.WriteString(fmt.Sprintf("**Node**: %s\n", pod["node"]))
if restarts, ok := pod["restarts"].(float64); ok && restarts > 0 {
summary.WriteString(fmt.Sprintf("**⚠️ Restarts**: %.0f\n", restarts))
}
// Creation time
if createdAt, ok := pod["createdAt"].(string); ok {
if t, err := time.Parse(time.RFC3339, createdAt); err == nil {
age := time.Since(t)
summary.WriteString(fmt.Sprintf("**Age**: %s\n", formatDuration(age)))
}
}
summary.WriteString("\n## Containers\n\n")
// Container information
if containers, ok := pod["containers"].([]interface{}); ok {
for _, container := range containers {
if c, ok := container.(map[string]interface{}); ok {
name := c["name"].(string)
image := c["image"].(string)
ready := c["ready"].(bool)
state := c["state"].(string)
status := "🟢 Ready"
if !ready {
status = "🔴 Not Ready"
}
summary.WriteString(fmt.Sprintf("- **%s**: %s\n", name, status))
summary.WriteString(fmt.Sprintf(" - Image: `%s`\n", image))
summary.WriteString(fmt.Sprintf(" - State: %s\n", state))
if restarts, ok := c["restarts"].(float64); ok && restarts > 0 {
summary.WriteString(fmt.Sprintf(" - Restarts: %.0f\n", restarts))
}
}
}
}
// Conditions
if conditions, ok := pod["conditions"].([]interface{}); ok && len(conditions) > 0 {
summary.WriteString("\n## Conditions\n\n")
for _, condition := range conditions {
summary.WriteString(fmt.Sprintf("- %s\n", condition))
}
}
// Labels
if labels, ok := pod["labels"].(map[string]interface{}); ok && len(labels) > 0 {
summary.WriteString("\n## Labels\n\n")
for key, value := range labels {
summary.WriteString(fmt.Sprintf("- `%s`: `%s`\n", key, value))
}
}
summary.WriteString("\n---\n")
summary.WriteString("*Use this information to understand the pod's current state and troubleshoot any issues.*")
return summary.String(), nil
}
// FormatDeploymentForAI creates an AI-optimized view of deployment information
func (f *ResourceFormatter) FormatDeploymentForAI(deploymentData string) (string, error) {
var deployment map[string]interface{}
if err := json.Unmarshal([]byte(deploymentData), &deployment); err != nil {
return "", err
}
summary := &strings.Builder{}
summary.WriteString("# Deployment Summary\n\n")
// Basic information
summary.WriteString(fmt.Sprintf("**Name**: %s\n", deployment["name"]))
summary.WriteString(fmt.Sprintf("**Namespace**: %s\n", deployment["namespace"]))
summary.WriteString(fmt.Sprintf("**Strategy**: %s\n", deployment["strategy"]))
// Replica status
total := deployment["totalReplicas"].(float64)
ready := deployment["readyReplicas"].(float64)
updated := deployment["updatedReplicas"].(float64)
healthStatus := "🟢 Healthy"
if ready < total {
healthStatus = "🟡 Scaling"
}
if ready == 0 {
healthStatus = "🔴 Failed"
}
summary.WriteString(fmt.Sprintf("**Status**: %s\n", healthStatus))
summary.WriteString(fmt.Sprintf("**Replicas**: %.0f desired, %.0f ready, %.0f updated\n", total, ready, updated))
// Progress indicator
if total > 0 {
percentage := (ready / total) * 100
summary.WriteString(fmt.Sprintf("**Progress**: %.1f%% ready\n", percentage))
}
// Creation time
if createdAt, ok := deployment["createdAt"].(string); ok {
if t, err := time.Parse(time.RFC3339, createdAt); err == nil {
age := time.Since(t)
summary.WriteString(fmt.Sprintf("**Age**: %s\n", formatDuration(age)))
}
}
// Selector
if selector, ok := deployment["selector"].(map[string]interface{}); ok && len(selector) > 0 {
summary.WriteString("\n## Selector\n\n")
for key, value := range selector {
summary.WriteString(fmt.Sprintf("- `%s`: `%s`\n", key, value))
}
}
// Conditions
if conditions, ok := deployment["conditions"].([]interface{}); ok && len(conditions) > 0 {
summary.WriteString("\n## Conditions\n\n")
for _, condition := range conditions {
summary.WriteString(fmt.Sprintf("- %s\n", condition))
}
}
// Recommendations
summary.WriteString("\n## AI Assistant Notes\n\n")
if ready < total {
summary.WriteString("⚠️ **Action Needed**: Some replicas are not ready. Check pod status and logs.\n")
}
if ready == 0 {
summary.WriteString("🚨 **Critical**: No replicas are ready. This deployment may be failing.\n")
}
if ready == total {
summary.WriteString("✅ **Status**: Deployment is healthy and all replicas are ready.\n")
}
return summary.String(), nil
}
// FormatServiceForAI creates an AI-optimized view of service information
func (f *ResourceFormatter) FormatServiceForAI(serviceData string) (string, error) {
var service map[string]interface{}
if err := json.Unmarshal([]byte(serviceData), &service); err != nil {
return "", err
}
summary := &strings.Builder{}
summary.WriteString("# Service Summary\n\n")
// Basic information
summary.WriteString(fmt.Sprintf("**Name**: %s\n", service["name"]))
summary.WriteString(fmt.Sprintf("**Namespace**: %s\n", service["namespace"]))
summary.WriteString(fmt.Sprintf("**Type**: %s\n", service["type"]))
summary.WriteString(fmt.Sprintf("**Cluster IP**: %s\n", service["clusterIP"]))
// Port information
if ports, ok := service["ports"].([]interface{}); ok && len(ports) > 0 {
summary.WriteString("\n## Ports\n\n")
for _, port := range ports {
if p, ok := port.(map[string]interface{}); ok {
name := ""
if n, exists := p["name"].(string); exists && n != "" {
name = fmt.Sprintf(" (%s)", n)
}
summary.WriteString(fmt.Sprintf("- **Port %s%s**: %.0f → %s (%s)\n",
p["port"], name, p["port"], p["targetPort"], p["protocol"]))
}
}
}
// Selector
if selector, ok := service["selector"].(map[string]interface{}); ok && len(selector) > 0 {
summary.WriteString("\n## Selector\n\n")
summary.WriteString("This service routes traffic to pods with these labels:\n")
for key, value := range selector {
summary.WriteString(fmt.Sprintf("- `%s`: `%s`\n", key, value))
}
}
// Service type specific information
serviceType := service["type"].(string)
summary.WriteString("\n## Access Information\n\n")
switch serviceType {
case "ClusterIP":
summary.WriteString("🔒 **Internal Access Only**: This service is only accessible within the cluster.\n")
case "NodePort":
summary.WriteString("🌐 **External Access**: This service is accessible from outside the cluster via node IPs.\n")
case "LoadBalancer":
summary.WriteString("⚖️ **Load Balancer**: This service has an external load balancer.\n")
case "ExternalName":
summary.WriteString("🔗 **External Name**: This service maps to an external DNS name.\n")
}
return summary.String(), nil
}
// Helper function to format duration in a human-readable way
func formatDuration(d time.Duration) string {
if d < time.Minute {
return fmt.Sprintf("%.0fs", d.Seconds())
}
if d < time.Hour {
return fmt.Sprintf("%.0fm", d.Minutes())
}
if d < 24*time.Hour {
return fmt.Sprintf("%.1fh", d.Hours())
}
days := d.Hours() / 24
return fmt.Sprintf("%.1fd", days)
}