-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
186 lines (171 loc) · 4.12 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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/jeevanshu/kube-bot/kubehandler"
)
var (
// Token var for getting discord token
Token string
)
func init() {
flag.StringVar(&Token, "t", "", "Bot Token")
flag.Parse()
}
func main() {
dg, err := discordgo.New("Bot " + Token)
if err != nil {
fmt.Println("error in creating Discord session", err)
return
}
dg.AddHandler(commandHandler)
dg.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsGuildMessages)
err = dg.Open()
if err != nil {
fmt.Println("Error opening connection ", err)
return
}
fmt.Println("Bot is now running.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
dg.Close()
}
func commandHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
user := m.Author
channel := m.ChannelID
command := m.Content
if m.Author.ID == s.State.User.ID {
return
}
fmt.Printf("Command %v from user %v in channel %v \n", command, user, channel)
var namespace string
namespace = ""
args := strings.Fields(command)
if args[0] != "!k" {
return
}
if args[1] == "get" {
switch {
case args[2] == "pods":
if len(args) == 4 {
namespace = args[3]
}
kubehandler.GetPods(s, m, namespace)
case args[2] == "ns":
kubehandler.GetNamespace(s, m)
case args[2] == "deploy":
if len(args) == 4 {
namespace = args[3]
}
kubehandler.GetDeploy(s, m, namespace)
case args[2] == "svc":
if len(args) == 4 {
namespace = args[3]
}
kubehandler.GetSvc(s, m, namespace)
case args[2] == "ingress":
if len(args) == 4 {
namespace = args[3]
}
kubehandler.GetIngress(s, m, namespace)
case args[2] == "cm":
if len(args) == 4 {
namespace = args[3]
}
kubehandler.GetConfigMap(s, m, namespace)
case args[2] == "nodes":
kubehandler.GetNodes(s, m)
}
} else if args[1] == "logs" {
kubehandler.GetPodLogs(s, m, args[2], args[3])
} else if args[1] == "scale" {
kubehandler.UpdateDeployment(s, m, args[2], args[3], args[4])
} else if args[1] == "delete" {
switch {
case args[2] == "ns":
kubehandler.DeleteNamespace(s, m, args[3])
case args[2] == "deploy":
if len(args) == 5 {
namespace = args[4]
}
kubehandler.DeleteDeployment(s, m, args[3], namespace)
case args[2] == "svc":
if len(args) == 5 {
namespace = args[4]
}
kubehandler.DeleteSvc(s, m, args[3], namespace)
case args[2] == "ingress":
if len(args) == 5 {
namespace = args[4]
}
kubehandler.DeleteIngress(s, m, args[3], namespace)
case args[2] == "cm":
if len(args) == 5 {
namespace = args[4]
}
kubehandler.DeleteCm(s, m, args[3], namespace)
}
} else if args[1] == "help" {
msg := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{
Name: "kube-bot help menu",
},
Description: `To use enter **!k** followed by command, object and namespace
`,
Footer: &discordgo.MessageEmbedFooter{
Text: "kube-bot build using client-go and discordgo, source code at https://github.com/jeevanshu/kube-bot",
},
Fields: []*discordgo.MessageEmbedField{
{
Name: "get",
Value: `
Use this command to read objects **!k get** followed by object
Available objects are:
- pods
- svc
- deploy
- ns
- cm
- nodes
- ingress
example: **!k get deploy test-ns**
`,
},
{
Name: "logs",
Value: `
Use this command to get logs from pod **!k logs <pod-name> <namespace>**
example: **!k logs nginx nginx-namespace**
`,
},
{
Name: "scale",
Value: `
To scale up and down number of replicas of deployment **!k scale <namespace> <deployment> <replicas>**
examoke **!k scale default nginx 3**
`,
},
{
Name: "delete",
Value: `
To delete objects **!k delete <object> <object-name> <namespace>** and give confirmation to message
examoke **!k delete deploy nginx default**
Available objects are:
- svc
- deploy
- ns
- cm
- ingress
`,
},
},
}
s.ChannelMessageSendEmbed(m.ChannelID, msg)
}
}