2019-06-20 18:36:40 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-12-02 12:45:26 -05:00
|
|
|
"context"
|
2019-06-20 18:36:40 -05:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-09-02 07:56:02 -05:00
|
|
|
"os"
|
2022-01-21 15:30:09 -05:00
|
|
|
"runtime"
|
2022-09-09 00:41:13 -05:00
|
|
|
"strconv"
|
2022-01-21 15:30:09 -05:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
2021-04-22 14:18:33 -05:00
|
|
|
"time"
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
"github.com/docker/distribution/registry/storage/driver/factory"
|
2019-11-25 17:33:58 -05:00
|
|
|
"github.com/gorilla/handlers"
|
2019-07-10 00:23:59 -05:00
|
|
|
"github.com/gorilla/mux"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/errors"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
|
|
|
ext "zotregistry.io/zot/pkg/extensions"
|
|
|
|
"zotregistry.io/zot/pkg/extensions/monitoring"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
2023-01-09 15:37:44 -05:00
|
|
|
"zotregistry.io/zot/pkg/meta/repodb"
|
|
|
|
"zotregistry.io/zot/pkg/meta/repodb/repodbfactory"
|
2022-09-23 00:27:56 -05:00
|
|
|
"zotregistry.io/zot/pkg/scheduler"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2022-11-02 17:53:08 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage/cache"
|
2022-11-23 03:19:15 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage/constants"
|
2022-09-30 12:35:16 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage/local"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage/s3"
|
2019-06-20 18:36:40 -05:00
|
|
|
)
|
|
|
|
|
2021-04-22 14:18:33 -05:00
|
|
|
const (
|
2022-10-05 05:21:14 -05:00
|
|
|
idleTimeout = 120 * time.Second
|
|
|
|
readHeaderTimeout = 5 * time.Second
|
2021-04-22 14:18:33 -05:00
|
|
|
)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
type Controller struct {
|
2022-03-24 07:49:51 -05:00
|
|
|
Config *config.Config
|
|
|
|
Router *mux.Router
|
2023-01-09 15:37:44 -05:00
|
|
|
RepoDB repodb.RepoDB
|
2022-03-24 07:49:51 -05:00
|
|
|
StoreController storage.StoreController
|
|
|
|
Log log.Logger
|
|
|
|
Audit *log.Logger
|
|
|
|
Server *http.Server
|
|
|
|
Metrics monitoring.MetricServer
|
2023-02-10 00:04:52 -05:00
|
|
|
CveInfo ext.CveInfo
|
2022-09-09 00:41:13 -05:00
|
|
|
// runtime params
|
|
|
|
chosenPort int // kernel-chosen port
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
func NewController(config *config.Config) *Controller {
|
2021-05-25 03:38:21 -05:00
|
|
|
var controller Controller
|
|
|
|
|
|
|
|
logger := log.NewLogger(config.Log.Level, config.Log.Output)
|
|
|
|
controller.Config = config
|
|
|
|
controller.Log = logger
|
|
|
|
|
|
|
|
if config.Log.Audit != "" {
|
|
|
|
audit := log.NewAuditLogger(config.Log.Level, config.Log.Audit)
|
|
|
|
controller.Audit = audit
|
|
|
|
}
|
|
|
|
|
|
|
|
return &controller
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
2022-01-21 15:30:09 -05:00
|
|
|
func DumpRuntimeParams(log log.Logger) {
|
|
|
|
var rLimit syscall.Rlimit
|
|
|
|
|
|
|
|
evt := log.Info().Int("cpus", runtime.NumCPU())
|
|
|
|
|
|
|
|
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
|
|
|
|
if err == nil {
|
|
|
|
evt = evt.Uint64("max. open files", rLimit.Cur)
|
|
|
|
}
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
if content, err := os.ReadFile("/proc/sys/net/core/somaxconn"); err == nil {
|
2022-01-21 15:30:09 -05:00
|
|
|
evt = evt.Str("listen backlog", strings.TrimSuffix(string(content), "\n"))
|
|
|
|
}
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
if content, err := os.ReadFile("/proc/sys/user/max_inotify_watches"); err == nil {
|
2022-01-21 15:30:09 -05:00
|
|
|
evt = evt.Str("max. inotify watches", strings.TrimSuffix(string(content), "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
evt.Msg("runtime params")
|
|
|
|
}
|
|
|
|
|
2022-09-09 00:41:13 -05:00
|
|
|
func (c *Controller) GetPort() int {
|
|
|
|
return c.chosenPort
|
|
|
|
}
|
|
|
|
|
2022-03-24 07:49:51 -05:00
|
|
|
func (c *Controller) Run(reloadCtx context.Context) error {
|
2023-02-10 00:04:52 -05:00
|
|
|
c.StartBackgroundTasks(reloadCtx)
|
2022-01-21 15:30:09 -05:00
|
|
|
|
|
|
|
// setup HTTP API router
|
2019-07-10 00:23:59 -05:00
|
|
|
engine := mux.NewRouter()
|
2022-01-21 15:30:09 -05:00
|
|
|
|
|
|
|
// rate-limit HTTP requests if enabled
|
|
|
|
if c.Config.HTTP.Ratelimit != nil {
|
|
|
|
if c.Config.HTTP.Ratelimit.Rate != nil {
|
|
|
|
engine.Use(RateLimiter(c, *c.Config.HTTP.Ratelimit.Rate))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mrlim := range c.Config.HTTP.Ratelimit.Methods {
|
|
|
|
engine.Use(MethodRateLimiter(c, mrlim.Method, mrlim.Rate))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
engine.Use(
|
2021-10-15 10:05:00 -05:00
|
|
|
SessionLogger(c),
|
2021-05-05 17:31:56 -05:00
|
|
|
handlers.RecoveryHandler(handlers.RecoveryLogger(c.Log),
|
|
|
|
handlers.PrintRecoveryStack(false)))
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2021-05-25 03:38:21 -05:00
|
|
|
if c.Audit != nil {
|
2021-10-15 10:05:00 -05:00
|
|
|
engine.Use(SessionAuditLogger(c.Audit))
|
2021-05-25 03:38:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
c.Router = engine
|
|
|
|
c.Router.UseEncodedPath()
|
|
|
|
|
2022-03-07 07:50:15 -05:00
|
|
|
monitoring.SetServerInfo(c.Metrics, c.Config.Commit, c.Config.BinaryType, c.Config.GoVersion,
|
|
|
|
c.Config.DistSpecVersion)
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
//nolint: contextcheck
|
2021-10-05 04:12:22 -05:00
|
|
|
_ = NewRouteHandler(c)
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("%s:%s", c.Config.HTTP.Address, c.Config.HTTP.Port)
|
|
|
|
server := &http.Server{
|
2022-10-05 05:21:14 -05:00
|
|
|
Addr: addr,
|
|
|
|
Handler: c.Router,
|
|
|
|
IdleTimeout: idleTimeout,
|
|
|
|
ReadHeaderTimeout: readHeaderTimeout,
|
2021-10-05 04:12:22 -05:00
|
|
|
}
|
|
|
|
c.Server = server
|
|
|
|
|
|
|
|
// Create the listener
|
2021-12-13 14:23:31 -05:00
|
|
|
listener, err := net.Listen("tcp", addr)
|
2021-10-05 04:12:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-09 00:41:13 -05:00
|
|
|
if c.Config.HTTP.Port == "0" || c.Config.HTTP.Port == "" {
|
|
|
|
chosenAddr, ok := listener.Addr().(*net.TCPAddr)
|
|
|
|
if !ok {
|
|
|
|
c.Log.Error().Str("port", c.Config.HTTP.Port).Msg("invalid addr type")
|
|
|
|
|
|
|
|
return errors.ErrBadType
|
|
|
|
}
|
|
|
|
|
|
|
|
c.chosenPort = chosenAddr.Port
|
|
|
|
|
|
|
|
c.Log.Info().Int("port", chosenAddr.Port).IPAddr("address", chosenAddr.IP).Msg(
|
|
|
|
"port is unspecified, listening on kernel chosen port",
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
chosenPort, _ := strconv.ParseInt(c.Config.HTTP.Port, 10, 64)
|
|
|
|
|
|
|
|
c.chosenPort = int(chosenPort)
|
|
|
|
}
|
|
|
|
|
2021-10-05 04:12:22 -05:00
|
|
|
if c.Config.HTTP.TLS != nil && c.Config.HTTP.TLS.Key != "" && c.Config.HTTP.TLS.Cert != "" {
|
2022-03-01 15:57:56 -05:00
|
|
|
server.TLSConfig = &tls.Config{
|
|
|
|
CipherSuites: []uint16{
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
},
|
|
|
|
CurvePreferences: []tls.CurveID{
|
|
|
|
tls.CurveP256,
|
|
|
|
tls.X25519,
|
|
|
|
},
|
|
|
|
PreferServerCipherSuites: true,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
|
|
|
|
2021-10-05 04:12:22 -05:00
|
|
|
if c.Config.HTTP.TLS.CACert != "" {
|
|
|
|
clientAuth := tls.VerifyClientCertIfGiven
|
2022-07-14 10:13:46 -05:00
|
|
|
if (c.Config.HTTP.Auth == nil || c.Config.HTTP.Auth.HTPasswd.Path == "") &&
|
2023-03-08 14:47:15 -05:00
|
|
|
!anonymousPolicyExists(c.Config.HTTP.AccessControl) {
|
2021-10-05 04:12:22 -05:00
|
|
|
clientAuth = tls.RequireAndVerifyClientCert
|
|
|
|
}
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
caCert, err := os.ReadFile(c.Config.HTTP.TLS.CACert)
|
2021-10-05 04:12:22 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
|
|
|
|
if !caCertPool.AppendCertsFromPEM(caCert) {
|
|
|
|
panic(errors.ErrBadCACert)
|
|
|
|
}
|
|
|
|
|
2022-03-01 15:57:56 -05:00
|
|
|
server.TLSConfig.ClientAuth = clientAuth
|
|
|
|
server.TLSConfig.ClientCAs = caCertPool
|
2021-10-05 04:12:22 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return server.ServeTLS(listener, c.Config.HTTP.TLS.Cert, c.Config.HTTP.TLS.Key)
|
2021-10-05 04:12:22 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return server.Serve(listener)
|
2021-10-05 04:12:22 -05:00
|
|
|
}
|
|
|
|
|
2023-02-10 00:04:52 -05:00
|
|
|
func (c *Controller) Init(reloadCtx context.Context) error {
|
|
|
|
// print the current configuration, but strip secrets
|
|
|
|
c.Log.Info().Interface("params", c.Config.Sanitize()).Msg("configuration settings")
|
|
|
|
|
|
|
|
// print the current runtime environment
|
|
|
|
DumpRuntimeParams(c.Log)
|
|
|
|
|
|
|
|
var enabled bool
|
|
|
|
if c.Config != nil &&
|
|
|
|
c.Config.Extensions != nil &&
|
|
|
|
c.Config.Extensions.Metrics != nil &&
|
|
|
|
*c.Config.Extensions.Metrics.Enable {
|
|
|
|
enabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Metrics = monitoring.NewMetricsServer(enabled, c.Log)
|
|
|
|
|
|
|
|
if err := c.InitImageStore(reloadCtx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.InitRepoDB(reloadCtx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.InitCVEInfo()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) InitCVEInfo() {
|
|
|
|
// Enable CVE extension if extension config is provided
|
|
|
|
if c.Config != nil && c.Config.Extensions != nil {
|
|
|
|
c.CveInfo = ext.GetCVEInfo(c.Config, c.StoreController, c.RepoDB, c.Log)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
func (c *Controller) InitImageStore(ctx context.Context) error {
|
2021-04-05 19:40:33 -05:00
|
|
|
c.StoreController = storage.StoreController{}
|
|
|
|
|
2022-06-24 08:08:47 -05:00
|
|
|
linter := ext.GetLinter(c.Config, c.Log)
|
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
if c.Config.Storage.RootDirectory != "" {
|
2022-04-12 05:01:04 -05:00
|
|
|
// no need to validate hard links work on s3
|
|
|
|
if c.Config.Storage.Dedupe && c.Config.Storage.StorageDriver == nil {
|
2022-09-30 12:35:16 -05:00
|
|
|
err := local.ValidateHardLink(c.Config.Storage.RootDirectory)
|
2021-04-05 19:40:33 -05:00
|
|
|
if err != nil {
|
|
|
|
c.Log.Warn().Msg("input storage root directory filesystem does not supports hardlinking," +
|
|
|
|
"disabling dedupe functionality")
|
|
|
|
|
|
|
|
c.Config.Storage.Dedupe = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
var defaultStore storage.ImageStore
|
2022-04-12 05:01:04 -05:00
|
|
|
if c.Config.Storage.StorageDriver == nil {
|
2022-06-24 08:08:47 -05:00
|
|
|
// false positive lint - linter does not implement Lint method
|
2022-11-22 13:29:57 -05:00
|
|
|
//nolint:typecheck,contextcheck
|
2022-09-30 12:35:16 -05:00
|
|
|
defaultStore = local.NewImageStore(c.Config.Storage.RootDirectory,
|
2022-06-24 08:08:47 -05:00
|
|
|
c.Config.Storage.GC, c.Config.Storage.GCDelay,
|
|
|
|
c.Config.Storage.Dedupe, c.Config.Storage.Commit, c.Log, c.Metrics, linter,
|
2022-11-02 17:53:08 -05:00
|
|
|
CreateCacheDatabaseDriver(c.Config.Storage.StorageConfig, c.Log),
|
2022-06-24 08:08:47 -05:00
|
|
|
)
|
2021-07-16 22:53:05 -05:00
|
|
|
} else {
|
|
|
|
storeName := fmt.Sprintf("%v", c.Config.Storage.StorageDriver["name"])
|
|
|
|
if storeName != storage.S3StorageDriverName {
|
2023-04-27 21:44:22 -05:00
|
|
|
c.Log.Fatal().Err(errors.ErrBadConfig).Str("storageDriver", storeName).
|
|
|
|
Msg("unsupported storage driver")
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
// Init a Storager from connection string.
|
|
|
|
store, err := factory.Create(storeName, c.Config.Storage.StorageDriver)
|
|
|
|
if err != nil {
|
|
|
|
c.Log.Error().Err(err).Str("rootDir", c.Config.Storage.RootDirectory).Msg("unable to create s3 service")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-12 05:01:04 -05:00
|
|
|
/* in the case of s3 c.Config.Storage.RootDirectory is used for caching blobs locally and
|
|
|
|
c.Config.Storage.StorageDriver["rootdirectory"] is the actual rootDir in s3 */
|
|
|
|
rootDir := "/"
|
|
|
|
if c.Config.Storage.StorageDriver["rootdirectory"] != nil {
|
|
|
|
rootDir = fmt.Sprintf("%v", c.Config.Storage.StorageDriver["rootdirectory"])
|
|
|
|
}
|
|
|
|
|
2022-06-24 08:08:47 -05:00
|
|
|
// false positive lint - linter does not implement Lint method
|
2022-11-22 13:29:57 -05:00
|
|
|
//nolint: typecheck,contextcheck
|
2022-04-12 05:01:04 -05:00
|
|
|
defaultStore = s3.NewImageStore(rootDir, c.Config.Storage.RootDirectory,
|
2022-02-09 19:51:35 -05:00
|
|
|
c.Config.Storage.GC, c.Config.Storage.GCDelay, c.Config.Storage.Dedupe,
|
2022-11-02 17:53:08 -05:00
|
|
|
c.Config.Storage.Commit, c.Log, c.Metrics, linter, store,
|
|
|
|
CreateCacheDatabaseDriver(c.Config.Storage.StorageConfig, c.Log))
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
2021-04-05 19:40:33 -05:00
|
|
|
|
|
|
|
c.StoreController.DefaultStore = defaultStore
|
|
|
|
} else {
|
|
|
|
// we can't proceed without global storage
|
|
|
|
c.Log.Error().Err(errors.ErrImgStoreNotFound).Msg("controller: no storage config provided")
|
|
|
|
|
|
|
|
return errors.ErrImgStoreNotFound
|
2020-02-17 16:57:15 -05:00
|
|
|
}
|
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
if c.Config.Storage.SubPaths != nil {
|
|
|
|
if len(c.Config.Storage.SubPaths) > 0 {
|
|
|
|
subPaths := c.Config.Storage.SubPaths
|
|
|
|
|
2022-11-22 13:29:57 -05:00
|
|
|
//nolint: contextcheck
|
2022-08-10 17:28:52 -05:00
|
|
|
subImageStore, err := c.getSubStore(subPaths, linter)
|
|
|
|
if err != nil {
|
|
|
|
c.Log.Error().Err(err).Msg("controller: error getting sub image store")
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2022-08-10 17:28:52 -05:00
|
|
|
return err
|
2021-04-05 19:40:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
c.StoreController.SubStore = subImageStore
|
|
|
|
}
|
2020-06-25 03:21:47 -05:00
|
|
|
}
|
|
|
|
|
2021-10-05 04:12:22 -05:00
|
|
|
return nil
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2021-12-02 12:45:26 -05:00
|
|
|
|
2022-08-10 17:28:52 -05:00
|
|
|
func (c *Controller) getSubStore(subPaths map[string]config.StorageConfig,
|
2022-10-05 05:21:14 -05:00
|
|
|
linter storage.Lint,
|
2022-08-10 17:28:52 -05:00
|
|
|
) (map[string]storage.ImageStore, error) {
|
|
|
|
imgStoreMap := make(map[string]storage.ImageStore, 0)
|
|
|
|
|
|
|
|
subImageStore := make(map[string]storage.ImageStore)
|
|
|
|
|
|
|
|
// creating image store per subpaths
|
|
|
|
for route, storageConfig := range subPaths {
|
|
|
|
// no need to validate hard links work on s3
|
|
|
|
if storageConfig.Dedupe && storageConfig.StorageDriver == nil {
|
2022-09-30 12:35:16 -05:00
|
|
|
err := local.ValidateHardLink(storageConfig.RootDirectory)
|
2022-08-10 17:28:52 -05:00
|
|
|
if err != nil {
|
|
|
|
c.Log.Warn().Msg("input storage root directory filesystem does not supports hardlinking, " +
|
|
|
|
"disabling dedupe functionality")
|
|
|
|
|
|
|
|
storageConfig.Dedupe = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if storageConfig.StorageDriver == nil {
|
|
|
|
// Compare if subpath root dir is same as default root dir
|
|
|
|
isSame, _ := config.SameFile(c.Config.Storage.RootDirectory, storageConfig.RootDirectory)
|
|
|
|
|
|
|
|
if isSame {
|
|
|
|
c.Log.Error().Err(errors.ErrBadConfig).Msg("sub path storage directory is same as root directory")
|
|
|
|
|
|
|
|
return nil, errors.ErrBadConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
isUnique := true
|
|
|
|
|
|
|
|
// Compare subpath unique files
|
|
|
|
for file := range imgStoreMap {
|
|
|
|
// We already have image storage for this file
|
|
|
|
if compareImageStore(file, storageConfig.RootDirectory) {
|
|
|
|
subImageStore[route] = imgStoreMap[file]
|
|
|
|
|
|
|
|
isUnique = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// subpath root directory is unique
|
|
|
|
// add it to uniqueSubFiles
|
|
|
|
// Create a new image store and assign it to imgStoreMap
|
|
|
|
if isUnique {
|
2022-09-30 12:35:16 -05:00
|
|
|
imgStoreMap[storageConfig.RootDirectory] = local.NewImageStore(storageConfig.RootDirectory,
|
2022-11-02 17:53:08 -05:00
|
|
|
storageConfig.GC, storageConfig.GCDelay, storageConfig.Dedupe,
|
|
|
|
storageConfig.Commit, c.Log, c.Metrics, linter, CreateCacheDatabaseDriver(storageConfig, c.Log))
|
2022-08-10 17:28:52 -05:00
|
|
|
|
|
|
|
subImageStore[route] = imgStoreMap[storageConfig.RootDirectory]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
storeName := fmt.Sprintf("%v", storageConfig.StorageDriver["name"])
|
|
|
|
if storeName != storage.S3StorageDriverName {
|
2023-04-27 21:44:22 -05:00
|
|
|
c.Log.Fatal().Err(errors.ErrBadConfig).Str("storageDriver", storeName).
|
|
|
|
Msg("unsupported storage driver")
|
2022-08-10 17:28:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init a Storager from connection string.
|
|
|
|
store, err := factory.Create(storeName, storageConfig.StorageDriver)
|
|
|
|
if err != nil {
|
|
|
|
c.Log.Error().Err(err).Str("rootDir", storageConfig.RootDirectory).Msg("Unable to create s3 service")
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
/* in the case of s3 c.Config.Storage.RootDirectory is used for caching blobs locally and
|
|
|
|
c.Config.Storage.StorageDriver["rootdirectory"] is the actual rootDir in s3 */
|
|
|
|
rootDir := "/"
|
|
|
|
if c.Config.Storage.StorageDriver["rootdirectory"] != nil {
|
|
|
|
rootDir = fmt.Sprintf("%v", c.Config.Storage.StorageDriver["rootdirectory"])
|
|
|
|
}
|
|
|
|
|
|
|
|
// false positive lint - linter does not implement Lint method
|
2022-10-05 05:21:14 -05:00
|
|
|
//nolint: typecheck
|
2022-08-10 17:28:52 -05:00
|
|
|
subImageStore[route] = s3.NewImageStore(rootDir, storageConfig.RootDirectory,
|
|
|
|
storageConfig.GC, storageConfig.GCDelay,
|
|
|
|
storageConfig.Dedupe, storageConfig.Commit, c.Log, c.Metrics, linter, store,
|
2022-11-02 17:53:08 -05:00
|
|
|
CreateCacheDatabaseDriver(storageConfig, c.Log),
|
2022-08-10 17:28:52 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return subImageStore, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareImageStore(root1, root2 string) bool {
|
|
|
|
isSameFile, err := config.SameFile(root1, root2)
|
|
|
|
// This error is path error that means either of root directory doesn't exist, in that case do string match
|
|
|
|
if err != nil {
|
|
|
|
return strings.EqualFold(root1, root2)
|
|
|
|
}
|
|
|
|
|
|
|
|
return isSameFile
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
func getUseRelPaths(storageConfig *config.StorageConfig) bool {
|
|
|
|
return storageConfig.StorageDriver == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateCacheDatabaseDriver(storageConfig config.StorageConfig, log log.Logger) cache.Cache {
|
2023-04-07 11:49:24 -05:00
|
|
|
if storageConfig.Dedupe || storageConfig.StorageDriver != nil {
|
2022-11-02 17:53:08 -05:00
|
|
|
if !storageConfig.RemoteCache {
|
|
|
|
params := cache.BoltDBDriverParameters{}
|
|
|
|
params.RootDir = storageConfig.RootDirectory
|
2022-11-23 03:19:15 -05:00
|
|
|
params.Name = constants.BoltdbName
|
2023-04-07 11:49:24 -05:00
|
|
|
|
|
|
|
if storageConfig.StorageDriver != nil {
|
|
|
|
params.Name = s3.CacheDBName
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
params.UseRelPaths = getUseRelPaths(&storageConfig)
|
|
|
|
|
|
|
|
driver, _ := storage.Create("boltdb", params, log)
|
|
|
|
|
|
|
|
return driver
|
|
|
|
}
|
2022-11-23 03:19:15 -05:00
|
|
|
|
|
|
|
// remote cache
|
2022-11-22 13:29:57 -05:00
|
|
|
if storageConfig.CacheDriver != nil {
|
2022-11-23 03:19:15 -05:00
|
|
|
name, ok := storageConfig.CacheDriver["name"].(string)
|
|
|
|
if !ok {
|
|
|
|
log.Warn().Msg("remote cache driver name missing!")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if name != constants.DynamoDBDriverName {
|
|
|
|
log.Warn().Str("driver", name).Msg("remote cache driver unsupported!")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dynamodb
|
2022-11-22 13:29:57 -05:00
|
|
|
dynamoParams := cache.DynamoDBDriverParameters{}
|
|
|
|
dynamoParams.Endpoint, _ = storageConfig.CacheDriver["endpoint"].(string)
|
|
|
|
dynamoParams.Region, _ = storageConfig.CacheDriver["region"].(string)
|
2023-01-09 15:37:44 -05:00
|
|
|
dynamoParams.TableName, _ = storageConfig.CacheDriver["cachetablename"].(string)
|
2022-11-22 13:29:57 -05:00
|
|
|
|
|
|
|
driver, _ := storage.Create("dynamodb", dynamoParams, log)
|
|
|
|
|
|
|
|
return driver
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
func (c *Controller) InitRepoDB(reloadCtx context.Context) error {
|
|
|
|
if c.Config.Extensions != nil && c.Config.Extensions.Search != nil && *c.Config.Extensions.Search.Enable {
|
2023-03-28 12:20:09 -05:00
|
|
|
driver, err := repodbfactory.New(c.Config.Storage.StorageConfig, c.Log) //nolint:contextcheck
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = driver.PatchDB()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-10 13:37:29 -05:00
|
|
|
err = repodb.ParseStorage(driver, c.StoreController, c.Log)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.RepoDB = driver
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 07:49:51 -05:00
|
|
|
func (c *Controller) LoadNewConfig(reloadCtx context.Context, config *config.Config) {
|
2022-02-10 09:17:49 -05:00
|
|
|
// reload access control config
|
2023-03-08 14:47:15 -05:00
|
|
|
c.Config.HTTP.AccessControl = config.HTTP.AccessControl
|
2022-02-10 09:17:49 -05:00
|
|
|
|
|
|
|
// Enable extensions if extension config is provided
|
|
|
|
if config.Extensions != nil && config.Extensions.Sync != nil {
|
|
|
|
// reload sync config
|
|
|
|
c.Config.Extensions.Sync = config.Extensions.Sync
|
2023-03-29 13:37:58 -05:00
|
|
|
ext.EnableSyncExtension(reloadCtx, c.Config, c.RepoDB, c.StoreController, c.Log)
|
2022-02-10 09:17:49 -05:00
|
|
|
} else if c.Config.Extensions != nil {
|
|
|
|
c.Config.Extensions.Sync = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Log.Info().Interface("reloaded params", c.Config.Sanitize()).Msg("new configuration settings")
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:45:26 -05:00
|
|
|
func (c *Controller) Shutdown() {
|
|
|
|
ctx := context.Background()
|
|
|
|
_ = c.Server.Shutdown(ctx)
|
|
|
|
}
|
2022-03-21 13:40:37 -05:00
|
|
|
|
|
|
|
func (c *Controller) StartBackgroundTasks(reloadCtx context.Context) {
|
2022-09-23 00:27:56 -05:00
|
|
|
taskScheduler := scheduler.NewScheduler(c.Log)
|
|
|
|
taskScheduler.RunScheduler(reloadCtx)
|
|
|
|
|
|
|
|
// Enable running garbage-collect periodically for DefaultStore
|
|
|
|
if c.Config.Storage.GC && c.Config.Storage.GCInterval != 0 {
|
|
|
|
c.StoreController.DefaultStore.RunGCPeriodically(c.Config.Storage.GCInterval, taskScheduler)
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:49:24 -05:00
|
|
|
// Enable running dedupe blobs both ways (dedupe or restore deduped blobs)
|
|
|
|
c.StoreController.DefaultStore.RunDedupeBlobs(time.Duration(0), taskScheduler)
|
|
|
|
|
2022-03-21 13:40:37 -05:00
|
|
|
// Enable extensions if extension config is provided for DefaultStore
|
|
|
|
if c.Config != nil && c.Config.Extensions != nil {
|
2022-04-27 01:00:20 -05:00
|
|
|
ext.EnableMetricsExtension(c.Config, c.Log, c.Config.Storage.RootDirectory)
|
2023-03-02 12:43:54 -05:00
|
|
|
ext.EnableSearchExtension(c.Config, c.StoreController, c.RepoDB, taskScheduler, c.CveInfo, c.Log)
|
2022-03-21 13:40:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Config.Storage.SubPaths != nil {
|
2022-09-23 00:27:56 -05:00
|
|
|
for route, storageConfig := range c.Config.Storage.SubPaths {
|
|
|
|
// Enable running garbage-collect periodically for subImageStore
|
|
|
|
if storageConfig.GC && storageConfig.GCInterval != 0 {
|
|
|
|
c.StoreController.SubStore[route].RunGCPeriodically(storageConfig.GCInterval, taskScheduler)
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:40:37 -05:00
|
|
|
// Enable extensions if extension config is provided for subImageStore
|
|
|
|
if c.Config != nil && c.Config.Extensions != nil {
|
2022-04-27 01:00:20 -05:00
|
|
|
ext.EnableMetricsExtension(c.Config, c.Log, storageConfig.RootDirectory)
|
2022-03-21 13:40:37 -05:00
|
|
|
}
|
2023-04-07 11:49:24 -05:00
|
|
|
|
|
|
|
// Enable running dedupe blobs both ways (dedupe or restore deduped blobs) for subpaths
|
|
|
|
substore := c.StoreController.SubStore[route]
|
|
|
|
if substore != nil {
|
|
|
|
substore.RunDedupeBlobs(time.Duration(0), taskScheduler)
|
|
|
|
}
|
2022-03-21 13:40:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable extensions if extension config is provided for storeController
|
|
|
|
if c.Config.Extensions != nil {
|
2022-04-27 01:00:20 -05:00
|
|
|
if c.Config.Extensions.Sync != nil {
|
2023-03-29 13:37:58 -05:00
|
|
|
ext.EnableSyncExtension(reloadCtx, c.Config, c.RepoDB, c.StoreController, c.Log)
|
2022-03-21 13:40:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Config.Extensions != nil {
|
2022-09-23 00:27:56 -05:00
|
|
|
ext.EnableScrubExtension(c.Config, c.Log, c.StoreController, taskScheduler)
|
2022-05-09 17:30:11 -05:00
|
|
|
}
|
2022-03-21 13:40:37 -05:00
|
|
|
}
|