Skip to content

Commit

Permalink
Merge pull request #3686 from Shopify/dbg-tool
Browse files Browse the repository at this point in the history
Add debug binary to the docker image
  • Loading branch information
k8s-ci-robot authored Feb 11, 2019
2 parents 4b64453 + 190b60f commit 9ba6799
Show file tree
Hide file tree
Showing 27 changed files with 4,356 additions and 34 deletions.
17 changes: 17 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ container: clean-container .container-$(ARCH)
@echo "+ Copying artifact to temporary directory"
mkdir -p $(TEMP_DIR)/rootfs
cp bin/$(ARCH)/nginx-ingress-controller $(TEMP_DIR)/rootfs/nginx-ingress-controller

cp bin/$(ARCH)/dbg $(TEMP_DIR)/rootfs/dbg
@echo "+ Building container image $(MULTI_ARCH_IMG):$(TAG)"
cp -RP ./* $(TEMP_DIR)
$(SED_I) "s|BASEIMAGE|$(BASEIMAGE)|g" $(DOCKERFILE)
Expand Down
8 changes: 8 additions & 0 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ go build \
-X ${PKG}/version.COMMIT=${GIT_COMMIT} \
-X ${PKG}/version.REPO=${REPO_INFO}" \
-o bin/${ARCH}/nginx-ingress-controller ${PKG}/cmd/nginx

go build \
${GOBUILD_FLAGS} \
-ldflags "-s -w \
-X ${PKG}/version.RELEASE=${TAG} \
-X ${PKG}/version.COMMIT=${GIT_COMMIT} \
-X ${PKG}/version.REPO=${REPO_INFO}" \
-o bin/${ARCH}/dbg ${PKG}/cmd/dbg
203 changes: 203 additions & 0 deletions cmd/dbg/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/*
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"
"github.com/spf13/cobra"
"k8s.io/ingress-nginx/internal/nginx"
"os"
)

const (
backendsPath = "/configuration/backends"
generalPath = "/configuration/general"
)

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(cmd *cobra.Command, args []string) {
backendsAll()
},
}
backendsCmd.AddCommand(backendsAllCmd)

backendsListCmd := &cobra.Command{
Use: "list",
Short: "Output a newline-separated list of the backend names",
Run: func(cmd *cobra.Command, args []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(cmd *cobra.Command, args []string) {
backendsGet(args[0])
},
}
backendsCmd.AddCommand(backendsGetCmd)

generalCmd := &cobra.Command{
Use: "general",
Short: "Output the general dynamic lua state",
Run: func(cmd *cobra.Command, args []string) {
general()
},
}
rootCmd.AddCommand(generalCmd)

confCmd := &cobra.Command{
Use: "conf",
Short: "Dump the contents of /etc/nginx/nginx.conf",
Run: func(cmd *cobra.Command, args []string) {
readNginxConf()
},
}
rootCmd.AddCommand(confCmd)

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", statusCode)
return
}

var prettyBuffer bytes.Buffer
indentErr := json.Indent(&prettyBuffer, body, "", " ")
if indentErr != nil {
fmt.Println(indentErr)
return
}

fmt.Println(string(prettyBuffer.Bytes()))
}

func backendsList() {
statusCode, body, requestErr := nginx.NewGetStatusRequest(backendsPath)
if requestErr != nil {
fmt.Println(requestErr)
return
}
if statusCode != 200 {
fmt.Printf("Nginx returned code %v", statusCode)
return
}

var f interface{}
unmarshalErr := json.Unmarshal(body, &f)
if unmarshalErr != nil {
fmt.Println(unmarshalErr)
return
}
backends := f.([]interface{})

for _, backendi := range backends {
backend := backendi.(map[string]interface{})
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", statusCode)
return
}

var f interface{}
unmarshalErr := json.Unmarshal(body, &f)
if unmarshalErr != nil {
fmt.Println(unmarshalErr)
return
}
backends := f.([]interface{})

for _, backendi := range backends {
backend := backendi.(map[string]interface{})
if backend["name"].(string) == name {
printed, _ := json.MarshalIndent(backend, "", " ")
fmt.Println(string(printed))
return
}
}
fmt.Println("A backend of this name was not found.")
}

func general() {
statusCode, body, requestErr := nginx.NewGetStatusRequest(generalPath)
if requestErr != nil {
fmt.Println(requestErr)
return
}
if statusCode != 200 {
fmt.Printf("Nginx returned code %v", statusCode)
return
}

var prettyBuffer bytes.Buffer
indentErr := json.Indent(&prettyBuffer, body, "", " ")
if indentErr != nil {
fmt.Println(indentErr)
return
}

fmt.Println(string(prettyBuffer.Bytes()))
}

func readNginxConf() {
conf, err := nginx.ReadNginxConf()
if err != nil {
fmt.Println(err)
return
}

fmt.Println(conf)
}
81 changes: 81 additions & 0 deletions docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,87 @@ kube-system kube-dns ClusterIP 10.96.0.10 <none>
kube-system kubernetes-dashboard NodePort 10.103.128.17 <none> 80:30000/TCP 30m
```

Use the `/dbg` Tool to Check Dynamic Configuration

```console
$ kubectl exec -n <namespace-of-ingress-controller> nginx-ingress-controller-67956bf89d-fv58j /dbg
dbg is a tool for quickly inspecting the state of the nginx instance

Usage:
dbg [command]

Available Commands:
backends Inspect the dynamically-loaded backends information
conf Dump the contents of /etc/nginx/nginx.conf
general Output the general dynamic lua state
help Help about any command

Flags:
-h, --help help for dbg

Use "dbg [command] --help" for more information about a command.

```

```console
$ kubectl exec -n <namespace-of-ingress-controller> nginx-ingress-controller-67956bf89d-fv58j /dbg backends
Inspect the dynamically-loaded backends information.

Usage:
dbg backends [command]

Available Commands:
all Output the all dynamic backend information as a JSON array
get Output the backend information only for the backend that has this name
list Output a newline-separated list of the backend names

Flags:
-h, --help help for backends

Use "dbg backends [command] --help" for more information about a command.
```

```console
$ kubectl exec -n <namespace-of-ingress-controller> nginx-ingress-controller-67956bf89d-fv58j /dbg backends list
coffee-svc-80
tea-svc-80
upstream-default-backend
```

```console
$ kubectl exec -n <namespace-of-ingress-controller> nginx-ingress-controller-67956bf89d-fv58j /dbg backends get coffee-svc-80
{
"endpoints": [
{
"address": "10.1.1.112",
"port": "8080"
},
{
"address": "10.1.1.119",
"port": "8080"
},
{
"address": "10.1.1.121",
"port": "8080"
}
],
"load-balance": "ewma",
"name": "coffee-svc-80",
"noServer": false,
"port": 0,
"secureCACert": {
"caFilename": "",
"pemSha": "",
"secret": ""
},
"service": {
"metadata": {
"creationTimestamp": null
},
"spec": {
....
```

## Debug Logging

Using the flag `--v=XX` it is possible to increase the level of logging. This is performed by editing
Expand Down
16 changes: 16 additions & 0 deletions internal/nginx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"time"

"github.com/tv42/httpunix"
Expand Down Expand Up @@ -87,6 +88,21 @@ func NewPostStatusRequest(path, contentType string, data interface{}) (int, []by
return res.StatusCode, body, nil
}

// ReadNginxConf reads the nginx configuration file into a string
func ReadNginxConf() (string, error) {
confFile, err := os.Open("/etc/nginx/nginx.conf")
if err != nil {
return "", err
}
defer confFile.Close()

contents, err := ioutil.ReadAll(confFile)
if err != nil {
return "", err
}
return string(contents), nil
}

func buildUnixSocketClient() *http.Client {
u := &httpunix.Transport{
DialTimeout: 1 * time.Second,
Expand Down
Loading

0 comments on commit 9ba6799

Please sign in to comment.