diff --git a/BUILD.bazel b/BUILD.bazel index 7d8bb39c4..4b01d2814 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -46,6 +46,7 @@ filegroup( "//pkg/client/listers/certmanager/v1alpha1:all-srcs", "//pkg/controller:all-srcs", "//pkg/issuer:all-srcs", + "//pkg/logs:all-srcs", "//pkg/scheduler:all-srcs", "//pkg/util:all-srcs", "//test/e2e:all-srcs", diff --git a/cmd/acmesolver/BUILD.bazel b/cmd/acmesolver/BUILD.bazel index 65cff9750..4fc39ab30 100644 --- a/cmd/acmesolver/BUILD.bazel +++ b/cmd/acmesolver/BUILD.bazel @@ -16,7 +16,10 @@ go_library( srcs = ["main.go"], importpath = "github.com/jetstack/cert-manager/cmd/acmesolver", visibility = ["//visibility:private"], - deps = ["//pkg/issuer/acme/http/solver:go_default_library"], + deps = [ + "//pkg/issuer/acme/http/solver:go_default_library", + "//pkg/logs:go_default_library", + ], ) go_binary( diff --git a/cmd/acmesolver/main.go b/cmd/acmesolver/main.go index 9dc27b855..e5cf6992c 100644 --- a/cmd/acmesolver/main.go +++ b/cmd/acmesolver/main.go @@ -21,6 +21,7 @@ import ( "log" "github.com/jetstack/cert-manager/pkg/issuer/acme/http/solver" + "github.com/jetstack/cert-manager/pkg/logs" ) // acmesolver solves ACME http-01 challenges. This is intended to run as a pod @@ -35,6 +36,9 @@ var ( ) func main() { + logs.InitLogs() + defer logs.FlushLogs() + flag.Parse() s := &solver.HTTP01Solver{ diff --git a/cmd/controller/BUILD.bazel b/cmd/controller/BUILD.bazel index 296c10ceb..9e9190acb 100644 --- a/cmd/controller/BUILD.bazel +++ b/cmd/controller/BUILD.bazel @@ -33,6 +33,7 @@ go_library( "//pkg/issuer/ca:go_default_library", "//pkg/issuer/selfsigned:go_default_library", "//pkg/issuer/vault:go_default_library", + "//pkg/logs:go_default_library", "//pkg/util:go_default_library", "//vendor/github.com/golang/glog:go_default_library", "//vendor/github.com/gorilla/mux:go_default_library", diff --git a/cmd/controller/main.go b/cmd/controller/main.go index f4107369f..06e4f255a 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -23,9 +23,13 @@ import ( "syscall" "github.com/golang/glog" + + "github.com/jetstack/cert-manager/pkg/logs" ) func main() { + logs.InitLogs() + defer logs.FlushLogs() stopCh := SetupSignalHandler() cmd := NewCommandStartCertManagerController(os.Stdout, os.Stderr, stopCh) diff --git a/cmd/webhook/BUILD.bazel b/cmd/webhook/BUILD.bazel index b08f0ca37..74e12afd0 100644 --- a/cmd/webhook/BUILD.bazel +++ b/cmd/webhook/BUILD.bazel @@ -18,6 +18,7 @@ go_library( visibility = ["//visibility:private"], deps = [ "//pkg/apis/certmanager/validation/webhooks:go_default_library", + "//pkg/logs:go_default_library", "//vendor/github.com/openshift/generic-admission-server/pkg/cmd:go_default_library", ], ) diff --git a/cmd/webhook/main.go b/cmd/webhook/main.go index c09a8ad1b..c39203e21 100644 --- a/cmd/webhook/main.go +++ b/cmd/webhook/main.go @@ -19,8 +19,10 @@ package main import ( "flag" - "github.com/jetstack/cert-manager/pkg/apis/certmanager/validation/webhooks" "github.com/openshift/generic-admission-server/pkg/cmd" + + "github.com/jetstack/cert-manager/pkg/apis/certmanager/validation/webhooks" + "github.com/jetstack/cert-manager/pkg/logs" ) var certHook cmd.ValidatingAdmissionHook = &webhooks.CertificateAdmissionHook{} @@ -28,6 +30,9 @@ var issuerHook cmd.ValidatingAdmissionHook = &webhooks.IssuerAdmissionHook{} var clusterIssuerHook cmd.ValidatingAdmissionHook = &webhooks.ClusterIssuerAdmissionHook{} func main() { + logs.InitLogs() + defer logs.FlushLogs() + // Avoid "logging before flag.Parse" errors from glog flag.CommandLine.Parse([]string{}) diff --git a/pkg/logs/BUILD.bazel b/pkg/logs/BUILD.bazel new file mode 100644 index 000000000..a974bd549 --- /dev/null +++ b/pkg/logs/BUILD.bazel @@ -0,0 +1,27 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["logs.go"], + importpath = "github.com/jetstack/cert-manager/pkg/logs", + visibility = ["//visibility:public"], + deps = [ + "//vendor/github.com/golang/glog:go_default_library", + "//vendor/github.com/spf13/pflag:go_default_library", + "//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/pkg/logs/logs.go b/pkg/logs/logs.go new file mode 100644 index 000000000..fc5edf77c --- /dev/null +++ b/pkg/logs/logs.go @@ -0,0 +1,61 @@ +/* +Copyright 2018 The Jetstack cert-manager contributors. + +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 logs + +import ( + "flag" + "log" + "time" + + "github.com/golang/glog" + "github.com/spf13/pflag" + "k8s.io/apimachinery/pkg/util/wait" +) + +var logFlushFreq = pflag.Duration("log-flush-frequency", 5*time.Second, "Maximum number of seconds between log flushes") + +// TODO(thockin): This is temporary until we agree on log dirs and put those into each cmd. +func init() { + flag.Set("logtostderr", "true") +} + +// GlogWriter serves as a bridge between the standard log package and the glog package. +type GlogWriter struct{} + +// Write implements the io.Writer interface. +func (writer GlogWriter) Write(data []byte) (n int, err error) { + glog.Info(string(data)) + return len(data), nil +} + +// InitLogs initializes logs the way we want for kubernetes. +func InitLogs() { + log.SetOutput(GlogWriter{}) + log.SetFlags(0) + // The default glog flush interval is 30 seconds, which is frighteningly long. + go wait.Until(glog.Flush, *logFlushFreq, wait.NeverStop) +} + +// FlushLogs flushes logs immediately. +func FlushLogs() { + glog.Flush() +} + +// NewLogger creates a new log.Logger which sends logs to glog.Info. +func NewLogger(prefix string) *log.Logger { + return log.New(GlogWriter{}, prefix, 0) +} diff --git a/test/e2e/BUILD.bazel b/test/e2e/BUILD.bazel index 1e4f8c446..9f7110be7 100644 --- a/test/e2e/BUILD.bazel +++ b/test/e2e/BUILD.bazel @@ -30,6 +30,7 @@ go_test( embed = [":go_default_library"], tags = ["manual"], deps = [ + "//pkg/logs:go_default_library", "//test/e2e/framework:go_default_library", "//test/e2e/suite:go_default_library", "//vendor/github.com/onsi/ginkgo:go_default_library", diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index 9748868f6..38b6ee2c4 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -29,6 +29,7 @@ import ( "github.com/onsi/gomega" "k8s.io/apimachinery/pkg/util/wait" + "github.com/jetstack/cert-manager/pkg/logs" "github.com/jetstack/cert-manager/test/e2e/framework" _ "github.com/jetstack/cert-manager/test/e2e/suite" ) @@ -48,6 +49,9 @@ func init() { } func TestE2E(t *testing.T) { + logs.InitLogs() + defer logs.FlushLogs() + if err := framework.DefaultConfig.Validate(); err != nil { t.Errorf("Invalid test config: %v", err) t.Fail()