diff --git a/boskos/BUILD.bazel b/boskos/BUILD.bazel index 0f023cdef0d6..1f18ff5effdc 100644 --- a/boskos/BUILD.bazel +++ b/boskos/BUILD.bazel @@ -1,73 +1,23 @@ package(default_visibility = ["//visibility:public"]) load("//def:image.bzl", "tags") -load("//prow:def.bzl", "prow_image", "prow_push") -load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") - -go_binary( - name = "boskos", - embed = [":go_default_library"], - pure = "on", -) - -prow_image( - name = "image", - visibility = ["//visibility:public"], -) +load("//prow:def.bzl", "prow_push") prow_push( name = "push", images = tags(targets = { "{STABLE_PROW_REPO}/boskos/aws-janitor": "//boskos/aws-janitor:image", "{STABLE_PROW_REPO}/boskos/aws-janitor-boskos": "//boskos/aws-janitor/cmd/aws-janitor-boskos:image", - "{STABLE_PROW_REPO}/boskos/boskos": "//boskos:image", - "{STABLE_PROW_REPO}/boskos/fake-mason": "//boskos/mason/fake-mason:image", - "{STABLE_PROW_REPO}/boskos/cleaner": "//boskos/cleaner/cmd:image", - "{STABLE_PROW_REPO}/boskos/janitor": "//boskos/janitor:image", - "{STABLE_PROW_REPO}/boskos/metrics": "//boskos/metrics:image", - "{STABLE_PROW_REPO}/boskos/reaper": "//boskos/reaper:image", + "{STABLE_PROW_REPO}/boskos/boskos": "//boskos/cmd/boskos:image", + "{STABLE_PROW_REPO}/boskos/fake-mason": "//boskos/cmd/fake-mason:image", + "{STABLE_PROW_REPO}/boskos/cleaner": "//boskos/cmd/cleaner:image", + "{STABLE_PROW_REPO}/boskos/janitor": "//boskos/cmd/janitor:image", + "{STABLE_PROW_REPO}/boskos/metrics": "//boskos/cmd/metrics:image", + "{STABLE_PROW_REPO}/boskos/reaper": "//boskos/cmd/reaper:image", "{STABLE_PROW_REPO}/boskos/cli": "//boskos/cmd/cli:image", }), ) -go_test( - name = "go_default_test", - srcs = [ - "boskos_test.go", - "server_client_test.go", - "storage_test.go", - ], - embed = [":go_default_library"], - deps = [ - "//boskos/client:go_default_library", - "//boskos/common:go_default_library", - "//boskos/crds:go_default_library", - "//boskos/ranch:go_default_library", - "//boskos/storage:go_default_library", - ], -) - -go_library( - name = "go_default_library", - srcs = ["boskos.go"], - importpath = "k8s.io/test-infra/boskos", - deps = [ - "//boskos/common:go_default_library", - "//boskos/crds:go_default_library", - "//boskos/ranch:go_default_library", - "//prow/config:go_default_library", - "//prow/interrupts:go_default_library", - "//prow/logrusutil:go_default_library", - "//prow/metrics:go_default_library", - "//prow/pjutil:go_default_library", - "//prow/simplifypath:go_default_library", - "@com_github_fsnotify_fsnotify//:go_default_library", - "@com_github_prometheus_client_golang//prometheus:go_default_library", - "@com_github_sirupsen_logrus//:go_default_library", - "@com_github_spf13_viper//:go_default_library", - ], -) - filegroup( name = "package-srcs", srcs = glob(["**"]), @@ -82,14 +32,18 @@ filegroup( "//boskos/aws-janitor:all-srcs", "//boskos/cleaner:all-srcs", "//boskos/client:all-srcs", + "//boskos/cmd/boskos:all-srcs", + "//boskos/cmd/cleaner:all-srcs", "//boskos/cmd/cli:all-srcs", + "//boskos/cmd/fake-mason:all-srcs", + "//boskos/cmd/janitor:all-srcs", + "//boskos/cmd/metrics:all-srcs", + "//boskos/cmd/reaper:all-srcs", "//boskos/common:all-srcs", "//boskos/crds:all-srcs", - "//boskos/janitor:all-srcs", + "//boskos/handlers:all-srcs", "//boskos/mason:all-srcs", - "//boskos/metrics:all-srcs", "//boskos/ranch:all-srcs", - "//boskos/reaper:all-srcs", "//boskos/storage:all-srcs", ], tags = ["automanaged"], diff --git a/boskos/cleaner/BUILD.bazel b/boskos/cleaner/BUILD.bazel index 1b576fb88b41..4875b4a6c7b3 100644 --- a/boskos/cleaner/BUILD.bazel +++ b/boskos/cleaner/BUILD.bazel @@ -21,10 +21,7 @@ filegroup( filegroup( name = "all-srcs", - srcs = [ - ":package-srcs", - "//boskos/cleaner/cmd:all-srcs", - ], + srcs = [":package-srcs"], tags = ["automanaged"], visibility = ["//visibility:public"], ) diff --git a/boskos/cmd/boskos/BUILD.bazel b/boskos/cmd/boskos/BUILD.bazel new file mode 100644 index 000000000000..55be2252b31a --- /dev/null +++ b/boskos/cmd/boskos/BUILD.bazel @@ -0,0 +1,49 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") +load("//prow:def.bzl", "prow_image") + +go_binary( + name = "boskos", + embed = [":go_default_library"], + pure = "on", + visibility = ["//visibility:public"], +) + +prow_image( + name = "image", + visibility = ["//visibility:public"], +) + +go_library( + name = "go_default_library", + srcs = ["boskos.go"], + importpath = "k8s.io/test-infra/boskos/cmd/boskos", + visibility = ["//visibility:private"], + deps = [ + "//boskos/crds:go_default_library", + "//boskos/handlers:go_default_library", + "//boskos/ranch:go_default_library", + "//prow/config:go_default_library", + "//prow/interrupts:go_default_library", + "//prow/logrusutil:go_default_library", + "//prow/metrics:go_default_library", + "//prow/pjutil:go_default_library", + "@com_github_fsnotify_fsnotify//:go_default_library", + "@com_github_prometheus_client_golang//prometheus:go_default_library", + "@com_github_sirupsen_logrus//:go_default_library", + "@com_github_spf13_viper//: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/boskos/cmd/boskos/boskos.go b/boskos/cmd/boskos/boskos.go new file mode 100644 index 000000000000..6c8c45f0eee6 --- /dev/null +++ b/boskos/cmd/boskos/boskos.go @@ -0,0 +1,138 @@ +/* +Copyright 2017 The Kubernetes Authors. + +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 main + +import ( + "flag" + "fmt" + "net/http" + "runtime" + "time" + + "github.com/fsnotify/fsnotify" + "github.com/prometheus/client_golang/prometheus" + "github.com/sirupsen/logrus" + "github.com/spf13/viper" + + "k8s.io/test-infra/boskos/crds" + "k8s.io/test-infra/boskos/handlers" + "k8s.io/test-infra/boskos/ranch" + "k8s.io/test-infra/prow/config" + "k8s.io/test-infra/prow/interrupts" + "k8s.io/test-infra/prow/logrusutil" + "k8s.io/test-infra/prow/metrics" + "k8s.io/test-infra/prow/pjutil" +) + +const ( + defaultDynamicResourceUpdatePeriod = 10 * time.Minute + defaultRequestTTL = 30 * time.Second + defaultRequestGCPeriod = time.Minute +) + +var ( + configPath = flag.String("config", "config.yaml", "Path to init resource file") + dynamicResourceUpdatePeriod = flag.Duration("dynamic-resource-update-period", defaultDynamicResourceUpdatePeriod, + "Period at which to update dynamic resources. Set to 0 to disable.") + storagePath = flag.String("storage", "", "Path to persistent volume to load the state") + requestTTL = flag.Duration("request-ttl", defaultRequestTTL, "request TTL before losing priority in the queue") + kubeClientOptions crds.KubernetesClientOptions + logLevel = flag.String("log-level", "info", fmt.Sprintf("Log level is one of %v.", logrus.AllLevels)) +) + +var ( + httpRequestDuration = metrics.HttpRequestDuration("boskos", 0.005, 1200) + httpResponseSize = metrics.HttpResponseSize("boskos", 128, 65536) + traceHandler = metrics.TraceHandler(handlers.NewBoskosSimplifier(), httpRequestDuration, httpResponseSize) +) + +func init() { + prometheus.MustRegister(httpRequestDuration) + prometheus.MustRegister(httpResponseSize) +} + +func main() { + logrusutil.ComponentInit("boskos") + kubeClientOptions.AddFlags(flag.CommandLine) + flag.Parse() + level, err := logrus.ParseLevel(*logLevel) + if err != nil { + logrus.WithError(err).Fatal("invalid log level specified") + } + logrus.SetLevel(level) + kubeClientOptions.Validate() + + // collect data on mutex holders and blocking profiles + runtime.SetBlockProfileRate(1) + runtime.SetMutexProfileFraction(1) + + defer interrupts.WaitForGracefulShutdown() + pjutil.ServePProf() + metrics.ExposeMetrics("boskos", config.PushGateway{}) + // signal to the world that we are healthy + // this needs to be in a separate port as we don't start the + // main server with the main mux until we're ready + health := pjutil.NewHealth() + + rc, err := kubeClientOptions.Client(crds.ResourceType) + if err != nil { + logrus.WithError(err).Fatal("unable to create a Resource CRD client") + } + dc, err := kubeClientOptions.Client(crds.DRLCType) + if err != nil { + logrus.WithError(err).Fatal("unable to create a DynamicResourceLifeCycle CRD client") + } + + resourceStorage := crds.NewCRDStorage(rc) + dRLCStorage := crds.NewCRDStorage(dc) + storage, err := ranch.NewStorage(resourceStorage, dRLCStorage, *storagePath) + if err != nil { + logrus.WithError(err).Fatal("failed to create storage") + } + + r, err := ranch.NewRanch(*configPath, storage, *requestTTL) + if err != nil { + logrus.WithError(err).Fatalf("failed to create ranch! Config: %v", *configPath) + } + + boskos := &http.Server{ + Handler: traceHandler(handlers.NewBoskosHandler(r)), + Addr: ":8080", + } + + v := viper.New() + v.SetConfigFile(*configPath) + v.SetConfigType("yaml") + v.WatchConfig() + v.OnConfigChange(func(in fsnotify.Event) { + logrus.Infof("Updating Boskos Config") + if err := r.SyncConfig(*configPath); err != nil { + logrus.WithError(err).Errorf("Failed to update config") + } else { + logrus.Infof("Updated Boskos Config successfully") + } + }) + + r.StartDynamicResourceUpdater(*dynamicResourceUpdatePeriod) + r.StartRequestGC(defaultRequestGCPeriod) + + logrus.Info("Start Service") + interrupts.ListenAndServe(boskos, 5*time.Second) + + // signal to the world that we're ready + health.ServeReady() +} diff --git a/boskos/cleaner/cmd/BUILD.bazel b/boskos/cmd/cleaner/BUILD.bazel similarity index 94% rename from boskos/cleaner/cmd/BUILD.bazel rename to boskos/cmd/cleaner/BUILD.bazel index f7376b725289..c4f684f6b5b2 100644 --- a/boskos/cleaner/cmd/BUILD.bazel +++ b/boskos/cmd/cleaner/BUILD.bazel @@ -4,7 +4,7 @@ load("//prow:def.bzl", "prow_image") go_library( name = "go_default_library", srcs = ["main.go"], - importpath = "k8s.io/test-infra/boskos/cleaner/cmd", + importpath = "k8s.io/test-infra/boskos/cmd/cleaner", visibility = ["//visibility:private"], deps = [ "//boskos/cleaner:go_default_library", diff --git a/boskos/cleaner/cmd/example-deployment.yaml b/boskos/cmd/cleaner/example-deployment.yaml similarity index 100% rename from boskos/cleaner/cmd/example-deployment.yaml rename to boskos/cmd/cleaner/example-deployment.yaml diff --git a/boskos/cleaner/cmd/main.go b/boskos/cmd/cleaner/main.go similarity index 100% rename from boskos/cleaner/cmd/main.go rename to boskos/cmd/cleaner/main.go diff --git a/boskos/mason/fake-mason/BUILD.bazel b/boskos/cmd/fake-mason/BUILD.bazel similarity index 94% rename from boskos/mason/fake-mason/BUILD.bazel rename to boskos/cmd/fake-mason/BUILD.bazel index 825b42e23f92..eb1b0f989613 100644 --- a/boskos/mason/fake-mason/BUILD.bazel +++ b/boskos/cmd/fake-mason/BUILD.bazel @@ -4,7 +4,7 @@ load("//prow:def.bzl", "prow_image") go_library( name = "go_default_library", srcs = ["main.go"], - importpath = "k8s.io/test-infra/boskos/mason/fake-mason", + importpath = "k8s.io/test-infra/boskos/cmd/fake-mason", visibility = ["//visibility:private"], deps = [ "//boskos/client:go_default_library", diff --git a/boskos/mason/fake-mason/main.go b/boskos/cmd/fake-mason/main.go similarity index 100% rename from boskos/mason/fake-mason/main.go rename to boskos/cmd/fake-mason/main.go diff --git a/boskos/janitor/BUILD.bazel b/boskos/cmd/janitor/BUILD.bazel similarity index 96% rename from boskos/janitor/BUILD.bazel rename to boskos/cmd/janitor/BUILD.bazel index ad725a3920b3..f9a5baf6605c 100644 --- a/boskos/janitor/BUILD.bazel +++ b/boskos/cmd/janitor/BUILD.bazel @@ -19,7 +19,7 @@ go_binary( go_library( name = "go_default_library", srcs = ["janitor.go"], - importpath = "k8s.io/test-infra/boskos/janitor", + importpath = "k8s.io/test-infra/boskos/cmd/janitor", deps = [ "//boskos/client:go_default_library", "//boskos/common:go_default_library", diff --git a/boskos/janitor/gcp_janitor.py b/boskos/cmd/janitor/gcp_janitor.py similarity index 100% rename from boskos/janitor/gcp_janitor.py rename to boskos/cmd/janitor/gcp_janitor.py diff --git a/boskos/janitor/janitor.go b/boskos/cmd/janitor/janitor.go similarity index 100% rename from boskos/janitor/janitor.go rename to boskos/cmd/janitor/janitor.go diff --git a/boskos/janitor/janitor_test.go b/boskos/cmd/janitor/janitor_test.go similarity index 100% rename from boskos/janitor/janitor_test.go rename to boskos/cmd/janitor/janitor_test.go diff --git a/boskos/metrics/BUILD.bazel b/boskos/cmd/metrics/BUILD.bazel similarity index 95% rename from boskos/metrics/BUILD.bazel rename to boskos/cmd/metrics/BUILD.bazel index 90f53a23c09d..3351d760be32 100644 --- a/boskos/metrics/BUILD.bazel +++ b/boskos/cmd/metrics/BUILD.bazel @@ -19,7 +19,7 @@ go_binary( go_library( name = "go_default_library", srcs = ["metrics.go"], - importpath = "k8s.io/test-infra/boskos/metrics", + importpath = "k8s.io/test-infra/boskos/cmd/metrics", deps = [ "//boskos/client:go_default_library", "//boskos/common:go_default_library", diff --git a/boskos/metrics/metrics.go b/boskos/cmd/metrics/metrics.go similarity index 100% rename from boskos/metrics/metrics.go rename to boskos/cmd/metrics/metrics.go diff --git a/boskos/reaper/BUILD.bazel b/boskos/cmd/reaper/BUILD.bazel similarity index 93% rename from boskos/reaper/BUILD.bazel rename to boskos/cmd/reaper/BUILD.bazel index 216c4d4cdf09..ef700fe9da8d 100644 --- a/boskos/reaper/BUILD.bazel +++ b/boskos/cmd/reaper/BUILD.bazel @@ -12,7 +12,7 @@ go_binary( go_library( name = "go_default_library", srcs = ["reaper.go"], - importpath = "k8s.io/test-infra/boskos/reaper", + importpath = "k8s.io/test-infra/boskos/cmd/reaper", deps = [ "//boskos/client:go_default_library", "//boskos/common:go_default_library", diff --git a/boskos/reaper/reaper.go b/boskos/cmd/reaper/reaper.go similarity index 100% rename from boskos/reaper/reaper.go rename to boskos/cmd/reaper/reaper.go diff --git a/boskos/handlers/BUILD.bazel b/boskos/handlers/BUILD.bazel new file mode 100644 index 000000000000..fb82bb4ec357 --- /dev/null +++ b/boskos/handlers/BUILD.bazel @@ -0,0 +1,43 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = ["handlers.go"], + importpath = "k8s.io/test-infra/boskos/handlers", + visibility = ["//visibility:public"], + deps = [ + "//boskos/common:go_default_library", + "//boskos/ranch:go_default_library", + "//prow/simplifypath:go_default_library", + "@com_github_sirupsen_logrus//:go_default_library", + ], +) + +go_test( + name = "go_default_test", + srcs = [ + "handlers_test.go", + "server_client_test.go", + ], + embed = [":go_default_library"], + deps = [ + "//boskos/client:go_default_library", + "//boskos/common:go_default_library", + "//boskos/crds:go_default_library", + "//boskos/ranch: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/boskos/boskos.go b/boskos/handlers/handlers.go similarity index 72% rename from boskos/boskos.go rename to boskos/handlers/handlers.go index 975f2a865123..c8193afb72b4 100644 --- a/boskos/boskos.go +++ b/boskos/handlers/handlers.go @@ -14,146 +14,38 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package handlers import ( "bytes" "encoding/json" - "flag" "fmt" "io" "net/http" - "runtime" "strings" "time" - "github.com/fsnotify/fsnotify" - "github.com/prometheus/client_golang/prometheus" "github.com/sirupsen/logrus" - "github.com/spf13/viper" - "k8s.io/test-infra/boskos/common" - "k8s.io/test-infra/boskos/crds" "k8s.io/test-infra/boskos/ranch" - "k8s.io/test-infra/prow/config" - "k8s.io/test-infra/prow/interrupts" - "k8s.io/test-infra/prow/logrusutil" - "k8s.io/test-infra/prow/metrics" - "k8s.io/test-infra/prow/pjutil" "k8s.io/test-infra/prow/simplifypath" ) -const ( - defaultDynamicResourceUpdatePeriod = 10 * time.Minute - defaultRequestTTL = 30 * time.Second - defaultRequestGCPeriod = time.Minute -) - -var ( - configPath = flag.String("config", "config.yaml", "Path to init resource file") - dynamicResourceUpdatePeriod = flag.Duration("dynamic-resource-update-period", defaultDynamicResourceUpdatePeriod, - "Period at which to update dynamic resources. Set to 0 to disable.") - storagePath = flag.String("storage", "", "Path to persistent volume to load the state") - requestTTL = flag.Duration("request-ttl", defaultRequestTTL, "request TTL before losing priority in the queue") - kubeClientOptions crds.KubernetesClientOptions - logLevel = flag.String("log-level", "info", fmt.Sprintf("Log level is one of %v.", logrus.AllLevels)) -) - -var ( - httpRequestDuration = metrics.HttpRequestDuration("boskos", 0.005, 1200) - httpResponseSize = metrics.HttpResponseSize("boskos", 128, 65536) - traceHandler = metrics.TraceHandler(simplifier, httpRequestDuration, httpResponseSize) -) - -func init() { - prometheus.MustRegister(httpRequestDuration) - prometheus.MustRegister(httpResponseSize) -} - -var simplifier = simplifypath.NewSimplifier(l("", // shadow element mimicing the root - l("acquire"), - l("acquirebystate"), - l("release"), - l("reset"), - l("update"), - l("metric"), -)) - // l keeps the tree legible func l(fragment string, children ...simplifypath.Node) simplifypath.Node { return simplifypath.L(fragment, children...) } -func main() { - logrusutil.ComponentInit("boskos") - kubeClientOptions.AddFlags(flag.CommandLine) - flag.Parse() - level, err := logrus.ParseLevel(*logLevel) - if err != nil { - logrus.WithError(err).Fatal("invalid log level specified") - } - logrus.SetLevel(level) - kubeClientOptions.Validate() - - // collect data on mutex holders and blocking profiles - runtime.SetBlockProfileRate(1) - runtime.SetMutexProfileFraction(1) - - defer interrupts.WaitForGracefulShutdown() - pjutil.ServePProf() - metrics.ExposeMetrics("boskos", config.PushGateway{}) - // signal to the world that we are healthy - // this needs to be in a separate port as we don't start the - // main server with the main mux until we're ready - health := pjutil.NewHealth() - - rc, err := kubeClientOptions.Client(crds.ResourceType) - if err != nil { - logrus.WithError(err).Fatal("unable to create a Resource CRD client") - } - dc, err := kubeClientOptions.Client(crds.DRLCType) - if err != nil { - logrus.WithError(err).Fatal("unable to create a DynamicResourceLifeCycle CRD client") - } - - resourceStorage := crds.NewCRDStorage(rc) - dRLCStorage := crds.NewCRDStorage(dc) - storage, err := ranch.NewStorage(resourceStorage, dRLCStorage, *storagePath) - if err != nil { - logrus.WithError(err).Fatal("failed to create storage") - } - - r, err := ranch.NewRanch(*configPath, storage, *requestTTL) - if err != nil { - logrus.WithError(err).Fatalf("failed to create ranch! Config: %v", *configPath) - } - - boskos := &http.Server{ - Handler: traceHandler(NewBoskosHandler(r)), - Addr: ":8080", - } - - v := viper.New() - v.SetConfigFile(*configPath) - v.SetConfigType("yaml") - v.WatchConfig() - v.OnConfigChange(func(in fsnotify.Event) { - logrus.Infof("Updating Boskos Config") - if err := r.SyncConfig(*configPath); err != nil { - logrus.WithError(err).Errorf("Failed to update config") - } else { - logrus.Infof("Updated Boskos Config successfully") - } - }) - - r.StartDynamicResourceUpdater(*dynamicResourceUpdatePeriod) - r.StartRequestGC(defaultRequestGCPeriod) - - logrus.Info("Start Service") - interrupts.ListenAndServe(boskos, 5*time.Second) - - // signal to the world that we're ready - health.ServeReady() +// NewBoskosSimplifier returns a Simplifier for all Boskos URIs to be used with metrics collection. +func NewBoskosSimplifier() simplifypath.Simplifier { + return simplifypath.NewSimplifier(l("", // shadow element mimicing the root + l("acquire"), + l("acquirebystate"), + l("release"), + l("reset"), + l("update"), + l("metric"), + )) } //NewBoskosHandler constructs the boskos handler. @@ -169,8 +61,8 @@ func NewBoskosHandler(r *ranch.Ranch) *http.ServeMux { return mux } -// ErrorToStatus translates error into http code -func ErrorToStatus(err error) int { +// errorToStatus translates error into http code +func errorToStatus(err error) int { switch err.(type) { default: return http.StatusInternalServerError @@ -229,14 +121,14 @@ func handleAcquire(r *ranch.Ranch) http.HandlerFunc { if err != nil { logrus.WithError(err).Errorf("No available resource") - http.Error(res, err.Error(), ErrorToStatus(err)) + http.Error(res, err.Error(), errorToStatus(err)) return } resJSON, err := json.Marshal(resource) if err != nil { logrus.WithError(err).Errorf("json.Marshal failed: %v, resource will be released", resource) - http.Error(res, err.Error(), ErrorToStatus(err)) + http.Error(res, err.Error(), errorToStatus(err)) // release the resource, though this is not expected to happen. err = r.Release(resource.Name, state, owner) if err != nil { @@ -288,7 +180,7 @@ func handleAcquireByState(r *ranch.Ranch) http.HandlerFunc { if err != nil { logrus.WithError(err).Errorf("No available resources") - http.Error(res, err.Error(), ErrorToStatus(err)) + http.Error(res, err.Error(), errorToStatus(err)) return } @@ -296,7 +188,7 @@ func handleAcquireByState(r *ranch.Ranch) http.HandlerFunc { if err := json.NewEncoder(resBytes).Encode(resources); err != nil { logrus.WithError(err).Errorf("json.Marshal failed: %v, resources will be released", resources) - http.Error(res, err.Error(), ErrorToStatus(err)) + http.Error(res, err.Error(), errorToStatus(err)) for _, resource := range resources { err := r.Release(resource.Name, state, owner) if err != nil { @@ -339,7 +231,7 @@ func handleRelease(r *ranch.Ranch) http.HandlerFunc { if err := r.Release(name, dest, owner); err != nil { logrus.WithError(err).Errorf("Done failed: %v - %v (from %v)", name, dest, owner) - http.Error(res, err.Error(), ErrorToStatus(err)) + http.Error(res, err.Error(), errorToStatus(err)) return } @@ -395,7 +287,7 @@ func handleReset(r *ranch.Ranch) http.HandlerFunc { resJSON, err := json.Marshal(rmap) if err != nil { logrus.WithError(err).Errorf("json.Marshal failed: %v", rmap) - http.Error(res, err.Error(), ErrorToStatus(err)) + http.Error(res, err.Error(), errorToStatus(err)) return } logrus.Infof("Resource %v reset successful, %d items moved to state %v", rtype, len(rmap), dest) @@ -448,7 +340,7 @@ func handleUpdate(r *ranch.Ranch) http.HandlerFunc { if err := r.Update(name, owner, state, &userData); err != nil { logrus.WithError(err).Errorf("Update failed: %v - %v (%v)", name, state, owner) - http.Error(res, err.Error(), ErrorToStatus(err)) + http.Error(res, err.Error(), errorToStatus(err)) return } @@ -479,14 +371,14 @@ func handleMetric(r *ranch.Ranch) http.HandlerFunc { metric, err := r.Metric(rtype) if err != nil { logrus.WithError(err).Errorf("Metric for %s failed", rtype) - http.Error(res, err.Error(), ErrorToStatus(err)) + http.Error(res, err.Error(), errorToStatus(err)) return } js, err := json.Marshal(metric) if err != nil { logrus.WithError(err).Error("Fail to marshal metric") - http.Error(res, err.Error(), ErrorToStatus(err)) + http.Error(res, err.Error(), errorToStatus(err)) return } diff --git a/boskos/boskos_test.go b/boskos/handlers/handlers_test.go similarity index 98% rename from boskos/boskos_test.go rename to boskos/handlers/handlers_test.go index a174f1abc9e4..1d4092a7cdbc 100644 --- a/boskos/boskos_test.go +++ b/boskos/handlers/handlers_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package handlers import ( "encoding/json" @@ -30,6 +30,13 @@ import ( "k8s.io/test-infra/boskos/ranch" ) +// json does not serialized time with nanosecond precision +func now() time.Time { + format := "2006-01-02 15:04:05.000" + now, _ := time.Parse(format, time.Now().Format(format)) + return now +} + var ( fakeNow = now() testTTL = time.Millisecond diff --git a/boskos/server_client_test.go b/boskos/handlers/server_client_test.go similarity index 97% rename from boskos/server_client_test.go rename to boskos/handlers/server_client_test.go index f34ab0c469c0..754c9592bea5 100644 --- a/boskos/server_client_test.go +++ b/boskos/handlers/server_client_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package handlers import ( "fmt" @@ -31,13 +31,6 @@ import ( "k8s.io/test-infra/boskos/ranch" ) -// json does not serialized time with nanosecond precision -func now() time.Time { - format := "2006-01-02 15:04:05.000" - now, _ := time.Parse(format, time.Now().Format(format)) - return now -} - func makeTestBoskos(r *ranch.Ranch) *httptest.Server { handler := NewBoskosHandler(r) return httptest.NewServer(handler) diff --git a/boskos/mason/BUILD.bazel b/boskos/mason/BUILD.bazel index 7684309ede22..e00dc17ce485 100644 --- a/boskos/mason/BUILD.bazel +++ b/boskos/mason/BUILD.bazel @@ -44,10 +44,7 @@ filegroup( filegroup( name = "all-srcs", - srcs = [ - ":package-srcs", - "//boskos/mason/fake-mason:all-srcs", - ], + srcs = [":package-srcs"], tags = ["automanaged"], visibility = ["//visibility:public"], ) diff --git a/boskos/storage/BUILD.bazel b/boskos/storage/BUILD.bazel index e71c9659a5e9..bc424c30560c 100644 --- a/boskos/storage/BUILD.bazel +++ b/boskos/storage/BUILD.bazel @@ -1,4 +1,4 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "go_default_library", @@ -8,6 +8,16 @@ go_library( deps = ["//boskos/common:go_default_library"], ) +go_test( + name = "go_default_test", + srcs = ["storage_test.go"], + embed = [":go_default_library"], + deps = [ + "//boskos/common:go_default_library", + "//boskos/crds:go_default_library", + ], +) + filegroup( name = "package-srcs", srcs = glob(["**"]), diff --git a/boskos/storage_test.go b/boskos/storage/storage_test.go similarity index 96% rename from boskos/storage_test.go rename to boskos/storage/storage_test.go index 25aa27e588ff..4400aefd660a 100644 --- a/boskos/storage_test.go +++ b/boskos/storage/storage_test.go @@ -14,7 +14,9 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +// This is an xtest because it imports the crds package, but the crds package +// also imports storage, creating a cycle. +package storage_test import ( "fmt" diff --git a/config/jobs/kubernetes/test-infra/janitors.yaml b/config/jobs/kubernetes/test-infra/janitors.yaml index 995de9febf03..47670cebde3a 100644 --- a/config/jobs/kubernetes/test-infra/janitors.yaml +++ b/config/jobs/kubernetes/test-infra/janitors.yaml @@ -39,7 +39,7 @@ periodics: - experiment/ci-janitor/main.go - --config-path=config/prow/config.yaml - --job-config-path=config/jobs - - --janitor-path=boskos/janitor/gcp_janitor.py + - --janitor-path=boskos/cmd/janitor/gcp_janitor.py image: gcr.io/k8s-testimages/kubekins-e2e:v20200205-602500d-master resources: requests: diff --git a/scenarios/kubernetes_janitor.py b/scenarios/kubernetes_janitor.py index c3ed9b9467a1..a76f76551d58 100755 --- a/scenarios/kubernetes_janitor.py +++ b/scenarios/kubernetes_janitor.py @@ -63,7 +63,7 @@ def clean_project(project, hours=24, dryrun=False, ratelimit=None, filt=None): return CHECKED.add(project) - cmd = ['python', test_infra('boskos/janitor/gcp_janitor.py'), '--project=%s' % project] + cmd = ['python', test_infra('boskos/cmd/janitor/gcp_janitor.py'), '--project=%s' % project] cmd.append('--hour=%d' % hours) if dryrun: cmd.append('--dryrun')