forked from kubernetes/ingress-nginx
/
main.go
251 lines (215 loc) · 5.6 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
248
249
250
251
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"github.com/spf13/cobra"
"k8s.io/ingress-nginx/internal/nginx"
)
const (
backendsPath = "/configuration/backends"
generalPath = "/configuration/general"
certsPath = "/configuration/certs"
)
func main() {
rootCmd := &cobra.Command{
Use: "dbg",
Short: "dbg is a tool for quickly inspecting the state of the nginx instance",
}
backendsCmd := &cobra.Command{
Use: "backends",
Short: "Inspect the dynamically-loaded backends information",
}
rootCmd.AddCommand(backendsCmd)
backendsAllCmd := &cobra.Command{
Use: "all",
Short: "Output the all dynamic backend information as a JSON array",
Run: func(_ *cobra.Command, _ []string) {
backendsAll()
},
}
backendsCmd.AddCommand(backendsAllCmd)
backendsListCmd := &cobra.Command{
Use: "list",
Short: "Output a newline-separated list of the backend names",
Run: func(_ *cobra.Command, _ []string) {
backendsList()
},
}
backendsCmd.AddCommand(backendsListCmd)
backendsGetCmd := &cobra.Command{
Use: "get [backend name]",
Short: "Output the backend information only for the backend that has this name",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
backendsGet(args[0])
},
}
backendsCmd.AddCommand(backendsGetCmd)
certCmd := &cobra.Command{
Use: "certs",
Short: "Inspect dynamic SSL certificates",
}
certGetCmd := &cobra.Command{
Use: "get [hostname]",
Short: "Get the dynamically-loaded certificate information for the given hostname",
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
certGet(args[0])
return nil
},
}
certCmd.AddCommand(certGetCmd)
rootCmd.AddCommand(certCmd)
generalCmd := &cobra.Command{
Use: "general",
Short: "Output the general dynamic lua state",
Run: func(_ *cobra.Command, _ []string) {
general()
},
}
rootCmd.AddCommand(generalCmd)
confCmd := &cobra.Command{
Use: "conf",
Short: "Dump the contents of /etc/nginx/nginx.conf",
Run: func(_ *cobra.Command, _ []string) {
readNginxConf()
},
}
rootCmd.AddCommand(confCmd)
rootCmd.PersistentFlags().IntVar(&nginx.StatusPort, "status-port", 10246, `Port to use for the lua HTTP endpoint configuration.`)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func backendsAll() {
statusCode, body, requestErr := nginx.NewGetStatusRequest(backendsPath)
if requestErr != nil {
fmt.Println(requestErr)
return
}
if statusCode != 200 {
fmt.Printf("Nginx returned code %v\n", statusCode)
return
}
var prettyBuffer bytes.Buffer
indentErr := json.Indent(&prettyBuffer, body, "", " ")
if indentErr != nil {
fmt.Println(indentErr)
return
}
fmt.Println(prettyBuffer.String())
}
func backendsList() {
statusCode, body, requestErr := nginx.NewGetStatusRequest(backendsPath)
if requestErr != nil {
fmt.Println(requestErr)
return
}
if statusCode != 200 {
fmt.Printf("Nginx returned code %v\n", statusCode)
return
}
var f interface{}
unmarshalErr := json.Unmarshal(body, &f)
if unmarshalErr != nil {
fmt.Println(unmarshalErr)
return
}
backends, ok := f.([]interface{})
if !ok {
fmt.Printf("unexpected type: %T", f)
}
for _, backendi := range backends {
backend, ok := backendi.(map[string]interface{})
if !ok {
fmt.Printf("unexpected type: %T", backendi)
}
fmt.Println(backend["name"].(string))
}
}
func backendsGet(name string) {
statusCode, body, requestErr := nginx.NewGetStatusRequest(backendsPath)
if requestErr != nil {
fmt.Println(requestErr)
return
}
if statusCode != 200 {
fmt.Printf("Nginx returned code %v\n", statusCode)
return
}
var f interface{}
unmarshalErr := json.Unmarshal(body, &f)
if unmarshalErr != nil {
fmt.Println(unmarshalErr)
return
}
backends, ok := f.([]interface{})
if !ok {
fmt.Printf("unexpected type: %T", f)
}
for _, backendi := range backends {
backend, ok := backendi.(map[string]interface{})
if !ok {
fmt.Printf("unexpected type: %T", backendi)
}
if backend["name"].(string) == name {
printed, err := json.MarshalIndent(backend, "", " ")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(printed))
return
}
}
fmt.Println("A backend of this name was not found.")
}
func certGet(host string) {
statusCode, body, requestErr := nginx.NewGetStatusRequest(certsPath + "?hostname=" + host)
if requestErr != nil {
fmt.Println(requestErr)
return
}
if statusCode == 200 {
fmt.Print(string(body))
return
} else if statusCode != 404 {
fmt.Printf("Nginx returned code %v\n", statusCode)
fmt.Println(string(body))
return
}
fmt.Printf("No cert found for host %v\n", host)
}
func general() {
// TODO: refactor to obtain ingress-nginx pod count from the api server
var prettyBuffer bytes.Buffer
indentErr := json.Indent(&prettyBuffer, []byte("{}"), "", " ")
if indentErr != nil {
fmt.Println(indentErr)
return
}
fmt.Println(prettyBuffer.String())
}
func readNginxConf() {
conf, err := nginx.ReadNginxConf()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(conf)
}