-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
247 lines (206 loc) · 6.85 KB
/
main.go
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
242
243
244
245
246
247
package main
import (
"bytes"
"context"
"fmt"
"net/url"
"os"
"os/exec"
"strings"
"github.com/nshafer/phx"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"github.com/ranching-farm/kubectl-addon/pkg/logging"
)
var (
version = "unknown" // This will be set by the build flag
clusterId string
secret string
endpointURL string
kubeconfig string
outputFormat string
)
var rootCmd = &cobra.Command{
Use: "kubectl-ranching_farm",
Short: "Connect to ranching.farm AI-powered Kubernetes management",
Long: `This plugin allows you to connect your Kubernetes cluster to the ranching.farm
platform. Once connected, you can interact with your cluster through our
AI-powered interface for simplified management and troubleshooting.`,
Run: func(cmd *cobra.Command, args []string) {
// This is called when no subcommands are provided
cmd.Help()
},
}
var connectCmd = &cobra.Command{
Use: "connect",
Short: "Connect to ranching.farm",
Long: `Connect your Kubernetes cluster to the ranching.farm platform.
This command establishes a secure connection between your local environment
and our AI services, enabling real-time analysis and assistance.`,
Run: func(cmd *cobra.Command, args []string) {
if clusterId == "" || secret == "" {
logging.Log("Error", "Cluster ID and Secret are required. Use --cluster-id and --secret flags.", outputFormat)
cmd.Usage()
os.Exit(1)
}
runMain()
},
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of ranching.farm",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("ranching.farm version %s\n", version)
},
}
func init() {
rootCmd.AddCommand(connectCmd)
rootCmd.AddCommand(versionCmd)
connectCmd.Flags().StringVarP(&clusterId, "cluster-id", "i", "", "Cluster ID (required)")
connectCmd.Flags().StringVarP(&secret, "secret", "s", "", "Cluster Secret (required)")
connectCmd.Flags().StringVarP(&endpointURL, "endpoint", "e", "wss://ranching.farm/socket/kubernetes/cluster", "WebSocket endpoint URL")
connectCmd.Flags().StringVarP(&kubeconfig, "kubeconfig", "k", "", "Path to kubeconfig file")
connectCmd.Flags().StringVarP(&outputFormat, "output", "o", "", "Output format (json|yaml|table)")
connectCmd.MarkFlagRequired("cluster-id")
connectCmd.MarkFlagRequired("secret")
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func runMain() {
if kubeconfig == "" {
kubeconfig = os.Getenv("KUBECONFIG")
if kubeconfig == "" {
kubeconfig = os.Getenv("HOME") + "/.kube/config"
}
}
logging.Log("Starting", "kubectl ranching.farm addon", outputFormat)
endPoint, err := url.Parse(endpointURL)
if err != nil {
logging.Log("Error", fmt.Sprintf("Failed to parse WebSocket URL: %v", err), outputFormat)
os.Exit(1)
}
socket := phx.NewSocket(endPoint)
err = socket.Connect()
if err != nil {
logging.Log("Error", fmt.Sprintf("Failed to connect to socket: %v", err), outputFormat)
os.Exit(1)
}
logging.Log("Success", "Connected to WebSocket", outputFormat)
channel := socket.Channel(fmt.Sprintf("cluster:%s", clusterId), nil)
join, err := channel.Join()
if err != nil {
logging.Log("Error", fmt.Sprintf("Failed to join channel: %v", err), outputFormat)
os.Exit(1)
}
join.Receive("ok", func(response any) {
logging.Log("Success", fmt.Sprintf("Joined channel: %s", channel.Topic()), outputFormat)
sendClusterInfo(channel)
})
channel.On("cmd", func(payload any) {
handleCommand(channel, payload)
})
logging.Log("Info", "Main loop started. Waiting for events...", outputFormat)
select {} // Keep the program running
}
func sendClusterInfo(channel *phx.Channel) {
logging.Log("Info", "Sending cluster info...", outputFormat)
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
logging.Log("Error", fmt.Sprintf("Failed to build config from kubeconfig: %v", err), outputFormat)
return
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
logging.Log("Error", fmt.Sprintf("Failed to create Kubernetes client: %v", err), outputFormat)
return
}
nodes, err := clientset.CoreV1().Nodes().List(context.Background(), v1.ListOptions{})
if err != nil {
logging.Log("Error", fmt.Sprintf("Failed to list nodes: %v", err), outputFormat)
return
}
nodeInfo := make([]map[string]string, 0)
for _, node := range nodes.Items {
nodeInfo = append(nodeInfo, map[string]string{
"name": node.Name,
"status": string(node.Status.Phase),
})
}
clusterInfo := map[string]interface{}{
"nodes": nodeInfo,
}
logging.Log("Info", fmt.Sprintf("Found %d nodes in the cluster", len(nodes.Items)), outputFormat)
logging.Log("Node Info", nodeInfo, outputFormat)
push, err := channel.Push("info", clusterInfo)
if err != nil {
logging.Log("Error", fmt.Sprintf("Failed to send cluster info: %v", err), outputFormat)
return
}
push.Receive("ok", func(response any) {
logging.Log("Success", "Cluster info sent successfully", outputFormat)
})
}
func handleCommand(channel *phx.Channel, payload any) {
logging.Log("Info", fmt.Sprintf("Received command payload: %v", payload), outputFormat)
cmd, ok := payload.(map[string]interface{})
if !ok {
logging.Log("Error", "Invalid payload format", outputFormat)
return
}
command, ok := cmd["command"].(string)
if !ok {
logging.Log("Error", "Invalid command format", outputFormat)
return
}
args, ok := cmd["arguments"].(string)
if !ok {
logging.Log("Error", "Invalid args format", outputFormat)
return
}
uuid, ok := cmd["uuid"].(string)
if !ok {
logging.Log("Error", "Invalid uuid format", outputFormat)
return
}
output, err := executeCommand(command, args)
if err != nil {
logging.Log("Error", fmt.Sprintf("Error executing command: %v", err), outputFormat)
output = fmt.Sprintf("Error: %v", err)
} else {
logging.Log("Success", "Command executed successfully", outputFormat)
}
response := map[string]interface{}{
"command": command,
"arguments": args,
"output": output,
"uuid": uuid,
}
push, err := channel.Push("output", response)
if err != nil {
logging.Log("Error", fmt.Sprintf("Failed to send command output: %v", err), outputFormat)
return
}
push.Receive("ok", func(response any) {
logging.Log("Success", "Command output sent successfully", outputFormat)
})
}
func executeCommand(command string, params string) (string, error) {
logging.Log("Info", fmt.Sprintf("Executing command: %s %s", command, params), outputFormat)
args := strings.Fields(params)
cmd := exec.Command(command, args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return fmt.Sprintf("Error: %v\nStdout: %s\nStderr: %s", err, stdout.String(), stderr.String()), err
}
logging.Log("Success", "Command executed successfully", outputFormat)
return stdout.String(), nil
}