Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Boskos directory structure #16152

Merged
merged 2 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 14 additions & 60 deletions boskos/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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(["**"]),
Expand All @@ -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"],
Expand Down
5 changes: 1 addition & 4 deletions boskos/cleaner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
Expand Down
49 changes: 49 additions & 0 deletions boskos/cmd/boskos/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
138 changes: 138 additions & 0 deletions boskos/cmd/boskos/boskos.go
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
File renamed without changes.
43 changes: 43 additions & 0 deletions boskos/handlers/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
Loading