Skip to content

Commit f1e2904

Browse files
author
Ayush Sobti
authored
Merge pull request #41 from asobti/loglevel
Add option to set loglevel in kube client
2 parents dcf224a + 22ab719 commit f1e2904

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ A single line gets ignored if the first non-blank character is # in that line.
8181
* `POLL_INTERVAL_SECONDS` - (int) Number of seconds to wait between each check for new commits to the repo (default is 5). Set to 0 to disable the wait period.
8282
* <a name="run-interval"></a>`FULL_RUN_INTERVAL_SECONDS` - (int) Number of seconds between automatic full runs (default is 300, or 5 minutes). Set to 0 to disable the wait period.
8383
* `DIFF_URL_FORMAT` - (string) If specified, allows the status page to display a link to the source code referencing the diff for a specific commit. `DIFF_URL_FORMAT` should be a URL for a hosted remote repo that supports linking to a commit hash. Replace the commit hash portion with "%s" so it can be filled in by kube-applier (e.g. `https://github.com/kubernetes/kubernetes/commit/%s`).
84+
* `LOG_LEVEL` - (int) Sets the `-v` flag on all `kubectl` commands run. Use this option to configure more verbose logging. If not specified, the `-v` flag is not set on `kubectl` commands defaulting to standard log verbosity.
8485

8586
### Mounting the Git Repository
8687

kube/client.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package kube
22

33
import (
44
"fmt"
5-
"github.com/box/kube-applier/sysutil"
65
"io/ioutil"
76
"log"
87
"os/exec"
98
"regexp"
109
"strconv"
1110
"strings"
11+
12+
"github.com/box/kube-applier/sysutil"
1213
)
1314

1415
const (
@@ -31,6 +32,8 @@ type Client struct {
3132
Server string
3233
// Location of the written kubeconfig file within the container
3334
kubeconfigFilePath string
35+
// if <0, no verbosity level is specified in the commands run
36+
LogLevel int
3437
}
3538

3639
// Configure writes the kubeconfig file to be used for authenticating kubectl commands.
@@ -42,7 +45,7 @@ func (c *Client) Configure() error {
4245

4346
f, err := ioutil.TempFile("", "kubeConfig")
4447
c.kubeconfigFilePath = f.Name()
45-
log.Printf("Using kubeConfig file:", c.kubeconfigFilePath)
48+
log.Printf("Using kubeConfig file: %s", c.kubeconfigFilePath)
4649

4750
if err != nil {
4851
return fmt.Errorf("Error creating kubeconfig file: %v", err)
@@ -75,6 +78,9 @@ func (c *Client) Configure() error {
7578
// CheckVersion returns an error if the server and client have incompatible versions, otherwise returns nil.
7679
func (c *Client) CheckVersion() error {
7780
args := []string{"kubectl", "version"}
81+
if c.LogLevel > -1 {
82+
args = append(args, fmt.Sprintf("-v=%d", c.LogLevel))
83+
}
7884
if c.Server != "" {
7985
args = append(args, fmt.Sprintf("--kubeconfig=%s", c.kubeconfigFilePath))
8086
}
@@ -126,6 +132,9 @@ func isCompatible(clientMajor, clientMinor, serverMajor, serverMinor string) err
126132
// It returns the full apply command and its output.
127133
func (c *Client) Apply(path string) (cmd, output string, err error) {
128134
args := []string{"kubectl", "apply", "-f", path}
135+
if c.LogLevel > -1 {
136+
args = append(args, fmt.Sprintf("-v=%d", c.LogLevel))
137+
}
129138
if c.Server != "" {
130139
args = append(args, fmt.Sprintf("--kubeconfig=%s", c.kubeconfigFilePath))
131140
}

main.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package main
22

33
import (
4+
"log"
5+
"strings"
6+
"time"
7+
48
"github.com/box/kube-applier/applylist"
59
"github.com/box/kube-applier/git"
610
"github.com/box/kube-applier/kube"
711
"github.com/box/kube-applier/metrics"
812
"github.com/box/kube-applier/run"
913
"github.com/box/kube-applier/sysutil"
1014
"github.com/box/kube-applier/webserver"
11-
"log"
12-
"strings"
13-
"time"
1415
)
1516

1617
const (
@@ -30,6 +31,7 @@ func main() {
3031
listenPort := sysutil.GetRequiredEnvInt("LISTEN_PORT")
3132
server := sysutil.GetEnvStringOrDefault("SERVER", "")
3233
blacklistPath := sysutil.GetEnvStringOrDefault("BLACKLIST_PATH", "")
34+
logLevel := sysutil.GetEnvIntOrDefault("LOG_LEVEL", -1)
3335

3436
// A file that contains a list of files to consider for application.
3537
// If the env var is not defined or if the file is empty act like a no-op and
@@ -49,7 +51,10 @@ func main() {
4951
log.Fatal(err)
5052
}
5153

52-
kubeClient := &kube.Client{Server: server}
54+
kubeClient := &kube.Client{
55+
Server: server,
56+
LogLevel: logLevel,
57+
}
5358
kubeClient.Configure()
5459

5560
gitUtil := &git.GitUtil{repoPath}

0 commit comments

Comments
 (0)