package prompts
import (
"fmt"
"strings"
)
// PromptType represents different types of diagnostic prompts
type PromptType string
const (
PromptTypeDiagnose PromptType = "diagnose"
PromptTypeTroubleshoot PromptType = "troubleshoot"
PromptTypeOptimize PromptType = "optimize"
PromptTypeSecurity PromptType = "security"
PromptTypePerformance PromptType = "performance"
PromptTypeDeployment PromptType = "deployment"
)
// DiagnosticPrompt represents a structured diagnostic prompt
type DiagnosticPrompt struct {
Type PromptType
Title string
Description string
Keywords []string
Steps []PromptStep
Tools []string
}
// PromptStep represents a step in the diagnostic process
type PromptStep struct {
Number int
Title string
Description string
Command string
Expected string
NextAction string
}
// GetDiagnosticPrompt returns a diagnostic prompt based on type and context
func GetDiagnosticPrompt(promptType PromptType, keyword, namespace string) *DiagnosticPrompt {
switch promptType {
case PromptTypeDiagnose:
return getK8sDiagnosePrompt(keyword, namespace)
case PromptTypeTroubleshoot:
return getK8sTroubleshootPrompt(keyword, namespace)
case PromptTypeOptimize:
return getK8sOptimizePrompt(keyword, namespace)
case PromptTypeSecurity:
return getK8sSecurityPrompt(keyword, namespace)
case PromptTypePerformance:
return getK8sPerformancePrompt(keyword, namespace)
case PromptTypeDeployment:
return getK8sDeploymentPrompt(keyword, namespace)
default:
return getK8sDiagnosePrompt(keyword, namespace)
}
}
// getK8sDiagnosePrompt returns the enhanced K8s diagnose prompt
func getK8sDiagnosePrompt(keyword, namespace string) *DiagnosticPrompt {
nsFilter := ""
if namespace != "" && namespace != "all" {
nsFilter = fmt.Sprintf(" in namespace '%s'", namespace)
}
return &DiagnosticPrompt{
Type: PromptTypeDiagnose,
Title: "🔍 Kubernetes Pod Diagnostic Flow",
Description: fmt.Sprintf("Systematic troubleshooting for pods matching '%s'%s", keyword, nsFilter),
Keywords: []string{keyword, "pod", "diagnose", "troubleshoot"},
Tools: []string{
"k8s_list_resources",
"k8s_get_resource",
"k8s_get_logs",
"k8s_get_events",
"k8s_exec_in_pod",
"kubectl_describe",
},
Steps: []PromptStep{
{
Number: 1,
Title: "🔍 Identify Target Pods",
Description: fmt.Sprintf("Find all pods matching '%s'%s", keyword, nsFilter),
Command: fmt.Sprintf("k8s_list_resources --resource=pods --label-selector=app=%s --namespace=%s", keyword, namespace),
Expected: "List of pods with their current status",
NextAction: "If no pods found, check with broader search or different namespace",
},
{
Number: 2,
Title: "📊 Check Pod Status",
Description: "Examine pod status and conditions for each identified pod",
Command: "k8s_get_resource --resource=pods --name=<POD_NAME> --namespace=<NAMESPACE>",
Expected: "Pod status showing Ready/NotReady, Running/Pending/Failed states",
NextAction: "Focus on pods with non-Running status or non-Ready conditions",
},
{
Number: 3,
Title: "📋 Analyze Events",
Description: "Check recent events for the problematic pods",
Command: "k8s_get_events --namespace=<NAMESPACE> --field-selector=involvedObject.name=<POD_NAME>",
Expected: "Recent events showing warnings, errors, or status changes",
NextAction: "Look for specific error messages or warnings",
},
{
Number: 4,
Title: "📝 Examine Logs",
Description: "Check container logs for error messages",
Command: "k8s_get_logs --pod=<POD_NAME> --namespace=<NAMESPACE> --tail=100",
Expected: "Application logs showing startup, errors, or runtime issues",
NextAction: "Look for error patterns, stack traces, or configuration issues",
},
{
Number: 5,
Title: "🔧 Deep Dive Analysis",
Description: "Get detailed pod description and check resource constraints",
Command: "kubectl_describe --resource=pods --name=<POD_NAME> --namespace=<NAMESPACE>",
Expected: "Detailed pod spec, events, and resource information",
NextAction: "Check resource limits, volume mounts, and environment variables",
},
{
Number: 6,
Title: "🛠️ Interactive Debugging",
Description: "Execute commands inside the pod for live debugging",
Command: "k8s_exec_in_pod --pod=<POD_NAME> --namespace=<NAMESPACE> --command='ps aux'",
Expected: "Process list and system information from inside the pod",
NextAction: "Check running processes, file system, and network connectivity",
},
{
Number: 7,
Title: "📈 Resource Analysis",
Description: "Check resource usage and limits",
Command: "k8s_exec_in_pod --pod=<POD_NAME> --namespace=<NAMESPACE> --command='top'",
Expected: "CPU and memory usage statistics",
NextAction: "Compare with resource requests/limits in pod spec",
},
{
Number: 8,
Title: "🔗 Network Connectivity",
Description: "Test network connectivity and DNS resolution",
Command: "k8s_exec_in_pod --pod=<POD_NAME> --namespace=<NAMESPACE> --command='nslookup kubernetes.default'",
Expected: "Successful DNS resolution and network connectivity",
NextAction: "If DNS fails, check CoreDNS pods and network policies",
},
},
}
}
// getK8sTroubleshootPrompt returns a comprehensive troubleshooting prompt
func getK8sTroubleshootPrompt(keyword, namespace string) *DiagnosticPrompt {
return &DiagnosticPrompt{
Type: PromptTypeTroubleshoot,
Title: "🛠️ Advanced Kubernetes Troubleshooting",
Description: fmt.Sprintf("Comprehensive troubleshooting for '%s' in namespace '%s'", keyword, namespace),
Keywords: []string{keyword, "troubleshoot", "debug", "fix"},
Tools: []string{
"k8s_cluster_info",
"k8s_list_resources",
"k8s_get_resource",
"k8s_get_logs",
"k8s_get_events",
"k8s_exec_in_pod",
"kubectl_describe",
"kubectl_get",
},
Steps: []PromptStep{
{
Number: 1,
Title: "🌐 Cluster Health Check",
Description: "Verify overall cluster health and node status",
Command: "k8s_cluster_info",
Expected: "Healthy cluster with all nodes in Ready state",
NextAction: "If cluster issues, focus on node problems first",
},
{
Number: 2,
Title: "🔍 Resource Discovery",
Description: "Find all related resources (pods, services, deployments, etc.)",
Command: "k8s_list_resources --resource=all --label-selector=app=%s --namespace=%s",
Expected: "Complete resource inventory",
NextAction: "Analyze relationships between resources",
},
{
Number: 3,
Title: "📊 Status Analysis",
Description: "Check status of all related resources",
Command: "k8s_get_resource --resource=<RESOURCE_TYPE> --name=<RESOURCE_NAME> --namespace=%s",
Expected: "Resource status and conditions",
NextAction: "Identify the root cause resource",
},
{
Number: 4,
Title: "📝 Log Analysis",
Description: "Collect logs from all containers",
Command: "k8s_get_logs --pod=<POD_NAME> --namespace=%s --tail=200 --previous",
Expected: "Comprehensive log analysis",
NextAction: "Look for error patterns and correlation",
},
{
Number: 5,
Title: "🔧 Configuration Check",
Description: "Verify configuration and environment",
Command: "kubectl_describe --resource=<RESOURCE_TYPE> --name=<RESOURCE_NAME> --namespace=%s",
Expected: "Configuration details and recent changes",
NextAction: "Check for misconfigurations",
},
},
}
}
// getK8sOptimizePrompt returns a performance optimization prompt
func getK8sOptimizePrompt(keyword, namespace string) *DiagnosticPrompt {
return &DiagnosticPrompt{
Type: PromptTypeOptimize,
Title: "⚡ Kubernetes Performance Optimization",
Description: fmt.Sprintf("Optimize performance for '%s' in namespace '%s'", keyword, namespace),
Keywords: []string{keyword, "optimize", "performance", "tune"},
Tools: []string{
"k8s_list_resources",
"k8s_get_resource",
"k8s_exec_in_pod",
"kubectl_describe",
},
Steps: []PromptStep{
{
Number: 1,
Title: "📊 Resource Usage Analysis",
Description: "Analyze current resource consumption",
Command: "k8s_exec_in_pod --pod=<POD_NAME> --namespace=%s --command='top'",
Expected: "CPU and memory usage patterns",
NextAction: "Identify resource bottlenecks",
},
{
Number: 2,
Title: "🔧 Resource Limits Review",
Description: "Check current resource requests and limits",
Command: "kubectl_describe --resource=pods --name=<POD_NAME> --namespace=%s",
Expected: "Resource requests and limits configuration",
NextAction: "Compare with actual usage",
},
{
Number: 3,
Title: "📈 Scaling Analysis",
Description: "Analyze scaling patterns and HPA configuration",
Command: "k8s_get_resource --resource=horizontalpodautoscaler --name=<HPA_NAME> --namespace=%s",
Expected: "HPA configuration and metrics",
NextAction: "Optimize scaling parameters",
},
},
}
}
// getK8sSecurityPrompt returns a security analysis prompt
func getK8sSecurityPrompt(keyword, namespace string) *DiagnosticPrompt {
return &DiagnosticPrompt{
Type: PromptTypeSecurity,
Title: "🔒 Kubernetes Security Analysis",
Description: fmt.Sprintf("Security analysis for '%s' in namespace '%s'", keyword, namespace),
Keywords: []string{keyword, "security", "audit", "compliance"},
Tools: []string{
"k8s_list_resources",
"k8s_get_resource",
"kubectl_describe",
},
Steps: []PromptStep{
{
Number: 1,
Title: "🔍 RBAC Analysis",
Description: "Check service account and RBAC permissions",
Command: "k8s_get_resource --resource=serviceaccounts --name=<SA_NAME> --namespace=%s",
Expected: "Service account and role bindings",
NextAction: "Verify least privilege principle",
},
{
Number: 2,
Title: "🛡️ Security Context Review",
Description: "Analyze pod security context",
Command: "kubectl_describe --resource=pods --name=<POD_NAME> --namespace=%s",
Expected: "Security context configuration",
NextAction: "Check for security best practices",
},
},
}
}
// getK8sPerformancePrompt returns a performance monitoring prompt
func getK8sPerformancePrompt(keyword, namespace string) *DiagnosticPrompt {
return &DiagnosticPrompt{
Type: PromptTypePerformance,
Title: "📈 Kubernetes Performance Monitoring",
Description: fmt.Sprintf("Performance monitoring for '%s' in namespace '%s'", keyword, namespace),
Keywords: []string{keyword, "performance", "monitoring", "metrics"},
Tools: []string{
"k8s_list_resources",
"k8s_get_resource",
"k8s_exec_in_pod",
},
Steps: []PromptStep{
{
Number: 1,
Title: "📊 Resource Metrics",
Description: "Collect current resource metrics",
Command: "k8s_exec_in_pod --pod=<POD_NAME> --namespace=%s --command='cat /proc/meminfo'",
Expected: "Memory usage and availability",
NextAction: "Analyze memory patterns",
},
},
}
}
// getK8sDeploymentPrompt returns a deployment analysis prompt
func getK8sDeploymentPrompt(keyword, namespace string) *DiagnosticPrompt {
return &DiagnosticPrompt{
Type: PromptTypeDeployment,
Title: "🚀 Kubernetes Deployment Analysis",
Description: fmt.Sprintf("Deployment analysis for '%s' in namespace '%s'", keyword, namespace),
Keywords: []string{keyword, "deployment", "rollout", "strategy"},
Tools: []string{
"k8s_list_resources",
"k8s_get_resource",
"kubectl_rollout",
},
Steps: []PromptStep{
{
Number: 1,
Title: "🚀 Deployment Status",
Description: "Check deployment status and rollout history",
Command: "kubectl_rollout --action=status --resource=deployment --name=<DEPLOYMENT_NAME> --namespace=%s",
Expected: "Deployment status and rollout progress",
NextAction: "Check for failed rollouts",
},
},
}
}
// FormatPrompt formats a diagnostic prompt for display
func (p *DiagnosticPrompt) FormatPrompt() string {
var output strings.Builder
output.WriteString(fmt.Sprintf("# %s\n\n", p.Title))
output.WriteString(fmt.Sprintf("**Description:** %s\n\n", p.Description))
output.WriteString("## 🛠️ Available Tools\n")
for _, tool := range p.Tools {
output.WriteString(fmt.Sprintf("- `%s`\n", tool))
}
output.WriteString("\n")
output.WriteString("## 📋 Diagnostic Steps\n\n")
for _, step := range p.Steps {
output.WriteString(fmt.Sprintf("### Step %d: %s\n", step.Number, step.Title))
output.WriteString(fmt.Sprintf("**Description:** %s\n\n", step.Description))
output.WriteString(fmt.Sprintf("**Command:**\n```bash\n%s\n```\n\n", step.Command))
output.WriteString(fmt.Sprintf("**Expected Output:** %s\n\n", step.Expected))
output.WriteString(fmt.Sprintf("**Next Action:** %s\n\n", step.NextAction))
output.WriteString("---\n\n")
}
return output.String()
}
// GetPromptSuggestions returns suggested prompts based on context
func GetPromptSuggestions(context string) []string {
suggestions := []string{
"🔍 /k8s-diagnose <keyword> [namespace] - Systematic pod troubleshooting",
"🛠️ /k8s-troubleshoot <keyword> [namespace] - Advanced debugging",
"⚡ /k8s-optimize <keyword> [namespace] - Performance optimization",
"🔒 /k8s-security <keyword> [namespace] - Security analysis",
"📈 /k8s-performance <keyword> [namespace] - Performance monitoring",
"🚀 /k8s-deployment <keyword> [namespace] - Deployment analysis",
}
// Filter suggestions based on context
if strings.Contains(strings.ToLower(context), "error") || strings.Contains(strings.ToLower(context), "fail") {
return suggestions[:2] // Focus on diagnostic and troubleshooting
}
if strings.Contains(strings.ToLower(context), "slow") || strings.Contains(strings.ToLower(context), "performance") {
return []string{suggestions[2], suggestions[4]} // Focus on optimization and performance
}
return suggestions
}