Skip to content

Commit

Permalink
Merge pull request #41 from asobti/loglevel
Browse files Browse the repository at this point in the history
Add option to set loglevel in kube client
  • Loading branch information
Ayush Sobti authored Sep 25, 2019
2 parents dcf224a + 22ab719 commit f1e2904
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ A single line gets ignored if the first non-blank character is # in that line.
* `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.
* <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.
* `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`).
* `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.

### Mounting the Git Repository

Expand Down
13 changes: 11 additions & 2 deletions kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package kube

import (
"fmt"
"github.com/box/kube-applier/sysutil"
"io/ioutil"
"log"
"os/exec"
"regexp"
"strconv"
"strings"

"github.com/box/kube-applier/sysutil"
)

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

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

f, err := ioutil.TempFile("", "kubeConfig")
c.kubeconfigFilePath = f.Name()
log.Printf("Using kubeConfig file:", c.kubeconfigFilePath)
log.Printf("Using kubeConfig file: %s", c.kubeconfigFilePath)

if err != nil {
return fmt.Errorf("Error creating kubeconfig file: %v", err)
Expand Down Expand Up @@ -75,6 +78,9 @@ func (c *Client) Configure() error {
// CheckVersion returns an error if the server and client have incompatible versions, otherwise returns nil.
func (c *Client) CheckVersion() error {
args := []string{"kubectl", "version"}
if c.LogLevel > -1 {
args = append(args, fmt.Sprintf("-v=%d", c.LogLevel))
}
if c.Server != "" {
args = append(args, fmt.Sprintf("--kubeconfig=%s", c.kubeconfigFilePath))
}
Expand Down Expand Up @@ -126,6 +132,9 @@ func isCompatible(clientMajor, clientMinor, serverMajor, serverMinor string) err
// It returns the full apply command and its output.
func (c *Client) Apply(path string) (cmd, output string, err error) {
args := []string{"kubectl", "apply", "-f", path}
if c.LogLevel > -1 {
args = append(args, fmt.Sprintf("-v=%d", c.LogLevel))
}
if c.Server != "" {
args = append(args, fmt.Sprintf("--kubeconfig=%s", c.kubeconfigFilePath))
}
Expand Down
13 changes: 9 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package main

import (
"log"
"strings"
"time"

"github.com/box/kube-applier/applylist"
"github.com/box/kube-applier/git"
"github.com/box/kube-applier/kube"
"github.com/box/kube-applier/metrics"
"github.com/box/kube-applier/run"
"github.com/box/kube-applier/sysutil"
"github.com/box/kube-applier/webserver"
"log"
"strings"
"time"
)

const (
Expand All @@ -30,6 +31,7 @@ func main() {
listenPort := sysutil.GetRequiredEnvInt("LISTEN_PORT")
server := sysutil.GetEnvStringOrDefault("SERVER", "")
blacklistPath := sysutil.GetEnvStringOrDefault("BLACKLIST_PATH", "")
logLevel := sysutil.GetEnvIntOrDefault("LOG_LEVEL", -1)

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

kubeClient := &kube.Client{Server: server}
kubeClient := &kube.Client{
Server: server,
LogLevel: logLevel,
}
kubeClient.Configure()

gitUtil := &git.GitUtil{repoPath}
Expand Down

0 comments on commit f1e2904

Please sign in to comment.