From ea83919d5698104d8dea82e42a3ed8d111b8f35e Mon Sep 17 00:00:00 2001 From: Dominik Roos Date: Wed, 23 Mar 2022 19:52:08 +0100 Subject: [PATCH] build: do not bundle management API docs by default This reduces the build time and binary size GitOrigin-RevId: 527ff9f044ac347cbf96195f5e3560fd1680fde5 --- BUILD.bazel | 14 +++++++++++++ control/mgmtapi/BUILD.bazel | 5 ++++- control/mgmtapi/dummy.html | 0 control/mgmtapi/index.html | 8 -------- control/mgmtapi/spec.go | 10 ++++++--- daemon/mgmtapi/BUILD.bazel | 6 +++++- daemon/mgmtapi/dummy.html | 0 daemon/mgmtapi/index.html | 8 -------- daemon/mgmtapi/spec.go | 10 ++++++--- dispatcher/mgmtapi/BUILD.bazel | 6 +++++- dispatcher/mgmtapi/dummy.html | 0 dispatcher/mgmtapi/index.html | 8 -------- dispatcher/mgmtapi/spec.go | 10 ++++++--- gateway/mgmtapi/BUILD.bazel | 6 +++++- gateway/mgmtapi/dummy.html | 0 gateway/mgmtapi/index.html | 8 -------- gateway/mgmtapi/spec.go | 10 ++++++--- private/mgmtapi/BUILD.bazel | 2 ++ private/mgmtapi/index.html | 13 ++++++++++++ private/mgmtapi/spec.go | 37 ++++++++++++++++++++++++++++++++++ router/mgmtapi/BUILD.bazel | 5 ++++- router/mgmtapi/dummy.html | 0 router/mgmtapi/index.html | 8 -------- router/mgmtapi/spec.go | 10 ++++++--- 24 files changed, 124 insertions(+), 60 deletions(-) create mode 100644 control/mgmtapi/dummy.html delete mode 100644 control/mgmtapi/index.html create mode 100644 daemon/mgmtapi/dummy.html delete mode 100644 daemon/mgmtapi/index.html create mode 100644 dispatcher/mgmtapi/dummy.html delete mode 100644 dispatcher/mgmtapi/index.html create mode 100644 gateway/mgmtapi/dummy.html delete mode 100644 gateway/mgmtapi/index.html create mode 100644 private/mgmtapi/index.html create mode 100644 private/mgmtapi/spec.go create mode 100644 router/mgmtapi/dummy.html delete mode 100644 router/mgmtapi/index.html diff --git a/BUILD.bazel b/BUILD.bazel index 41b3bea81a..ee389214e9 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,3 +1,4 @@ +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") load("@bazel_gazelle//:def.bzl", "gazelle") load("@rules_pkg//:pkg.bzl", "pkg_tar") load("@io_bazel_rules_go//go:def.bzl", "nogo") @@ -36,6 +37,19 @@ flake8_lint_config( ], ) +# Add a build flag to enable bundling the management API documentation with the +# binaries. This can be enabled by passing --//:mgmtapi_bundle_doc=true when +# invoking bazel +bool_flag( + name = "mgmtapi_bundle_doc", + build_setting_default = False, +) + +config_setting( + name = "mgmtapi_bundle_doc_build", + flag_values = {":mgmtapi_bundle_doc": "true"}, +) + # This is the SCION distributable package. It contains all (binary) services, # and the required tools. pkg_tar( diff --git a/control/mgmtapi/BUILD.bazel b/control/mgmtapi/BUILD.bazel index 3580d478f8..22e82f8f9c 100644 --- a/control/mgmtapi/BUILD.bazel +++ b/control/mgmtapi/BUILD.bazel @@ -29,7 +29,10 @@ go_library( "spec.go", ":api_generated", # keep ], - embedsrcs = [":bundle"], # keep + embedsrcs = select({ + "//:mgmtapi_bundle_doc_build": [":bundle"], + "//conditions:default": [":dummy.html"], + }), # keep importpath = "github.com/scionproto/scion/control/mgmtapi", visibility = ["//visibility:public"], deps = [ diff --git a/control/mgmtapi/dummy.html b/control/mgmtapi/dummy.html new file mode 100644 index 0000000000..e69de29bb2 diff --git a/control/mgmtapi/index.html b/control/mgmtapi/index.html deleted file mode 100644 index 41cfbfbb48..0000000000 --- a/control/mgmtapi/index.html +++ /dev/null @@ -1,8 +0,0 @@ - - - -

Service Management UI not bundled

- - The service management UI is not bundled in this binary. - - diff --git a/control/mgmtapi/spec.go b/control/mgmtapi/spec.go index 7ab22fff91..77f2632b95 100644 --- a/control/mgmtapi/spec.go +++ b/control/mgmtapi/spec.go @@ -16,15 +16,19 @@ package mgmtapi import ( "bytes" - _ "embed" + "embed" "net/http" "time" + + "github.com/scionproto/scion/private/mgmtapi" ) -//go:embed index.html -var index []byte +//go:embed *.html +var f embed.FS var ( + index = mgmtapi.ResolveIndex(f) + modtime = time.Now() spec = func() []byte { diff --git a/daemon/mgmtapi/BUILD.bazel b/daemon/mgmtapi/BUILD.bazel index e2a25f9784..38c491dcb7 100644 --- a/daemon/mgmtapi/BUILD.bazel +++ b/daemon/mgmtapi/BUILD.bazel @@ -29,10 +29,14 @@ go_library( "spec.go", ":api_generated", # keep ], - embedsrcs = [":bundle"], # keep + embedsrcs = select({ + "//:mgmtapi_bundle_doc_build": [":bundle"], + "//conditions:default": [":dummy.html"], + }), # keep importpath = "github.com/scionproto/scion/daemon/mgmtapi", visibility = ["//visibility:public"], deps = [ + "//private/mgmtapi:go_default_library", "//private/mgmtapi/cppki/api:go_default_library", "//private/mgmtapi/segments/api:go_default_library", "@com_github_deepmap_oapi_codegen//pkg/runtime:go_default_library", # keep diff --git a/daemon/mgmtapi/dummy.html b/daemon/mgmtapi/dummy.html new file mode 100644 index 0000000000..e69de29bb2 diff --git a/daemon/mgmtapi/index.html b/daemon/mgmtapi/index.html deleted file mode 100644 index 41cfbfbb48..0000000000 --- a/daemon/mgmtapi/index.html +++ /dev/null @@ -1,8 +0,0 @@ - - - -

Service Management UI not bundled

- - The service management UI is not bundled in this binary. - - diff --git a/daemon/mgmtapi/spec.go b/daemon/mgmtapi/spec.go index 7ab22fff91..77f2632b95 100644 --- a/daemon/mgmtapi/spec.go +++ b/daemon/mgmtapi/spec.go @@ -16,15 +16,19 @@ package mgmtapi import ( "bytes" - _ "embed" + "embed" "net/http" "time" + + "github.com/scionproto/scion/private/mgmtapi" ) -//go:embed index.html -var index []byte +//go:embed *.html +var f embed.FS var ( + index = mgmtapi.ResolveIndex(f) + modtime = time.Now() spec = func() []byte { diff --git a/dispatcher/mgmtapi/BUILD.bazel b/dispatcher/mgmtapi/BUILD.bazel index 85bbb74d85..3362726f6b 100644 --- a/dispatcher/mgmtapi/BUILD.bazel +++ b/dispatcher/mgmtapi/BUILD.bazel @@ -29,10 +29,14 @@ go_library( "spec.go", ":api_generated", # keep ], - embedsrcs = [":bundle"], # keep + embedsrcs = select({ + "//:mgmtapi_bundle_doc_build": [":bundle"], + "//conditions:default": [":dummy.html"], + }), # keep importpath = "github.com/scionproto/scion/dispatcher/mgmtapi", visibility = ["//visibility:public"], deps = [ + "//private/mgmtapi:go_default_library", "@com_github_getkin_kin_openapi//openapi3:go_default_library", # keep "@com_github_go_chi_chi_v5//:go_default_library", # keep ], diff --git a/dispatcher/mgmtapi/dummy.html b/dispatcher/mgmtapi/dummy.html new file mode 100644 index 0000000000..e69de29bb2 diff --git a/dispatcher/mgmtapi/index.html b/dispatcher/mgmtapi/index.html deleted file mode 100644 index 41cfbfbb48..0000000000 --- a/dispatcher/mgmtapi/index.html +++ /dev/null @@ -1,8 +0,0 @@ - - - -

Service Management UI not bundled

- - The service management UI is not bundled in this binary. - - diff --git a/dispatcher/mgmtapi/spec.go b/dispatcher/mgmtapi/spec.go index 7ab22fff91..77f2632b95 100644 --- a/dispatcher/mgmtapi/spec.go +++ b/dispatcher/mgmtapi/spec.go @@ -16,15 +16,19 @@ package mgmtapi import ( "bytes" - _ "embed" + "embed" "net/http" "time" + + "github.com/scionproto/scion/private/mgmtapi" ) -//go:embed index.html -var index []byte +//go:embed *.html +var f embed.FS var ( + index = mgmtapi.ResolveIndex(f) + modtime = time.Now() spec = func() []byte { diff --git a/gateway/mgmtapi/BUILD.bazel b/gateway/mgmtapi/BUILD.bazel index ef7960e1dc..83ea7d7692 100644 --- a/gateway/mgmtapi/BUILD.bazel +++ b/gateway/mgmtapi/BUILD.bazel @@ -29,10 +29,14 @@ go_library( "spec.go", ":api_generated", # keep ], - embedsrcs = [":bundle"], # keep + embedsrcs = select({ + "//:mgmtapi_bundle_doc_build": [":bundle"], + "//conditions:default": [":dummy.html"], + }), # keep importpath = "github.com/scionproto/scion/gateway/mgmtapi", visibility = ["//visibility:public"], deps = [ + "//private/mgmtapi:go_default_library", "@com_github_deepmap_oapi_codegen//pkg/runtime:go_default_library", # keep "@com_github_getkin_kin_openapi//openapi3:go_default_library", # keep "@com_github_go_chi_chi_v5//:go_default_library", # keep diff --git a/gateway/mgmtapi/dummy.html b/gateway/mgmtapi/dummy.html new file mode 100644 index 0000000000..e69de29bb2 diff --git a/gateway/mgmtapi/index.html b/gateway/mgmtapi/index.html deleted file mode 100644 index 41cfbfbb48..0000000000 --- a/gateway/mgmtapi/index.html +++ /dev/null @@ -1,8 +0,0 @@ - - - -

Service Management UI not bundled

- - The service management UI is not bundled in this binary. - - diff --git a/gateway/mgmtapi/spec.go b/gateway/mgmtapi/spec.go index 7ab22fff91..77f2632b95 100644 --- a/gateway/mgmtapi/spec.go +++ b/gateway/mgmtapi/spec.go @@ -16,15 +16,19 @@ package mgmtapi import ( "bytes" - _ "embed" + "embed" "net/http" "time" + + "github.com/scionproto/scion/private/mgmtapi" ) -//go:embed index.html -var index []byte +//go:embed *.html +var f embed.FS var ( + index = mgmtapi.ResolveIndex(f) + modtime = time.Now() spec = func() []byte { diff --git a/private/mgmtapi/BUILD.bazel b/private/mgmtapi/BUILD.bazel index cb70a725d7..f86657fa48 100644 --- a/private/mgmtapi/BUILD.bazel +++ b/private/mgmtapi/BUILD.bazel @@ -6,7 +6,9 @@ go_library( "config.go", "errors.go", "helpers.go", + "spec.go", ], + embedsrcs = ["index.html"], importpath = "github.com/scionproto/scion/private/mgmtapi", visibility = ["//visibility:public"], deps = ["//private/config:go_default_library"], diff --git a/private/mgmtapi/index.html b/private/mgmtapi/index.html new file mode 100644 index 0000000000..7000507fd2 --- /dev/null +++ b/private/mgmtapi/index.html @@ -0,0 +1,13 @@ + + + + +

Service Management UI not bundled

+ + The service management UI is not bundled in this binary. + + The OpenAPI spec is available at /spec.json + + + + diff --git a/private/mgmtapi/spec.go b/private/mgmtapi/spec.go new file mode 100644 index 0000000000..d2df35affe --- /dev/null +++ b/private/mgmtapi/spec.go @@ -0,0 +1,37 @@ +// Copyright 2022 Anapaya Systems +// +// 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 mgmtapi + +import ( + "embed" + "errors" + "io/fs" +) + +//go:embed index.html +var index []byte + +// ResolveIndex resolves the service management API index page from the passed +// filesystem. If it is not present, a placeholder is returned. +func ResolveIndex(f embed.FS) []byte { + raw, err := f.ReadFile("index.html") + if errors.Is(err, fs.ErrNotExist) { + return append([]byte(nil), index...) + } + if err != nil { + panic(err) + } + return raw +} diff --git a/router/mgmtapi/BUILD.bazel b/router/mgmtapi/BUILD.bazel index c2189096dc..00797045f3 100644 --- a/router/mgmtapi/BUILD.bazel +++ b/router/mgmtapi/BUILD.bazel @@ -29,7 +29,10 @@ go_library( "spec.go", ":api_generated", # keep ], - embedsrcs = [":bundle"], # keep + embedsrcs = select({ + "//:mgmtapi_bundle_doc_build": [":bundle"], + "//conditions:default": [":dummy.html"], + }), # keep importpath = "github.com/scionproto/scion/router/mgmtapi", visibility = ["//visibility:public"], deps = [ diff --git a/router/mgmtapi/dummy.html b/router/mgmtapi/dummy.html new file mode 100644 index 0000000000..e69de29bb2 diff --git a/router/mgmtapi/index.html b/router/mgmtapi/index.html deleted file mode 100644 index 41cfbfbb48..0000000000 --- a/router/mgmtapi/index.html +++ /dev/null @@ -1,8 +0,0 @@ - - - -

Service Management UI not bundled

- - The service management UI is not bundled in this binary. - - diff --git a/router/mgmtapi/spec.go b/router/mgmtapi/spec.go index 7ab22fff91..77f2632b95 100644 --- a/router/mgmtapi/spec.go +++ b/router/mgmtapi/spec.go @@ -16,15 +16,19 @@ package mgmtapi import ( "bytes" - _ "embed" + "embed" "net/http" "time" + + "github.com/scionproto/scion/private/mgmtapi" ) -//go:embed index.html -var index []byte +//go:embed *.html +var f embed.FS var ( + index = mgmtapi.ResolveIndex(f) + modtime = time.Now() spec = func() []byte {