Skip to content

Commit

Permalink
Move config to a dedicated pkg
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Sep 14, 2021
1 parent e07f388 commit feb42c8
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 74 deletions.
46 changes: 0 additions & 46 deletions cmd/buildkitd/config.go

This file was deleted.

41 changes: 25 additions & 16 deletions cmd/buildkitd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
localremotecache "github.com/moby/buildkit/cache/remotecache/local"
registryremotecache "github.com/moby/buildkit/cache/remotecache/registry"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/moby/buildkit/control"
"github.com/moby/buildkit/executor/oci"
"github.com/moby/buildkit/frontend"
Expand All @@ -42,6 +41,7 @@ import (
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/solver/bboltcachestorage"
"github.com/moby/buildkit/util/apicaps"
"github.com/moby/buildkit/util/appconfig"
"github.com/moby/buildkit/util/appcontext"
"github.com/moby/buildkit/util/appdefaults"
"github.com/moby/buildkit/util/archutil"
Expand Down Expand Up @@ -83,7 +83,7 @@ func init() {
var propagators = propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})

type workerInitializerOpt struct {
config *config.Config
config *appconfig.Config
sessionManager *session.Manager
traceSocket string
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func main() {
ctx, cancel := context.WithCancel(appcontext.Context())
defer cancel()

cfg, err := LoadFile(c.GlobalString("config"))
cfg, err := appconfig.LoadFile(c.GlobalString("config"))
if err != nil {
return err
}
Expand Down Expand Up @@ -319,7 +319,7 @@ func main() {
}
}

func serveGRPC(cfg config.GRPCConfig, server *grpc.Server, errCh chan error) error {
func serveGRPC(cfg appconfig.GRPCConfig, server *grpc.Server, errCh chan error) error {
addrs := cfg.Address
if len(addrs) == 0 {
return errors.New("--addr cannot be empty")
Expand Down Expand Up @@ -367,12 +367,12 @@ func defaultConfigPath() string {
return filepath.Join(appdefaults.ConfigDir, "buildkitd.toml")
}

func defaultConf() (config.Config, error) {
cfg, err := LoadFile(defaultConfigPath())
func defaultConf() (appconfig.Config, error) {
cfg, err := appconfig.LoadFile(defaultConfigPath())
if err != nil {
var pe *os.PathError
if !errors.As(err, &pe) {
return config.Config{}, err
return appconfig.Config{}, err
}
return cfg, nil
}
Expand All @@ -381,7 +381,7 @@ func defaultConf() (config.Config, error) {
return cfg, nil
}

func setDefaultNetworkConfig(nc config.NetworkConfig) config.NetworkConfig {
func setDefaultNetworkConfig(nc appconfig.NetworkConfig) appconfig.NetworkConfig {
if nc.Mode == "" {
nc.Mode = "auto"
}
Expand All @@ -394,7 +394,7 @@ func setDefaultNetworkConfig(nc config.NetworkConfig) config.NetworkConfig {
return nc
}

func setDefaultConfig(cfg *config.Config) {
func setDefaultConfig(cfg *appconfig.Config) {
orig := *cfg

if cfg.Root == "" {
Expand Down Expand Up @@ -431,7 +431,7 @@ func setDefaultConfig(cfg *config.Config) {
}
}

func applyMainFlags(c *cli.Context, cfg *config.Config) error {
func applyMainFlags(c *cli.Context, cfg *appconfig.Config) error {
if c.IsSet("debug") {
cfg.Debug = c.Bool("debug")
}
Expand Down Expand Up @@ -576,7 +576,7 @@ func unaryInterceptor(globalCtx context.Context, tp trace.TracerProvider) grpc.U
}
}

func serverCredentials(cfg config.TLSConfig) (*tls.Config, error) {
func serverCredentials(cfg appconfig.TLSConfig) (*tls.Config, error) {
certFile := cfg.Cert
keyFile := cfg.Key
caFile := cfg.CA
Expand Down Expand Up @@ -613,7 +613,7 @@ func serverCredentials(cfg config.TLSConfig) (*tls.Config, error) {
return tlsConf, nil
}

func newController(c *cli.Context, cfg *config.Config) (*control.Controller, error) {
func newController(c *cli.Context, cfg *appconfig.Config) (*control.Controller, error) {
sessionManager, err := session.NewManager()
if err != nil {
return nil, err
Expand Down Expand Up @@ -680,7 +680,7 @@ func newController(c *cli.Context, cfg *config.Config) (*control.Controller, err
})
}

func resolverFunc(cfg *config.Config) docker.RegistryHosts {
func resolverFunc(cfg *appconfig.Config) docker.RegistryHosts {
return resolver.NewRegistryConfig(cfg.Registries)
}

Expand Down Expand Up @@ -746,12 +746,12 @@ func parsePlatforms(platformsStr []string) ([]ocispecs.Platform, error) {
return out, nil
}

func getGCPolicy(cfg config.GCConfig, root string) []client.PruneInfo {
func getGCPolicy(cfg appconfig.GCConfig, root string) []client.PruneInfo {
if cfg.GC != nil && !*cfg.GC {
return nil
}
if len(cfg.GCPolicy) == 0 {
cfg.GCPolicy = config.DefaultGCPolicy(root, cfg.GCKeepStorage)
cfg.GCPolicy = appconfig.DefaultGCPolicy(root, cfg.GCKeepStorage)
}
out := make([]client.PruneInfo, 0, len(cfg.GCPolicy))
for _, rule := range cfg.GCPolicy {
Expand All @@ -765,7 +765,7 @@ func getGCPolicy(cfg config.GCConfig, root string) []client.PruneInfo {
return out
}

func getDNSConfig(cfg *config.DNSConfig) *oci.DNSConfig {
func getDNSConfig(cfg *appconfig.DNSConfig) *oci.DNSConfig {
var dns *oci.DNSConfig
if cfg != nil {
dns = &oci.DNSConfig{
Expand All @@ -777,6 +777,15 @@ func getDNSConfig(cfg *config.DNSConfig) *oci.DNSConfig {
return dns
}

// parseBoolOrAuto returns (nil, nil) if s is "auto"
func parseBoolOrAuto(s string) (*bool, error) {
if s == "" || strings.ToLower(s) == "auto" {
return nil, nil
}
b, err := strconv.ParseBool(s)
return &b, err
}

func runTraceController(p string, exp sdktrace.SpanExporter) error {
server := grpc.NewServer()
tracev1.RegisterTraceServiceServer(server, &traceCollector{exporter: exp})
Expand Down
6 changes: 3 additions & 3 deletions cmd/buildkitd/main_containerd_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"

ctd "github.com/containerd/containerd"
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/moby/buildkit/util/appconfig"
"github.com/moby/buildkit/util/network/cniprovider"
"github.com/moby/buildkit/util/network/netproviders"
"github.com/moby/buildkit/worker"
Expand Down Expand Up @@ -117,7 +117,7 @@ func init() {
if defaultConf.Workers.Containerd.GCKeepStorage != 0 {
return defaultConf.Workers.Containerd.GCKeepStorage / 1e6
}
return config.DetectDefaultGCCap(defaultConf.Root) / 1e6
return appconfig.DetectDefaultGCCap(defaultConf.Root) / 1e6
}(),
Hidden: len(defaultConf.Workers.Containerd.GCPolicy) != 0,
})
Expand All @@ -133,7 +133,7 @@ func init() {
// TODO(AkihiroSuda): allow using multiple snapshotters. should be useful for some applications that does not work with the default overlay snapshotter. e.g. mysql (docker/for-linux#72)",
}

func applyContainerdFlags(c *cli.Context, cfg *config.Config) error {
func applyContainerdFlags(c *cli.Context, cfg *appconfig.Config) error {
if cfg.Workers.Containerd.Address == "" {
cfg.Workers.Containerd.Address = defaultContainerdAddress
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/buildkitd/main_oci_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import (
sgzconf "github.com/containerd/stargz-snapshotter/fs/config"
sgzsource "github.com/containerd/stargz-snapshotter/fs/source"
remotesn "github.com/containerd/stargz-snapshotter/snapshot"
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/moby/buildkit/executor/oci"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/util/appconfig"
"github.com/moby/buildkit/util/network/cniprovider"
"github.com/moby/buildkit/util/network/netproviders"
"github.com/moby/buildkit/util/resolver"
Expand Down Expand Up @@ -143,7 +143,7 @@ func init() {
if defaultConf.Workers.OCI.GCKeepStorage != 0 {
return defaultConf.Workers.OCI.GCKeepStorage / 1e6
}
return config.DetectDefaultGCCap(defaultConf.Root) / 1e6
return appconfig.DetectDefaultGCCap(defaultConf.Root) / 1e6
}(),
Hidden: len(defaultConf.Workers.OCI.GCPolicy) != 0,
})
Expand All @@ -158,7 +158,7 @@ func init() {
// TODO: allow multiple oci runtimes
}

func applyOCIFlags(c *cli.Context, cfg *config.Config) error {
func applyOCIFlags(c *cli.Context, cfg *appconfig.Config) error {
if cfg.Workers.OCI.Snapshotter == "" {
cfg.Workers.OCI.Snapshotter = "auto"
}
Expand Down Expand Up @@ -308,7 +308,7 @@ func ociWorkerInitializer(c *cli.Context, common workerInitializerOpt) ([]worker
return []worker.Worker{w}, nil
}

func snapshotterFactory(commonRoot string, cfg config.OCIConfig, sm *session.Manager, hosts docker.RegistryHosts) (runc.SnapshotterFactory, error) {
func snapshotterFactory(commonRoot string, cfg appconfig.OCIConfig, sm *session.Manager, hosts docker.RegistryHosts) (runc.SnapshotterFactory, error) {
var (
name = cfg.Snapshotter
address = cfg.ProxySnapshotterPath
Expand Down
34 changes: 33 additions & 1 deletion cmd/buildkitd/config/config.go → util/appconfig/appconfig.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package config
package appconfig

import (
"io"
"os"

"github.com/moby/buildkit/util/resolver"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
)

// Config provides containerd configuration data for the server
Expand Down Expand Up @@ -113,3 +118,30 @@ type DNSConfig struct {
Options []string `toml:"options"`
SearchDomains []string `toml:"searchDomains"`
}

// Load loads buildkitd config
func Load(r io.Reader) (Config, error) {
var c Config
t, err := toml.LoadReader(r)
if err != nil {
return c, errors.Wrap(err, "failed to parse config")
}
err = t.Unmarshal(&c)
if err != nil {
return c, errors.Wrap(err, "failed to parse config")
}
return c, nil
}

// LoadFile loads buildkitd config file
func LoadFile(fp string) (Config, error) {
f, err := os.Open(fp)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return Config{}, nil
}
return Config{}, errors.Wrapf(err, "failed to load config from %s", fp)
}
defer f.Close()
return Load(f)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package appconfig

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package appconfig

const defaultCap int64 = 2e9 // 2GB

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build !windows

package config
package appconfig

import (
"syscall"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build windows

package config
package appconfig

func DetectDefaultGCCap(root string) int64 {
return defaultCap
Expand Down

0 comments on commit feb42c8

Please sign in to comment.