Add -log-level flag for install and inject commands (#239)

* Add -log-level flag for install and inject commands

Signed-off-by: Kevin Lingerfelt <kl@buoyant.io>

* Turn off all CLI logging by default, rename inject and install flags

Signed-off-by: Kevin Lingerfelt <kl@buoyant.io>

* Re-enable color logging

Signed-off-by: Kevin Lingerfelt <kl@buoyant.io>
This commit is contained in:
Kevin Lingerfelt 2018-02-01 12:38:07 -08:00 committed by GitHub
parent eddc37de28
commit 9ff439ef44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 25 deletions

View File

@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"fmt"
"log"
"github.com/spf13/cobra"
)
@ -36,13 +35,14 @@ var completionCmd = &cobra.Command{
Example: example,
Args: cobra.ExactArgs(1),
ValidArgs: []string{"bash", "zsh"},
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
out, err := getCompletion(args[0])
if err != nil {
log.Fatal(err.Error())
} else {
fmt.Printf(out)
return err
}
fmt.Printf(out)
return nil
},
}

View File

@ -2,11 +2,11 @@ package cmd
import (
"fmt"
"os"
"github.com/pkg/browser"
"github.com/runconduit/conduit/pkg/k8s"
"github.com/runconduit/conduit/pkg/shell"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@ -20,12 +20,13 @@ var dashboardCmd = &cobra.Command{
Long: "Open the Conduit dashboard in a web browser.",
RunE: func(cmd *cobra.Command, args []string) error {
if proxyPort <= 0 {
log.Fatalf("port must be positive, was %d", proxyPort)
return fmt.Errorf("port must be positive, was %d", proxyPort)
}
kubectl, err := k8s.NewKubectl(shell.NewUnixShell())
if err != nil {
log.Fatalf("Failed to start kubectl: %v", err)
fmt.Fprintf(os.Stderr, "Failed to start kubectl: %s", err)
os.Exit(1)
}
asyncProcessErr := make(chan error, 1)
@ -33,26 +34,30 @@ var dashboardCmd = &cobra.Command{
err = kubectl.StartProxy(asyncProcessErr, proxyPort)
if err != nil {
log.Fatalf("Failed to start kubectl proxy: %v", err)
fmt.Fprintf(os.Stderr, "Failed to start kubectl proxy: %s", err)
os.Exit(1)
}
url, err := kubectl.UrlFor(controlPlaneNamespace, "/services/web:http/proxy/")
if err != nil {
log.Fatalf("Failed to generate URL for dashboard: %v", err)
fmt.Fprintf(os.Stderr, "Failed to generate URL for dashboard: %s", err)
os.Exit(1)
}
fmt.Printf("Opening [%s] in the default browser\n", url)
err = browser.OpenURL(url.String())
if err != nil {
log.Fatalf("failed to open URL %s in the default browser: %v", url, err)
fmt.Fprintf(os.Stderr, "Failed to open URL %s in the default browser: %s", url, err)
os.Exit(1)
}
select {
case err = <-asyncProcessErr:
if err != nil {
log.Fatalf("Error starting proxy via kubectl: %v", err)
fmt.Fprintf(os.Stderr, "Error starting proxy via kubectl: %s", err)
os.Exit(1)
}
}
close(asyncProcessErr)

View File

@ -28,6 +28,7 @@ var (
ignoreOutboundPorts []uint
proxyControlPort uint
proxyAPIPort uint
proxyLogLevel string
conduitCreatedByAnnotation = "conduit.io/created-by"
conduitProxyVersionAnnotation = "conduit.io/proxy-version"
conduitControlLabel = "conduit.io/controller"
@ -259,7 +260,7 @@ func injectPodTemplateSpec(t *v1.PodTemplateSpec) enhancedPodTemplateSpec {
},
},
Env: []v1.EnvVar{
v1.EnvVar{Name: "CONDUIT_PROXY_LOG", Value: "warn,conduit_proxy=info"},
v1.EnvVar{Name: "CONDUIT_PROXY_LOG", Value: proxyLogLevel},
v1.EnvVar{
Name: "CONDUIT_PROXY_CONTROL_URL",
Value: fmt.Sprintf("tcp://proxy-api.%s.svc.cluster.local:%d", controlPlaneNamespace, proxyAPIPort),
@ -386,4 +387,5 @@ func init() {
injectCmd.PersistentFlags().UintSliceVar(&ignoreOutboundPorts, "skip-outbound-ports", nil, "outbound ports that should skip the proxy")
injectCmd.PersistentFlags().UintVar(&proxyControlPort, "control-port", 4190, "proxy port to use for control")
injectCmd.PersistentFlags().UintVar(&proxyAPIPort, "api-port", 8086, "port where the Conduit controller is running")
injectCmd.PersistentFlags().StringVar(&proxyLogLevel, "proxy-log-level", "warn,conduit_proxy=info", "log level for the proxy")
}

View File

@ -2,13 +2,13 @@ package cmd
import (
"fmt"
"log"
"os"
"regexp"
"text/template"
"github.com/runconduit/conduit/pkg/version"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@ -167,6 +167,7 @@ spec:
- "-metrics-addr=:9995"
- "-telemetry-addr=127.0.0.1:8087"
- "-tap-addr=127.0.0.1:8088"
- "-log-level={{.ControllerLogLevel}}"
- name: destination
ports:
- name: grpc
@ -179,6 +180,7 @@ spec:
- "destination"
- "-addr=:8089"
- "-metrics-addr=:9999"
- "-log-level={{.ControllerLogLevel}}"
- name: proxy-api
ports:
- name: grpc
@ -193,6 +195,7 @@ spec:
- "-metrics-addr=:9996"
- "-destination-addr=:8089"
- "-telemetry-addr=:8087"
- "-log-level={{.ControllerLogLevel}}"
- name: tap
ports:
- name: grpc
@ -205,6 +208,7 @@ spec:
- "tap"
- "-addr=:8088"
- "-metrics-addr=:9998"
- "-log-level={{.ControllerLogLevel}}"
- name: telemetry
ports:
- name: grpc
@ -219,6 +223,7 @@ spec:
- "-metrics-addr=:9997"
- "-ignore-namespaces=kube-system"
- "-prometheus-url=http://prometheus:9090"
- "-log-level={{.ControllerLogLevel}}"
### Web ###
---
@ -281,6 +286,7 @@ spec:
- "-static-dir=/dist"
- "-template-dir=/templates"
- "-uuid={{.UUID}}"
- "-log-level={{.ControllerLogLevel}}"
### Prometheus ###
---
@ -397,6 +403,7 @@ type installConfig struct {
ImagePullPolicy string
UUID string
CliVersion string
ControllerLogLevel string
}
var (
@ -406,6 +413,7 @@ var (
webReplicas uint
prometheusReplicas uint
imagePullPolicy string
controllerLogLevel string
)
var installCmd = &cobra.Command{
@ -414,7 +422,7 @@ var installCmd = &cobra.Command{
Long: "Output Kubernetes configs to install Conduit.",
RunE: func(cmd *cobra.Command, args []string) error {
if err := validate(); err != nil {
log.Fatal(err.Error())
return err
}
template, err := template.New("conduit").Parse(conduitTemplate)
if err != nil {
@ -431,6 +439,7 @@ var installCmd = &cobra.Command{
ImagePullPolicy: imagePullPolicy,
UUID: uuid.NewV4().String(),
CliVersion: fmt.Sprintf("conduit/cli %s", version.Version),
ControllerLogLevel: controllerLogLevel,
})
return nil
},
@ -453,7 +462,10 @@ func validate() error {
return fmt.Errorf("%s is not a valid Docker registry", dockerRegistry)
}
if imagePullPolicy != "Always" && imagePullPolicy != "IfNotPresent" && imagePullPolicy != "Never" {
return fmt.Errorf("imagePullPolicy must be one of Always, IfNotPresent, or Never")
return fmt.Errorf("--image-pull-policy must be one of: Always, IfNotPresent, Never")
}
if _, err := log.ParseLevel(controllerLogLevel); err != nil {
return fmt.Errorf("--controller-log-level must be one of: panic, fatal, error, warn, info, debug")
}
return nil
}
@ -466,4 +478,5 @@ func init() {
installCmd.PersistentFlags().UintVar(&webReplicas, "web-replicas", 1, "replicas of the web server to deploy")
installCmd.PersistentFlags().UintVar(&prometheusReplicas, "prometheus-replicas", 1, "replicas of prometheus to deploy")
installCmd.PersistentFlags().StringVar(&imagePullPolicy, "image-pull-policy", "IfNotPresent", "Docker image pull policy")
installCmd.PersistentFlags().StringVar(&controllerLogLevel, "controller-log-level", "info", "log level for the controller and web components")
}

View File

@ -13,25 +13,25 @@ var cfgFile string
var controlPlaneNamespace string
var apiAddr string // An empty value means "use the Kubernetes configuration"
var kubeconfigPath string
var logLevel string
var verbose bool
var RootCmd = &cobra.Command{
Use: "conduit",
Short: "conduit manages the Conduit service mesh",
Long: `conduit manages the Conduit service mesh.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// set global log level
level, err := log.ParseLevel(logLevel)
if err != nil {
log.Fatalf("invalid log-level: %s", logLevel)
// enable / disable logging
if verbose {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.PanicLevel)
}
log.SetLevel(level)
},
}
func init() {
RootCmd.PersistentFlags().StringVarP(&controlPlaneNamespace, "conduit-namespace", "n", "conduit", "namespace in which Conduit is installed")
RootCmd.PersistentFlags().StringVar(&logLevel, "log-level", log.FatalLevel.String(), "log level, must be one of: panic, fatal, error, warn, info, debug")
RootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "turn on debug logging")
}
// TODO: decide if we want to use viper

View File

@ -23,7 +23,7 @@ var versionCmd = &cobra.Command{
client, err := newPublicAPIClient()
if err != nil {
fmt.Fprintf(os.Stderr, "Error connecting to server: %s\n", err)
return
os.Exit(1)
}
fmt.Printf("Server version: %s\n", getServerVersion(client))

View File

@ -181,17 +181,21 @@ func NewServer(addr, prometheusUrl string, ignoredNamespaces []string, kubeconfi
}
func (s *server) Query(ctx context.Context, req *read.QueryRequest) (*read.QueryResponse, error) {
log.Debugf("Query request: %+v", req)
start := time.Unix(0, req.StartMs*int64(time.Millisecond))
end := time.Unix(0, req.EndMs*int64(time.Millisecond))
step, err := time.ParseDuration(req.Step)
if err != nil {
log.Errorf("ParseDuration(%+v) failed with: %+v", req.Step, err)
return nil, err
}
queryRange := v1.Range{Start: start, End: end, Step: step}
res, err := s.prometheusApi.QueryRange(ctx, req.Query, queryRange)
if err != nil {
log.Errorf("QueryRange(%+v, %+v) failed with: %+v", req.Query, queryRange, err)
return nil, err
}
@ -208,6 +212,7 @@ func (s *server) Query(ctx context.Context, req *read.QueryRequest) (*read.Query
}
func (s *server) ListPods(ctx context.Context, req *read.ListPodsRequest) (*public.ListPodsResponse, error) {
log.Debugf("ListPods request: %+v", req)
pods, err := s.pods.List()
if err != nil {
@ -259,13 +264,15 @@ func (s *server) ListPods(ctx context.Context, req *read.ListPodsRequest) (*publ
}
func (s *server) Report(ctx context.Context, req *write.ReportRequest) (*write.ReportResponse, error) {
log.Debugf("Report request: %+v", req)
id := "unknown"
if req.Process != nil {
id = req.Process.ScheduledNamespace + "/" + req.Process.ScheduledInstance
}
log := log.WithFields(log.Fields{"id": id})
log.Debugf("received report with %d requests", len(req.Requests))
log.Debugf("Received report with %d requests", len(req.Requests))
s.instances.update(id)