Skip to content

Commit 10bfd4b

Browse files
committed
support service analyse
1 parent e6a5ee8 commit 10bfd4b

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

cmd/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package cmd

cmd/service.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/CirillaQL/kubectl-openai/pkg/client"
7+
"github.com/CirillaQL/kubectl-openai/pkg/log"
8+
"github.com/CirillaQL/kubectl-openai/pkg/openai"
9+
"github.com/CirillaQL/kubectl-openai/pkg/token"
10+
"github.com/CirillaQL/kubectl-openai/pkg/util"
11+
"github.com/spf13/cobra"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
)
14+
15+
const serviceQuestion = "I will give you a kubernetes' service's detail, please help me to analyze it"
16+
17+
var serviceCmd = &cobra.Command{
18+
Use: "service",
19+
Short: "Use chatGPT to help analyse service status",
20+
Long: "Use chatGPT to help analyse service status",
21+
Run: func(cmd *cobra.Command, args []string) {
22+
ctx := context.Background()
23+
name := args[0]
24+
25+
if len(name) <= 0 {
26+
log.Logger.Error("Failed to get pod namespace")
27+
}
28+
client, err := client.LoadClient()
29+
if err != nil {
30+
log.Logger.Fatalf("Failed to load k8s client, error: %+v", err)
31+
panic(err)
32+
}
33+
34+
service, err := client.CoreV1().Services(namespace).Get(ctx, name, metav1.GetOptions{})
35+
if err != nil {
36+
log.Logger.Fatalf("Failed to get Service: %s, Error: %v", name, err)
37+
panic(err.Error())
38+
}
39+
podStr := fmt.Sprint(service)
40+
tokenString, err := token.ReadToken(TokenPath)
41+
if err != nil {
42+
panic(err)
43+
}
44+
util.LoadingStart()
45+
result, err := openai.Ask(tokenString, serviceQuestion+"\n"+podStr)
46+
util.LoadingStop()
47+
if err != nil {
48+
panic(err)
49+
}
50+
fmt.Println(result)
51+
},
52+
}
53+
54+
func init() {
55+
rootCmd.AddCommand(serviceCmd)
56+
57+
serviceCmd.Flags().StringVarP(&namespace, "namespace", "n", "default", "Pod's namespace")
58+
}

0 commit comments

Comments
 (0)