Skip to content

Commit

Permalink
create DefaultFieldsFormatter and inject component into all prow mess…
Browse files Browse the repository at this point in the history
…ages
  • Loading branch information
BenTheElder committed Feb 15, 2018
1 parent f5b517c commit 0811332
Show file tree
Hide file tree
Showing 24 changed files with 255 additions and 140 deletions.
1 change: 1 addition & 0 deletions experiment/nursery/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_library(
deps = [
"//experiment/nursery/diskcache:go_default_library",
"//experiment/nursery/diskutil:go_default_library",
"//prow/logrusutil:go_default_library",
"//vendor/github.com/sirupsen/logrus:go_default_library",
],
)
Expand Down
19 changes: 2 additions & 17 deletions experiment/nursery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (

"k8s.io/test-infra/experiment/nursery/diskcache"
"k8s.io/test-infra/experiment/nursery/diskutil"
"k8s.io/test-infra/prow/logrusutil"

"github.com/sirupsen/logrus"
)
Expand All @@ -64,24 +65,8 @@ var diskCheckInterval = flag.Duration("disk-check-interval", time.Minute,
var remount = flag.Bool("remount", false,
"attempt to remount --dir with strictatime,lazyatime to improve eviction")

// DefaultFieldsFormatter wraps another logrus.Formatter, injecting
// DefaultFields into each Format() call
type DefaultFieldsFormatter struct {
WrappedFormatter logrus.Formatter
DefaultFields logrus.Fields
}

// Format implements logrus.Formatter's Format
func (d *DefaultFieldsFormatter) Format(entry *logrus.Entry) ([]byte, error) {
decorated := entry
if entry != nil {
decorated = entry.WithFields(d.DefaultFields)
}
return d.WrappedFormatter.Format(decorated)
}

func init() {
logrus.SetFormatter(&DefaultFieldsFormatter{
logrus.SetFormatter(&logrusutil.DefaultFieldsFormatter{
WrappedFormatter: &logrus.JSONFormatter{},
DefaultFields: logrus.Fields{
"component": "nursery",
Expand Down
1 change: 1 addition & 0 deletions prow/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ filegroup(
"//prow/hook:all-srcs",
"//prow/jenkins:all-srcs",
"//prow/kube:all-srcs",
"//prow/logrusutil:all-srcs",
"//prow/metrics:all-srcs",
"//prow/phony:all-srcs",
"//prow/pjutil:all-srcs",
Expand Down
1 change: 1 addition & 0 deletions prow/cmd/deck/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ go_library(
"//prow/config:go_default_library",
"//prow/githuboauth:go_default_library",
"//prow/kube:go_default_library",
"//prow/logrusutil:go_default_library",
"//prow/pjutil:go_default_library",
"//prow/pluginhelp:go_default_library",
"//prow/tide:go_default_library",
Expand Down
37 changes: 21 additions & 16 deletions prow/cmd/deck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/githuboauth"
"k8s.io/test-infra/prow/kube"
"k8s.io/test-infra/prow/logrusutil"
"k8s.io/test-infra/prow/pjutil"
"k8s.io/test-infra/prow/pluginhelp"
"k8s.io/test-infra/prow/userdashboard"
Expand Down Expand Up @@ -70,8 +71,12 @@ func main() {
// common setup
flag.Parse()

logrus.SetFormatter(&logrus.JSONFormatter{})
logger := logrus.WithField("component", "deck")
logrus.SetFormatter(&logrusutil.DefaultFieldsFormatter{
WrappedFormatter: &logrus.JSONFormatter{},
DefaultFields: logrus.Fields{
"component": "deck",
},
})

mux := http.NewServeMux()

Expand All @@ -85,24 +90,24 @@ func main() {
mux.Handle("/", staticHandlerFromDir("./static"))
} else {
mux.Handle("/", staticHandlerFromDir("/static"))
prodOnlyMain(logger, mux)
prodOnlyMain(mux)
}

// setup done, actually start the server
logger.WithError(http.ListenAndServe(":8080", mux)).Fatal("ListenAndServe returned.")
logrus.WithError(http.ListenAndServe(":8080", mux)).Fatal("ListenAndServe returned.")
}

// prodOnlyMain contains logic only used when running deployed, not locally
func prodOnlyMain(logger *logrus.Entry, mux *http.ServeMux) {
func prodOnlyMain(mux *http.ServeMux) {
// setup config agent, pod log clients etc.
configAgent := &config.Agent{}
if err := configAgent.Start(*configPath); err != nil {
logger.WithError(err).Fatal("Error starting config agent.")
logrus.WithError(err).Fatal("Error starting config agent.")
}

kc, err := kube.NewClientInCluster(configAgent.Config().ProwJobNamespace)
if err != nil {
logger.WithError(err).Fatal("Error getting client.")
logrus.WithError(err).Fatal("Error getting client.")
}
kc.SetHiddenReposProvider(func() []string { return configAgent.Config().Deck.HiddenRepos }, *hiddenOnly)

Expand All @@ -112,7 +117,7 @@ func prodOnlyMain(logger *logrus.Entry, mux *http.ServeMux) {
} else {
pkcs, err = kube.ClientMapFromFile(*buildCluster, configAgent.Config().PodNamespace)
if err != nil {
logger.WithError(err).Fatal("Error getting kube client to build cluster.")
logrus.WithError(err).Fatal("Error getting kube client to build cluster.")
}
}
plClients := map[string]podLogClient{}
Expand Down Expand Up @@ -142,7 +147,7 @@ func prodOnlyMain(logger *logrus.Entry, mux *http.ServeMux) {

if *tideURL != "" {
ta := &tideAgent{
log: logger.WithField("agent", "tide"),
log: logrus.WithField("agent", "tide"),
path: *tideURL,
updatePeriod: func() time.Duration {
return configAgent.Config().Deck.TideUpdatePeriod
Expand All @@ -158,30 +163,30 @@ func prodOnlyMain(logger *logrus.Entry, mux *http.ServeMux) {
if *oauthUrl != "" {
githubOAuthConfigRaw, err := loadToken(*githubOAuthConfigFile)
if err != nil {
logger.WithError(err).Fatal("Could not read github oauth config file.")
logrus.WithError(err).Fatal("Could not read github oauth config file.")
}

cookieSecretRaw, err := loadToken(*cookieSecretFile)
if err != nil {
logger.WithError(err).Fatal("Could not read cookie secret file.")
logrus.WithError(err).Fatal("Could not read cookie secret file.")
}

mux.Handle("/user-data.js", handleUserData(*oauthUrl))

var githubOAuthConfig config.GithubOAuthConfig
if err := yaml.Unmarshal(githubOAuthConfigRaw, &githubOAuthConfig); err != nil {
logger.WithError(err).Fatal("Error unmarshalling github oauth config")
logrus.WithError(err).Fatal("Error unmarshalling github oauth config")
}
if !isValidatedGitOAuthConfig(&githubOAuthConfig) {
logger.Fatal("Error invalid github oauth config")
logrus.Fatal("Error invalid github oauth config")
}

decodedSecret, err := base64.StdEncoding.DecodeString(string(cookieSecretRaw))
if err != nil {
logger.WithError(err).Fatal("Error decoding cookie secret")
logrus.WithError(err).Fatal("Error decoding cookie secret")
}
if len(decodedSecret) == 0 {
logger.Fatal("Cookie secret should not be empty")
logrus.Fatal("Cookie secret should not be empty")
}
cookie := sessions.NewCookieStore(decodedSecret)
githubOAuthConfig.InitGithubOAuthConfig(cookie)
Expand Down Expand Up @@ -211,7 +216,7 @@ func prodOnlyMain(logger *logrus.Entry, mux *http.ServeMux) {
if r.Header.Get("x-forwarded-proto") == "http" {
redirectURL, err := url.Parse(r.URL.String())
if err != nil {
logger.Errorf("Failed to parse URL: %s.", r.URL.String())
logrus.Errorf("Failed to parse URL: %s.", r.URL.String())
http.Error(w, "Failed to perform https redirect.", http.StatusInternalServerError)
return
}
Expand Down
1 change: 1 addition & 0 deletions prow/cmd/hook/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ go_library(
"//prow/github:go_default_library",
"//prow/hook:go_default_library",
"//prow/kube:go_default_library",
"//prow/logrusutil:go_default_library",
"//prow/metrics:go_default_library",
"//prow/pluginhelp/hook:go_default_library",
"//prow/plugins:go_default_library",
Expand Down
35 changes: 20 additions & 15 deletions prow/cmd/hook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/hook"
"k8s.io/test-infra/prow/kube"
"k8s.io/test-infra/prow/logrusutil"
"k8s.io/test-infra/prow/metrics"
pluginhelp "k8s.io/test-infra/prow/pluginhelp/hook"
"k8s.io/test-infra/prow/plugins"
Expand All @@ -60,12 +61,16 @@ var (

func main() {
flag.Parse()
logrus.SetFormatter(&logrus.JSONFormatter{})
logger := logrus.WithField("component", "hook")
logrus.SetFormatter(&logrusutil.DefaultFieldsFormatter{
WrappedFormatter: &logrus.JSONFormatter{},
DefaultFields: logrus.Fields{
"component": "hook",
},
})

configAgent := &config.Agent{}
if err := configAgent.Start(*configPath); err != nil {
logger.WithError(err).Fatal("Error starting config agent.")
logrus.WithError(err).Fatal("Error starting config agent.")
}

// Ignore SIGTERM so that we don't drop hooks when the pod is removed.
Expand All @@ -75,28 +80,28 @@ func main() {

webhookSecretRaw, err := ioutil.ReadFile(*webhookSecretFile)
if err != nil {
logger.WithError(err).Fatal("Could not read webhook secret file.")
logrus.WithError(err).Fatal("Could not read webhook secret file.")
}
webhookSecret := bytes.TrimSpace(webhookSecretRaw)

oauthSecretRaw, err := ioutil.ReadFile(*githubTokenFile)
if err != nil {
logger.WithError(err).Fatal("Could not read oauth secret file.")
logrus.WithError(err).Fatal("Could not read oauth secret file.")
}
oauthSecret := string(bytes.TrimSpace(oauthSecretRaw))

var teamToken string
if *slackTokenFile != "" {
teamTokenRaw, err := ioutil.ReadFile(*slackTokenFile)
if err != nil {
logger.WithError(err).Fatal("Could not read slack token file.")
logrus.WithError(err).Fatal("Could not read slack token file.")
}
teamToken = string(bytes.TrimSpace(teamTokenRaw))
}

_, err = url.Parse(*githubEndpoint)
if err != nil {
logger.WithError(err).Fatal("Must specify a valid --github-endpoint URL.")
logrus.WithError(err).Fatal("Must specify a valid --github-endpoint URL.")
}

var githubClient *github.Client
Expand All @@ -109,29 +114,29 @@ func main() {
if *cluster == "" {
kubeClient, err = kube.NewClientInCluster(configAgent.Config().ProwJobNamespace)
if err != nil {
logger.WithError(err).Fatal("Error getting kube client.")
logrus.WithError(err).Fatal("Error getting kube client.")
}
} else {
kubeClient, err = kube.NewClientFromFile(*cluster, configAgent.Config().ProwJobNamespace)
if err != nil {
logger.WithError(err).Fatal("Error getting kube client.")
logrus.WithError(err).Fatal("Error getting kube client.")
}
}
}

var slackClient *slack.Client
if !*dryRun && teamToken != "" {
logger.Info("Using real slack client.")
logrus.Info("Using real slack client.")
slackClient = slack.NewClient(teamToken)
}
if slackClient == nil {
logger.Info("Using fake slack client.")
logrus.Info("Using fake slack client.")
slackClient = slack.NewFakeClient()
}

gitClient, err := git.NewClient()
if err != nil {
logger.WithError(err).Fatal("Error getting git client.")
logrus.WithError(err).Fatal("Error getting git client.")
}

pluginAgent := &plugins.PluginAgent{}
Expand All @@ -142,10 +147,10 @@ func main() {
GitClient: gitClient,
SlackClient: slackClient,
OwnersClient: repoowners.NewClient(gitClient, githubClient, pluginAgent.MDYAMLEnabled),
Logger: logger,
Logger: logrus.WithField("agent", "plugin"),
}
if err := pluginAgent.Start(*pluginConfig); err != nil {
logger.WithError(err).Fatal("Error starting plugins.")
logrus.WithError(err).Fatal("Error starting plugins.")
}

promMetrics := hook.NewMetrics()
Expand All @@ -171,5 +176,5 @@ func main() {
// Serve plugin help information from /plugin-help.
http.Handle("/plugin-help", pluginhelp.NewHelpAgent(pluginAgent, githubClient))

logger.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil))
logrus.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil))
}
1 change: 1 addition & 0 deletions prow/cmd/horologium/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ go_library(
"//prow/config:go_default_library",
"//prow/cron:go_default_library",
"//prow/kube:go_default_library",
"//prow/logrusutil:go_default_library",
"//prow/pjutil:go_default_library",
"//vendor/github.com/sirupsen/logrus:go_default_library",
],
Expand Down
8 changes: 7 additions & 1 deletion prow/cmd/horologium/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ import (
"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/cron"
"k8s.io/test-infra/prow/kube"
"k8s.io/test-infra/prow/logrusutil"
"k8s.io/test-infra/prow/pjutil"
)

var configPath = flag.String("config-path", "/etc/config/config", "Path to config.yaml.")

func main() {
flag.Parse()
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetFormatter(&logrusutil.DefaultFieldsFormatter{
WrappedFormatter: &logrus.JSONFormatter{},
DefaultFields: logrus.Fields{
"component": "horologium",
},
})

configAgent := config.Agent{}
if err := configAgent.Start(*configPath); err != nil {
Expand Down
1 change: 1 addition & 0 deletions prow/cmd/jenkins-operator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ go_library(
"//prow/github:go_default_library",
"//prow/jenkins:go_default_library",
"//prow/kube:go_default_library",
"//prow/logrusutil:go_default_library",
"//prow/metrics:go_default_library",
"//vendor/github.com/NYTimes/gziphandler:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus/promhttp:go_default_library",
Expand Down
Loading

0 comments on commit 0811332

Please sign in to comment.